blob: 44e7f0dc1be82bb67075dd14ceda6173671b3148 [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;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700767 if (out_snd_device == SND_DEVICE_NONE) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700768 out_snd_device = get_output_snd_device(adev,
769 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700770 if (usecase->stream.out == adev->primary_output &&
771 adev->active_input &&
772 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
773 select_devices(adev, adev->active_input->usecase);
774 }
775 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700776 } else if (usecase->type == PCM_CAPTURE) {
777 usecase->devices = usecase->stream.in->device;
778 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700779 if (in_snd_device == SND_DEVICE_NONE) {
780 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
781 adev->primary_output && !adev->primary_output->standby) {
782 in_snd_device = get_input_snd_device(adev,
783 adev->primary_output->devices);
784 } else {
785 in_snd_device = get_input_snd_device(adev, AUDIO_DEVICE_NONE);
786 }
787 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700788 }
789 }
790
791 if (out_snd_device == usecase->out_snd_device &&
792 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800793 return 0;
794 }
795
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800796 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
797 out_snd_device, device_table[out_snd_device],
798 in_snd_device, device_table[in_snd_device]);
799
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800800 /*
801 * Limitation: While in call, to do a device switch we need to disable
802 * and enable both RX and TX devices though one of them is same as current
803 * device.
804 */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700805 if (usecase->type == VOICE_CALL && adev->csd_client != NULL &&
806 usecase->in_snd_device != SND_DEVICE_NONE &&
807 usecase->out_snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800808 /* This must be called before disabling the mixer controls on APQ side */
809 if (adev->csd_disable_device == NULL) {
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -0800810 ALOGE("%s: dlsym error for csd_client_disable_device", __func__);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800811 } else {
812 status = adev->csd_disable_device();
813 if (status < 0) {
814 ALOGE("%s: csd_client_disable_device, failed, error %d",
815 __func__, status);
816 }
817 }
818 }
819
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700820 /* Disable current sound devices */
821 if (usecase->out_snd_device != SND_DEVICE_NONE) {
822 disable_audio_route(adev, usecase, true);
823 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800824 }
825
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700826 if (usecase->in_snd_device != SND_DEVICE_NONE) {
827 disable_audio_route(adev, usecase, true);
828 disable_snd_device(adev, usecase->in_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800829 }
830
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700831 /* Enable new sound devices */
832 if (out_snd_device != SND_DEVICE_NONE) {
833 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
834 check_usecases_codec_backend(adev, usecase, out_snd_device);
835 enable_snd_device(adev, out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800836 }
837
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700838 if (in_snd_device != SND_DEVICE_NONE)
839 enable_snd_device(adev, in_snd_device, false);
840
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800841 audio_route_update_mixer(adev->audio_route);
842
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700843 usecase->in_snd_device = in_snd_device;
844 usecase->out_snd_device = out_snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800845
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700846 enable_audio_route(adev, usecase, true);
847
848 if (usecase->type == VOICE_CALL && adev->csd_client) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800849 if (adev->csd_enable_device == NULL) {
850 ALOGE("%s: dlsym error for csd_client_enable_device",
851 __func__);
852 } else {
853 acdb_rx_id = get_acdb_device_id(out_snd_device);
854 acdb_tx_id = get_acdb_device_id(in_snd_device);
855
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700856 if (acdb_rx_id > 0 || acdb_tx_id > 0) {
857 status = adev->csd_enable_device(acdb_rx_id, acdb_tx_id,
858 adev->acdb_settings);
859 if (status < 0) {
860 ALOGE("%s: csd_client_enable_device, failed, error %d",
861 __func__, status);
862 }
863 } else {
864 ALOGE("%s: Incorrect ACDB IDs (rx: %d tx: %d)", __func__,
865 acdb_rx_id, acdb_tx_id);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800866 }
867 }
868 }
869
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800870 return status;
871}
872
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800873static int stop_input_stream(struct stream_in *in)
874{
875 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800876 struct audio_usecase *uc_info;
877 struct audio_device *adev = in->dev;
878
Eric Laurentc8400632013-02-14 19:04:54 -0800879 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800880
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700881 ALOGD("%s: enter: usecase(%d: %s)", __func__,
882 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800883 uc_info = get_usecase_from_list(adev, in->usecase);
884 if (uc_info == NULL) {
885 ALOGE("%s: Could not find the usecase (%d) in the list",
886 __func__, in->usecase);
887 return -EINVAL;
888 }
889
Eric Laurent150dbfe2013-02-27 14:31:02 -0800890 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700891 disable_audio_route(adev, uc_info, true);
892
893 /* 2. Disable the tx device */
894 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800895
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800896 list_remove(&uc_info->list);
897 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800898
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800899 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800900 return ret;
901}
902
903int start_input_stream(struct stream_in *in)
904{
905 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800906 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800907 struct audio_usecase *uc_info;
908 struct audio_device *adev = in->dev;
909
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800910 ALOGD("%s: enter: usecase(%d)", __func__, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800911 in->pcm_device_id = get_pcm_device_id(adev->audio_route,
912 in->usecase,
913 PCM_CAPTURE);
914 if (in->pcm_device_id < 0) {
915 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
916 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800917 ret = -EINVAL;
918 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800919 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700920
921 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800922 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
923 uc_info->id = in->usecase;
924 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800925 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700926 uc_info->devices = in->device;
927 uc_info->in_snd_device = SND_DEVICE_NONE;
928 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800929
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800930 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700931 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800932
Eric Laurentc8400632013-02-14 19:04:54 -0800933 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
934 __func__, SOUND_CARD, in->pcm_device_id, in->config.channels);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800935 in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id,
936 PCM_IN, &in->config);
937 if (in->pcm && !pcm_is_ready(in->pcm)) {
938 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
939 pcm_close(in->pcm);
940 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800941 ret = -EIO;
942 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800943 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800944 ALOGD("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800945 return ret;
946
947error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800948 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800949
950error_config:
951 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700952 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800953
954 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800955}
956
957static int stop_output_stream(struct stream_out *out)
958{
959 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800960 struct audio_usecase *uc_info;
961 struct audio_device *adev = out->dev;
962
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700963 ALOGD("%s: enter: usecase(%d: %s)", __func__,
964 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800965 uc_info = get_usecase_from_list(adev, out->usecase);
966 if (uc_info == NULL) {
967 ALOGE("%s: Could not find the usecase (%d) in the list",
968 __func__, out->usecase);
969 return -EINVAL;
970 }
971
Eric Laurent150dbfe2013-02-27 14:31:02 -0800972 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700973 disable_audio_route(adev, uc_info, true);
974
975 /* 2. Disable the rx device */
976 disable_snd_device(adev, uc_info->out_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800977
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800978 list_remove(&uc_info->list);
979 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800980
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700981 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800982 return ret;
983}
984
985int start_output_stream(struct stream_out *out)
986{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800987 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800988 struct audio_usecase *uc_info;
989 struct audio_device *adev = out->dev;
990
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700991 ALOGD("%s: enter: usecase(%d: %s) devices(%#x)",
992 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800993 out->pcm_device_id = get_pcm_device_id(adev->audio_route,
994 out->usecase,
995 PCM_PLAYBACK);
996 if (out->pcm_device_id < 0) {
997 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
998 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800999 ret = -EINVAL;
1000 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001001 }
1002
1003 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1004 uc_info->id = out->usecase;
1005 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001006 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001007 uc_info->devices = out->devices;
1008 uc_info->in_snd_device = SND_DEVICE_NONE;
1009 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001010
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001011 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001012
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001013 select_devices(adev, out->usecase);
1014
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001015 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
1016 __func__, 0, out->pcm_device_id);
1017 out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id,
1018 PCM_OUT, &out->config);
1019 if (out->pcm && !pcm_is_ready(out->pcm)) {
1020 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1021 pcm_close(out->pcm);
1022 out->pcm = NULL;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001023 ret = -EIO;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001024 goto error_pcm_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001025 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001026 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001027 return 0;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001028error_pcm_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001029 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001030error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001031 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001032}
1033
1034static int stop_voice_call(struct audio_device *adev)
1035{
1036 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001037 struct audio_usecase *uc_info;
1038
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001039 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001040 adev->in_call = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001041 if (adev->csd_client) {
1042 if (adev->csd_stop_voice == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08001043 ALOGE("dlsym error for csd_client_disable_device");
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001044 } else {
1045 ret = adev->csd_stop_voice();
1046 if (ret < 0) {
1047 ALOGE("%s: csd_client error %d\n", __func__, ret);
1048 }
1049 }
1050 }
1051
1052 /* 1. Close the PCM devices */
1053 if (adev->voice_call_rx) {
1054 pcm_close(adev->voice_call_rx);
1055 adev->voice_call_rx = NULL;
1056 }
1057 if (adev->voice_call_tx) {
1058 pcm_close(adev->voice_call_tx);
1059 adev->voice_call_tx = NULL;
1060 }
1061
1062 uc_info = get_usecase_from_list(adev, USECASE_VOICE_CALL);
1063 if (uc_info == NULL) {
1064 ALOGE("%s: Could not find the usecase (%d) in the list",
1065 __func__, USECASE_VOICE_CALL);
1066 return -EINVAL;
1067 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001068
1069 /* 2. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001070 disable_audio_route(adev, uc_info, true);
1071
1072 /* 3. Disable the rx and tx devices */
1073 disable_snd_device(adev, uc_info->out_snd_device, false);
1074 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001075
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001076 list_remove(&uc_info->list);
1077 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001078
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001079 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001080 return ret;
1081}
1082
1083static int start_voice_call(struct audio_device *adev)
1084{
1085 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001086 struct audio_usecase *uc_info;
1087 int pcm_dev_rx_id, pcm_dev_tx_id;
1088
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001089 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001090
1091 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1092 uc_info->id = USECASE_VOICE_CALL;
1093 uc_info->type = VOICE_CALL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001094 uc_info->stream.out = adev->primary_output;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001095 uc_info->devices = adev->primary_output->devices;
1096 uc_info->in_snd_device = SND_DEVICE_NONE;
1097 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001098
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001099 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001100
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001101 select_devices(adev, USECASE_VOICE_CALL);
1102
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001103 pcm_dev_rx_id = get_pcm_device_id(adev->audio_route, uc_info->id,
1104 PCM_PLAYBACK);
1105 pcm_dev_tx_id = get_pcm_device_id(adev->audio_route, uc_info->id,
1106 PCM_CAPTURE);
1107
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001108 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
1109 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
1110 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001111 ret = -EIO;
1112 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001113 }
1114
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001115 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001116 __func__, SOUND_CARD, pcm_dev_rx_id);
1117 adev->voice_call_rx = pcm_open(SOUND_CARD,
1118 pcm_dev_rx_id,
1119 PCM_OUT, &pcm_config_voice_call);
1120 if (adev->voice_call_rx && !pcm_is_ready(adev->voice_call_rx)) {
1121 ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_rx));
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001122 ret = -EIO;
1123 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001124 }
1125
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001126 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001127 __func__, SOUND_CARD, pcm_dev_tx_id);
1128 adev->voice_call_tx = pcm_open(SOUND_CARD,
1129 pcm_dev_tx_id,
1130 PCM_IN, &pcm_config_voice_call);
1131 if (adev->voice_call_tx && !pcm_is_ready(adev->voice_call_tx)) {
1132 ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_tx));
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001133 ret = -EIO;
1134 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001135 }
1136 pcm_start(adev->voice_call_rx);
1137 pcm_start(adev->voice_call_tx);
1138
1139 if (adev->csd_client) {
1140 if (adev->csd_start_voice == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08001141 ALOGE("dlsym error for csd_client_start_voice");
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001142 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001143 } else {
1144 ret = adev->csd_start_voice();
1145 if (ret < 0) {
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001146 ALOGE("%s: csd_start_voice error %d\n", __func__, ret);
1147 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001148 }
1149 }
1150 }
1151
1152 adev->in_call = true;
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001153 return 0;
1154
1155error_start_voice:
1156 stop_voice_call(adev);
1157
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001158 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001159 return ret;
1160}
1161
1162static int check_input_parameters(uint32_t sample_rate,
1163 audio_format_t format,
1164 int channel_count)
1165{
1166 if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
1167
1168 if ((channel_count < 1) || (channel_count > 2)) return -EINVAL;
1169
1170 switch (sample_rate) {
1171 case 8000:
1172 case 11025:
1173 case 12000:
1174 case 16000:
1175 case 22050:
1176 case 24000:
1177 case 32000:
1178 case 44100:
1179 case 48000:
1180 break;
1181 default:
1182 return -EINVAL;
1183 }
1184
1185 return 0;
1186}
1187
1188static size_t get_input_buffer_size(uint32_t sample_rate,
1189 audio_format_t format,
1190 int channel_count)
1191{
1192 size_t size = 0;
1193
1194 if (check_input_parameters(sample_rate, format, channel_count) != 0) return 0;
1195
1196 if (sample_rate == 8000 || sample_rate == 16000 || sample_rate == 32000) {
1197 size = (sample_rate * 20) / 1000;
1198 } else if (sample_rate == 11025 || sample_rate == 12000) {
1199 size = 256;
1200 } else if (sample_rate == 22050 || sample_rate == 24000) {
1201 size = 512;
1202 } else if (sample_rate == 44100 || sample_rate == 48000) {
1203 size = 1024;
1204 }
1205
1206 return size * sizeof(short) * channel_count;
1207}
1208
1209static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1210{
1211 struct stream_out *out = (struct stream_out *)stream;
1212
1213 return out->config.rate;
1214}
1215
1216static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1217{
1218 return -ENOSYS;
1219}
1220
1221static size_t out_get_buffer_size(const struct audio_stream *stream)
1222{
1223 struct stream_out *out = (struct stream_out *)stream;
1224
1225 return out->config.period_size * audio_stream_frame_size(stream);
1226}
1227
1228static uint32_t out_get_channels(const struct audio_stream *stream)
1229{
1230 struct stream_out *out = (struct stream_out *)stream;
1231
1232 return out->channel_mask;
1233}
1234
1235static audio_format_t out_get_format(const struct audio_stream *stream)
1236{
1237 return AUDIO_FORMAT_PCM_16_BIT;
1238}
1239
1240static int out_set_format(struct audio_stream *stream, audio_format_t format)
1241{
1242 return -ENOSYS;
1243}
1244
1245static int out_standby(struct audio_stream *stream)
1246{
1247 struct stream_out *out = (struct stream_out *)stream;
1248 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001249 ALOGD("%s: enter: usecase(%d: %s)", __func__,
1250 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001251 pthread_mutex_lock(&out->lock);
1252
1253 if (!out->standby) {
1254 out->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001255 if (out->pcm) {
1256 pcm_close(out->pcm);
1257 out->pcm = NULL;
1258 }
1259 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001260 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001261 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001262 }
1263 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001264 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001265 return 0;
1266}
1267
1268static int out_dump(const struct audio_stream *stream, int fd)
1269{
1270 return 0;
1271}
1272
1273static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1274{
1275 struct stream_out *out = (struct stream_out *)stream;
1276 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001277 struct audio_usecase *usecase;
1278 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001279 struct str_parms *parms;
1280 char value[32];
1281 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001282 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001283
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001284 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
1285 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001286 parms = str_parms_create_str(kvpairs);
1287 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1288 if (ret >= 0) {
1289 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001290 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001291 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001292
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001293 /*
1294 * When HDMI cable is unplugged the music playback is paused and
1295 * the policy manager sends routing=0. But the audioflinger
1296 * continues to write data until standby time (3sec).
1297 * As the HDMI core is turned off, the write gets blocked.
1298 * Avoid this by routing audio to speaker until standby.
1299 */
1300 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1301 val == AUDIO_DEVICE_NONE) {
1302 val = AUDIO_DEVICE_OUT_SPEAKER;
1303 }
1304
1305 /*
1306 * select_devices() call below switches all the usecases on the same
1307 * backend to the new device. Refer to check_usecases_codec_backend() in
1308 * the select_devices(). But how do we undo this?
1309 *
1310 * For example, music playback is active on headset (deep-buffer usecase)
1311 * and if we go to ringtones and select a ringtone, low-latency usecase
1312 * will be started on headset+speaker. As we can't enable headset+speaker
1313 * and headset devices at the same time, select_devices() switches the music
1314 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1315 * So when the ringtone playback is completed, how do we undo the same?
1316 *
1317 * We are relying on the out_set_parameters() call on deep-buffer output,
1318 * once the ringtone playback is ended.
1319 * NOTE: We should not check if the current devices are same as new devices.
1320 * Because select_devices() must be called to switch back the music
1321 * playback to headset.
1322 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001323 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001324 out->devices = val;
1325
1326 if (!out->standby)
1327 select_devices(adev, out->usecase);
1328
1329 if ((adev->mode == AUDIO_MODE_IN_CALL) && !adev->in_call &&
1330 (out == adev->primary_output)) {
1331 start_voice_call(adev);
1332 } else if ((adev->mode == AUDIO_MODE_IN_CALL) && adev->in_call &&
1333 (out == adev->primary_output)) {
1334 select_devices(adev, USECASE_VOICE_CALL);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001335 }
1336 }
1337
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001338 if ((adev->mode != AUDIO_MODE_IN_CALL) && adev->in_call &&
1339 (out == adev->primary_output)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001340 stop_voice_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001341 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001342
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001343 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001344 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001345 }
1346 str_parms_destroy(parms);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001347 ALOGD("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001348 return ret;
1349}
1350
1351static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1352{
1353 struct stream_out *out = (struct stream_out *)stream;
1354 struct str_parms *query = str_parms_create_str(keys);
1355 char *str;
1356 char value[256];
1357 struct str_parms *reply = str_parms_create();
1358 size_t i, j;
1359 int ret;
1360 bool first = true;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001361 ALOGD("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001362 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1363 if (ret >= 0) {
1364 value[0] = '\0';
1365 i = 0;
1366 while (out->supported_channel_masks[i] != 0) {
1367 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1368 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1369 if (!first) {
1370 strcat(value, "|");
1371 }
1372 strcat(value, out_channels_name_to_enum_table[j].name);
1373 first = false;
1374 break;
1375 }
1376 }
1377 i++;
1378 }
1379 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1380 str = str_parms_to_str(reply);
1381 } else {
1382 str = strdup(keys);
1383 }
1384 str_parms_destroy(query);
1385 str_parms_destroy(reply);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001386 ALOGD("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001387 return str;
1388}
1389
1390static uint32_t out_get_latency(const struct audio_stream_out *stream)
1391{
1392 struct stream_out *out = (struct stream_out *)stream;
1393
1394 return (out->config.period_count * out->config.period_size * 1000) / (out->config.rate);
1395}
1396
1397static int out_set_volume(struct audio_stream_out *stream, float left,
1398 float right)
1399{
Eric Laurenta9024de2013-04-04 09:19:12 -07001400 struct stream_out *out = (struct stream_out *)stream;
1401 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1402 /* only take left channel into account: the API is for stereo anyway */
1403 out->muted = (left == 0.0f);
1404 return 0;
1405 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001406 return -ENOSYS;
1407}
1408
1409static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1410 size_t bytes)
1411{
1412 struct stream_out *out = (struct stream_out *)stream;
1413 struct audio_device *adev = out->dev;
1414 int i, ret = -1;
1415
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001416 pthread_mutex_lock(&out->lock);
1417 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001418 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001419 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001420 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001421 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001422 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001423 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001424 goto exit;
1425 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001426 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001427
1428 if (out->pcm) {
Eric Laurenta9024de2013-04-04 09:19:12 -07001429 if (out->muted)
1430 memset((void *)buffer, 0, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001431 //ALOGV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1432 ret = pcm_write(out->pcm, (void *)buffer, bytes);
1433 }
1434
1435exit:
1436 pthread_mutex_unlock(&out->lock);
1437
1438 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001439 if (out->pcm)
1440 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001441 out_standby(&out->stream.common);
1442 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1443 out_get_sample_rate(&out->stream.common));
1444 }
1445 return bytes;
1446}
1447
1448static int out_get_render_position(const struct audio_stream_out *stream,
1449 uint32_t *dsp_frames)
1450{
1451 return -EINVAL;
1452}
1453
1454static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1455{
1456 return 0;
1457}
1458
1459static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1460{
1461 return 0;
1462}
1463
1464static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1465 int64_t *timestamp)
1466{
1467 return -EINVAL;
1468}
1469
1470/** audio_stream_in implementation **/
1471static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1472{
1473 struct stream_in *in = (struct stream_in *)stream;
1474
1475 return in->config.rate;
1476}
1477
1478static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1479{
1480 return -ENOSYS;
1481}
1482
1483static size_t in_get_buffer_size(const struct audio_stream *stream)
1484{
1485 struct stream_in *in = (struct stream_in *)stream;
1486
1487 return in->config.period_size * audio_stream_frame_size(stream);
1488}
1489
1490static uint32_t in_get_channels(const struct audio_stream *stream)
1491{
1492 struct stream_in *in = (struct stream_in *)stream;
1493
1494 return in->channel_mask;
1495}
1496
1497static audio_format_t in_get_format(const struct audio_stream *stream)
1498{
1499 return AUDIO_FORMAT_PCM_16_BIT;
1500}
1501
1502static int in_set_format(struct audio_stream *stream, audio_format_t format)
1503{
1504 return -ENOSYS;
1505}
1506
1507static int in_standby(struct audio_stream *stream)
1508{
1509 struct stream_in *in = (struct stream_in *)stream;
1510 struct audio_device *adev = in->dev;
1511 int status = 0;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001512 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001513 pthread_mutex_lock(&in->lock);
1514 if (!in->standby) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001515 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001516 if (in->pcm) {
1517 pcm_close(in->pcm);
1518 in->pcm = NULL;
1519 }
1520 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001521 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001522 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001523 }
1524 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001525 ALOGD("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001526 return status;
1527}
1528
1529static int in_dump(const struct audio_stream *stream, int fd)
1530{
1531 return 0;
1532}
1533
1534static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1535{
1536 struct stream_in *in = (struct stream_in *)stream;
1537 struct audio_device *adev = in->dev;
1538 struct str_parms *parms;
1539 char *str;
1540 char value[32];
1541 int ret, val = 0;
1542
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001543 ALOGD("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001544 parms = str_parms_create_str(kvpairs);
1545
1546 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1547
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001548 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001549 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001550 if (ret >= 0) {
1551 val = atoi(value);
1552 /* no audio source uses val == 0 */
1553 if ((in->source != val) && (val != 0)) {
1554 in->source = val;
1555 }
1556 }
1557
1558 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1559 if (ret >= 0) {
1560 val = atoi(value);
1561 if ((in->device != val) && (val != 0)) {
1562 in->device = val;
1563 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001564 if (!in->standby)
1565 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001566 }
1567 }
1568
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001569 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001570 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001571
1572 str_parms_destroy(parms);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001573 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001574 return ret;
1575}
1576
1577static char* in_get_parameters(const struct audio_stream *stream,
1578 const char *keys)
1579{
1580 return strdup("");
1581}
1582
1583static int in_set_gain(struct audio_stream_in *stream, float gain)
1584{
1585 return 0;
1586}
1587
1588static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1589 size_t bytes)
1590{
1591 struct stream_in *in = (struct stream_in *)stream;
1592 struct audio_device *adev = in->dev;
1593 int i, ret = -1;
1594
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001595 pthread_mutex_lock(&in->lock);
1596 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001597 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001598 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001599 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001600 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001601 goto exit;
1602 }
1603 in->standby = 0;
1604 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001605
1606 if (in->pcm) {
1607 ret = pcm_read(in->pcm, buffer, bytes);
1608 }
1609
1610 /*
1611 * Instead of writing zeroes here, we could trust the hardware
1612 * to always provide zeroes when muted.
1613 */
1614 if (ret == 0 && adev->mic_mute)
1615 memset(buffer, 0, bytes);
1616
1617exit:
1618 pthread_mutex_unlock(&in->lock);
1619
1620 if (ret != 0) {
1621 in_standby(&in->stream.common);
1622 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1623 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1624 in_get_sample_rate(&in->stream.common));
1625 }
1626 return bytes;
1627}
1628
1629static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1630{
1631 return 0;
1632}
1633
1634static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1635{
1636 return 0;
1637}
1638
1639static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1640{
1641 return 0;
1642}
1643
1644static int adev_open_output_stream(struct audio_hw_device *dev,
1645 audio_io_handle_t handle,
1646 audio_devices_t devices,
1647 audio_output_flags_t flags,
1648 struct audio_config *config,
1649 struct audio_stream_out **stream_out)
1650{
1651 struct audio_device *adev = (struct audio_device *)dev;
1652 struct stream_out *out;
1653 int i, ret;
1654
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001655 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001656 __func__, config->sample_rate, config->channel_mask, devices, flags);
1657 *stream_out = NULL;
1658 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1659
1660 if (devices == AUDIO_DEVICE_NONE)
1661 devices = AUDIO_DEVICE_OUT_SPEAKER;
1662
1663 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
1664 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1665 out->flags = flags;
1666 out->devices = devices;
1667
1668 /* Init use case and pcm_config */
1669 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
1670 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001671 pthread_mutex_lock(&adev->lock);
1672 ret = read_hdmi_channel_masks(out);
1673 pthread_mutex_unlock(&adev->lock);
1674 if (ret != 0) {
1675 /* If HDMI does not support multi channel playback, set the default */
1676 out->config.channels = popcount(out->channel_mask);
1677 set_hdmi_channels(adev->mixer, out->config.channels);
1678 goto error_open;
1679 }
1680
1681 if (config->sample_rate == 0)
1682 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1683 if (config->channel_mask == 0)
1684 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1685
1686 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001687 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
1688 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001689 out->config.rate = config->sample_rate;
1690 out->config.channels = popcount(out->channel_mask);
1691 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
1692 set_hdmi_channels(adev->mixer, out->config.channels);
1693 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
1694 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
1695 out->config = pcm_config_deep_buffer;
1696 } else {
1697 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
1698 out->config = pcm_config_low_latency;
1699 }
1700
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001701 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1702 if(adev->primary_output == NULL)
1703 adev->primary_output = out;
1704 else {
1705 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001706 ret = -EEXIST;
1707 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001708 }
1709 }
1710
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001711 /* Check if this usecase is already existing */
1712 pthread_mutex_lock(&adev->lock);
1713 if (get_usecase_from_list(adev, out->usecase) != NULL) {
1714 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001715 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001716 ret = -EEXIST;
1717 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001718 }
1719 pthread_mutex_unlock(&adev->lock);
1720
1721 out->stream.common.get_sample_rate = out_get_sample_rate;
1722 out->stream.common.set_sample_rate = out_set_sample_rate;
1723 out->stream.common.get_buffer_size = out_get_buffer_size;
1724 out->stream.common.get_channels = out_get_channels;
1725 out->stream.common.get_format = out_get_format;
1726 out->stream.common.set_format = out_set_format;
1727 out->stream.common.standby = out_standby;
1728 out->stream.common.dump = out_dump;
1729 out->stream.common.set_parameters = out_set_parameters;
1730 out->stream.common.get_parameters = out_get_parameters;
1731 out->stream.common.add_audio_effect = out_add_audio_effect;
1732 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1733 out->stream.get_latency = out_get_latency;
1734 out->stream.set_volume = out_set_volume;
1735 out->stream.write = out_write;
1736 out->stream.get_render_position = out_get_render_position;
1737 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
1738
1739 out->dev = adev;
1740 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07001741 /* out->muted = false; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001742
1743 config->format = out->stream.common.get_format(&out->stream.common);
1744 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
1745 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
1746
1747 *stream_out = &out->stream;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001748 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001749 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001750
1751error_open:
1752 free(out);
1753 *stream_out = NULL;
1754 ALOGD("%s: exit: ret %d", __func__, ret);
1755 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001756}
1757
1758static void adev_close_output_stream(struct audio_hw_device *dev,
1759 struct audio_stream_out *stream)
1760{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001761 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001762 out_standby(&stream->common);
1763 free(stream);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001764 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001765}
1766
1767static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
1768{
1769 struct audio_device *adev = (struct audio_device *)dev;
1770 struct str_parms *parms;
1771 char *str;
1772 char value[32];
1773 int ret;
1774
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001775 ALOGD("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001776
1777 parms = str_parms_create_str(kvpairs);
1778 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value));
1779 if (ret >= 0) {
1780 int tty_mode;
1781
1782 if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0)
1783 tty_mode = TTY_MODE_OFF;
1784 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0)
1785 tty_mode = TTY_MODE_VCO;
1786 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0)
1787 tty_mode = TTY_MODE_HCO;
1788 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0)
1789 tty_mode = TTY_MODE_FULL;
1790 else
1791 return -EINVAL;
1792
1793 pthread_mutex_lock(&adev->lock);
1794 if (tty_mode != adev->tty_mode) {
1795 adev->tty_mode = tty_mode;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08001796 adev->acdb_settings = (adev->acdb_settings & TTY_MODE_CLEAR) | tty_mode;
1797 if (adev->in_call)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001798 select_devices(adev, USECASE_VOICE_CALL);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001799 }
1800 pthread_mutex_unlock(&adev->lock);
1801 }
1802
1803 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
1804 if (ret >= 0) {
1805 /* When set to false, HAL should disable EC and NS
1806 * But it is currently not supported.
1807 */
1808 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
1809 adev->bluetooth_nrec = true;
1810 else
1811 adev->bluetooth_nrec = false;
1812 }
1813
1814 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
1815 if (ret >= 0) {
1816 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
1817 adev->screen_off = false;
1818 else
1819 adev->screen_off = true;
1820 }
1821
1822 str_parms_destroy(parms);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001823 ALOGD("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001824 return ret;
1825}
1826
1827static char* adev_get_parameters(const struct audio_hw_device *dev,
1828 const char *keys)
1829{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001830 return strdup("");
1831}
1832
1833static int adev_init_check(const struct audio_hw_device *dev)
1834{
1835 return 0;
1836}
1837
1838static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
1839{
1840 struct audio_device *adev = (struct audio_device *)dev;
1841 int vol, err = 0;
1842
1843 pthread_mutex_lock(&adev->lock);
1844 adev->voice_volume = volume;
1845 if (adev->mode == AUDIO_MODE_IN_CALL) {
1846 if (volume < 0.0) {
1847 volume = 0.0;
1848 } else if (volume > 1.0) {
1849 volume = 1.0;
1850 }
1851
1852 vol = lrint(volume * 100.0);
1853
1854 // Voice volume levels from android are mapped to driver volume levels as follows.
1855 // 0 -> 5, 20 -> 4, 40 ->3, 60 -> 2, 80 -> 1, 100 -> 0
1856 // So adjust the volume to get the correct volume index in driver
1857 vol = 100 - vol;
1858
1859 if (adev->csd_client) {
1860 if (adev->csd_volume == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08001861 ALOGE("%s: dlsym error for csd_client_volume", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001862 } else {
1863 err = adev->csd_volume(vol);
1864 if (err < 0) {
1865 ALOGE("%s: csd_client error %d", __func__, err);
1866 }
1867 }
1868 } else {
1869 ALOGE("%s: No CSD Client present", __func__);
1870 }
1871 }
1872 pthread_mutex_unlock(&adev->lock);
1873 return err;
1874}
1875
1876static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
1877{
1878 return -ENOSYS;
1879}
1880
1881static int adev_get_master_volume(struct audio_hw_device *dev,
1882 float *volume)
1883{
1884 return -ENOSYS;
1885}
1886
1887static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
1888{
1889 return -ENOSYS;
1890}
1891
1892static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
1893{
1894 return -ENOSYS;
1895}
1896
1897static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
1898{
1899 struct audio_device *adev = (struct audio_device *)dev;
1900
1901 pthread_mutex_lock(&adev->lock);
1902 if (adev->mode != mode) {
1903 adev->mode = mode;
1904 }
1905 pthread_mutex_unlock(&adev->lock);
1906 return 0;
1907}
1908
1909static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
1910{
1911 struct audio_device *adev = (struct audio_device *)dev;
1912 int err = 0;
1913
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001914 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001915 adev->mic_mute = state;
1916 if (adev->mode == AUDIO_MODE_IN_CALL) {
1917 if (adev->csd_client) {
1918 if (adev->csd_mic_mute == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08001919 ALOGE("%s: dlsym error for csd_mic_mute", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001920 } else {
1921 err = adev->csd_mic_mute(state);
1922 if (err < 0) {
1923 ALOGE("%s: csd_client error %d", __func__, err);
1924 }
1925 }
1926 } else {
1927 ALOGE("%s: No CSD Client present", __func__);
1928 }
1929 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001930 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001931 return err;
1932}
1933
1934static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1935{
1936 struct audio_device *adev = (struct audio_device *)dev;
1937
1938 *state = adev->mic_mute;
1939
1940 return 0;
1941}
1942
1943static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
1944 const struct audio_config *config)
1945{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001946 int channel_count = popcount(config->channel_mask);
1947
1948 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
1949}
1950
1951static int adev_open_input_stream(struct audio_hw_device *dev,
1952 audio_io_handle_t handle,
1953 audio_devices_t devices,
1954 struct audio_config *config,
1955 struct audio_stream_in **stream_in)
1956{
1957 struct audio_device *adev = (struct audio_device *)dev;
1958 struct stream_in *in;
1959 int ret, buffer_size, frame_size;
1960 int channel_count = popcount(config->channel_mask);
1961
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001962 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001963 *stream_in = NULL;
1964 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
1965 return -EINVAL;
1966
1967 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
1968
1969 in->stream.common.get_sample_rate = in_get_sample_rate;
1970 in->stream.common.set_sample_rate = in_set_sample_rate;
1971 in->stream.common.get_buffer_size = in_get_buffer_size;
1972 in->stream.common.get_channels = in_get_channels;
1973 in->stream.common.get_format = in_get_format;
1974 in->stream.common.set_format = in_set_format;
1975 in->stream.common.standby = in_standby;
1976 in->stream.common.dump = in_dump;
1977 in->stream.common.set_parameters = in_set_parameters;
1978 in->stream.common.get_parameters = in_get_parameters;
1979 in->stream.common.add_audio_effect = in_add_audio_effect;
1980 in->stream.common.remove_audio_effect = in_remove_audio_effect;
1981 in->stream.set_gain = in_set_gain;
1982 in->stream.read = in_read;
1983 in->stream.get_input_frames_lost = in_get_input_frames_lost;
1984
1985 in->device = devices;
1986 in->source = AUDIO_SOURCE_DEFAULT;
1987 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001988 in->standby = 1;
1989 in->channel_mask = config->channel_mask;
1990
1991 /* Update config params with the requested sample rate and channels */
1992 in->usecase = USECASE_AUDIO_RECORD;
1993 in->config = pcm_config_audio_capture;
1994 in->config.channels = channel_count;
1995 in->config.rate = config->sample_rate;
1996
1997 frame_size = audio_stream_frame_size((struct audio_stream *)in);
1998 buffer_size = get_input_buffer_size(config->sample_rate,
1999 config->format,
2000 channel_count);
2001 in->config.period_size = buffer_size / frame_size;
2002
2003 *stream_in = &in->stream;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002004 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002005 return 0;
2006
2007err_open:
2008 free(in);
2009 *stream_in = NULL;
2010 return ret;
2011}
2012
2013static void adev_close_input_stream(struct audio_hw_device *dev,
2014 struct audio_stream_in *stream)
2015{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002016 ALOGD("%s", __func__);
2017
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002018 in_standby(&stream->common);
2019 free(stream);
2020
2021 return;
2022}
2023
2024static int adev_dump(const audio_hw_device_t *device, int fd)
2025{
2026 return 0;
2027}
2028
2029static int adev_close(hw_device_t *device)
2030{
2031 struct audio_device *adev = (struct audio_device *)device;
2032 audio_route_free(adev->audio_route);
2033 free(device);
2034 return 0;
2035}
2036
2037static void init_platform_data(struct audio_device *adev)
2038{
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002039 char platform[PROPERTY_VALUE_MAX];
2040 char baseband[PROPERTY_VALUE_MAX];
2041 char value[PROPERTY_VALUE_MAX];
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002042
2043 adev->dualmic_config = DUALMIC_CONFIG_NONE;
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -08002044 adev->fluence_in_spkr_mode = false;
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002045 adev->fluence_in_voice_call = false;
2046 adev->fluence_in_voice_rec = false;
2047 adev->mic_type_analog = false;
2048
2049 property_get("persist.audio.handset.mic.type",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002050 if (!strcmp("analog", value))
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002051 adev->mic_type_analog = true;
2052
2053 property_get("persist.audio.dualmic.config",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002054 if (!strcmp("broadside", value)) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002055 adev->dualmic_config = DUALMIC_CONFIG_BROADSIDE;
2056 adev->acdb_settings |= DMIC_FLAG;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002057 } else if (!strcmp("endfire", value)) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002058 adev->dualmic_config = DUALMIC_CONFIG_ENDFIRE;
2059 adev->acdb_settings |= DMIC_FLAG;
2060 }
2061
2062 if (adev->dualmic_config != DUALMIC_CONFIG_NONE) {
2063 property_get("persist.audio.fluence.voicecall",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002064 if (!strcmp("true", value)) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002065 adev->fluence_in_voice_call = true;
2066 }
2067
2068 property_get("persist.audio.fluence.voicerec",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002069 if (!strcmp("true", value)) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002070 adev->fluence_in_voice_rec = true;
2071 }
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -08002072
2073 property_get("persist.audio.fluence.speaker",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002074 if (!strcmp("true", value)) {
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -08002075 adev->fluence_in_spkr_mode = true;
2076 }
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002077 }
2078
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002079 adev->acdb_handle = dlopen(LIB_ACDB_LOADER, RTLD_NOW);
2080 if (adev->acdb_handle == NULL) {
2081 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_ACDB_LOADER);
2082 } else {
2083 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_ACDB_LOADER);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002084 adev->acdb_deallocate = (acdb_deallocate_t)dlsym(adev->acdb_handle,
2085 "acdb_loader_deallocate_ACDB");
2086 adev->acdb_send_audio_cal = (acdb_send_audio_cal_t)dlsym(adev->acdb_handle,
2087 "acdb_loader_send_audio_cal");
2088 adev->acdb_send_voice_cal = (acdb_send_voice_cal_t)dlsym(adev->acdb_handle,
2089 "acdb_loader_send_voice_cal");
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002090 adev->acdb_init = (acdb_init_t)dlsym(adev->acdb_handle,
2091 "acdb_loader_init_ACDB");
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002092 if (adev->acdb_init == NULL)
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002093 ALOGE("%s: dlsym error %s for acdb_loader_init_ACDB", __func__, dlerror());
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002094 else
2095 adev->acdb_init();
2096 }
2097
2098 /* If platform is Fusion3, load CSD Client specific symbols
2099 * Voice call is handled by MDM and apps processor talks to
2100 * MDM through CSD Client
2101 */
2102 property_get("ro.board.platform", platform, "");
2103 property_get("ro.baseband", baseband, "");
2104 if (!strcmp("msm8960", platform) && !strcmp("mdm", baseband)) {
2105 adev->csd_client = dlopen(LIB_CSD_CLIENT, RTLD_NOW);
2106 if (adev->csd_client == NULL)
2107 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_CSD_CLIENT);
2108 }
2109
2110 if (adev->csd_client) {
2111 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_CSD_CLIENT);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002112 adev->csd_client_deinit = (csd_client_deinit_t)dlsym(adev->csd_client,
2113 "csd_client_deinit");
2114 adev->csd_disable_device = (csd_disable_device_t)dlsym(adev->csd_client,
2115 "csd_client_disable_device");
2116 adev->csd_enable_device = (csd_enable_device_t)dlsym(adev->csd_client,
2117 "csd_client_enable_device");
2118 adev->csd_start_voice = (csd_start_voice_t)dlsym(adev->csd_client,
2119 "csd_client_start_voice");
2120 adev->csd_stop_voice = (csd_stop_voice_t)dlsym(adev->csd_client,
2121 "csd_client_stop_voice");
2122 adev->csd_volume = (csd_volume_t)dlsym(adev->csd_client,
2123 "csd_client_volume");
2124 adev->csd_mic_mute = (csd_mic_mute_t)dlsym(adev->csd_client,
2125 "csd_client_mic_mute");
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002126 adev->csd_client_init = (csd_client_init_t)dlsym(adev->csd_client,
2127 "csd_client_init");
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002128
2129 if (adev->csd_client_init == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002130 ALOGE("%s: dlsym error %s for csd_client_init", __func__, dlerror());
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002131 } else {
2132 adev->csd_client_init();
2133 }
2134 }
2135}
2136
2137static int adev_open(const hw_module_t *module, const char *name,
2138 hw_device_t **device)
2139{
2140 struct audio_device *adev;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002141 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002142
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002143 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002144 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2145
2146 adev = calloc(1, sizeof(struct audio_device));
2147
2148 adev->mixer = mixer_open(MIXER_CARD);
2149 if (!adev->mixer) {
2150 ALOGE("Unable to open the mixer, aborting.");
2151 return -ENOSYS;
2152 }
2153
2154 adev->audio_route = audio_route_init(MIXER_CARD, MIXER_XML_PATH);
2155 if (!adev->audio_route) {
2156 free(adev);
2157 ALOGE("%s: Failed to init audio route controls, aborting.", __func__);
2158 *device = NULL;
2159 return -EINVAL;
2160 }
2161
2162 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2163 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2164 adev->device.common.module = (struct hw_module_t *)module;
2165 adev->device.common.close = adev_close;
2166
2167 adev->device.init_check = adev_init_check;
2168 adev->device.set_voice_volume = adev_set_voice_volume;
2169 adev->device.set_master_volume = adev_set_master_volume;
2170 adev->device.get_master_volume = adev_get_master_volume;
2171 adev->device.set_master_mute = adev_set_master_mute;
2172 adev->device.get_master_mute = adev_get_master_mute;
2173 adev->device.set_mode = adev_set_mode;
2174 adev->device.set_mic_mute = adev_set_mic_mute;
2175 adev->device.get_mic_mute = adev_get_mic_mute;
2176 adev->device.set_parameters = adev_set_parameters;
2177 adev->device.get_parameters = adev_get_parameters;
2178 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2179 adev->device.open_output_stream = adev_open_output_stream;
2180 adev->device.close_output_stream = adev_close_output_stream;
2181 adev->device.open_input_stream = adev_open_input_stream;
2182 adev->device.close_input_stream = adev_close_input_stream;
2183 adev->device.dump = adev_dump;
2184
2185 /* Set the default route before the PCM stream is opened */
2186 pthread_mutex_lock(&adev->lock);
2187 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002188 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002189 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002190 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002191 adev->voice_call_rx = NULL;
2192 adev->voice_call_tx = NULL;
2193 adev->voice_volume = 1.0f;
2194 adev->tty_mode = TTY_MODE_OFF;
2195 adev->bluetooth_nrec = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002196 adev->in_call = false;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002197 adev->acdb_settings = TTY_MODE_OFF;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002198 for (i = 0; i < SND_DEVICE_MAX; i++) {
2199 adev->snd_dev_ref_cnt[i] = 0;
2200 }
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002201 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002202 pthread_mutex_unlock(&adev->lock);
2203
2204 /* Loads platform specific libraries dynamically */
2205 init_platform_data(adev);
2206
2207 *device = &adev->device.common;
2208
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002209 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002210 return 0;
2211}
2212
2213static struct hw_module_methods_t hal_module_methods = {
2214 .open = adev_open,
2215};
2216
2217struct audio_module HAL_MODULE_INFO_SYM = {
2218 .common = {
2219 .tag = HARDWARE_MODULE_TAG,
2220 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2221 .hal_api_version = HARDWARE_HAL_API_VERSION,
2222 .id = AUDIO_HARDWARE_MODULE_ID,
2223 .name = "QCOM Audio HAL",
2224 .author = "Code Aurora Forum",
2225 .methods = &hal_module_methods,
2226 },
2227};