blob: c5acd17e5a795d794f9612fe8004c4c8c5958c47 [file] [log] [blame]
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "audio_hw_primary"
18/*#define LOG_NDEBUG 0*/
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -080019#define LOG_NDDEBUG 0
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080020
21#include <errno.h>
22#include <pthread.h>
23#include <stdint.h>
24#include <sys/time.h>
25#include <stdlib.h>
26#include <dlfcn.h>
27#include <math.h>
28
29#include <cutils/log.h>
30#include <cutils/str_parms.h>
31#include <cutils/properties.h>
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -080032#include <cutils/list.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080033
34#include "audio_hw.h"
35
36#define LIB_ACDB_LOADER "/system/lib/libacdbloader.so"
37#define LIB_CSD_CLIENT "/system/lib/libcsd-client.so"
38#define MIXER_XML_PATH "/system/etc/mixer_paths.xml"
39#define MIXER_CARD 0
40
41#define STRING_TO_ENUM(string) { #string, string }
42
43/* Flags used to initialize acdb_settings variable that goes to ACDB library */
44#define DMIC_FLAG 0x00000002
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -080045#define TTY_MODE_OFF 0x00000010
46#define TTY_MODE_FULL 0x00000020
47#define TTY_MODE_VCO 0x00000040
48#define TTY_MODE_HCO 0x00000080
49#define TTY_MODE_CLEAR 0xFFFFFF0F
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080050
51struct string_to_enum {
52 const char *name;
53 uint32_t value;
54};
55
56static const struct string_to_enum out_channels_name_to_enum_table[] = {
57 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
58 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
59 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
60};
61
62static const char * const use_case_table[AUDIO_USECASE_MAX] = {
63 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
64 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
65 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
66 [USECASE_AUDIO_RECORD] = "audio-record",
67 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
68 [USECASE_VOICE_CALL] = "voice-call",
69};
70
71static const int pcm_device_table[AUDIO_USECASE_MAX][2] = {
72 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0},
Sungmin Choi5195a4b2013-04-03 21:54:22 -070073#ifdef MSM8974
74 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15},
75#else
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080076 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {14, 14},
Sungmin Choi5195a4b2013-04-03 21:54:22 -070077#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080078 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = {1, 1},
79 [USECASE_AUDIO_RECORD] = {0, 0},
80 [USECASE_AUDIO_RECORD_LOW_LATENCY] = {14, 14},
81 [USECASE_VOICE_CALL] = {12, 12},
82};
83
84/* Array to store sound devices */
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -080085static const char * const device_table[SND_DEVICE_MAX] = {
86 [SND_DEVICE_NONE] = "none",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080087 /* Playback sound devices */
88 [SND_DEVICE_OUT_HANDSET] = "handset",
89 [SND_DEVICE_OUT_SPEAKER] = "speaker",
90 [SND_DEVICE_OUT_HEADPHONES] = "headphones",
91 [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = "speaker-and-headphones",
92 [SND_DEVICE_OUT_VOICE_SPEAKER] = "voice-speaker",
93 [SND_DEVICE_OUT_VOICE_HEADPHONES] = "voice-headphones",
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -080094 [SND_DEVICE_OUT_HDMI] = "hdmi",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080095 [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = "speaker-and-hdmi",
96 [SND_DEVICE_OUT_BT_SCO] = "bt-sco-headset",
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -080097 [SND_DEVICE_OUT_VOICE_HANDSET_TMUS] = "voice-handset-tmus",
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -080098 [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = "voice-tty-full-headphones",
99 [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = "voice-tty-vco-headphones",
100 [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = "voice-tty-hco-handset",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800101
102 /* Capture sound devices */
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800103 [SND_DEVICE_IN_HANDSET_MIC] = "handset-mic",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800104 [SND_DEVICE_IN_SPEAKER_MIC] = "speaker-mic",
105 [SND_DEVICE_IN_HEADSET_MIC] = "headset-mic",
106 [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = "voice-speaker-mic",
107 [SND_DEVICE_IN_VOICE_HEADSET_MIC] = "voice-headset-mic",
108 [SND_DEVICE_IN_HDMI_MIC] = "hdmi-mic",
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800109 [SND_DEVICE_IN_BT_SCO_MIC] = "bt-sco-mic",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800110 [SND_DEVICE_IN_CAMCORDER_MIC] = "camcorder-mic",
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800111 [SND_DEVICE_IN_VOICE_DMIC_EF] = "voice-dmic-ef",
112 [SND_DEVICE_IN_VOICE_DMIC_BS] = "voice-dmic-bs",
113 [SND_DEVICE_IN_VOICE_DMIC_EF_TMUS] = "voice-dmic-ef-tmus",
114 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF] = "voice-speaker-dmic-ef",
115 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS] = "voice-speaker-dmic-bs",
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800116 [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = "voice-tty-full-headset-mic",
117 [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = "voice-tty-vco-handset-mic",
118 [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = "voice-tty-hco-headset-mic",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800119 [SND_DEVICE_IN_VOICE_REC_MIC] = "voice-rec-mic",
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800120 [SND_DEVICE_IN_VOICE_REC_DMIC_EF] = "voice-rec-dmic-ef",
121 [SND_DEVICE_IN_VOICE_REC_DMIC_BS] = "voice-rec-dmic-bs",
Eric Laurentc8400632013-02-14 19:04:54 -0800122 [SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE] = "voice-rec-dmic-ef-fluence",
123 [SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE] = "voice-rec-dmic-bs-fluence",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800124};
125
Eric Laurentc8400632013-02-14 19:04:54 -0800126/* ACDB IDs (audio DSP path configuration IDs) for each sound device */
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800127static const int acdb_device_table[SND_DEVICE_MAX] = {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700128 [SND_DEVICE_NONE] = -1,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800129 [SND_DEVICE_OUT_HANDSET] = 7,
130 [SND_DEVICE_OUT_SPEAKER] = 14,
131 [SND_DEVICE_OUT_HEADPHONES] = 10,
132 [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = 10,
133 [SND_DEVICE_OUT_VOICE_SPEAKER] = 14,
134 [SND_DEVICE_OUT_VOICE_HEADPHONES] = 10,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800135 [SND_DEVICE_OUT_HDMI] = 18,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800136 [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = 14,
137 [SND_DEVICE_OUT_BT_SCO] = 22,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800138 [SND_DEVICE_OUT_VOICE_HANDSET_TMUS] = 81,
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800139 [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = 17,
140 [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = 17,
141 [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = 37,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800142
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800143 [SND_DEVICE_IN_HANDSET_MIC] = 4,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800144 [SND_DEVICE_IN_SPEAKER_MIC] = 4,
145 [SND_DEVICE_IN_HEADSET_MIC] = 8,
146 [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = 11,
147 [SND_DEVICE_IN_VOICE_HEADSET_MIC] = 8,
148 [SND_DEVICE_IN_HDMI_MIC] = 4,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800149 [SND_DEVICE_IN_BT_SCO_MIC] = 21,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800150 [SND_DEVICE_IN_CAMCORDER_MIC] = 61,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800151 [SND_DEVICE_IN_VOICE_DMIC_EF] = 6,
152 [SND_DEVICE_IN_VOICE_DMIC_BS] = 5,
153 [SND_DEVICE_IN_VOICE_DMIC_EF_TMUS] = 91,
154 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF] = 13,
155 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS] = 12,
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800156 [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = 16,
157 [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = 36,
158 [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = 16,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800159 [SND_DEVICE_IN_VOICE_REC_MIC] = 62,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800160 [SND_DEVICE_IN_VOICE_REC_DMIC_EF] = 62,
161 [SND_DEVICE_IN_VOICE_REC_DMIC_BS] = 62,
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800162 /* TODO: Update with proper acdb ids */
Eric Laurentc8400632013-02-14 19:04:54 -0800163 [SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE] = 62,
164 [SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE] = 62,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800165};
166
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800167int edid_get_max_channels(void);
168
Ravi Kumar Alamanda37718842013-02-22 18:44:45 -0800169static pthread_once_t check_op_once_ctl = PTHREAD_ONCE_INIT;
170static bool is_tmus = false;
171
172static void check_operator()
173{
174 char value[PROPERTY_VALUE_MAX];
175 int mccmnc;
176 property_get("gsm.sim.operator.numeric",value,"0");
177 mccmnc = atoi(value);
178 ALOGD("%s: tmus mccmnc %d", __func__, mccmnc);
179 switch(mccmnc) {
180 /* TMUS MCC(310), MNC(490, 260, 026) */
181 case 310490:
182 case 310260:
183 case 310026:
184 is_tmus = true;
185 break;
186 }
187}
188
189static bool is_operator_tmus()
190{
191 pthread_once(&check_op_once_ctl, check_operator);
192 return is_tmus;
193}
194
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800195static int get_pcm_device_id(struct audio_route *ar,
196 audio_usecase_t usecase,
197 int device_type)
198{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800199 int device_id;
200 if (device_type == PCM_PLAYBACK)
201 device_id = pcm_device_table[usecase][0];
202 else
203 device_id = pcm_device_table[usecase][1];
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800204 return device_id;
205}
206
207static int get_acdb_device_id(snd_device_t snd_device)
208{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700209 return acdb_device_table[snd_device];
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800210}
211
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800212static void add_backend_name(char *mixer_path,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700213 snd_device_t snd_device)
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800214{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700215 if (snd_device == SND_DEVICE_IN_BT_SCO_MIC)
216 strcat(mixer_path, " bt-sco");
217 else if(snd_device == SND_DEVICE_OUT_BT_SCO)
218 strcat(mixer_path, " bt-sco");
219 else if (snd_device == SND_DEVICE_OUT_HDMI)
220 strcat(mixer_path, " hdmi");
221 else if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_HDMI)
222 strcat(mixer_path, " speaker-and-hdmi");
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800223}
224
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700225static int enable_audio_route(struct audio_device *adev,
226 struct audio_usecase *usecase,
227 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800228{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700229 snd_device_t snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800230 char mixer_path[50];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800231
232 if (usecase == NULL)
233 return -EINVAL;
234
235 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
236
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800237 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700238 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800239 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700240 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800241
242 strcpy(mixer_path, use_case_table[usecase->id]);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700243 add_backend_name(mixer_path, snd_device);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800244 ALOGD("%s: apply mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700245 audio_route_apply_path(adev->audio_route, mixer_path);
246 if (update_mixer)
247 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800248
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800249 ALOGV("%s: exit", __func__);
250 return 0;
251}
252
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700253static int disable_audio_route(struct audio_device *adev,
254 struct audio_usecase *usecase,
255 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800256{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700257 snd_device_t snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800258 char mixer_path[50];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800259
260 if (usecase == NULL)
261 return -EINVAL;
262
263 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700264 if (usecase->type == PCM_CAPTURE)
265 snd_device = usecase->in_snd_device;
266 else
267 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800268 strcpy(mixer_path, use_case_table[usecase->id]);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700269 add_backend_name(mixer_path, snd_device);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800270 ALOGD("%s: reset mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700271 audio_route_reset_path(adev->audio_route, mixer_path);
272 if (update_mixer)
273 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800274
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800275 ALOGV("%s: exit", __func__);
276 return 0;
277}
278
279static int enable_snd_device(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700280 snd_device_t snd_device,
281 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800282{
283 int acdb_dev_id, acdb_dev_type;
284
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800285 if (snd_device < SND_DEVICE_MIN ||
286 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800287 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800288 return -EINVAL;
289 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700290
291 adev->snd_dev_ref_cnt[snd_device]++;
292 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
293 ALOGD("%s: snd_device(%d: %s) is already active",
294 __func__, snd_device, device_table[snd_device]);
295 return 0;
296 }
297
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800298 acdb_dev_id = get_acdb_device_id(snd_device);
299 if (acdb_dev_id < 0) {
300 ALOGE("%s: Could not find acdb id for device(%d)",
301 __func__, snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700302 adev->snd_dev_ref_cnt[snd_device]--;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800303 return -EINVAL;
304 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800305 if (adev->acdb_send_audio_cal) {
306 ALOGV("%s: sending audio calibration for snd_device(%d) acdb_id(%d)",
307 __func__, snd_device, acdb_dev_id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700308 if (snd_device >= SND_DEVICE_OUT_BEGIN &&
309 snd_device < SND_DEVICE_OUT_END)
310 acdb_dev_type = ACDB_DEV_TYPE_OUT;
311 else
312 acdb_dev_type = ACDB_DEV_TYPE_IN;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800313 adev->acdb_send_audio_cal(acdb_dev_id, acdb_dev_type);
314 } else {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700315 ALOGW("%s: Could not find the symbol acdb_send_audio_cal from %s",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800316 __func__, LIB_ACDB_LOADER);
317 }
318
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700319 ALOGD("%s: snd_device(%d: %s)", __func__,
320 snd_device, device_table[snd_device]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800321 audio_route_apply_path(adev->audio_route, device_table[snd_device]);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700322 if (update_mixer)
323 audio_route_update_mixer(adev->audio_route);
324
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800325 return 0;
326}
327
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700328static int disable_snd_device(struct audio_device *adev,
329 snd_device_t snd_device,
330 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800331{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800332 if (snd_device < SND_DEVICE_MIN ||
333 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800334 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800335 return -EINVAL;
336 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700337 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
338 ALOGE("%s: device ref cnt is already 0", __func__);
339 return -EINVAL;
340 }
341 adev->snd_dev_ref_cnt[snd_device]--;
342 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
343 ALOGD("%s: snd_device(%d: %s)", __func__,
344 snd_device, device_table[snd_device]);
345 audio_route_reset_path(adev->audio_route, device_table[snd_device]);
346 if (update_mixer)
347 audio_route_update_mixer(adev->audio_route);
348 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800349 return 0;
350}
351
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700352static void check_usecases_codec_backend(struct audio_device *adev,
353 struct audio_usecase *uc_info,
354 snd_device_t snd_device)
355{
356 struct listnode *node;
357 struct audio_usecase *usecase;
358 bool switch_device[AUDIO_USECASE_MAX];
359 int i, num_uc_to_switch = 0;
360
361 /*
362 * This function is to make sure that all the usecases that are active on
363 * the hardware codec backend are always routed to any one device that is
364 * handled by the hardware codec.
365 * For example, if low-latency and deep-buffer usecases are currently active
366 * on speaker and out_set_parameters(headset) is received on low-latency
367 * output, then we have to make sure deep-buffer is also switched to headset,
368 * because of the limitation that both the devices cannot be enabled
369 * at the same time as they share the same backend.
370 */
371 /* Disable all the usecases on the shared backend other than the
372 specified usecase */
373 for (i = 0; i < AUDIO_USECASE_MAX; i++)
374 switch_device[i] = false;
375
376 list_for_each(node, &adev->usecase_list) {
377 usecase = node_to_item(node, struct audio_usecase, list);
378 if (usecase->type != PCM_CAPTURE &&
379 usecase != uc_info &&
380 usecase->out_snd_device != snd_device &&
381 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
382 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
383 __func__, use_case_table[usecase->id],
384 device_table[usecase->out_snd_device]);
385 disable_audio_route(adev, usecase, false);
386 switch_device[usecase->id] = true;
387 num_uc_to_switch++;
388 }
389 }
390
391 if (num_uc_to_switch) {
392 /* Make sure all the streams are de-routed before disabling the device */
393 audio_route_update_mixer(adev->audio_route);
394
395 list_for_each(node, &adev->usecase_list) {
396 usecase = node_to_item(node, struct audio_usecase, list);
397 if (switch_device[usecase->id]) {
398 disable_snd_device(adev, usecase->out_snd_device, false);
399 enable_snd_device(adev, snd_device, false);
400 }
401 }
402
403 /* Make sure new snd device is enabled before re-routing the streams */
404 audio_route_update_mixer(adev->audio_route);
405
406 /* Re-route all the usecases on the shared backend other than the
407 specified usecase to new snd devices */
408 list_for_each(node, &adev->usecase_list) {
409 usecase = node_to_item(node, struct audio_usecase, list);
410 /* Update the out_snd_device only before enabling the audio route */
411 if (switch_device[usecase->id] ) {
412 usecase->out_snd_device = snd_device;
413 enable_audio_route(adev, usecase, false);
414 }
415 }
416
417 audio_route_update_mixer(adev->audio_route);
418 }
419}
420
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800421static int set_hdmi_channels(struct mixer *mixer,
422 int channel_count)
423{
424 struct mixer_ctl *ctl;
425 const char *channel_cnt_str = NULL;
426 const char *mixer_ctl_name = "HDMI_RX Channels";
427 switch (channel_count) {
428 case 8:
429 channel_cnt_str = "Eight"; break;
430 case 7:
431 channel_cnt_str = "Seven"; break;
432 case 6:
433 channel_cnt_str = "Six"; break;
434 case 5:
435 channel_cnt_str = "Five"; break;
436 case 4:
437 channel_cnt_str = "Four"; break;
438 case 3:
439 channel_cnt_str = "Three"; break;
440 default:
441 channel_cnt_str = "Two"; break;
442 }
443 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
444 if (!ctl) {
445 ALOGE("%s: Could not get ctl for mixer cmd - %s",
446 __func__, mixer_ctl_name);
447 return -EINVAL;
448 }
449 ALOGV("HDMI channel count: %s", channel_cnt_str);
450 mixer_ctl_set_enum_by_string(ctl, channel_cnt_str);
451 return 0;
452}
453
454/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700455static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800456{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700457 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800458 int channels = edid_get_max_channels();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800459
460 switch (channels) {
461 /*
462 * Do not handle stereo output in Multi-channel cases
463 * Stereo case is handled in normal playback path
464 */
465 case 6:
466 ALOGV("%s: HDMI supports 5.1", __func__);
467 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
468 break;
469 case 8:
470 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
471 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
472 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
473 break;
474 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700475 ALOGE("HDMI does not support multi channel playback");
476 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800477 break;
478 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700479 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800480}
481
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700482static snd_device_t get_output_snd_device(struct audio_device *adev,
483 audio_devices_t devices)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800484{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700485 audio_mode_t mode = adev->mode;
486 snd_device_t snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800487
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800488 ALOGV("%s: enter: output devices(%#x)", __func__, devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800489 if (devices == AUDIO_DEVICE_NONE ||
490 devices & AUDIO_DEVICE_BIT_IN) {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800491 ALOGV("%s: Invalid output devices (%#x)", __func__, devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800492 goto exit;
493 }
494
495 if (mode == AUDIO_MODE_IN_CALL) {
496 if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
497 devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800498 if (adev->tty_mode == TTY_MODE_FULL)
499 snd_device = SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES;
500 else if (adev->tty_mode == TTY_MODE_VCO)
501 snd_device = SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES;
502 else if (adev->tty_mode == TTY_MODE_HCO)
503 snd_device = SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET;
504 else
505 snd_device = SND_DEVICE_OUT_VOICE_HEADPHONES;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800506 } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
507 snd_device = SND_DEVICE_OUT_BT_SCO;
508 } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
509 snd_device = SND_DEVICE_OUT_VOICE_SPEAKER;
510 } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
Ravi Kumar Alamanda37718842013-02-22 18:44:45 -0800511 if (is_operator_tmus())
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800512 snd_device = SND_DEVICE_OUT_VOICE_HANDSET_TMUS;
513 else
514 snd_device = SND_DEVICE_OUT_HANDSET;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800515 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800516 if (snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800517 goto exit;
518 }
519 }
520
521 if (popcount(devices) == 2) {
522 if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
523 AUDIO_DEVICE_OUT_SPEAKER)) {
524 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
525 } else if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADSET |
526 AUDIO_DEVICE_OUT_SPEAKER)) {
527 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
528 } else if (devices == (AUDIO_DEVICE_OUT_AUX_DIGITAL |
529 AUDIO_DEVICE_OUT_SPEAKER)) {
530 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HDMI;
531 } else {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800532 ALOGE("%s: Invalid combo device(%#x)", __func__, devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800533 goto exit;
534 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800535 if (snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800536 goto exit;
537 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800538 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800539
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800540 if (popcount(devices) != 1) {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800541 ALOGE("%s: Invalid output devices(%#x)", __func__, devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800542 goto exit;
543 }
544
545 if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
546 devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
547 snd_device = SND_DEVICE_OUT_HEADPHONES;
548 } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
549 snd_device = SND_DEVICE_OUT_SPEAKER;
550 } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
551 snd_device = SND_DEVICE_OUT_BT_SCO;
552 } else if (devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
553 snd_device = SND_DEVICE_OUT_HDMI ;
554 } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
555 snd_device = SND_DEVICE_OUT_HANDSET;
556 } else {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800557 ALOGE("%s: Unknown device(s) %#x", __func__, devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800558 }
559exit:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800560 ALOGV("%s: exit: snd_device(%s)", __func__, device_table[snd_device]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800561 return snd_device;
562}
563
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700564static snd_device_t get_input_snd_device(struct audio_device *adev,
565 audio_devices_t out_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800566{
Eric Laurentc8400632013-02-14 19:04:54 -0800567 audio_source_t source = (adev->active_input == NULL) ?
568 AUDIO_SOURCE_DEFAULT : adev->active_input->source;
569
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800570 audio_mode_t mode = adev->mode;
Eric Laurentc8400632013-02-14 19:04:54 -0800571 audio_devices_t in_device = ((adev->active_input == NULL) ?
572 AUDIO_DEVICE_NONE : adev->active_input->device)
573 & ~AUDIO_DEVICE_BIT_IN;
574 audio_channel_mask_t channel_mask = (adev->active_input == NULL) ?
575 AUDIO_CHANNEL_IN_MONO : adev->active_input->channel_mask;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800576 snd_device_t snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800577
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800578 ALOGV("%s: enter: out_device(%#x) in_device(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800579 __func__, out_device, in_device);
580 if (mode == AUDIO_MODE_IN_CALL) {
581 if (out_device == AUDIO_DEVICE_NONE) {
582 ALOGE("%s: No output device set for voice call", __func__);
583 goto exit;
584 }
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800585 if (adev->tty_mode != TTY_MODE_OFF) {
586 if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
587 out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
588 switch (adev->tty_mode) {
589 case TTY_MODE_FULL:
590 snd_device = SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC;
591 break;
592 case TTY_MODE_VCO:
593 snd_device = SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC;
594 break;
595 case TTY_MODE_HCO:
596 snd_device = SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC;
597 break;
598 default:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800599 ALOGE("%s: Invalid TTY mode (%#x)", __func__, adev->tty_mode);
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800600 }
601 goto exit;
602 }
603 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800604 if (out_device & AUDIO_DEVICE_OUT_EARPIECE ||
605 out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800606 if (adev->mic_type_analog || adev->fluence_in_voice_call == false) {
607 snd_device = SND_DEVICE_IN_HANDSET_MIC;
608 } else {
609 if (adev->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
Ravi Kumar Alamanda37718842013-02-22 18:44:45 -0800610 if (is_operator_tmus())
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800611 snd_device = SND_DEVICE_IN_VOICE_DMIC_EF_TMUS;
612 else
613 snd_device = SND_DEVICE_IN_VOICE_DMIC_EF;
614 } else if(adev->dualmic_config == DUALMIC_CONFIG_BROADSIDE)
615 snd_device = SND_DEVICE_IN_VOICE_DMIC_BS;
616 else
617 snd_device = SND_DEVICE_IN_HANDSET_MIC;
618 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800619 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
620 snd_device = SND_DEVICE_IN_VOICE_HEADSET_MIC;
621 } else if (out_device & AUDIO_DEVICE_OUT_ALL_SCO) {
622 snd_device = SND_DEVICE_IN_BT_SCO_MIC ;
623 } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -0800624 if (adev->fluence_in_voice_call && adev->fluence_in_spkr_mode &&
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800625 adev->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
626 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF;
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -0800627 } else if (adev->fluence_in_voice_call && adev->fluence_in_spkr_mode &&
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800628 adev->dualmic_config == DUALMIC_CONFIG_BROADSIDE) {
629 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS;
630 } else {
631 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC;
632 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800633 }
634 } else if (source == AUDIO_SOURCE_CAMCORDER) {
635 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC ||
636 in_device & AUDIO_DEVICE_IN_BACK_MIC) {
637 snd_device = SND_DEVICE_IN_CAMCORDER_MIC;
638 }
639 } else if (source == AUDIO_SOURCE_VOICE_RECOGNITION) {
640 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
Eric Laurentc8400632013-02-14 19:04:54 -0800641 if (adev->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
642 if (channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK)
643 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_EF;
644 else if (adev->fluence_in_voice_rec)
645 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE;
646 else
647 snd_device = SND_DEVICE_IN_VOICE_REC_MIC;
648 } else if (adev->dualmic_config == DUALMIC_CONFIG_BROADSIDE) {
649 if (channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK)
650 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_BS;
651 else if (adev->fluence_in_voice_rec)
652 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE;
653 else
654 snd_device = SND_DEVICE_IN_VOICE_REC_MIC;
655 } else
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800656 snd_device = SND_DEVICE_IN_VOICE_REC_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800657 }
Ravi Kumar Alamanda8455fa72013-02-19 14:53:19 -0800658 } else if (source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
659 if (out_device & AUDIO_DEVICE_OUT_SPEAKER)
660 in_device = AUDIO_DEVICE_IN_BACK_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800661 } else if (source == AUDIO_SOURCE_DEFAULT) {
662 goto exit;
663 }
664
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800665 if (snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800666 goto exit;
667 }
668
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800669 if (in_device != AUDIO_DEVICE_NONE &&
Ravi Kumar Alamanda8455fa72013-02-19 14:53:19 -0800670 !(in_device & AUDIO_DEVICE_IN_VOICE_CALL) &&
671 !(in_device & AUDIO_DEVICE_IN_COMMUNICATION)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800672 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800673 snd_device = SND_DEVICE_IN_HANDSET_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800674 } else if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800675 if (adev->mic_type_analog)
676 snd_device = SND_DEVICE_IN_HANDSET_MIC;
677 else
678 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800679 } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
680 snd_device = SND_DEVICE_IN_HEADSET_MIC;
681 } else if (in_device & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
682 snd_device = SND_DEVICE_IN_BT_SCO_MIC ;
683 } else if (in_device & AUDIO_DEVICE_IN_AUX_DIGITAL) {
684 snd_device = SND_DEVICE_IN_HDMI_MIC;
685 } else {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800686 ALOGE("%s: Unknown input device(s) %#x", __func__, in_device);
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800687 ALOGW("%s: Using default handset-mic", __func__);
688 snd_device = SND_DEVICE_IN_HANDSET_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800689 }
690 } else {
691 if (out_device & AUDIO_DEVICE_OUT_EARPIECE) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800692 snd_device = SND_DEVICE_IN_HANDSET_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800693 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
694 snd_device = SND_DEVICE_IN_HEADSET_MIC;
695 } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
696 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
697 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800698 snd_device = SND_DEVICE_IN_HANDSET_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800699 } else if (out_device & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800700 snd_device = SND_DEVICE_IN_BT_SCO_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800701 } else if (out_device & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
702 snd_device = SND_DEVICE_IN_HDMI_MIC;
703 } else {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800704 ALOGE("%s: Unknown output device(s) %#x", __func__, out_device);
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800705 ALOGW("%s: Using default handset-mic", __func__);
706 snd_device = SND_DEVICE_IN_HANDSET_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800707 }
708 }
709exit:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800710 ALOGV("%s: exit: in_snd_device(%s)", __func__, device_table[snd_device]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800711 return snd_device;
712}
713
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700714static struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
715 audio_usecase_t uc_id)
716{
717 struct audio_usecase *usecase;
718 struct listnode *node;
719
720 list_for_each(node, &adev->usecase_list) {
721 usecase = node_to_item(node, struct audio_usecase, list);
722 if (usecase->id == uc_id)
723 return usecase;
724 }
725 return NULL;
726}
727
728static int select_devices(struct audio_device *adev,
729 audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800730{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800731 snd_device_t out_snd_device = SND_DEVICE_NONE;
732 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700733 struct audio_usecase *usecase = NULL;
734 struct audio_usecase *vc_usecase = NULL;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800735 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800736 int acdb_rx_id, acdb_tx_id;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700737 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800738
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700739 usecase = get_usecase_from_list(adev, uc_id);
740 if (usecase == NULL) {
741 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
742 return -EINVAL;
743 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800744
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700745 if (usecase->type == VOICE_CALL) {
746 out_snd_device = get_output_snd_device(adev, usecase->stream.out->devices);
747 in_snd_device = get_input_snd_device(adev, usecase->stream.out->devices);
748 usecase->devices = usecase->stream.out->devices;
749 } else {
750 /*
751 * If the voice call is active, use the sound devices of voice call usecase
752 * so that it would not result any device switch. All the usecases will
753 * be switched to new device when select_devices() is called for voice call
754 * usecase. This is to avoid switching devices for voice call when
755 * check_usecases_codec_backend() is called below.
756 */
757 if (adev->in_call) {
758 vc_usecase = get_usecase_from_list(adev, USECASE_VOICE_CALL);
759 if (vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
760 in_snd_device = vc_usecase->in_snd_device;
761 out_snd_device = vc_usecase->out_snd_device;
762 }
763 }
764 if (usecase->type == PCM_PLAYBACK) {
765 usecase->devices = usecase->stream.out->devices;
766 in_snd_device = SND_DEVICE_NONE;
767 if (out_snd_device == SND_DEVICE_NONE)
768 out_snd_device = get_output_snd_device(adev,
769 usecase->stream.out->devices);
770 } else if (usecase->type == PCM_CAPTURE) {
771 usecase->devices = usecase->stream.in->device;
772 out_snd_device = SND_DEVICE_NONE;
773 if (in_snd_device == SND_DEVICE_NONE)
774 in_snd_device = get_input_snd_device(adev, SND_DEVICE_NONE);
775 }
776 }
777
778 if (out_snd_device == usecase->out_snd_device &&
779 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800780 return 0;
781 }
782
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800783 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
784 out_snd_device, device_table[out_snd_device],
785 in_snd_device, device_table[in_snd_device]);
786
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800787 /*
788 * Limitation: While in call, to do a device switch we need to disable
789 * and enable both RX and TX devices though one of them is same as current
790 * device.
791 */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700792 if (usecase->type == VOICE_CALL && adev->csd_client != NULL &&
793 usecase->in_snd_device != SND_DEVICE_NONE &&
794 usecase->out_snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800795 /* This must be called before disabling the mixer controls on APQ side */
796 if (adev->csd_disable_device == NULL) {
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -0800797 ALOGE("%s: dlsym error for csd_client_disable_device", __func__);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800798 } else {
799 status = adev->csd_disable_device();
800 if (status < 0) {
801 ALOGE("%s: csd_client_disable_device, failed, error %d",
802 __func__, status);
803 }
804 }
805 }
806
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700807 /* Disable current sound devices */
808 if (usecase->out_snd_device != SND_DEVICE_NONE) {
809 disable_audio_route(adev, usecase, true);
810 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800811 }
812
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700813 if (usecase->in_snd_device != SND_DEVICE_NONE) {
814 disable_audio_route(adev, usecase, true);
815 disable_snd_device(adev, usecase->in_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800816 }
817
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700818 /* Enable new sound devices */
819 if (out_snd_device != SND_DEVICE_NONE) {
820 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
821 check_usecases_codec_backend(adev, usecase, out_snd_device);
822 enable_snd_device(adev, out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800823 }
824
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700825 if (in_snd_device != SND_DEVICE_NONE)
826 enable_snd_device(adev, in_snd_device, false);
827
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800828 audio_route_update_mixer(adev->audio_route);
829
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700830 usecase->in_snd_device = in_snd_device;
831 usecase->out_snd_device = out_snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800832
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700833 enable_audio_route(adev, usecase, true);
834
835 if (usecase->type == VOICE_CALL && adev->csd_client) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800836 if (adev->csd_enable_device == NULL) {
837 ALOGE("%s: dlsym error for csd_client_enable_device",
838 __func__);
839 } else {
840 acdb_rx_id = get_acdb_device_id(out_snd_device);
841 acdb_tx_id = get_acdb_device_id(in_snd_device);
842
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700843 if (acdb_rx_id > 0 || acdb_tx_id > 0) {
844 status = adev->csd_enable_device(acdb_rx_id, acdb_tx_id,
845 adev->acdb_settings);
846 if (status < 0) {
847 ALOGE("%s: csd_client_enable_device, failed, error %d",
848 __func__, status);
849 }
850 } else {
851 ALOGE("%s: Incorrect ACDB IDs (rx: %d tx: %d)", __func__,
852 acdb_rx_id, acdb_tx_id);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800853 }
854 }
855 }
856
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800857 return status;
858}
859
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800860static int stop_input_stream(struct stream_in *in)
861{
862 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800863 struct audio_usecase *uc_info;
864 struct audio_device *adev = in->dev;
865
Eric Laurentc8400632013-02-14 19:04:54 -0800866 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800867
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700868 ALOGD("%s: enter: usecase(%d: %s)", __func__,
869 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800870 uc_info = get_usecase_from_list(adev, in->usecase);
871 if (uc_info == NULL) {
872 ALOGE("%s: Could not find the usecase (%d) in the list",
873 __func__, in->usecase);
874 return -EINVAL;
875 }
876
Eric Laurent150dbfe2013-02-27 14:31:02 -0800877 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700878 disable_audio_route(adev, uc_info, true);
879
880 /* 2. Disable the tx device */
881 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800882
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800883 list_remove(&uc_info->list);
884 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800885
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800886 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800887 return ret;
888}
889
890int start_input_stream(struct stream_in *in)
891{
892 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800893 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800894 struct audio_usecase *uc_info;
895 struct audio_device *adev = in->dev;
896
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800897 ALOGD("%s: enter: usecase(%d)", __func__, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800898 in->pcm_device_id = get_pcm_device_id(adev->audio_route,
899 in->usecase,
900 PCM_CAPTURE);
901 if (in->pcm_device_id < 0) {
902 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
903 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800904 ret = -EINVAL;
905 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800906 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700907
908 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800909 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
910 uc_info->id = in->usecase;
911 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800912 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700913 uc_info->devices = in->device;
914 uc_info->in_snd_device = SND_DEVICE_NONE;
915 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800916
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800917 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700918 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800919
Eric Laurentc8400632013-02-14 19:04:54 -0800920 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
921 __func__, SOUND_CARD, in->pcm_device_id, in->config.channels);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800922 in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id,
923 PCM_IN, &in->config);
924 if (in->pcm && !pcm_is_ready(in->pcm)) {
925 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
926 pcm_close(in->pcm);
927 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800928 ret = -EIO;
929 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800930 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800931 ALOGD("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800932 return ret;
933
934error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800935 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800936
937error_config:
938 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700939 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800940
941 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800942}
943
944static int stop_output_stream(struct stream_out *out)
945{
946 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800947 struct audio_usecase *uc_info;
948 struct audio_device *adev = out->dev;
949
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700950 ALOGD("%s: enter: usecase(%d: %s)", __func__,
951 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800952 uc_info = get_usecase_from_list(adev, out->usecase);
953 if (uc_info == NULL) {
954 ALOGE("%s: Could not find the usecase (%d) in the list",
955 __func__, out->usecase);
956 return -EINVAL;
957 }
958
Eric Laurent150dbfe2013-02-27 14:31:02 -0800959 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700960 disable_audio_route(adev, uc_info, true);
961
962 /* 2. Disable the rx device */
963 disable_snd_device(adev, uc_info->out_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800964
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800965 list_remove(&uc_info->list);
966 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800967
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700968 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800969 return ret;
970}
971
972int start_output_stream(struct stream_out *out)
973{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800974 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800975 struct audio_usecase *uc_info;
976 struct audio_device *adev = out->dev;
977
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700978 ALOGD("%s: enter: usecase(%d: %s) devices(%#x)",
979 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800980 out->pcm_device_id = get_pcm_device_id(adev->audio_route,
981 out->usecase,
982 PCM_PLAYBACK);
983 if (out->pcm_device_id < 0) {
984 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
985 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800986 ret = -EINVAL;
987 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800988 }
989
990 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
991 uc_info->id = out->usecase;
992 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800993 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700994 uc_info->devices = out->devices;
995 uc_info->in_snd_device = SND_DEVICE_NONE;
996 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800997
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800998 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800999
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001000 select_devices(adev, out->usecase);
1001
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001002 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
1003 __func__, 0, out->pcm_device_id);
1004 out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id,
1005 PCM_OUT, &out->config);
1006 if (out->pcm && !pcm_is_ready(out->pcm)) {
1007 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1008 pcm_close(out->pcm);
1009 out->pcm = NULL;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001010 ret = -EIO;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001011 goto error_pcm_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001012 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001013 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001014 return 0;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001015error_pcm_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001016 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001017error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001018 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001019}
1020
1021static int stop_voice_call(struct audio_device *adev)
1022{
1023 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001024 struct audio_usecase *uc_info;
1025
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001026 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001027 adev->in_call = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001028 if (adev->csd_client) {
1029 if (adev->csd_stop_voice == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08001030 ALOGE("dlsym error for csd_client_disable_device");
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001031 } else {
1032 ret = adev->csd_stop_voice();
1033 if (ret < 0) {
1034 ALOGE("%s: csd_client error %d\n", __func__, ret);
1035 }
1036 }
1037 }
1038
1039 /* 1. Close the PCM devices */
1040 if (adev->voice_call_rx) {
1041 pcm_close(adev->voice_call_rx);
1042 adev->voice_call_rx = NULL;
1043 }
1044 if (adev->voice_call_tx) {
1045 pcm_close(adev->voice_call_tx);
1046 adev->voice_call_tx = NULL;
1047 }
1048
1049 uc_info = get_usecase_from_list(adev, USECASE_VOICE_CALL);
1050 if (uc_info == NULL) {
1051 ALOGE("%s: Could not find the usecase (%d) in the list",
1052 __func__, USECASE_VOICE_CALL);
1053 return -EINVAL;
1054 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001055
1056 /* 2. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001057 disable_audio_route(adev, uc_info, true);
1058
1059 /* 3. Disable the rx and tx devices */
1060 disable_snd_device(adev, uc_info->out_snd_device, false);
1061 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001062
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001063 list_remove(&uc_info->list);
1064 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001065
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001066 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001067 return ret;
1068}
1069
1070static int start_voice_call(struct audio_device *adev)
1071{
1072 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001073 struct audio_usecase *uc_info;
1074 int pcm_dev_rx_id, pcm_dev_tx_id;
1075
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001076 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001077
1078 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1079 uc_info->id = USECASE_VOICE_CALL;
1080 uc_info->type = VOICE_CALL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001081 uc_info->stream.out = adev->primary_output;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001082 uc_info->devices = adev->primary_output->devices;
1083 uc_info->in_snd_device = SND_DEVICE_NONE;
1084 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001085
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001086 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001087
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001088 select_devices(adev, USECASE_VOICE_CALL);
1089
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001090 pcm_dev_rx_id = get_pcm_device_id(adev->audio_route, uc_info->id,
1091 PCM_PLAYBACK);
1092 pcm_dev_tx_id = get_pcm_device_id(adev->audio_route, uc_info->id,
1093 PCM_CAPTURE);
1094
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001095 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
1096 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
1097 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001098 ret = -EIO;
1099 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001100 }
1101
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001102 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001103 __func__, SOUND_CARD, pcm_dev_rx_id);
1104 adev->voice_call_rx = pcm_open(SOUND_CARD,
1105 pcm_dev_rx_id,
1106 PCM_OUT, &pcm_config_voice_call);
1107 if (adev->voice_call_rx && !pcm_is_ready(adev->voice_call_rx)) {
1108 ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_rx));
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001109 ret = -EIO;
1110 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001111 }
1112
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001113 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001114 __func__, SOUND_CARD, pcm_dev_tx_id);
1115 adev->voice_call_tx = pcm_open(SOUND_CARD,
1116 pcm_dev_tx_id,
1117 PCM_IN, &pcm_config_voice_call);
1118 if (adev->voice_call_tx && !pcm_is_ready(adev->voice_call_tx)) {
1119 ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_tx));
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001120 ret = -EIO;
1121 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001122 }
1123 pcm_start(adev->voice_call_rx);
1124 pcm_start(adev->voice_call_tx);
1125
1126 if (adev->csd_client) {
1127 if (adev->csd_start_voice == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08001128 ALOGE("dlsym error for csd_client_start_voice");
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001129 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001130 } else {
1131 ret = adev->csd_start_voice();
1132 if (ret < 0) {
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001133 ALOGE("%s: csd_start_voice error %d\n", __func__, ret);
1134 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001135 }
1136 }
1137 }
1138
1139 adev->in_call = true;
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001140 return 0;
1141
1142error_start_voice:
1143 stop_voice_call(adev);
1144
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001145 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001146 return ret;
1147}
1148
1149static int check_input_parameters(uint32_t sample_rate,
1150 audio_format_t format,
1151 int channel_count)
1152{
1153 if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
1154
1155 if ((channel_count < 1) || (channel_count > 2)) return -EINVAL;
1156
1157 switch (sample_rate) {
1158 case 8000:
1159 case 11025:
1160 case 12000:
1161 case 16000:
1162 case 22050:
1163 case 24000:
1164 case 32000:
1165 case 44100:
1166 case 48000:
1167 break;
1168 default:
1169 return -EINVAL;
1170 }
1171
1172 return 0;
1173}
1174
1175static size_t get_input_buffer_size(uint32_t sample_rate,
1176 audio_format_t format,
1177 int channel_count)
1178{
1179 size_t size = 0;
1180
1181 if (check_input_parameters(sample_rate, format, channel_count) != 0) return 0;
1182
1183 if (sample_rate == 8000 || sample_rate == 16000 || sample_rate == 32000) {
1184 size = (sample_rate * 20) / 1000;
1185 } else if (sample_rate == 11025 || sample_rate == 12000) {
1186 size = 256;
1187 } else if (sample_rate == 22050 || sample_rate == 24000) {
1188 size = 512;
1189 } else if (sample_rate == 44100 || sample_rate == 48000) {
1190 size = 1024;
1191 }
1192
1193 return size * sizeof(short) * channel_count;
1194}
1195
1196static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1197{
1198 struct stream_out *out = (struct stream_out *)stream;
1199
1200 return out->config.rate;
1201}
1202
1203static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1204{
1205 return -ENOSYS;
1206}
1207
1208static size_t out_get_buffer_size(const struct audio_stream *stream)
1209{
1210 struct stream_out *out = (struct stream_out *)stream;
1211
1212 return out->config.period_size * audio_stream_frame_size(stream);
1213}
1214
1215static uint32_t out_get_channels(const struct audio_stream *stream)
1216{
1217 struct stream_out *out = (struct stream_out *)stream;
1218
1219 return out->channel_mask;
1220}
1221
1222static audio_format_t out_get_format(const struct audio_stream *stream)
1223{
1224 return AUDIO_FORMAT_PCM_16_BIT;
1225}
1226
1227static int out_set_format(struct audio_stream *stream, audio_format_t format)
1228{
1229 return -ENOSYS;
1230}
1231
1232static int out_standby(struct audio_stream *stream)
1233{
1234 struct stream_out *out = (struct stream_out *)stream;
1235 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001236 ALOGD("%s: enter: usecase(%d: %s)", __func__,
1237 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001238 pthread_mutex_lock(&out->lock);
1239
1240 if (!out->standby) {
1241 out->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001242 if (out->pcm) {
1243 pcm_close(out->pcm);
1244 out->pcm = NULL;
1245 }
1246 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001247 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001248 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001249 }
1250 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001251 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001252 return 0;
1253}
1254
1255static int out_dump(const struct audio_stream *stream, int fd)
1256{
1257 return 0;
1258}
1259
1260static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1261{
1262 struct stream_out *out = (struct stream_out *)stream;
1263 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001264 struct audio_usecase *usecase;
1265 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001266 struct str_parms *parms;
1267 char value[32];
1268 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001269 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001270
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001271 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
1272 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001273 parms = str_parms_create_str(kvpairs);
1274 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1275 if (ret >= 0) {
1276 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001277 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001278 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001279
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001280 /*
1281 * When HDMI cable is unplugged the music playback is paused and
1282 * the policy manager sends routing=0. But the audioflinger
1283 * continues to write data until standby time (3sec).
1284 * As the HDMI core is turned off, the write gets blocked.
1285 * Avoid this by routing audio to speaker until standby.
1286 */
1287 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1288 val == AUDIO_DEVICE_NONE) {
1289 val = AUDIO_DEVICE_OUT_SPEAKER;
1290 }
1291
1292 /*
1293 * select_devices() call below switches all the usecases on the same
1294 * backend to the new device. Refer to check_usecases_codec_backend() in
1295 * the select_devices(). But how do we undo this?
1296 *
1297 * For example, music playback is active on headset (deep-buffer usecase)
1298 * and if we go to ringtones and select a ringtone, low-latency usecase
1299 * will be started on headset+speaker. As we can't enable headset+speaker
1300 * and headset devices at the same time, select_devices() switches the music
1301 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1302 * So when the ringtone playback is completed, how do we undo the same?
1303 *
1304 * We are relying on the out_set_parameters() call on deep-buffer output,
1305 * once the ringtone playback is ended.
1306 * NOTE: We should not check if the current devices are same as new devices.
1307 * Because select_devices() must be called to switch back the music
1308 * playback to headset.
1309 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001310 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001311 out->devices = val;
1312
1313 if (!out->standby)
1314 select_devices(adev, out->usecase);
1315
1316 if ((adev->mode == AUDIO_MODE_IN_CALL) && !adev->in_call &&
1317 (out == adev->primary_output)) {
1318 start_voice_call(adev);
1319 } else if ((adev->mode == AUDIO_MODE_IN_CALL) && adev->in_call &&
1320 (out == adev->primary_output)) {
1321 select_devices(adev, USECASE_VOICE_CALL);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001322 }
1323 }
1324
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001325 if ((adev->mode != AUDIO_MODE_IN_CALL) && adev->in_call &&
1326 (out == adev->primary_output)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001327 stop_voice_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001328 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001329
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001330 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001331 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001332 }
1333 str_parms_destroy(parms);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001334 ALOGD("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001335 return ret;
1336}
1337
1338static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1339{
1340 struct stream_out *out = (struct stream_out *)stream;
1341 struct str_parms *query = str_parms_create_str(keys);
1342 char *str;
1343 char value[256];
1344 struct str_parms *reply = str_parms_create();
1345 size_t i, j;
1346 int ret;
1347 bool first = true;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001348 ALOGD("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001349 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1350 if (ret >= 0) {
1351 value[0] = '\0';
1352 i = 0;
1353 while (out->supported_channel_masks[i] != 0) {
1354 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1355 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1356 if (!first) {
1357 strcat(value, "|");
1358 }
1359 strcat(value, out_channels_name_to_enum_table[j].name);
1360 first = false;
1361 break;
1362 }
1363 }
1364 i++;
1365 }
1366 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1367 str = str_parms_to_str(reply);
1368 } else {
1369 str = strdup(keys);
1370 }
1371 str_parms_destroy(query);
1372 str_parms_destroy(reply);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001373 ALOGD("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001374 return str;
1375}
1376
1377static uint32_t out_get_latency(const struct audio_stream_out *stream)
1378{
1379 struct stream_out *out = (struct stream_out *)stream;
1380
1381 return (out->config.period_count * out->config.period_size * 1000) / (out->config.rate);
1382}
1383
1384static int out_set_volume(struct audio_stream_out *stream, float left,
1385 float right)
1386{
Eric Laurenta9024de2013-04-04 09:19:12 -07001387 struct stream_out *out = (struct stream_out *)stream;
1388 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1389 /* only take left channel into account: the API is for stereo anyway */
1390 out->muted = (left == 0.0f);
1391 return 0;
1392 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001393 return -ENOSYS;
1394}
1395
1396static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1397 size_t bytes)
1398{
1399 struct stream_out *out = (struct stream_out *)stream;
1400 struct audio_device *adev = out->dev;
1401 int i, ret = -1;
1402
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001403 pthread_mutex_lock(&out->lock);
1404 if (out->standby) {
Eric Laurent150dbfe2013-02-27 14:31:02 -08001405 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001406 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001407 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001408 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001409 goto exit;
1410 }
1411 out->standby = false;
1412 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001413
1414 if (out->pcm) {
Eric Laurenta9024de2013-04-04 09:19:12 -07001415 if (out->muted)
1416 memset((void *)buffer, 0, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001417 //ALOGV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1418 ret = pcm_write(out->pcm, (void *)buffer, bytes);
1419 }
1420
1421exit:
1422 pthread_mutex_unlock(&out->lock);
1423
1424 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001425 if (out->pcm)
1426 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001427 out_standby(&out->stream.common);
1428 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1429 out_get_sample_rate(&out->stream.common));
1430 }
1431 return bytes;
1432}
1433
1434static int out_get_render_position(const struct audio_stream_out *stream,
1435 uint32_t *dsp_frames)
1436{
1437 return -EINVAL;
1438}
1439
1440static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1441{
1442 return 0;
1443}
1444
1445static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1446{
1447 return 0;
1448}
1449
1450static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1451 int64_t *timestamp)
1452{
1453 return -EINVAL;
1454}
1455
1456/** audio_stream_in implementation **/
1457static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1458{
1459 struct stream_in *in = (struct stream_in *)stream;
1460
1461 return in->config.rate;
1462}
1463
1464static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1465{
1466 return -ENOSYS;
1467}
1468
1469static size_t in_get_buffer_size(const struct audio_stream *stream)
1470{
1471 struct stream_in *in = (struct stream_in *)stream;
1472
1473 return in->config.period_size * audio_stream_frame_size(stream);
1474}
1475
1476static uint32_t in_get_channels(const struct audio_stream *stream)
1477{
1478 struct stream_in *in = (struct stream_in *)stream;
1479
1480 return in->channel_mask;
1481}
1482
1483static audio_format_t in_get_format(const struct audio_stream *stream)
1484{
1485 return AUDIO_FORMAT_PCM_16_BIT;
1486}
1487
1488static int in_set_format(struct audio_stream *stream, audio_format_t format)
1489{
1490 return -ENOSYS;
1491}
1492
1493static int in_standby(struct audio_stream *stream)
1494{
1495 struct stream_in *in = (struct stream_in *)stream;
1496 struct audio_device *adev = in->dev;
1497 int status = 0;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001498 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001499 pthread_mutex_lock(&in->lock);
1500 if (!in->standby) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001501 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001502 if (in->pcm) {
1503 pcm_close(in->pcm);
1504 in->pcm = NULL;
1505 }
1506 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001507 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001508 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001509 }
1510 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001511 ALOGD("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001512 return status;
1513}
1514
1515static int in_dump(const struct audio_stream *stream, int fd)
1516{
1517 return 0;
1518}
1519
1520static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1521{
1522 struct stream_in *in = (struct stream_in *)stream;
1523 struct audio_device *adev = in->dev;
1524 struct str_parms *parms;
1525 char *str;
1526 char value[32];
1527 int ret, val = 0;
1528
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001529 ALOGD("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001530 parms = str_parms_create_str(kvpairs);
1531
1532 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1533
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001534 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001535 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001536 if (ret >= 0) {
1537 val = atoi(value);
1538 /* no audio source uses val == 0 */
1539 if ((in->source != val) && (val != 0)) {
1540 in->source = val;
1541 }
1542 }
1543
1544 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1545 if (ret >= 0) {
1546 val = atoi(value);
1547 if ((in->device != val) && (val != 0)) {
1548 in->device = val;
1549 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001550 if (!in->standby)
1551 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001552 }
1553 }
1554
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001555 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001556 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001557
1558 str_parms_destroy(parms);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001559 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001560 return ret;
1561}
1562
1563static char* in_get_parameters(const struct audio_stream *stream,
1564 const char *keys)
1565{
1566 return strdup("");
1567}
1568
1569static int in_set_gain(struct audio_stream_in *stream, float gain)
1570{
1571 return 0;
1572}
1573
1574static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1575 size_t bytes)
1576{
1577 struct stream_in *in = (struct stream_in *)stream;
1578 struct audio_device *adev = in->dev;
1579 int i, ret = -1;
1580
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001581 pthread_mutex_lock(&in->lock);
1582 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001583 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001584 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001585 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001586 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001587 goto exit;
1588 }
1589 in->standby = 0;
1590 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001591
1592 if (in->pcm) {
1593 ret = pcm_read(in->pcm, buffer, bytes);
1594 }
1595
1596 /*
1597 * Instead of writing zeroes here, we could trust the hardware
1598 * to always provide zeroes when muted.
1599 */
1600 if (ret == 0 && adev->mic_mute)
1601 memset(buffer, 0, bytes);
1602
1603exit:
1604 pthread_mutex_unlock(&in->lock);
1605
1606 if (ret != 0) {
1607 in_standby(&in->stream.common);
1608 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1609 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1610 in_get_sample_rate(&in->stream.common));
1611 }
1612 return bytes;
1613}
1614
1615static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1616{
1617 return 0;
1618}
1619
1620static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1621{
1622 return 0;
1623}
1624
1625static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1626{
1627 return 0;
1628}
1629
1630static int adev_open_output_stream(struct audio_hw_device *dev,
1631 audio_io_handle_t handle,
1632 audio_devices_t devices,
1633 audio_output_flags_t flags,
1634 struct audio_config *config,
1635 struct audio_stream_out **stream_out)
1636{
1637 struct audio_device *adev = (struct audio_device *)dev;
1638 struct stream_out *out;
1639 int i, ret;
1640
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001641 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001642 __func__, config->sample_rate, config->channel_mask, devices, flags);
1643 *stream_out = NULL;
1644 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1645
1646 if (devices == AUDIO_DEVICE_NONE)
1647 devices = AUDIO_DEVICE_OUT_SPEAKER;
1648
1649 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
1650 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1651 out->flags = flags;
1652 out->devices = devices;
1653
1654 /* Init use case and pcm_config */
1655 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
1656 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001657 pthread_mutex_lock(&adev->lock);
1658 ret = read_hdmi_channel_masks(out);
1659 pthread_mutex_unlock(&adev->lock);
1660 if (ret != 0) {
1661 /* If HDMI does not support multi channel playback, set the default */
1662 out->config.channels = popcount(out->channel_mask);
1663 set_hdmi_channels(adev->mixer, out->config.channels);
1664 goto error_open;
1665 }
1666
1667 if (config->sample_rate == 0)
1668 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1669 if (config->channel_mask == 0)
1670 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1671
1672 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001673 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
1674 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001675 out->config.rate = config->sample_rate;
1676 out->config.channels = popcount(out->channel_mask);
1677 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
1678 set_hdmi_channels(adev->mixer, out->config.channels);
1679 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
1680 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
1681 out->config = pcm_config_deep_buffer;
1682 } else {
1683 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
1684 out->config = pcm_config_low_latency;
1685 }
1686
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001687 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1688 if(adev->primary_output == NULL)
1689 adev->primary_output = out;
1690 else {
1691 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001692 ret = -EEXIST;
1693 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001694 }
1695 }
1696
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001697 /* Check if this usecase is already existing */
1698 pthread_mutex_lock(&adev->lock);
1699 if (get_usecase_from_list(adev, out->usecase) != NULL) {
1700 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001701 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001702 ret = -EEXIST;
1703 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001704 }
1705 pthread_mutex_unlock(&adev->lock);
1706
1707 out->stream.common.get_sample_rate = out_get_sample_rate;
1708 out->stream.common.set_sample_rate = out_set_sample_rate;
1709 out->stream.common.get_buffer_size = out_get_buffer_size;
1710 out->stream.common.get_channels = out_get_channels;
1711 out->stream.common.get_format = out_get_format;
1712 out->stream.common.set_format = out_set_format;
1713 out->stream.common.standby = out_standby;
1714 out->stream.common.dump = out_dump;
1715 out->stream.common.set_parameters = out_set_parameters;
1716 out->stream.common.get_parameters = out_get_parameters;
1717 out->stream.common.add_audio_effect = out_add_audio_effect;
1718 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1719 out->stream.get_latency = out_get_latency;
1720 out->stream.set_volume = out_set_volume;
1721 out->stream.write = out_write;
1722 out->stream.get_render_position = out_get_render_position;
1723 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
1724
1725 out->dev = adev;
1726 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07001727 /* out->muted = false; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001728
1729 config->format = out->stream.common.get_format(&out->stream.common);
1730 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
1731 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
1732
1733 *stream_out = &out->stream;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001734 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001735 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001736
1737error_open:
1738 free(out);
1739 *stream_out = NULL;
1740 ALOGD("%s: exit: ret %d", __func__, ret);
1741 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001742}
1743
1744static void adev_close_output_stream(struct audio_hw_device *dev,
1745 struct audio_stream_out *stream)
1746{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001747 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001748 out_standby(&stream->common);
1749 free(stream);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001750 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001751}
1752
1753static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
1754{
1755 struct audio_device *adev = (struct audio_device *)dev;
1756 struct str_parms *parms;
1757 char *str;
1758 char value[32];
1759 int ret;
1760
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001761 ALOGD("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001762
1763 parms = str_parms_create_str(kvpairs);
1764 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value));
1765 if (ret >= 0) {
1766 int tty_mode;
1767
1768 if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0)
1769 tty_mode = TTY_MODE_OFF;
1770 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0)
1771 tty_mode = TTY_MODE_VCO;
1772 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0)
1773 tty_mode = TTY_MODE_HCO;
1774 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0)
1775 tty_mode = TTY_MODE_FULL;
1776 else
1777 return -EINVAL;
1778
1779 pthread_mutex_lock(&adev->lock);
1780 if (tty_mode != adev->tty_mode) {
1781 adev->tty_mode = tty_mode;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08001782 adev->acdb_settings = (adev->acdb_settings & TTY_MODE_CLEAR) | tty_mode;
1783 if (adev->in_call)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001784 select_devices(adev, USECASE_VOICE_CALL);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001785 }
1786 pthread_mutex_unlock(&adev->lock);
1787 }
1788
1789 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
1790 if (ret >= 0) {
1791 /* When set to false, HAL should disable EC and NS
1792 * But it is currently not supported.
1793 */
1794 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
1795 adev->bluetooth_nrec = true;
1796 else
1797 adev->bluetooth_nrec = false;
1798 }
1799
1800 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
1801 if (ret >= 0) {
1802 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
1803 adev->screen_off = false;
1804 else
1805 adev->screen_off = true;
1806 }
1807
1808 str_parms_destroy(parms);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001809 ALOGD("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001810 return ret;
1811}
1812
1813static char* adev_get_parameters(const struct audio_hw_device *dev,
1814 const char *keys)
1815{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001816 return strdup("");
1817}
1818
1819static int adev_init_check(const struct audio_hw_device *dev)
1820{
1821 return 0;
1822}
1823
1824static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
1825{
1826 struct audio_device *adev = (struct audio_device *)dev;
1827 int vol, err = 0;
1828
1829 pthread_mutex_lock(&adev->lock);
1830 adev->voice_volume = volume;
1831 if (adev->mode == AUDIO_MODE_IN_CALL) {
1832 if (volume < 0.0) {
1833 volume = 0.0;
1834 } else if (volume > 1.0) {
1835 volume = 1.0;
1836 }
1837
1838 vol = lrint(volume * 100.0);
1839
1840 // Voice volume levels from android are mapped to driver volume levels as follows.
1841 // 0 -> 5, 20 -> 4, 40 ->3, 60 -> 2, 80 -> 1, 100 -> 0
1842 // So adjust the volume to get the correct volume index in driver
1843 vol = 100 - vol;
1844
1845 if (adev->csd_client) {
1846 if (adev->csd_volume == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08001847 ALOGE("%s: dlsym error for csd_client_volume", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001848 } else {
1849 err = adev->csd_volume(vol);
1850 if (err < 0) {
1851 ALOGE("%s: csd_client error %d", __func__, err);
1852 }
1853 }
1854 } else {
1855 ALOGE("%s: No CSD Client present", __func__);
1856 }
1857 }
1858 pthread_mutex_unlock(&adev->lock);
1859 return err;
1860}
1861
1862static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
1863{
1864 return -ENOSYS;
1865}
1866
1867static int adev_get_master_volume(struct audio_hw_device *dev,
1868 float *volume)
1869{
1870 return -ENOSYS;
1871}
1872
1873static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
1874{
1875 return -ENOSYS;
1876}
1877
1878static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
1879{
1880 return -ENOSYS;
1881}
1882
1883static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
1884{
1885 struct audio_device *adev = (struct audio_device *)dev;
1886
1887 pthread_mutex_lock(&adev->lock);
1888 if (adev->mode != mode) {
1889 adev->mode = mode;
1890 }
1891 pthread_mutex_unlock(&adev->lock);
1892 return 0;
1893}
1894
1895static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
1896{
1897 struct audio_device *adev = (struct audio_device *)dev;
1898 int err = 0;
1899
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001900 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001901 adev->mic_mute = state;
1902 if (adev->mode == AUDIO_MODE_IN_CALL) {
1903 if (adev->csd_client) {
1904 if (adev->csd_mic_mute == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08001905 ALOGE("%s: dlsym error for csd_mic_mute", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001906 } else {
1907 err = adev->csd_mic_mute(state);
1908 if (err < 0) {
1909 ALOGE("%s: csd_client error %d", __func__, err);
1910 }
1911 }
1912 } else {
1913 ALOGE("%s: No CSD Client present", __func__);
1914 }
1915 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001916 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001917 return err;
1918}
1919
1920static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1921{
1922 struct audio_device *adev = (struct audio_device *)dev;
1923
1924 *state = adev->mic_mute;
1925
1926 return 0;
1927}
1928
1929static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
1930 const struct audio_config *config)
1931{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001932 int channel_count = popcount(config->channel_mask);
1933
1934 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
1935}
1936
1937static int adev_open_input_stream(struct audio_hw_device *dev,
1938 audio_io_handle_t handle,
1939 audio_devices_t devices,
1940 struct audio_config *config,
1941 struct audio_stream_in **stream_in)
1942{
1943 struct audio_device *adev = (struct audio_device *)dev;
1944 struct stream_in *in;
1945 int ret, buffer_size, frame_size;
1946 int channel_count = popcount(config->channel_mask);
1947
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001948 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001949 *stream_in = NULL;
1950 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
1951 return -EINVAL;
1952
1953 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
1954
1955 in->stream.common.get_sample_rate = in_get_sample_rate;
1956 in->stream.common.set_sample_rate = in_set_sample_rate;
1957 in->stream.common.get_buffer_size = in_get_buffer_size;
1958 in->stream.common.get_channels = in_get_channels;
1959 in->stream.common.get_format = in_get_format;
1960 in->stream.common.set_format = in_set_format;
1961 in->stream.common.standby = in_standby;
1962 in->stream.common.dump = in_dump;
1963 in->stream.common.set_parameters = in_set_parameters;
1964 in->stream.common.get_parameters = in_get_parameters;
1965 in->stream.common.add_audio_effect = in_add_audio_effect;
1966 in->stream.common.remove_audio_effect = in_remove_audio_effect;
1967 in->stream.set_gain = in_set_gain;
1968 in->stream.read = in_read;
1969 in->stream.get_input_frames_lost = in_get_input_frames_lost;
1970
1971 in->device = devices;
1972 in->source = AUDIO_SOURCE_DEFAULT;
1973 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001974 in->standby = 1;
1975 in->channel_mask = config->channel_mask;
1976
1977 /* Update config params with the requested sample rate and channels */
1978 in->usecase = USECASE_AUDIO_RECORD;
1979 in->config = pcm_config_audio_capture;
1980 in->config.channels = channel_count;
1981 in->config.rate = config->sample_rate;
1982
1983 frame_size = audio_stream_frame_size((struct audio_stream *)in);
1984 buffer_size = get_input_buffer_size(config->sample_rate,
1985 config->format,
1986 channel_count);
1987 in->config.period_size = buffer_size / frame_size;
1988
1989 *stream_in = &in->stream;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001990 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001991 return 0;
1992
1993err_open:
1994 free(in);
1995 *stream_in = NULL;
1996 return ret;
1997}
1998
1999static void adev_close_input_stream(struct audio_hw_device *dev,
2000 struct audio_stream_in *stream)
2001{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002002 ALOGD("%s", __func__);
2003
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002004 in_standby(&stream->common);
2005 free(stream);
2006
2007 return;
2008}
2009
2010static int adev_dump(const audio_hw_device_t *device, int fd)
2011{
2012 return 0;
2013}
2014
2015static int adev_close(hw_device_t *device)
2016{
2017 struct audio_device *adev = (struct audio_device *)device;
2018 audio_route_free(adev->audio_route);
2019 free(device);
2020 return 0;
2021}
2022
2023static void init_platform_data(struct audio_device *adev)
2024{
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002025 char platform[PROPERTY_VALUE_MAX];
2026 char baseband[PROPERTY_VALUE_MAX];
2027 char value[PROPERTY_VALUE_MAX];
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002028
2029 adev->dualmic_config = DUALMIC_CONFIG_NONE;
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -08002030 adev->fluence_in_spkr_mode = false;
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002031 adev->fluence_in_voice_call = false;
2032 adev->fluence_in_voice_rec = false;
2033 adev->mic_type_analog = false;
2034
2035 property_get("persist.audio.handset.mic.type",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002036 if (!strcmp("analog", value))
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002037 adev->mic_type_analog = true;
2038
2039 property_get("persist.audio.dualmic.config",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002040 if (!strcmp("broadside", value)) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002041 adev->dualmic_config = DUALMIC_CONFIG_BROADSIDE;
2042 adev->acdb_settings |= DMIC_FLAG;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002043 } else if (!strcmp("endfire", value)) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002044 adev->dualmic_config = DUALMIC_CONFIG_ENDFIRE;
2045 adev->acdb_settings |= DMIC_FLAG;
2046 }
2047
2048 if (adev->dualmic_config != DUALMIC_CONFIG_NONE) {
2049 property_get("persist.audio.fluence.voicecall",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002050 if (!strcmp("true", value)) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002051 adev->fluence_in_voice_call = true;
2052 }
2053
2054 property_get("persist.audio.fluence.voicerec",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002055 if (!strcmp("true", value)) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002056 adev->fluence_in_voice_rec = true;
2057 }
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -08002058
2059 property_get("persist.audio.fluence.speaker",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002060 if (!strcmp("true", value)) {
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -08002061 adev->fluence_in_spkr_mode = true;
2062 }
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002063 }
2064
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002065 adev->acdb_handle = dlopen(LIB_ACDB_LOADER, RTLD_NOW);
2066 if (adev->acdb_handle == NULL) {
2067 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_ACDB_LOADER);
2068 } else {
2069 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_ACDB_LOADER);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002070 adev->acdb_deallocate = (acdb_deallocate_t)dlsym(adev->acdb_handle,
2071 "acdb_loader_deallocate_ACDB");
2072 adev->acdb_send_audio_cal = (acdb_send_audio_cal_t)dlsym(adev->acdb_handle,
2073 "acdb_loader_send_audio_cal");
2074 adev->acdb_send_voice_cal = (acdb_send_voice_cal_t)dlsym(adev->acdb_handle,
2075 "acdb_loader_send_voice_cal");
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002076 adev->acdb_init = (acdb_init_t)dlsym(adev->acdb_handle,
2077 "acdb_loader_init_ACDB");
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002078 if (adev->acdb_init == NULL)
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002079 ALOGE("%s: dlsym error %s for acdb_loader_init_ACDB", __func__, dlerror());
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002080 else
2081 adev->acdb_init();
2082 }
2083
2084 /* If platform is Fusion3, load CSD Client specific symbols
2085 * Voice call is handled by MDM and apps processor talks to
2086 * MDM through CSD Client
2087 */
2088 property_get("ro.board.platform", platform, "");
2089 property_get("ro.baseband", baseband, "");
2090 if (!strcmp("msm8960", platform) && !strcmp("mdm", baseband)) {
2091 adev->csd_client = dlopen(LIB_CSD_CLIENT, RTLD_NOW);
2092 if (adev->csd_client == NULL)
2093 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_CSD_CLIENT);
2094 }
2095
2096 if (adev->csd_client) {
2097 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_CSD_CLIENT);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002098 adev->csd_client_deinit = (csd_client_deinit_t)dlsym(adev->csd_client,
2099 "csd_client_deinit");
2100 adev->csd_disable_device = (csd_disable_device_t)dlsym(adev->csd_client,
2101 "csd_client_disable_device");
2102 adev->csd_enable_device = (csd_enable_device_t)dlsym(adev->csd_client,
2103 "csd_client_enable_device");
2104 adev->csd_start_voice = (csd_start_voice_t)dlsym(adev->csd_client,
2105 "csd_client_start_voice");
2106 adev->csd_stop_voice = (csd_stop_voice_t)dlsym(adev->csd_client,
2107 "csd_client_stop_voice");
2108 adev->csd_volume = (csd_volume_t)dlsym(adev->csd_client,
2109 "csd_client_volume");
2110 adev->csd_mic_mute = (csd_mic_mute_t)dlsym(adev->csd_client,
2111 "csd_client_mic_mute");
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002112 adev->csd_client_init = (csd_client_init_t)dlsym(adev->csd_client,
2113 "csd_client_init");
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002114
2115 if (adev->csd_client_init == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002116 ALOGE("%s: dlsym error %s for csd_client_init", __func__, dlerror());
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002117 } else {
2118 adev->csd_client_init();
2119 }
2120 }
2121}
2122
2123static int adev_open(const hw_module_t *module, const char *name,
2124 hw_device_t **device)
2125{
2126 struct audio_device *adev;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002127 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002128
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002129 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002130 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2131
2132 adev = calloc(1, sizeof(struct audio_device));
2133
2134 adev->mixer = mixer_open(MIXER_CARD);
2135 if (!adev->mixer) {
2136 ALOGE("Unable to open the mixer, aborting.");
2137 return -ENOSYS;
2138 }
2139
2140 adev->audio_route = audio_route_init(MIXER_CARD, MIXER_XML_PATH);
2141 if (!adev->audio_route) {
2142 free(adev);
2143 ALOGE("%s: Failed to init audio route controls, aborting.", __func__);
2144 *device = NULL;
2145 return -EINVAL;
2146 }
2147
2148 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2149 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2150 adev->device.common.module = (struct hw_module_t *)module;
2151 adev->device.common.close = adev_close;
2152
2153 adev->device.init_check = adev_init_check;
2154 adev->device.set_voice_volume = adev_set_voice_volume;
2155 adev->device.set_master_volume = adev_set_master_volume;
2156 adev->device.get_master_volume = adev_get_master_volume;
2157 adev->device.set_master_mute = adev_set_master_mute;
2158 adev->device.get_master_mute = adev_get_master_mute;
2159 adev->device.set_mode = adev_set_mode;
2160 adev->device.set_mic_mute = adev_set_mic_mute;
2161 adev->device.get_mic_mute = adev_get_mic_mute;
2162 adev->device.set_parameters = adev_set_parameters;
2163 adev->device.get_parameters = adev_get_parameters;
2164 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2165 adev->device.open_output_stream = adev_open_output_stream;
2166 adev->device.close_output_stream = adev_close_output_stream;
2167 adev->device.open_input_stream = adev_open_input_stream;
2168 adev->device.close_input_stream = adev_close_input_stream;
2169 adev->device.dump = adev_dump;
2170
2171 /* Set the default route before the PCM stream is opened */
2172 pthread_mutex_lock(&adev->lock);
2173 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002174 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002175 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002176 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002177 adev->voice_call_rx = NULL;
2178 adev->voice_call_tx = NULL;
2179 adev->voice_volume = 1.0f;
2180 adev->tty_mode = TTY_MODE_OFF;
2181 adev->bluetooth_nrec = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002182 adev->in_call = false;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002183 adev->acdb_settings = TTY_MODE_OFF;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002184 for (i = 0; i < SND_DEVICE_MAX; i++) {
2185 adev->snd_dev_ref_cnt[i] = 0;
2186 }
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002187 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002188 pthread_mutex_unlock(&adev->lock);
2189
2190 /* Loads platform specific libraries dynamically */
2191 init_platform_data(adev);
2192
2193 *device = &adev->device.common;
2194
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002195 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002196 return 0;
2197}
2198
2199static struct hw_module_methods_t hal_module_methods = {
2200 .open = adev_open,
2201};
2202
2203struct audio_module HAL_MODULE_INFO_SYM = {
2204 .common = {
2205 .tag = HARDWARE_MODULE_TAG,
2206 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2207 .hal_api_version = HARDWARE_HAL_API_VERSION,
2208 .id = AUDIO_HARDWARE_MODULE_ID,
2209 .name = "QCOM Audio HAL",
2210 .author = "Code Aurora Forum",
2211 .methods = &hal_module_methods,
2212 },
2213};