blob: 49b04348a87b20c1bcf8dae3eff466b911fc0fa9 [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",
Ravi Kumar Alamanda87f6ee02013-05-07 14:20:11 -070096 [SND_DEVICE_OUT_VOICE_HANDSET] = "voice-handset",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080097 [SND_DEVICE_OUT_VOICE_SPEAKER] = "voice-speaker",
98 [SND_DEVICE_OUT_VOICE_HEADPHONES] = "voice-headphones",
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -080099 [SND_DEVICE_OUT_HDMI] = "hdmi",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800100 [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = "speaker-and-hdmi",
101 [SND_DEVICE_OUT_BT_SCO] = "bt-sco-headset",
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800102 [SND_DEVICE_OUT_VOICE_HANDSET_TMUS] = "voice-handset-tmus",
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800103 [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = "voice-tty-full-headphones",
104 [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = "voice-tty-vco-headphones",
105 [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = "voice-tty-hco-handset",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800106
107 /* Capture sound devices */
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800108 [SND_DEVICE_IN_HANDSET_MIC] = "handset-mic",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800109 [SND_DEVICE_IN_SPEAKER_MIC] = "speaker-mic",
110 [SND_DEVICE_IN_HEADSET_MIC] = "headset-mic",
111 [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = "voice-speaker-mic",
112 [SND_DEVICE_IN_VOICE_HEADSET_MIC] = "voice-headset-mic",
113 [SND_DEVICE_IN_HDMI_MIC] = "hdmi-mic",
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800114 [SND_DEVICE_IN_BT_SCO_MIC] = "bt-sco-mic",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800115 [SND_DEVICE_IN_CAMCORDER_MIC] = "camcorder-mic",
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800116 [SND_DEVICE_IN_VOICE_DMIC_EF] = "voice-dmic-ef",
117 [SND_DEVICE_IN_VOICE_DMIC_BS] = "voice-dmic-bs",
118 [SND_DEVICE_IN_VOICE_DMIC_EF_TMUS] = "voice-dmic-ef-tmus",
119 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF] = "voice-speaker-dmic-ef",
120 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS] = "voice-speaker-dmic-bs",
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800121 [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = "voice-tty-full-headset-mic",
122 [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = "voice-tty-vco-handset-mic",
123 [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = "voice-tty-hco-headset-mic",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800124 [SND_DEVICE_IN_VOICE_REC_MIC] = "voice-rec-mic",
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800125 [SND_DEVICE_IN_VOICE_REC_DMIC_EF] = "voice-rec-dmic-ef",
126 [SND_DEVICE_IN_VOICE_REC_DMIC_BS] = "voice-rec-dmic-bs",
Eric Laurentc8400632013-02-14 19:04:54 -0800127 [SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE] = "voice-rec-dmic-ef-fluence",
128 [SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE] = "voice-rec-dmic-bs-fluence",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800129};
130
Eric Laurentc8400632013-02-14 19:04:54 -0800131/* ACDB IDs (audio DSP path configuration IDs) for each sound device */
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800132static const int acdb_device_table[SND_DEVICE_MAX] = {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700133 [SND_DEVICE_NONE] = -1,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800134 [SND_DEVICE_OUT_HANDSET] = 7,
Ravi Kumar Alamanda87f6ee02013-05-07 14:20:11 -0700135#ifdef MSM8974
136 [SND_DEVICE_OUT_SPEAKER] = 15,
137#else
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800138 [SND_DEVICE_OUT_SPEAKER] = 14,
Ravi Kumar Alamanda87f6ee02013-05-07 14:20:11 -0700139#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800140 [SND_DEVICE_OUT_HEADPHONES] = 10,
141 [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = 10,
Ravi Kumar Alamanda87f6ee02013-05-07 14:20:11 -0700142 [SND_DEVICE_OUT_VOICE_HANDSET] = 7,
143#ifdef MSM8974
144 [SND_DEVICE_OUT_VOICE_SPEAKER] = 15,
145#else
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800146 [SND_DEVICE_OUT_VOICE_SPEAKER] = 14,
Ravi Kumar Alamanda87f6ee02013-05-07 14:20:11 -0700147#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800148 [SND_DEVICE_OUT_VOICE_HEADPHONES] = 10,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800149 [SND_DEVICE_OUT_HDMI] = 18,
Ravi Kumar Alamanda87f6ee02013-05-07 14:20:11 -0700150#ifdef MSM8974
151 [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = 15,
152#else
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800153 [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = 14,
Ravi Kumar Alamanda87f6ee02013-05-07 14:20:11 -0700154#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800155 [SND_DEVICE_OUT_BT_SCO] = 22,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800156 [SND_DEVICE_OUT_VOICE_HANDSET_TMUS] = 81,
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800157 [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = 17,
158 [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = 17,
159 [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = 37,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800160
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800161 [SND_DEVICE_IN_HANDSET_MIC] = 4,
Ravi Kumar Alamanda87f6ee02013-05-07 14:20:11 -0700162 [SND_DEVICE_IN_SPEAKER_MIC] = 4, /* ToDo: Check if this needs to changed to 11 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800163 [SND_DEVICE_IN_HEADSET_MIC] = 8,
164 [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = 11,
165 [SND_DEVICE_IN_VOICE_HEADSET_MIC] = 8,
166 [SND_DEVICE_IN_HDMI_MIC] = 4,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800167 [SND_DEVICE_IN_BT_SCO_MIC] = 21,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800168 [SND_DEVICE_IN_CAMCORDER_MIC] = 61,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800169 [SND_DEVICE_IN_VOICE_DMIC_EF] = 6,
170 [SND_DEVICE_IN_VOICE_DMIC_BS] = 5,
171 [SND_DEVICE_IN_VOICE_DMIC_EF_TMUS] = 91,
172 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF] = 13,
173 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS] = 12,
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800174 [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = 16,
175 [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = 36,
176 [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = 16,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800177 [SND_DEVICE_IN_VOICE_REC_MIC] = 62,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800178 [SND_DEVICE_IN_VOICE_REC_DMIC_EF] = 62,
179 [SND_DEVICE_IN_VOICE_REC_DMIC_BS] = 62,
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800180 /* TODO: Update with proper acdb ids */
Eric Laurentc8400632013-02-14 19:04:54 -0800181 [SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE] = 62,
182 [SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE] = 62,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800183};
184
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800185int edid_get_max_channels(void);
186
Ravi Kumar Alamanda37718842013-02-22 18:44:45 -0800187static pthread_once_t check_op_once_ctl = PTHREAD_ONCE_INIT;
188static bool is_tmus = false;
189
190static void check_operator()
191{
192 char value[PROPERTY_VALUE_MAX];
193 int mccmnc;
194 property_get("gsm.sim.operator.numeric",value,"0");
195 mccmnc = atoi(value);
196 ALOGD("%s: tmus mccmnc %d", __func__, mccmnc);
197 switch(mccmnc) {
198 /* TMUS MCC(310), MNC(490, 260, 026) */
199 case 310490:
200 case 310260:
201 case 310026:
202 is_tmus = true;
203 break;
204 }
205}
206
207static bool is_operator_tmus()
208{
209 pthread_once(&check_op_once_ctl, check_operator);
210 return is_tmus;
211}
212
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800213static int get_pcm_device_id(struct audio_route *ar,
214 audio_usecase_t usecase,
215 int device_type)
216{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800217 int device_id;
218 if (device_type == PCM_PLAYBACK)
219 device_id = pcm_device_table[usecase][0];
220 else
221 device_id = pcm_device_table[usecase][1];
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800222 return device_id;
223}
224
225static int get_acdb_device_id(snd_device_t snd_device)
226{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700227 return acdb_device_table[snd_device];
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800228}
229
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800230static void add_backend_name(char *mixer_path,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700231 snd_device_t snd_device)
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800232{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700233 if (snd_device == SND_DEVICE_IN_BT_SCO_MIC)
234 strcat(mixer_path, " bt-sco");
235 else if(snd_device == SND_DEVICE_OUT_BT_SCO)
236 strcat(mixer_path, " bt-sco");
237 else if (snd_device == SND_DEVICE_OUT_HDMI)
238 strcat(mixer_path, " hdmi");
239 else if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_HDMI)
240 strcat(mixer_path, " speaker-and-hdmi");
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800241}
242
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700243static int enable_audio_route(struct audio_device *adev,
244 struct audio_usecase *usecase,
245 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800246{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700247 snd_device_t snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800248 char mixer_path[50];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800249
250 if (usecase == NULL)
251 return -EINVAL;
252
253 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
254
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800255 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700256 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800257 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700258 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800259
260 strcpy(mixer_path, use_case_table[usecase->id]);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700261 add_backend_name(mixer_path, snd_device);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800262 ALOGD("%s: apply mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700263 audio_route_apply_path(adev->audio_route, mixer_path);
264 if (update_mixer)
265 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800266
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800267 ALOGV("%s: exit", __func__);
268 return 0;
269}
270
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700271static int disable_audio_route(struct audio_device *adev,
272 struct audio_usecase *usecase,
273 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800274{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700275 snd_device_t snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800276 char mixer_path[50];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800277
278 if (usecase == NULL)
279 return -EINVAL;
280
281 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700282 if (usecase->type == PCM_CAPTURE)
283 snd_device = usecase->in_snd_device;
284 else
285 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800286 strcpy(mixer_path, use_case_table[usecase->id]);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700287 add_backend_name(mixer_path, snd_device);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800288 ALOGD("%s: reset mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700289 audio_route_reset_path(adev->audio_route, mixer_path);
290 if (update_mixer)
291 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800292
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800293 ALOGV("%s: exit", __func__);
294 return 0;
295}
296
297static int enable_snd_device(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700298 snd_device_t snd_device,
299 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800300{
301 int acdb_dev_id, acdb_dev_type;
302
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800303 if (snd_device < SND_DEVICE_MIN ||
304 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800305 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800306 return -EINVAL;
307 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700308
309 adev->snd_dev_ref_cnt[snd_device]++;
310 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
311 ALOGD("%s: snd_device(%d: %s) is already active",
312 __func__, snd_device, device_table[snd_device]);
313 return 0;
314 }
315
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800316 acdb_dev_id = get_acdb_device_id(snd_device);
317 if (acdb_dev_id < 0) {
318 ALOGE("%s: Could not find acdb id for device(%d)",
319 __func__, snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700320 adev->snd_dev_ref_cnt[snd_device]--;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800321 return -EINVAL;
322 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800323 if (adev->acdb_send_audio_cal) {
324 ALOGV("%s: sending audio calibration for snd_device(%d) acdb_id(%d)",
325 __func__, snd_device, acdb_dev_id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700326 if (snd_device >= SND_DEVICE_OUT_BEGIN &&
327 snd_device < SND_DEVICE_OUT_END)
328 acdb_dev_type = ACDB_DEV_TYPE_OUT;
329 else
330 acdb_dev_type = ACDB_DEV_TYPE_IN;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800331 adev->acdb_send_audio_cal(acdb_dev_id, acdb_dev_type);
332 } else {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700333 ALOGW("%s: Could not find the symbol acdb_send_audio_cal from %s",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800334 __func__, LIB_ACDB_LOADER);
335 }
336
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700337 ALOGD("%s: snd_device(%d: %s)", __func__,
338 snd_device, device_table[snd_device]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800339 audio_route_apply_path(adev->audio_route, device_table[snd_device]);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700340 if (update_mixer)
341 audio_route_update_mixer(adev->audio_route);
342
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800343 return 0;
344}
345
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700346static int disable_snd_device(struct audio_device *adev,
347 snd_device_t snd_device,
348 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800349{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800350 if (snd_device < SND_DEVICE_MIN ||
351 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800352 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800353 return -EINVAL;
354 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700355 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
356 ALOGE("%s: device ref cnt is already 0", __func__);
357 return -EINVAL;
358 }
359 adev->snd_dev_ref_cnt[snd_device]--;
360 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
361 ALOGD("%s: snd_device(%d: %s)", __func__,
362 snd_device, device_table[snd_device]);
363 audio_route_reset_path(adev->audio_route, device_table[snd_device]);
364 if (update_mixer)
365 audio_route_update_mixer(adev->audio_route);
366 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800367 return 0;
368}
369
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700370static void check_usecases_codec_backend(struct audio_device *adev,
371 struct audio_usecase *uc_info,
372 snd_device_t snd_device)
373{
374 struct listnode *node;
375 struct audio_usecase *usecase;
376 bool switch_device[AUDIO_USECASE_MAX];
377 int i, num_uc_to_switch = 0;
378
379 /*
380 * This function is to make sure that all the usecases that are active on
381 * the hardware codec backend are always routed to any one device that is
382 * handled by the hardware codec.
383 * For example, if low-latency and deep-buffer usecases are currently active
384 * on speaker and out_set_parameters(headset) is received on low-latency
385 * output, then we have to make sure deep-buffer is also switched to headset,
386 * because of the limitation that both the devices cannot be enabled
387 * at the same time as they share the same backend.
388 */
389 /* Disable all the usecases on the shared backend other than the
390 specified usecase */
391 for (i = 0; i < AUDIO_USECASE_MAX; i++)
392 switch_device[i] = false;
393
394 list_for_each(node, &adev->usecase_list) {
395 usecase = node_to_item(node, struct audio_usecase, list);
396 if (usecase->type != PCM_CAPTURE &&
397 usecase != uc_info &&
398 usecase->out_snd_device != snd_device &&
399 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
400 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
401 __func__, use_case_table[usecase->id],
402 device_table[usecase->out_snd_device]);
403 disable_audio_route(adev, usecase, false);
404 switch_device[usecase->id] = true;
405 num_uc_to_switch++;
406 }
407 }
408
409 if (num_uc_to_switch) {
410 /* Make sure all the streams are de-routed before disabling the device */
411 audio_route_update_mixer(adev->audio_route);
412
413 list_for_each(node, &adev->usecase_list) {
414 usecase = node_to_item(node, struct audio_usecase, list);
415 if (switch_device[usecase->id]) {
416 disable_snd_device(adev, usecase->out_snd_device, false);
417 enable_snd_device(adev, snd_device, false);
418 }
419 }
420
421 /* Make sure new snd device is enabled before re-routing the streams */
422 audio_route_update_mixer(adev->audio_route);
423
424 /* Re-route all the usecases on the shared backend other than the
425 specified usecase to new snd devices */
426 list_for_each(node, &adev->usecase_list) {
427 usecase = node_to_item(node, struct audio_usecase, list);
428 /* Update the out_snd_device only before enabling the audio route */
429 if (switch_device[usecase->id] ) {
430 usecase->out_snd_device = snd_device;
431 enable_audio_route(adev, usecase, false);
432 }
433 }
434
435 audio_route_update_mixer(adev->audio_route);
436 }
437}
438
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800439static int set_hdmi_channels(struct mixer *mixer,
440 int channel_count)
441{
442 struct mixer_ctl *ctl;
443 const char *channel_cnt_str = NULL;
444 const char *mixer_ctl_name = "HDMI_RX Channels";
445 switch (channel_count) {
446 case 8:
447 channel_cnt_str = "Eight"; break;
448 case 7:
449 channel_cnt_str = "Seven"; break;
450 case 6:
451 channel_cnt_str = "Six"; break;
452 case 5:
453 channel_cnt_str = "Five"; break;
454 case 4:
455 channel_cnt_str = "Four"; break;
456 case 3:
457 channel_cnt_str = "Three"; break;
458 default:
459 channel_cnt_str = "Two"; break;
460 }
461 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
462 if (!ctl) {
463 ALOGE("%s: Could not get ctl for mixer cmd - %s",
464 __func__, mixer_ctl_name);
465 return -EINVAL;
466 }
467 ALOGV("HDMI channel count: %s", channel_cnt_str);
468 mixer_ctl_set_enum_by_string(ctl, channel_cnt_str);
469 return 0;
470}
471
472/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700473static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800474{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700475 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800476 int channels = edid_get_max_channels();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800477
478 switch (channels) {
479 /*
480 * Do not handle stereo output in Multi-channel cases
481 * Stereo case is handled in normal playback path
482 */
483 case 6:
484 ALOGV("%s: HDMI supports 5.1", __func__);
485 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
486 break;
487 case 8:
488 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
489 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
490 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
491 break;
492 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700493 ALOGE("HDMI does not support multi channel playback");
494 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800495 break;
496 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700497 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800498}
499
Ravi Kumar Alamanda87f6ee02013-05-07 14:20:11 -0700500static int set_voice_volume(struct mixer *mixer,
501 int volume)
502{
503 struct mixer_ctl *ctl;
504 const char *mixer_ctl_name = "Voice Rx Volume";
505 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
506 if (!ctl) {
507 ALOGE("%s: Could not get ctl for mixer cmd - %s",
508 __func__, mixer_ctl_name);
509 return -EINVAL;
510 }
511 ALOGV("Setting voice volume: %d", volume);
512 mixer_ctl_set_value(ctl, 0, volume);
513 return 0;
514}
515
516static int set_mic_mute(struct mixer *mixer,
517 int mute)
518{
519 struct mixer_ctl *ctl;
520 const char *mixer_ctl_name = "Voice Tx Mute";
521 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
522 if (!ctl) {
523 ALOGE("%s: Could not get ctl for mixer cmd - %s",
524 __func__, mixer_ctl_name);
525 return -EINVAL;
526 }
527 ALOGV("Setting mic mute: %d", mute);
528 mixer_ctl_set_value(ctl, 0, mute);
529 return 0;
530}
531
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700532static snd_device_t get_output_snd_device(struct audio_device *adev,
533 audio_devices_t devices)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800534{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700535 audio_mode_t mode = adev->mode;
536 snd_device_t snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800537
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800538 ALOGV("%s: enter: output devices(%#x)", __func__, devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800539 if (devices == AUDIO_DEVICE_NONE ||
540 devices & AUDIO_DEVICE_BIT_IN) {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800541 ALOGV("%s: Invalid output devices (%#x)", __func__, devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800542 goto exit;
543 }
544
545 if (mode == AUDIO_MODE_IN_CALL) {
546 if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
547 devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800548 if (adev->tty_mode == TTY_MODE_FULL)
549 snd_device = SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES;
550 else if (adev->tty_mode == TTY_MODE_VCO)
551 snd_device = SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES;
552 else if (adev->tty_mode == TTY_MODE_HCO)
553 snd_device = SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET;
554 else
555 snd_device = SND_DEVICE_OUT_VOICE_HEADPHONES;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800556 } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
557 snd_device = SND_DEVICE_OUT_BT_SCO;
558 } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
559 snd_device = SND_DEVICE_OUT_VOICE_SPEAKER;
560 } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
Ravi Kumar Alamanda37718842013-02-22 18:44:45 -0800561 if (is_operator_tmus())
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800562 snd_device = SND_DEVICE_OUT_VOICE_HANDSET_TMUS;
563 else
564 snd_device = SND_DEVICE_OUT_HANDSET;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800565 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800566 if (snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800567 goto exit;
568 }
569 }
570
571 if (popcount(devices) == 2) {
572 if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
573 AUDIO_DEVICE_OUT_SPEAKER)) {
574 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
575 } else if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADSET |
576 AUDIO_DEVICE_OUT_SPEAKER)) {
577 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
578 } else if (devices == (AUDIO_DEVICE_OUT_AUX_DIGITAL |
579 AUDIO_DEVICE_OUT_SPEAKER)) {
580 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HDMI;
581 } else {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800582 ALOGE("%s: Invalid combo device(%#x)", __func__, devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800583 goto exit;
584 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800585 if (snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800586 goto exit;
587 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800588 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800589
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800590 if (popcount(devices) != 1) {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800591 ALOGE("%s: Invalid output devices(%#x)", __func__, devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800592 goto exit;
593 }
594
595 if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
596 devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
597 snd_device = SND_DEVICE_OUT_HEADPHONES;
598 } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
599 snd_device = SND_DEVICE_OUT_SPEAKER;
600 } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
601 snd_device = SND_DEVICE_OUT_BT_SCO;
602 } else if (devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
603 snd_device = SND_DEVICE_OUT_HDMI ;
604 } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
605 snd_device = SND_DEVICE_OUT_HANDSET;
606 } else {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800607 ALOGE("%s: Unknown device(s) %#x", __func__, devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800608 }
609exit:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800610 ALOGV("%s: exit: snd_device(%s)", __func__, device_table[snd_device]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800611 return snd_device;
612}
613
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700614static snd_device_t get_input_snd_device(struct audio_device *adev,
615 audio_devices_t out_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800616{
Eric Laurentc8400632013-02-14 19:04:54 -0800617 audio_source_t source = (adev->active_input == NULL) ?
618 AUDIO_SOURCE_DEFAULT : adev->active_input->source;
619
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800620 audio_mode_t mode = adev->mode;
Eric Laurentc8400632013-02-14 19:04:54 -0800621 audio_devices_t in_device = ((adev->active_input == NULL) ?
622 AUDIO_DEVICE_NONE : adev->active_input->device)
623 & ~AUDIO_DEVICE_BIT_IN;
624 audio_channel_mask_t channel_mask = (adev->active_input == NULL) ?
625 AUDIO_CHANNEL_IN_MONO : adev->active_input->channel_mask;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800626 snd_device_t snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800627
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800628 ALOGV("%s: enter: out_device(%#x) in_device(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800629 __func__, out_device, in_device);
630 if (mode == AUDIO_MODE_IN_CALL) {
631 if (out_device == AUDIO_DEVICE_NONE) {
632 ALOGE("%s: No output device set for voice call", __func__);
633 goto exit;
634 }
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800635 if (adev->tty_mode != TTY_MODE_OFF) {
636 if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
637 out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
638 switch (adev->tty_mode) {
639 case TTY_MODE_FULL:
640 snd_device = SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC;
641 break;
642 case TTY_MODE_VCO:
643 snd_device = SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC;
644 break;
645 case TTY_MODE_HCO:
646 snd_device = SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC;
647 break;
648 default:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800649 ALOGE("%s: Invalid TTY mode (%#x)", __func__, adev->tty_mode);
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800650 }
651 goto exit;
652 }
653 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800654 if (out_device & AUDIO_DEVICE_OUT_EARPIECE ||
655 out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800656 if (adev->mic_type_analog || adev->fluence_in_voice_call == false) {
657 snd_device = SND_DEVICE_IN_HANDSET_MIC;
658 } else {
659 if (adev->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
Ravi Kumar Alamanda37718842013-02-22 18:44:45 -0800660 if (is_operator_tmus())
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800661 snd_device = SND_DEVICE_IN_VOICE_DMIC_EF_TMUS;
662 else
663 snd_device = SND_DEVICE_IN_VOICE_DMIC_EF;
664 } else if(adev->dualmic_config == DUALMIC_CONFIG_BROADSIDE)
665 snd_device = SND_DEVICE_IN_VOICE_DMIC_BS;
666 else
667 snd_device = SND_DEVICE_IN_HANDSET_MIC;
668 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800669 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
670 snd_device = SND_DEVICE_IN_VOICE_HEADSET_MIC;
671 } else if (out_device & AUDIO_DEVICE_OUT_ALL_SCO) {
672 snd_device = SND_DEVICE_IN_BT_SCO_MIC ;
673 } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -0800674 if (adev->fluence_in_voice_call && adev->fluence_in_spkr_mode &&
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800675 adev->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
676 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF;
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -0800677 } else if (adev->fluence_in_voice_call && adev->fluence_in_spkr_mode &&
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800678 adev->dualmic_config == DUALMIC_CONFIG_BROADSIDE) {
679 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS;
680 } else {
681 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC;
682 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800683 }
684 } else if (source == AUDIO_SOURCE_CAMCORDER) {
685 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC ||
686 in_device & AUDIO_DEVICE_IN_BACK_MIC) {
687 snd_device = SND_DEVICE_IN_CAMCORDER_MIC;
688 }
689 } else if (source == AUDIO_SOURCE_VOICE_RECOGNITION) {
690 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
Eric Laurentc8400632013-02-14 19:04:54 -0800691 if (adev->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
692 if (channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK)
693 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_EF;
694 else if (adev->fluence_in_voice_rec)
695 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE;
696 else
697 snd_device = SND_DEVICE_IN_VOICE_REC_MIC;
698 } else if (adev->dualmic_config == DUALMIC_CONFIG_BROADSIDE) {
699 if (channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK)
700 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_BS;
701 else if (adev->fluence_in_voice_rec)
702 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE;
703 else
704 snd_device = SND_DEVICE_IN_VOICE_REC_MIC;
705 } else
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800706 snd_device = SND_DEVICE_IN_VOICE_REC_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800707 }
Ravi Kumar Alamanda8455fa72013-02-19 14:53:19 -0800708 } else if (source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
709 if (out_device & AUDIO_DEVICE_OUT_SPEAKER)
710 in_device = AUDIO_DEVICE_IN_BACK_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800711 } else if (source == AUDIO_SOURCE_DEFAULT) {
712 goto exit;
713 }
714
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800715 if (snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800716 goto exit;
717 }
718
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800719 if (in_device != AUDIO_DEVICE_NONE &&
Ravi Kumar Alamanda8455fa72013-02-19 14:53:19 -0800720 !(in_device & AUDIO_DEVICE_IN_VOICE_CALL) &&
721 !(in_device & AUDIO_DEVICE_IN_COMMUNICATION)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800722 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800723 snd_device = SND_DEVICE_IN_HANDSET_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800724 } else if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800725 if (adev->mic_type_analog)
726 snd_device = SND_DEVICE_IN_HANDSET_MIC;
727 else
728 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800729 } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
730 snd_device = SND_DEVICE_IN_HEADSET_MIC;
731 } else if (in_device & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
732 snd_device = SND_DEVICE_IN_BT_SCO_MIC ;
733 } else if (in_device & AUDIO_DEVICE_IN_AUX_DIGITAL) {
734 snd_device = SND_DEVICE_IN_HDMI_MIC;
735 } else {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800736 ALOGE("%s: Unknown input device(s) %#x", __func__, in_device);
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800737 ALOGW("%s: Using default handset-mic", __func__);
738 snd_device = SND_DEVICE_IN_HANDSET_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800739 }
740 } else {
741 if (out_device & AUDIO_DEVICE_OUT_EARPIECE) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800742 snd_device = SND_DEVICE_IN_HANDSET_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800743 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
744 snd_device = SND_DEVICE_IN_HEADSET_MIC;
745 } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
746 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
747 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800748 snd_device = SND_DEVICE_IN_HANDSET_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800749 } else if (out_device & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800750 snd_device = SND_DEVICE_IN_BT_SCO_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800751 } else if (out_device & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
752 snd_device = SND_DEVICE_IN_HDMI_MIC;
753 } else {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800754 ALOGE("%s: Unknown output device(s) %#x", __func__, out_device);
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800755 ALOGW("%s: Using default handset-mic", __func__);
756 snd_device = SND_DEVICE_IN_HANDSET_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800757 }
758 }
759exit:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800760 ALOGV("%s: exit: in_snd_device(%s)", __func__, device_table[snd_device]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800761 return snd_device;
762}
763
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700764static struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
765 audio_usecase_t uc_id)
766{
767 struct audio_usecase *usecase;
768 struct listnode *node;
769
770 list_for_each(node, &adev->usecase_list) {
771 usecase = node_to_item(node, struct audio_usecase, list);
772 if (usecase->id == uc_id)
773 return usecase;
774 }
775 return NULL;
776}
777
778static int select_devices(struct audio_device *adev,
779 audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800780{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800781 snd_device_t out_snd_device = SND_DEVICE_NONE;
782 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700783 struct audio_usecase *usecase = NULL;
784 struct audio_usecase *vc_usecase = NULL;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800785 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800786 int acdb_rx_id, acdb_tx_id;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700787 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800788
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700789 usecase = get_usecase_from_list(adev, uc_id);
790 if (usecase == NULL) {
791 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
792 return -EINVAL;
793 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800794
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700795 if (usecase->type == VOICE_CALL) {
796 out_snd_device = get_output_snd_device(adev, usecase->stream.out->devices);
797 in_snd_device = get_input_snd_device(adev, usecase->stream.out->devices);
798 usecase->devices = usecase->stream.out->devices;
799 } else {
800 /*
801 * If the voice call is active, use the sound devices of voice call usecase
802 * so that it would not result any device switch. All the usecases will
803 * be switched to new device when select_devices() is called for voice call
804 * usecase. This is to avoid switching devices for voice call when
805 * check_usecases_codec_backend() is called below.
806 */
807 if (adev->in_call) {
808 vc_usecase = get_usecase_from_list(adev, USECASE_VOICE_CALL);
809 if (vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
810 in_snd_device = vc_usecase->in_snd_device;
811 out_snd_device = vc_usecase->out_snd_device;
812 }
813 }
814 if (usecase->type == PCM_PLAYBACK) {
815 usecase->devices = usecase->stream.out->devices;
816 in_snd_device = SND_DEVICE_NONE;
817 if (out_snd_device == SND_DEVICE_NONE)
818 out_snd_device = get_output_snd_device(adev,
819 usecase->stream.out->devices);
820 } else if (usecase->type == PCM_CAPTURE) {
821 usecase->devices = usecase->stream.in->device;
822 out_snd_device = SND_DEVICE_NONE;
823 if (in_snd_device == SND_DEVICE_NONE)
824 in_snd_device = get_input_snd_device(adev, SND_DEVICE_NONE);
825 }
826 }
827
828 if (out_snd_device == usecase->out_snd_device &&
829 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800830 return 0;
831 }
832
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800833 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
834 out_snd_device, device_table[out_snd_device],
835 in_snd_device, device_table[in_snd_device]);
836
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800837 /*
838 * Limitation: While in call, to do a device switch we need to disable
839 * and enable both RX and TX devices though one of them is same as current
840 * device.
841 */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700842 if (usecase->type == VOICE_CALL && adev->csd_client != NULL &&
843 usecase->in_snd_device != SND_DEVICE_NONE &&
844 usecase->out_snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800845 /* This must be called before disabling the mixer controls on APQ side */
846 if (adev->csd_disable_device == NULL) {
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -0800847 ALOGE("%s: dlsym error for csd_client_disable_device", __func__);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800848 } else {
849 status = adev->csd_disable_device();
850 if (status < 0) {
851 ALOGE("%s: csd_client_disable_device, failed, error %d",
852 __func__, status);
853 }
854 }
855 }
856
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700857 /* Disable current sound devices */
858 if (usecase->out_snd_device != SND_DEVICE_NONE) {
859 disable_audio_route(adev, usecase, true);
860 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800861 }
862
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700863 if (usecase->in_snd_device != SND_DEVICE_NONE) {
864 disable_audio_route(adev, usecase, true);
865 disable_snd_device(adev, usecase->in_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800866 }
867
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700868 /* Enable new sound devices */
869 if (out_snd_device != SND_DEVICE_NONE) {
870 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
871 check_usecases_codec_backend(adev, usecase, out_snd_device);
872 enable_snd_device(adev, out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800873 }
874
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700875 if (in_snd_device != SND_DEVICE_NONE)
876 enable_snd_device(adev, in_snd_device, false);
877
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800878 audio_route_update_mixer(adev->audio_route);
879
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700880 usecase->in_snd_device = in_snd_device;
881 usecase->out_snd_device = out_snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800882
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700883 enable_audio_route(adev, usecase, true);
884
885 if (usecase->type == VOICE_CALL && adev->csd_client) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800886 if (adev->csd_enable_device == NULL) {
887 ALOGE("%s: dlsym error for csd_client_enable_device",
888 __func__);
889 } else {
890 acdb_rx_id = get_acdb_device_id(out_snd_device);
891 acdb_tx_id = get_acdb_device_id(in_snd_device);
892
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700893 if (acdb_rx_id > 0 || acdb_tx_id > 0) {
894 status = adev->csd_enable_device(acdb_rx_id, acdb_tx_id,
895 adev->acdb_settings);
896 if (status < 0) {
897 ALOGE("%s: csd_client_enable_device, failed, error %d",
898 __func__, status);
899 }
900 } else {
901 ALOGE("%s: Incorrect ACDB IDs (rx: %d tx: %d)", __func__,
902 acdb_rx_id, acdb_tx_id);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800903 }
904 }
905 }
906
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800907 return status;
908}
909
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800910static int stop_input_stream(struct stream_in *in)
911{
912 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800913 struct audio_usecase *uc_info;
914 struct audio_device *adev = in->dev;
915
Eric Laurentc8400632013-02-14 19:04:54 -0800916 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800917
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700918 ALOGD("%s: enter: usecase(%d: %s)", __func__,
919 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800920 uc_info = get_usecase_from_list(adev, in->usecase);
921 if (uc_info == NULL) {
922 ALOGE("%s: Could not find the usecase (%d) in the list",
923 __func__, in->usecase);
924 return -EINVAL;
925 }
926
Eric Laurent150dbfe2013-02-27 14:31:02 -0800927 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700928 disable_audio_route(adev, uc_info, true);
929
930 /* 2. Disable the tx device */
931 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800932
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800933 list_remove(&uc_info->list);
934 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800935
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800936 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800937 return ret;
938}
939
940int start_input_stream(struct stream_in *in)
941{
942 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800943 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800944 struct audio_usecase *uc_info;
945 struct audio_device *adev = in->dev;
946
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800947 ALOGD("%s: enter: usecase(%d)", __func__, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800948 in->pcm_device_id = get_pcm_device_id(adev->audio_route,
949 in->usecase,
950 PCM_CAPTURE);
951 if (in->pcm_device_id < 0) {
952 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
953 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800954 ret = -EINVAL;
955 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800956 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700957
958 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800959 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
960 uc_info->id = in->usecase;
961 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800962 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700963 uc_info->devices = in->device;
964 uc_info->in_snd_device = SND_DEVICE_NONE;
965 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800966
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800967 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700968 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800969
Eric Laurentc8400632013-02-14 19:04:54 -0800970 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
971 __func__, SOUND_CARD, in->pcm_device_id, in->config.channels);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800972 in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id,
973 PCM_IN, &in->config);
974 if (in->pcm && !pcm_is_ready(in->pcm)) {
975 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
976 pcm_close(in->pcm);
977 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800978 ret = -EIO;
979 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800980 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800981 ALOGD("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800982 return ret;
983
984error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800985 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800986
987error_config:
988 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700989 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800990
991 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800992}
993
994static int stop_output_stream(struct stream_out *out)
995{
996 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800997 struct audio_usecase *uc_info;
998 struct audio_device *adev = out->dev;
999
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001000 ALOGD("%s: enter: usecase(%d: %s)", __func__,
1001 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001002 uc_info = get_usecase_from_list(adev, out->usecase);
1003 if (uc_info == NULL) {
1004 ALOGE("%s: Could not find the usecase (%d) in the list",
1005 __func__, out->usecase);
1006 return -EINVAL;
1007 }
1008
Eric Laurent150dbfe2013-02-27 14:31:02 -08001009 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001010 disable_audio_route(adev, uc_info, true);
1011
1012 /* 2. Disable the rx device */
1013 disable_snd_device(adev, uc_info->out_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001014
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001015 list_remove(&uc_info->list);
1016 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001017
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001018 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001019 return ret;
1020}
1021
1022int start_output_stream(struct stream_out *out)
1023{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001024 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001025 struct audio_usecase *uc_info;
1026 struct audio_device *adev = out->dev;
1027
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001028 ALOGD("%s: enter: usecase(%d: %s) devices(%#x)",
1029 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001030 out->pcm_device_id = get_pcm_device_id(adev->audio_route,
1031 out->usecase,
1032 PCM_PLAYBACK);
1033 if (out->pcm_device_id < 0) {
1034 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1035 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001036 ret = -EINVAL;
1037 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001038 }
1039
1040 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1041 uc_info->id = out->usecase;
1042 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001043 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001044 uc_info->devices = out->devices;
1045 uc_info->in_snd_device = SND_DEVICE_NONE;
1046 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001047
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001048 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001049
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001050 select_devices(adev, out->usecase);
1051
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001052 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
1053 __func__, 0, out->pcm_device_id);
1054 out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id,
1055 PCM_OUT, &out->config);
1056 if (out->pcm && !pcm_is_ready(out->pcm)) {
1057 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1058 pcm_close(out->pcm);
1059 out->pcm = NULL;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001060 ret = -EIO;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001061 goto error_pcm_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001062 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001063 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001064 return 0;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001065error_pcm_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001066 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001067error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001068 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001069}
1070
1071static int stop_voice_call(struct audio_device *adev)
1072{
1073 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001074 struct audio_usecase *uc_info;
1075
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001076 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001077 adev->in_call = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001078 if (adev->csd_client) {
1079 if (adev->csd_stop_voice == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08001080 ALOGE("dlsym error for csd_client_disable_device");
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001081 } else {
1082 ret = adev->csd_stop_voice();
1083 if (ret < 0) {
1084 ALOGE("%s: csd_client error %d\n", __func__, ret);
1085 }
1086 }
1087 }
1088
1089 /* 1. Close the PCM devices */
1090 if (adev->voice_call_rx) {
1091 pcm_close(adev->voice_call_rx);
1092 adev->voice_call_rx = NULL;
1093 }
1094 if (adev->voice_call_tx) {
1095 pcm_close(adev->voice_call_tx);
1096 adev->voice_call_tx = NULL;
1097 }
1098
1099 uc_info = get_usecase_from_list(adev, USECASE_VOICE_CALL);
1100 if (uc_info == NULL) {
1101 ALOGE("%s: Could not find the usecase (%d) in the list",
1102 __func__, USECASE_VOICE_CALL);
1103 return -EINVAL;
1104 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001105
1106 /* 2. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001107 disable_audio_route(adev, uc_info, true);
1108
1109 /* 3. Disable the rx and tx devices */
1110 disable_snd_device(adev, uc_info->out_snd_device, false);
1111 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001112
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001113 list_remove(&uc_info->list);
1114 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001115
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001116 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001117 return ret;
1118}
1119
1120static int start_voice_call(struct audio_device *adev)
1121{
1122 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001123 struct audio_usecase *uc_info;
1124 int pcm_dev_rx_id, pcm_dev_tx_id;
1125
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001126 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001127
1128 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1129 uc_info->id = USECASE_VOICE_CALL;
1130 uc_info->type = VOICE_CALL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001131 uc_info->stream.out = adev->primary_output;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001132 uc_info->devices = adev->primary_output->devices;
1133 uc_info->in_snd_device = SND_DEVICE_NONE;
1134 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001135
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001136 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001137
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001138 select_devices(adev, USECASE_VOICE_CALL);
1139
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001140 pcm_dev_rx_id = get_pcm_device_id(adev->audio_route, uc_info->id,
1141 PCM_PLAYBACK);
1142 pcm_dev_tx_id = get_pcm_device_id(adev->audio_route, uc_info->id,
1143 PCM_CAPTURE);
1144
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001145 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
1146 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
1147 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001148 ret = -EIO;
1149 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001150 }
1151
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001152 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001153 __func__, SOUND_CARD, pcm_dev_rx_id);
1154 adev->voice_call_rx = pcm_open(SOUND_CARD,
1155 pcm_dev_rx_id,
1156 PCM_OUT, &pcm_config_voice_call);
1157 if (adev->voice_call_rx && !pcm_is_ready(adev->voice_call_rx)) {
1158 ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_rx));
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001159 ret = -EIO;
1160 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001161 }
1162
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001163 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001164 __func__, SOUND_CARD, pcm_dev_tx_id);
1165 adev->voice_call_tx = pcm_open(SOUND_CARD,
1166 pcm_dev_tx_id,
1167 PCM_IN, &pcm_config_voice_call);
1168 if (adev->voice_call_tx && !pcm_is_ready(adev->voice_call_tx)) {
1169 ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_tx));
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001170 ret = -EIO;
1171 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001172 }
1173 pcm_start(adev->voice_call_rx);
1174 pcm_start(adev->voice_call_tx);
1175
1176 if (adev->csd_client) {
1177 if (adev->csd_start_voice == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08001178 ALOGE("dlsym error for csd_client_start_voice");
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001179 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001180 } else {
1181 ret = adev->csd_start_voice();
1182 if (ret < 0) {
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001183 ALOGE("%s: csd_start_voice error %d\n", __func__, ret);
1184 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001185 }
1186 }
1187 }
1188
1189 adev->in_call = true;
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001190 return 0;
1191
1192error_start_voice:
1193 stop_voice_call(adev);
1194
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001195 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001196 return ret;
1197}
1198
1199static int check_input_parameters(uint32_t sample_rate,
1200 audio_format_t format,
1201 int channel_count)
1202{
1203 if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
1204
1205 if ((channel_count < 1) || (channel_count > 2)) return -EINVAL;
1206
1207 switch (sample_rate) {
1208 case 8000:
1209 case 11025:
1210 case 12000:
1211 case 16000:
1212 case 22050:
1213 case 24000:
1214 case 32000:
1215 case 44100:
1216 case 48000:
1217 break;
1218 default:
1219 return -EINVAL;
1220 }
1221
1222 return 0;
1223}
1224
1225static size_t get_input_buffer_size(uint32_t sample_rate,
1226 audio_format_t format,
1227 int channel_count)
1228{
1229 size_t size = 0;
1230
1231 if (check_input_parameters(sample_rate, format, channel_count) != 0) return 0;
1232
1233 if (sample_rate == 8000 || sample_rate == 16000 || sample_rate == 32000) {
1234 size = (sample_rate * 20) / 1000;
1235 } else if (sample_rate == 11025 || sample_rate == 12000) {
1236 size = 256;
1237 } else if (sample_rate == 22050 || sample_rate == 24000) {
1238 size = 512;
1239 } else if (sample_rate == 44100 || sample_rate == 48000) {
1240 size = 1024;
1241 }
1242
1243 return size * sizeof(short) * channel_count;
1244}
1245
1246static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1247{
1248 struct stream_out *out = (struct stream_out *)stream;
1249
1250 return out->config.rate;
1251}
1252
1253static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1254{
1255 return -ENOSYS;
1256}
1257
1258static size_t out_get_buffer_size(const struct audio_stream *stream)
1259{
1260 struct stream_out *out = (struct stream_out *)stream;
1261
1262 return out->config.period_size * audio_stream_frame_size(stream);
1263}
1264
1265static uint32_t out_get_channels(const struct audio_stream *stream)
1266{
1267 struct stream_out *out = (struct stream_out *)stream;
1268
1269 return out->channel_mask;
1270}
1271
1272static audio_format_t out_get_format(const struct audio_stream *stream)
1273{
1274 return AUDIO_FORMAT_PCM_16_BIT;
1275}
1276
1277static int out_set_format(struct audio_stream *stream, audio_format_t format)
1278{
1279 return -ENOSYS;
1280}
1281
1282static int out_standby(struct audio_stream *stream)
1283{
1284 struct stream_out *out = (struct stream_out *)stream;
1285 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001286 ALOGD("%s: enter: usecase(%d: %s)", __func__,
1287 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001288 pthread_mutex_lock(&out->lock);
1289
1290 if (!out->standby) {
1291 out->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001292 if (out->pcm) {
1293 pcm_close(out->pcm);
1294 out->pcm = NULL;
1295 }
1296 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001297 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001298 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001299 }
1300 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001301 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001302 return 0;
1303}
1304
1305static int out_dump(const struct audio_stream *stream, int fd)
1306{
1307 return 0;
1308}
1309
1310static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1311{
1312 struct stream_out *out = (struct stream_out *)stream;
1313 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001314 struct audio_usecase *usecase;
1315 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001316 struct str_parms *parms;
1317 char value[32];
1318 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001319 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001320
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001321 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
1322 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001323 parms = str_parms_create_str(kvpairs);
1324 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1325 if (ret >= 0) {
1326 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001327 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001328 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001329
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001330 /*
1331 * When HDMI cable is unplugged the music playback is paused and
1332 * the policy manager sends routing=0. But the audioflinger
1333 * continues to write data until standby time (3sec).
1334 * As the HDMI core is turned off, the write gets blocked.
1335 * Avoid this by routing audio to speaker until standby.
1336 */
1337 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1338 val == AUDIO_DEVICE_NONE) {
1339 val = AUDIO_DEVICE_OUT_SPEAKER;
1340 }
1341
1342 /*
1343 * select_devices() call below switches all the usecases on the same
1344 * backend to the new device. Refer to check_usecases_codec_backend() in
1345 * the select_devices(). But how do we undo this?
1346 *
1347 * For example, music playback is active on headset (deep-buffer usecase)
1348 * and if we go to ringtones and select a ringtone, low-latency usecase
1349 * will be started on headset+speaker. As we can't enable headset+speaker
1350 * and headset devices at the same time, select_devices() switches the music
1351 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1352 * So when the ringtone playback is completed, how do we undo the same?
1353 *
1354 * We are relying on the out_set_parameters() call on deep-buffer output,
1355 * once the ringtone playback is ended.
1356 * NOTE: We should not check if the current devices are same as new devices.
1357 * Because select_devices() must be called to switch back the music
1358 * playback to headset.
1359 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001360 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001361 out->devices = val;
1362
1363 if (!out->standby)
1364 select_devices(adev, out->usecase);
1365
1366 if ((adev->mode == AUDIO_MODE_IN_CALL) && !adev->in_call &&
1367 (out == adev->primary_output)) {
1368 start_voice_call(adev);
1369 } else if ((adev->mode == AUDIO_MODE_IN_CALL) && adev->in_call &&
1370 (out == adev->primary_output)) {
1371 select_devices(adev, USECASE_VOICE_CALL);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001372 }
1373 }
1374
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001375 if ((adev->mode != AUDIO_MODE_IN_CALL) && adev->in_call &&
1376 (out == adev->primary_output)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001377 stop_voice_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001378 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001379
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001380 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001381 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001382 }
1383 str_parms_destroy(parms);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001384 ALOGD("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001385 return ret;
1386}
1387
1388static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1389{
1390 struct stream_out *out = (struct stream_out *)stream;
1391 struct str_parms *query = str_parms_create_str(keys);
1392 char *str;
1393 char value[256];
1394 struct str_parms *reply = str_parms_create();
1395 size_t i, j;
1396 int ret;
1397 bool first = true;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001398 ALOGD("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001399 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1400 if (ret >= 0) {
1401 value[0] = '\0';
1402 i = 0;
1403 while (out->supported_channel_masks[i] != 0) {
1404 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1405 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1406 if (!first) {
1407 strcat(value, "|");
1408 }
1409 strcat(value, out_channels_name_to_enum_table[j].name);
1410 first = false;
1411 break;
1412 }
1413 }
1414 i++;
1415 }
1416 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1417 str = str_parms_to_str(reply);
1418 } else {
1419 str = strdup(keys);
1420 }
1421 str_parms_destroy(query);
1422 str_parms_destroy(reply);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001423 ALOGD("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001424 return str;
1425}
1426
1427static uint32_t out_get_latency(const struct audio_stream_out *stream)
1428{
1429 struct stream_out *out = (struct stream_out *)stream;
1430
1431 return (out->config.period_count * out->config.period_size * 1000) / (out->config.rate);
1432}
1433
1434static int out_set_volume(struct audio_stream_out *stream, float left,
1435 float right)
1436{
Eric Laurenta9024de2013-04-04 09:19:12 -07001437 struct stream_out *out = (struct stream_out *)stream;
1438 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1439 /* only take left channel into account: the API is for stereo anyway */
1440 out->muted = (left == 0.0f);
1441 return 0;
1442 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001443 return -ENOSYS;
1444}
1445
1446static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1447 size_t bytes)
1448{
1449 struct stream_out *out = (struct stream_out *)stream;
1450 struct audio_device *adev = out->dev;
1451 int i, ret = -1;
1452
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001453 pthread_mutex_lock(&out->lock);
1454 if (out->standby) {
Eric Laurent150dbfe2013-02-27 14:31:02 -08001455 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001456 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001457 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001458 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001459 goto exit;
1460 }
1461 out->standby = false;
1462 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001463
1464 if (out->pcm) {
Eric Laurenta9024de2013-04-04 09:19:12 -07001465 if (out->muted)
1466 memset((void *)buffer, 0, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001467 //ALOGV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1468 ret = pcm_write(out->pcm, (void *)buffer, bytes);
1469 }
1470
1471exit:
1472 pthread_mutex_unlock(&out->lock);
1473
1474 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001475 if (out->pcm)
1476 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001477 out_standby(&out->stream.common);
1478 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1479 out_get_sample_rate(&out->stream.common));
1480 }
1481 return bytes;
1482}
1483
1484static int out_get_render_position(const struct audio_stream_out *stream,
1485 uint32_t *dsp_frames)
1486{
1487 return -EINVAL;
1488}
1489
1490static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1491{
1492 return 0;
1493}
1494
1495static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1496{
1497 return 0;
1498}
1499
1500static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1501 int64_t *timestamp)
1502{
1503 return -EINVAL;
1504}
1505
1506/** audio_stream_in implementation **/
1507static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1508{
1509 struct stream_in *in = (struct stream_in *)stream;
1510
1511 return in->config.rate;
1512}
1513
1514static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1515{
1516 return -ENOSYS;
1517}
1518
1519static size_t in_get_buffer_size(const struct audio_stream *stream)
1520{
1521 struct stream_in *in = (struct stream_in *)stream;
1522
1523 return in->config.period_size * audio_stream_frame_size(stream);
1524}
1525
1526static uint32_t in_get_channels(const struct audio_stream *stream)
1527{
1528 struct stream_in *in = (struct stream_in *)stream;
1529
1530 return in->channel_mask;
1531}
1532
1533static audio_format_t in_get_format(const struct audio_stream *stream)
1534{
1535 return AUDIO_FORMAT_PCM_16_BIT;
1536}
1537
1538static int in_set_format(struct audio_stream *stream, audio_format_t format)
1539{
1540 return -ENOSYS;
1541}
1542
1543static int in_standby(struct audio_stream *stream)
1544{
1545 struct stream_in *in = (struct stream_in *)stream;
1546 struct audio_device *adev = in->dev;
1547 int status = 0;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001548 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001549 pthread_mutex_lock(&in->lock);
1550 if (!in->standby) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001551 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001552 if (in->pcm) {
1553 pcm_close(in->pcm);
1554 in->pcm = NULL;
1555 }
1556 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001557 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001558 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001559 }
1560 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001561 ALOGD("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001562 return status;
1563}
1564
1565static int in_dump(const struct audio_stream *stream, int fd)
1566{
1567 return 0;
1568}
1569
1570static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1571{
1572 struct stream_in *in = (struct stream_in *)stream;
1573 struct audio_device *adev = in->dev;
1574 struct str_parms *parms;
1575 char *str;
1576 char value[32];
1577 int ret, val = 0;
1578
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001579 ALOGD("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001580 parms = str_parms_create_str(kvpairs);
1581
1582 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1583
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001584 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001585 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001586 if (ret >= 0) {
1587 val = atoi(value);
1588 /* no audio source uses val == 0 */
1589 if ((in->source != val) && (val != 0)) {
1590 in->source = val;
1591 }
1592 }
1593
1594 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1595 if (ret >= 0) {
1596 val = atoi(value);
1597 if ((in->device != val) && (val != 0)) {
1598 in->device = val;
1599 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001600 if (!in->standby)
1601 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001602 }
1603 }
1604
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001605 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001606 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001607
1608 str_parms_destroy(parms);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001609 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001610 return ret;
1611}
1612
1613static char* in_get_parameters(const struct audio_stream *stream,
1614 const char *keys)
1615{
1616 return strdup("");
1617}
1618
1619static int in_set_gain(struct audio_stream_in *stream, float gain)
1620{
1621 return 0;
1622}
1623
1624static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1625 size_t bytes)
1626{
1627 struct stream_in *in = (struct stream_in *)stream;
1628 struct audio_device *adev = in->dev;
1629 int i, ret = -1;
1630
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001631 pthread_mutex_lock(&in->lock);
1632 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001633 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001634 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001635 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001636 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001637 goto exit;
1638 }
1639 in->standby = 0;
1640 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001641
1642 if (in->pcm) {
1643 ret = pcm_read(in->pcm, buffer, bytes);
1644 }
1645
1646 /*
1647 * Instead of writing zeroes here, we could trust the hardware
1648 * to always provide zeroes when muted.
1649 */
1650 if (ret == 0 && adev->mic_mute)
1651 memset(buffer, 0, bytes);
1652
1653exit:
1654 pthread_mutex_unlock(&in->lock);
1655
1656 if (ret != 0) {
1657 in_standby(&in->stream.common);
1658 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1659 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1660 in_get_sample_rate(&in->stream.common));
1661 }
1662 return bytes;
1663}
1664
1665static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1666{
1667 return 0;
1668}
1669
1670static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1671{
1672 return 0;
1673}
1674
1675static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1676{
1677 return 0;
1678}
1679
1680static int adev_open_output_stream(struct audio_hw_device *dev,
1681 audio_io_handle_t handle,
1682 audio_devices_t devices,
1683 audio_output_flags_t flags,
1684 struct audio_config *config,
1685 struct audio_stream_out **stream_out)
1686{
1687 struct audio_device *adev = (struct audio_device *)dev;
1688 struct stream_out *out;
1689 int i, ret;
1690
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001691 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001692 __func__, config->sample_rate, config->channel_mask, devices, flags);
1693 *stream_out = NULL;
1694 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1695
1696 if (devices == AUDIO_DEVICE_NONE)
1697 devices = AUDIO_DEVICE_OUT_SPEAKER;
1698
1699 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
1700 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1701 out->flags = flags;
1702 out->devices = devices;
1703
1704 /* Init use case and pcm_config */
1705 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
1706 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001707 pthread_mutex_lock(&adev->lock);
1708 ret = read_hdmi_channel_masks(out);
1709 pthread_mutex_unlock(&adev->lock);
1710 if (ret != 0) {
1711 /* If HDMI does not support multi channel playback, set the default */
1712 out->config.channels = popcount(out->channel_mask);
1713 set_hdmi_channels(adev->mixer, out->config.channels);
1714 goto error_open;
1715 }
1716
1717 if (config->sample_rate == 0)
1718 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1719 if (config->channel_mask == 0)
1720 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1721
1722 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001723 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
1724 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001725 out->config.rate = config->sample_rate;
1726 out->config.channels = popcount(out->channel_mask);
1727 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
1728 set_hdmi_channels(adev->mixer, out->config.channels);
1729 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
1730 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
1731 out->config = pcm_config_deep_buffer;
1732 } else {
1733 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
1734 out->config = pcm_config_low_latency;
1735 }
1736
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001737 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1738 if(adev->primary_output == NULL)
1739 adev->primary_output = out;
1740 else {
1741 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001742 ret = -EEXIST;
1743 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001744 }
1745 }
1746
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001747 /* Check if this usecase is already existing */
1748 pthread_mutex_lock(&adev->lock);
1749 if (get_usecase_from_list(adev, out->usecase) != NULL) {
1750 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001751 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001752 ret = -EEXIST;
1753 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001754 }
1755 pthread_mutex_unlock(&adev->lock);
1756
1757 out->stream.common.get_sample_rate = out_get_sample_rate;
1758 out->stream.common.set_sample_rate = out_set_sample_rate;
1759 out->stream.common.get_buffer_size = out_get_buffer_size;
1760 out->stream.common.get_channels = out_get_channels;
1761 out->stream.common.get_format = out_get_format;
1762 out->stream.common.set_format = out_set_format;
1763 out->stream.common.standby = out_standby;
1764 out->stream.common.dump = out_dump;
1765 out->stream.common.set_parameters = out_set_parameters;
1766 out->stream.common.get_parameters = out_get_parameters;
1767 out->stream.common.add_audio_effect = out_add_audio_effect;
1768 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1769 out->stream.get_latency = out_get_latency;
1770 out->stream.set_volume = out_set_volume;
1771 out->stream.write = out_write;
1772 out->stream.get_render_position = out_get_render_position;
1773 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
1774
1775 out->dev = adev;
1776 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07001777 /* out->muted = false; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001778
1779 config->format = out->stream.common.get_format(&out->stream.common);
1780 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
1781 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
1782
1783 *stream_out = &out->stream;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001784 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001785 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001786
1787error_open:
1788 free(out);
1789 *stream_out = NULL;
1790 ALOGD("%s: exit: ret %d", __func__, ret);
1791 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001792}
1793
1794static void adev_close_output_stream(struct audio_hw_device *dev,
1795 struct audio_stream_out *stream)
1796{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001797 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001798 out_standby(&stream->common);
1799 free(stream);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001800 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001801}
1802
1803static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
1804{
1805 struct audio_device *adev = (struct audio_device *)dev;
1806 struct str_parms *parms;
1807 char *str;
1808 char value[32];
1809 int ret;
1810
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001811 ALOGD("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001812
1813 parms = str_parms_create_str(kvpairs);
1814 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value));
1815 if (ret >= 0) {
1816 int tty_mode;
1817
1818 if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0)
1819 tty_mode = TTY_MODE_OFF;
1820 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0)
1821 tty_mode = TTY_MODE_VCO;
1822 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0)
1823 tty_mode = TTY_MODE_HCO;
1824 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0)
1825 tty_mode = TTY_MODE_FULL;
1826 else
1827 return -EINVAL;
1828
1829 pthread_mutex_lock(&adev->lock);
1830 if (tty_mode != adev->tty_mode) {
1831 adev->tty_mode = tty_mode;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08001832 adev->acdb_settings = (adev->acdb_settings & TTY_MODE_CLEAR) | tty_mode;
1833 if (adev->in_call)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001834 select_devices(adev, USECASE_VOICE_CALL);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001835 }
1836 pthread_mutex_unlock(&adev->lock);
1837 }
1838
1839 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
1840 if (ret >= 0) {
1841 /* When set to false, HAL should disable EC and NS
1842 * But it is currently not supported.
1843 */
1844 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
1845 adev->bluetooth_nrec = true;
1846 else
1847 adev->bluetooth_nrec = false;
1848 }
1849
1850 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
1851 if (ret >= 0) {
1852 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
1853 adev->screen_off = false;
1854 else
1855 adev->screen_off = true;
1856 }
1857
1858 str_parms_destroy(parms);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001859 ALOGD("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001860 return ret;
1861}
1862
1863static char* adev_get_parameters(const struct audio_hw_device *dev,
1864 const char *keys)
1865{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001866 return strdup("");
1867}
1868
1869static int adev_init_check(const struct audio_hw_device *dev)
1870{
1871 return 0;
1872}
1873
1874static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
1875{
1876 struct audio_device *adev = (struct audio_device *)dev;
1877 int vol, err = 0;
1878
1879 pthread_mutex_lock(&adev->lock);
1880 adev->voice_volume = volume;
1881 if (adev->mode == AUDIO_MODE_IN_CALL) {
1882 if (volume < 0.0) {
1883 volume = 0.0;
1884 } else if (volume > 1.0) {
1885 volume = 1.0;
1886 }
1887
1888 vol = lrint(volume * 100.0);
1889
1890 // Voice volume levels from android are mapped to driver volume levels as follows.
1891 // 0 -> 5, 20 -> 4, 40 ->3, 60 -> 2, 80 -> 1, 100 -> 0
1892 // So adjust the volume to get the correct volume index in driver
1893 vol = 100 - vol;
Ravi Kumar Alamanda87f6ee02013-05-07 14:20:11 -07001894#ifdef MSM8974
1895 set_voice_volume(adev->mixer, vol);
1896#else
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001897 if (adev->csd_client) {
1898 if (adev->csd_volume == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08001899 ALOGE("%s: dlsym error for csd_client_volume", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001900 } else {
1901 err = adev->csd_volume(vol);
1902 if (err < 0) {
1903 ALOGE("%s: csd_client error %d", __func__, err);
1904 }
1905 }
1906 } else {
1907 ALOGE("%s: No CSD Client present", __func__);
1908 }
Ravi Kumar Alamanda87f6ee02013-05-07 14:20:11 -07001909#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001910 }
1911 pthread_mutex_unlock(&adev->lock);
1912 return err;
1913}
1914
1915static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
1916{
1917 return -ENOSYS;
1918}
1919
1920static int adev_get_master_volume(struct audio_hw_device *dev,
1921 float *volume)
1922{
1923 return -ENOSYS;
1924}
1925
1926static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
1927{
1928 return -ENOSYS;
1929}
1930
1931static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
1932{
1933 return -ENOSYS;
1934}
1935
1936static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
1937{
1938 struct audio_device *adev = (struct audio_device *)dev;
1939
1940 pthread_mutex_lock(&adev->lock);
1941 if (adev->mode != mode) {
1942 adev->mode = mode;
1943 }
1944 pthread_mutex_unlock(&adev->lock);
1945 return 0;
1946}
1947
1948static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
1949{
1950 struct audio_device *adev = (struct audio_device *)dev;
1951 int err = 0;
1952
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001953 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001954 adev->mic_mute = state;
1955 if (adev->mode == AUDIO_MODE_IN_CALL) {
Ravi Kumar Alamanda87f6ee02013-05-07 14:20:11 -07001956#ifdef MSM8974
1957 set_mic_mute(adev->mixer, state);
1958#else
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001959 if (adev->csd_client) {
1960 if (adev->csd_mic_mute == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08001961 ALOGE("%s: dlsym error for csd_mic_mute", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001962 } else {
1963 err = adev->csd_mic_mute(state);
1964 if (err < 0) {
1965 ALOGE("%s: csd_client error %d", __func__, err);
1966 }
1967 }
1968 } else {
1969 ALOGE("%s: No CSD Client present", __func__);
1970 }
Ravi Kumar Alamanda87f6ee02013-05-07 14:20:11 -07001971#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001972 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001973 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001974 return err;
1975}
1976
1977static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1978{
1979 struct audio_device *adev = (struct audio_device *)dev;
1980
1981 *state = adev->mic_mute;
1982
1983 return 0;
1984}
1985
1986static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
1987 const struct audio_config *config)
1988{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001989 int channel_count = popcount(config->channel_mask);
1990
1991 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
1992}
1993
1994static int adev_open_input_stream(struct audio_hw_device *dev,
1995 audio_io_handle_t handle,
1996 audio_devices_t devices,
1997 struct audio_config *config,
1998 struct audio_stream_in **stream_in)
1999{
2000 struct audio_device *adev = (struct audio_device *)dev;
2001 struct stream_in *in;
2002 int ret, buffer_size, frame_size;
2003 int channel_count = popcount(config->channel_mask);
2004
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002005 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002006 *stream_in = NULL;
2007 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2008 return -EINVAL;
2009
2010 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
2011
2012 in->stream.common.get_sample_rate = in_get_sample_rate;
2013 in->stream.common.set_sample_rate = in_set_sample_rate;
2014 in->stream.common.get_buffer_size = in_get_buffer_size;
2015 in->stream.common.get_channels = in_get_channels;
2016 in->stream.common.get_format = in_get_format;
2017 in->stream.common.set_format = in_set_format;
2018 in->stream.common.standby = in_standby;
2019 in->stream.common.dump = in_dump;
2020 in->stream.common.set_parameters = in_set_parameters;
2021 in->stream.common.get_parameters = in_get_parameters;
2022 in->stream.common.add_audio_effect = in_add_audio_effect;
2023 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2024 in->stream.set_gain = in_set_gain;
2025 in->stream.read = in_read;
2026 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2027
2028 in->device = devices;
2029 in->source = AUDIO_SOURCE_DEFAULT;
2030 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002031 in->standby = 1;
2032 in->channel_mask = config->channel_mask;
2033
2034 /* Update config params with the requested sample rate and channels */
2035 in->usecase = USECASE_AUDIO_RECORD;
2036 in->config = pcm_config_audio_capture;
2037 in->config.channels = channel_count;
2038 in->config.rate = config->sample_rate;
2039
2040 frame_size = audio_stream_frame_size((struct audio_stream *)in);
2041 buffer_size = get_input_buffer_size(config->sample_rate,
2042 config->format,
2043 channel_count);
2044 in->config.period_size = buffer_size / frame_size;
2045
2046 *stream_in = &in->stream;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002047 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002048 return 0;
2049
2050err_open:
2051 free(in);
2052 *stream_in = NULL;
2053 return ret;
2054}
2055
2056static void adev_close_input_stream(struct audio_hw_device *dev,
2057 struct audio_stream_in *stream)
2058{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002059 ALOGD("%s", __func__);
2060
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002061 in_standby(&stream->common);
2062 free(stream);
2063
2064 return;
2065}
2066
2067static int adev_dump(const audio_hw_device_t *device, int fd)
2068{
2069 return 0;
2070}
2071
2072static int adev_close(hw_device_t *device)
2073{
2074 struct audio_device *adev = (struct audio_device *)device;
2075 audio_route_free(adev->audio_route);
2076 free(device);
2077 return 0;
2078}
2079
2080static void init_platform_data(struct audio_device *adev)
2081{
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002082 char platform[PROPERTY_VALUE_MAX];
2083 char baseband[PROPERTY_VALUE_MAX];
2084 char value[PROPERTY_VALUE_MAX];
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002085
2086 adev->dualmic_config = DUALMIC_CONFIG_NONE;
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -08002087 adev->fluence_in_spkr_mode = false;
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002088 adev->fluence_in_voice_call = false;
2089 adev->fluence_in_voice_rec = false;
2090 adev->mic_type_analog = false;
2091
2092 property_get("persist.audio.handset.mic.type",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002093 if (!strcmp("analog", value))
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002094 adev->mic_type_analog = true;
2095
2096 property_get("persist.audio.dualmic.config",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002097 if (!strcmp("broadside", value)) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002098 adev->dualmic_config = DUALMIC_CONFIG_BROADSIDE;
2099 adev->acdb_settings |= DMIC_FLAG;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002100 } else if (!strcmp("endfire", value)) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002101 adev->dualmic_config = DUALMIC_CONFIG_ENDFIRE;
2102 adev->acdb_settings |= DMIC_FLAG;
2103 }
2104
2105 if (adev->dualmic_config != DUALMIC_CONFIG_NONE) {
2106 property_get("persist.audio.fluence.voicecall",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002107 if (!strcmp("true", value)) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002108 adev->fluence_in_voice_call = true;
2109 }
2110
2111 property_get("persist.audio.fluence.voicerec",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002112 if (!strcmp("true", value)) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002113 adev->fluence_in_voice_rec = true;
2114 }
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -08002115
2116 property_get("persist.audio.fluence.speaker",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002117 if (!strcmp("true", value)) {
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -08002118 adev->fluence_in_spkr_mode = true;
2119 }
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002120 }
2121
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002122 adev->acdb_handle = dlopen(LIB_ACDB_LOADER, RTLD_NOW);
2123 if (adev->acdb_handle == NULL) {
2124 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_ACDB_LOADER);
2125 } else {
2126 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_ACDB_LOADER);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002127 adev->acdb_deallocate = (acdb_deallocate_t)dlsym(adev->acdb_handle,
2128 "acdb_loader_deallocate_ACDB");
2129 adev->acdb_send_audio_cal = (acdb_send_audio_cal_t)dlsym(adev->acdb_handle,
2130 "acdb_loader_send_audio_cal");
2131 adev->acdb_send_voice_cal = (acdb_send_voice_cal_t)dlsym(adev->acdb_handle,
2132 "acdb_loader_send_voice_cal");
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002133 adev->acdb_init = (acdb_init_t)dlsym(adev->acdb_handle,
2134 "acdb_loader_init_ACDB");
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002135 if (adev->acdb_init == NULL)
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002136 ALOGE("%s: dlsym error %s for acdb_loader_init_ACDB", __func__, dlerror());
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002137 else
2138 adev->acdb_init();
2139 }
2140
2141 /* If platform is Fusion3, load CSD Client specific symbols
2142 * Voice call is handled by MDM and apps processor talks to
2143 * MDM through CSD Client
2144 */
2145 property_get("ro.board.platform", platform, "");
2146 property_get("ro.baseband", baseband, "");
2147 if (!strcmp("msm8960", platform) && !strcmp("mdm", baseband)) {
2148 adev->csd_client = dlopen(LIB_CSD_CLIENT, RTLD_NOW);
2149 if (adev->csd_client == NULL)
2150 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_CSD_CLIENT);
2151 }
2152
2153 if (adev->csd_client) {
2154 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_CSD_CLIENT);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002155 adev->csd_client_deinit = (csd_client_deinit_t)dlsym(adev->csd_client,
2156 "csd_client_deinit");
2157 adev->csd_disable_device = (csd_disable_device_t)dlsym(adev->csd_client,
2158 "csd_client_disable_device");
2159 adev->csd_enable_device = (csd_enable_device_t)dlsym(adev->csd_client,
2160 "csd_client_enable_device");
2161 adev->csd_start_voice = (csd_start_voice_t)dlsym(adev->csd_client,
2162 "csd_client_start_voice");
2163 adev->csd_stop_voice = (csd_stop_voice_t)dlsym(adev->csd_client,
2164 "csd_client_stop_voice");
2165 adev->csd_volume = (csd_volume_t)dlsym(adev->csd_client,
2166 "csd_client_volume");
2167 adev->csd_mic_mute = (csd_mic_mute_t)dlsym(adev->csd_client,
2168 "csd_client_mic_mute");
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002169 adev->csd_client_init = (csd_client_init_t)dlsym(adev->csd_client,
2170 "csd_client_init");
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002171
2172 if (adev->csd_client_init == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002173 ALOGE("%s: dlsym error %s for csd_client_init", __func__, dlerror());
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002174 } else {
2175 adev->csd_client_init();
2176 }
2177 }
2178}
2179
2180static int adev_open(const hw_module_t *module, const char *name,
2181 hw_device_t **device)
2182{
2183 struct audio_device *adev;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002184 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002185
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002186 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002187 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2188
2189 adev = calloc(1, sizeof(struct audio_device));
2190
2191 adev->mixer = mixer_open(MIXER_CARD);
2192 if (!adev->mixer) {
2193 ALOGE("Unable to open the mixer, aborting.");
2194 return -ENOSYS;
2195 }
2196
2197 adev->audio_route = audio_route_init(MIXER_CARD, MIXER_XML_PATH);
2198 if (!adev->audio_route) {
2199 free(adev);
2200 ALOGE("%s: Failed to init audio route controls, aborting.", __func__);
2201 *device = NULL;
2202 return -EINVAL;
2203 }
2204
2205 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2206 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2207 adev->device.common.module = (struct hw_module_t *)module;
2208 adev->device.common.close = adev_close;
2209
2210 adev->device.init_check = adev_init_check;
2211 adev->device.set_voice_volume = adev_set_voice_volume;
2212 adev->device.set_master_volume = adev_set_master_volume;
2213 adev->device.get_master_volume = adev_get_master_volume;
2214 adev->device.set_master_mute = adev_set_master_mute;
2215 adev->device.get_master_mute = adev_get_master_mute;
2216 adev->device.set_mode = adev_set_mode;
2217 adev->device.set_mic_mute = adev_set_mic_mute;
2218 adev->device.get_mic_mute = adev_get_mic_mute;
2219 adev->device.set_parameters = adev_set_parameters;
2220 adev->device.get_parameters = adev_get_parameters;
2221 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2222 adev->device.open_output_stream = adev_open_output_stream;
2223 adev->device.close_output_stream = adev_close_output_stream;
2224 adev->device.open_input_stream = adev_open_input_stream;
2225 adev->device.close_input_stream = adev_close_input_stream;
2226 adev->device.dump = adev_dump;
2227
2228 /* Set the default route before the PCM stream is opened */
2229 pthread_mutex_lock(&adev->lock);
2230 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002231 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002232 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002233 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002234 adev->voice_call_rx = NULL;
2235 adev->voice_call_tx = NULL;
2236 adev->voice_volume = 1.0f;
2237 adev->tty_mode = TTY_MODE_OFF;
2238 adev->bluetooth_nrec = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002239 adev->in_call = false;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002240 adev->acdb_settings = TTY_MODE_OFF;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002241 for (i = 0; i < SND_DEVICE_MAX; i++) {
2242 adev->snd_dev_ref_cnt[i] = 0;
2243 }
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002244 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002245 pthread_mutex_unlock(&adev->lock);
2246
2247 /* Loads platform specific libraries dynamically */
2248 init_platform_data(adev);
2249
2250 *device = &adev->device.common;
2251
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002252 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002253 return 0;
2254}
2255
2256static struct hw_module_methods_t hal_module_methods = {
2257 .open = adev_open,
2258};
2259
2260struct audio_module HAL_MODULE_INFO_SYM = {
2261 .common = {
2262 .tag = HARDWARE_MODULE_TAG,
2263 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2264 .hal_api_version = HARDWARE_HAL_API_VERSION,
2265 .id = AUDIO_HARDWARE_MODULE_ID,
2266 .name = "QCOM Audio HAL",
2267 .author = "Code Aurora Forum",
2268 .methods = &hal_module_methods,
2269 },
2270};