blob: d96b1ea533fa548e841daee2194e3cd9b2e2c691 [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
Sungmin Choi75ebaa02013-05-22 13:14:28 -070036#define LIB_ACDB_LOADER "libacdbloader.so"
37#define LIB_CSD_CLIENT "libcsd-client.so"
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080038#define MIXER_XML_PATH "/system/etc/mixer_paths.xml"
39#define MIXER_CARD 0
40
41#define STRING_TO_ENUM(string) { #string, string }
42
43/* Flags used to initialize acdb_settings variable that goes to ACDB library */
44#define DMIC_FLAG 0x00000002
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -080045#define TTY_MODE_OFF 0x00000010
46#define TTY_MODE_FULL 0x00000020
47#define TTY_MODE_VCO 0x00000040
48#define TTY_MODE_HCO 0x00000080
49#define TTY_MODE_CLEAR 0xFFFFFF0F
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080050
51struct string_to_enum {
52 const char *name;
53 uint32_t value;
54};
55
56static const struct string_to_enum out_channels_name_to_enum_table[] = {
57 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
58 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
59 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
60};
61
62static const char * const use_case_table[AUDIO_USECASE_MAX] = {
63 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
64 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
65 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
66 [USECASE_AUDIO_RECORD] = "audio-record",
67 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
68 [USECASE_VOICE_CALL] = "voice-call",
69};
70
71static const int pcm_device_table[AUDIO_USECASE_MAX][2] = {
72 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0},
Sungmin Choi5195a4b2013-04-03 21:54:22 -070073#ifdef MSM8974
74 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15},
75#else
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080076 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {14, 14},
Sungmin Choi5195a4b2013-04-03 21:54:22 -070077#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080078 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = {1, 1},
79 [USECASE_AUDIO_RECORD] = {0, 0},
80 [USECASE_AUDIO_RECORD_LOW_LATENCY] = {14, 14},
Shashank Mittald2f3c3b2013-05-02 20:27:47 -070081#ifdef MSM8974
82 [USECASE_VOICE_CALL] = {2, 2},
83#else
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080084 [USECASE_VOICE_CALL] = {12, 12},
Shashank Mittald2f3c3b2013-05-02 20:27:47 -070085#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080086};
87
88/* Array to store sound devices */
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -080089static const char * const device_table[SND_DEVICE_MAX] = {
90 [SND_DEVICE_NONE] = "none",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080091 /* Playback sound devices */
92 [SND_DEVICE_OUT_HANDSET] = "handset",
93 [SND_DEVICE_OUT_SPEAKER] = "speaker",
Jean-Michel Trivic56336b2013-05-24 16:55:17 -070094 [SND_DEVICE_OUT_SPEAKER_REVERSE] = "speaker-reverse",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080095 [SND_DEVICE_OUT_HEADPHONES] = "headphones",
96 [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = "speaker-and-headphones",
Ravi Kumar Alamanda87f6ee02013-05-07 14:20:11 -070097 [SND_DEVICE_OUT_VOICE_HANDSET] = "voice-handset",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080098 [SND_DEVICE_OUT_VOICE_SPEAKER] = "voice-speaker",
99 [SND_DEVICE_OUT_VOICE_HEADPHONES] = "voice-headphones",
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800100 [SND_DEVICE_OUT_HDMI] = "hdmi",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800101 [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = "speaker-and-hdmi",
102 [SND_DEVICE_OUT_BT_SCO] = "bt-sco-headset",
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800103 [SND_DEVICE_OUT_VOICE_HANDSET_TMUS] = "voice-handset-tmus",
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800104 [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = "voice-tty-full-headphones",
105 [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = "voice-tty-vco-headphones",
106 [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = "voice-tty-hco-handset",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800107
108 /* Capture sound devices */
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800109 [SND_DEVICE_IN_HANDSET_MIC] = "handset-mic",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800110 [SND_DEVICE_IN_SPEAKER_MIC] = "speaker-mic",
111 [SND_DEVICE_IN_HEADSET_MIC] = "headset-mic",
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -0700112 [SND_DEVICE_IN_HANDSET_MIC_AEC] = "handset-mic",
113 [SND_DEVICE_IN_SPEAKER_MIC_AEC] = "voice-speaker-mic",
114 [SND_DEVICE_IN_HEADSET_MIC_AEC] = "headset-mic",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800115 [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = "voice-speaker-mic",
116 [SND_DEVICE_IN_VOICE_HEADSET_MIC] = "voice-headset-mic",
117 [SND_DEVICE_IN_HDMI_MIC] = "hdmi-mic",
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800118 [SND_DEVICE_IN_BT_SCO_MIC] = "bt-sco-mic",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800119 [SND_DEVICE_IN_CAMCORDER_MIC] = "camcorder-mic",
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800120 [SND_DEVICE_IN_VOICE_DMIC_EF] = "voice-dmic-ef",
121 [SND_DEVICE_IN_VOICE_DMIC_BS] = "voice-dmic-bs",
122 [SND_DEVICE_IN_VOICE_DMIC_EF_TMUS] = "voice-dmic-ef-tmus",
123 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF] = "voice-speaker-dmic-ef",
124 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS] = "voice-speaker-dmic-bs",
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800125 [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = "voice-tty-full-headset-mic",
126 [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = "voice-tty-vco-handset-mic",
127 [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = "voice-tty-hco-headset-mic",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800128 [SND_DEVICE_IN_VOICE_REC_MIC] = "voice-rec-mic",
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800129 [SND_DEVICE_IN_VOICE_REC_DMIC_EF] = "voice-rec-dmic-ef",
130 [SND_DEVICE_IN_VOICE_REC_DMIC_BS] = "voice-rec-dmic-bs",
Eric Laurentc8400632013-02-14 19:04:54 -0800131 [SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE] = "voice-rec-dmic-ef-fluence",
132 [SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE] = "voice-rec-dmic-bs-fluence",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800133};
134
Eric Laurentc8400632013-02-14 19:04:54 -0800135/* ACDB IDs (audio DSP path configuration IDs) for each sound device */
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800136static const int acdb_device_table[SND_DEVICE_MAX] = {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700137 [SND_DEVICE_NONE] = -1,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800138 [SND_DEVICE_OUT_HANDSET] = 7,
Ravi Kumar Alamanda87f6ee02013-05-07 14:20:11 -0700139#ifdef MSM8974
140 [SND_DEVICE_OUT_SPEAKER] = 15,
141#else
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800142 [SND_DEVICE_OUT_SPEAKER] = 14,
Ravi Kumar Alamanda87f6ee02013-05-07 14:20:11 -0700143#endif
Jean-Michel Trivic56336b2013-05-24 16:55:17 -0700144 [SND_DEVICE_OUT_SPEAKER_REVERSE] = 14,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800145 [SND_DEVICE_OUT_HEADPHONES] = 10,
146 [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = 10,
Ravi Kumar Alamanda87f6ee02013-05-07 14:20:11 -0700147 [SND_DEVICE_OUT_VOICE_HANDSET] = 7,
148#ifdef MSM8974
149 [SND_DEVICE_OUT_VOICE_SPEAKER] = 15,
150#else
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800151 [SND_DEVICE_OUT_VOICE_SPEAKER] = 14,
Ravi Kumar Alamanda87f6ee02013-05-07 14:20:11 -0700152#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800153 [SND_DEVICE_OUT_VOICE_HEADPHONES] = 10,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800154 [SND_DEVICE_OUT_HDMI] = 18,
Ravi Kumar Alamanda87f6ee02013-05-07 14:20:11 -0700155#ifdef MSM8974
156 [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = 15,
157#else
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800158 [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = 14,
Ravi Kumar Alamanda87f6ee02013-05-07 14:20:11 -0700159#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800160 [SND_DEVICE_OUT_BT_SCO] = 22,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800161 [SND_DEVICE_OUT_VOICE_HANDSET_TMUS] = 81,
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800162 [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = 17,
163 [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = 17,
164 [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = 37,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800165
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800166 [SND_DEVICE_IN_HANDSET_MIC] = 4,
Ravi Kumar Alamanda87f6ee02013-05-07 14:20:11 -0700167 [SND_DEVICE_IN_SPEAKER_MIC] = 4, /* ToDo: Check if this needs to changed to 11 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800168 [SND_DEVICE_IN_HEADSET_MIC] = 8,
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -0700169 [SND_DEVICE_IN_HANDSET_MIC_AEC] = 40,
170 [SND_DEVICE_IN_SPEAKER_MIC_AEC] = 42,
171 [SND_DEVICE_IN_HEADSET_MIC_AEC] = 47,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800172 [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = 11,
173 [SND_DEVICE_IN_VOICE_HEADSET_MIC] = 8,
174 [SND_DEVICE_IN_HDMI_MIC] = 4,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800175 [SND_DEVICE_IN_BT_SCO_MIC] = 21,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800176 [SND_DEVICE_IN_CAMCORDER_MIC] = 61,
sangwooea6ef9d2013-06-03 10:37:22 +0900177#ifdef MSM8974
178 [SND_DEVICE_IN_VOICE_DMIC_EF] = 41,
179#else
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800180 [SND_DEVICE_IN_VOICE_DMIC_EF] = 6,
sangwooea6ef9d2013-06-03 10:37:22 +0900181#endif
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800182 [SND_DEVICE_IN_VOICE_DMIC_BS] = 5,
183 [SND_DEVICE_IN_VOICE_DMIC_EF_TMUS] = 91,
sangwooea6ef9d2013-06-03 10:37:22 +0900184#ifdef MSM8974
185 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF] = 43,
186#else
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800187 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF] = 13,
sangwooea6ef9d2013-06-03 10:37:22 +0900188#endif
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800189 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS] = 12,
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800190 [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = 16,
191 [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = 36,
192 [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = 16,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800193 [SND_DEVICE_IN_VOICE_REC_MIC] = 62,
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -0700194 /* TODO: Update with proper acdb ids */
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800195 [SND_DEVICE_IN_VOICE_REC_DMIC_EF] = 62,
196 [SND_DEVICE_IN_VOICE_REC_DMIC_BS] = 62,
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -0700197 [SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE] = 6,
198 [SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE] = 5,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800199};
200
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800201int edid_get_max_channels(void);
202
Ravi Kumar Alamanda37718842013-02-22 18:44:45 -0800203static pthread_once_t check_op_once_ctl = PTHREAD_ONCE_INIT;
204static bool is_tmus = false;
205
206static void check_operator()
207{
208 char value[PROPERTY_VALUE_MAX];
209 int mccmnc;
210 property_get("gsm.sim.operator.numeric",value,"0");
211 mccmnc = atoi(value);
212 ALOGD("%s: tmus mccmnc %d", __func__, mccmnc);
213 switch(mccmnc) {
214 /* TMUS MCC(310), MNC(490, 260, 026) */
215 case 310490:
216 case 310260:
217 case 310026:
218 is_tmus = true;
219 break;
220 }
221}
222
223static bool is_operator_tmus()
224{
225 pthread_once(&check_op_once_ctl, check_operator);
226 return is_tmus;
227}
228
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800229static int get_pcm_device_id(struct audio_route *ar,
230 audio_usecase_t usecase,
231 int device_type)
232{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800233 int device_id;
234 if (device_type == PCM_PLAYBACK)
235 device_id = pcm_device_table[usecase][0];
236 else
237 device_id = pcm_device_table[usecase][1];
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800238 return device_id;
239}
240
241static int get_acdb_device_id(snd_device_t snd_device)
242{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700243 return acdb_device_table[snd_device];
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800244}
245
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800246static void add_backend_name(char *mixer_path,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700247 snd_device_t snd_device)
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800248{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700249 if (snd_device == SND_DEVICE_IN_BT_SCO_MIC)
250 strcat(mixer_path, " bt-sco");
251 else if(snd_device == SND_DEVICE_OUT_BT_SCO)
252 strcat(mixer_path, " bt-sco");
253 else if (snd_device == SND_DEVICE_OUT_HDMI)
254 strcat(mixer_path, " hdmi");
255 else if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_HDMI)
256 strcat(mixer_path, " speaker-and-hdmi");
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800257}
258
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700259static int enable_audio_route(struct audio_device *adev,
260 struct audio_usecase *usecase,
261 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800262{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700263 snd_device_t snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800264 char mixer_path[50];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800265
266 if (usecase == NULL)
267 return -EINVAL;
268
269 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
270
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800271 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700272 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800273 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700274 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800275
276 strcpy(mixer_path, use_case_table[usecase->id]);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700277 add_backend_name(mixer_path, snd_device);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800278 ALOGD("%s: apply mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700279 audio_route_apply_path(adev->audio_route, mixer_path);
280 if (update_mixer)
281 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800282
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800283 ALOGV("%s: exit", __func__);
284 return 0;
285}
286
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700287static int disable_audio_route(struct audio_device *adev,
288 struct audio_usecase *usecase,
289 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800290{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700291 snd_device_t snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800292 char mixer_path[50];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800293
294 if (usecase == NULL)
295 return -EINVAL;
296
297 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700298 if (usecase->type == PCM_CAPTURE)
299 snd_device = usecase->in_snd_device;
300 else
301 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800302 strcpy(mixer_path, use_case_table[usecase->id]);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700303 add_backend_name(mixer_path, snd_device);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800304 ALOGD("%s: reset mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700305 audio_route_reset_path(adev->audio_route, mixer_path);
306 if (update_mixer)
307 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800308
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800309 ALOGV("%s: exit", __func__);
310 return 0;
311}
312
313static int enable_snd_device(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700314 snd_device_t snd_device,
315 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800316{
317 int acdb_dev_id, acdb_dev_type;
318
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800319 if (snd_device < SND_DEVICE_MIN ||
320 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800321 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800322 return -EINVAL;
323 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700324
325 adev->snd_dev_ref_cnt[snd_device]++;
326 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
327 ALOGD("%s: snd_device(%d: %s) is already active",
328 __func__, snd_device, device_table[snd_device]);
329 return 0;
330 }
331
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800332 acdb_dev_id = get_acdb_device_id(snd_device);
333 if (acdb_dev_id < 0) {
334 ALOGE("%s: Could not find acdb id for device(%d)",
335 __func__, snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700336 adev->snd_dev_ref_cnt[snd_device]--;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800337 return -EINVAL;
338 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800339 if (adev->acdb_send_audio_cal) {
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -0700340 ALOGD("%s: sending audio calibration for snd_device(%d) acdb_id(%d)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800341 __func__, snd_device, acdb_dev_id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700342 if (snd_device >= SND_DEVICE_OUT_BEGIN &&
343 snd_device < SND_DEVICE_OUT_END)
344 acdb_dev_type = ACDB_DEV_TYPE_OUT;
345 else
346 acdb_dev_type = ACDB_DEV_TYPE_IN;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800347 adev->acdb_send_audio_cal(acdb_dev_id, acdb_dev_type);
348 } else {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700349 ALOGW("%s: Could not find the symbol acdb_send_audio_cal from %s",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800350 __func__, LIB_ACDB_LOADER);
351 }
352
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700353 ALOGD("%s: snd_device(%d: %s)", __func__,
354 snd_device, device_table[snd_device]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800355 audio_route_apply_path(adev->audio_route, device_table[snd_device]);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700356 if (update_mixer)
357 audio_route_update_mixer(adev->audio_route);
358
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800359 return 0;
360}
361
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700362static int disable_snd_device(struct audio_device *adev,
363 snd_device_t snd_device,
364 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800365{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800366 if (snd_device < SND_DEVICE_MIN ||
367 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800368 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800369 return -EINVAL;
370 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700371 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
372 ALOGE("%s: device ref cnt is already 0", __func__);
373 return -EINVAL;
374 }
375 adev->snd_dev_ref_cnt[snd_device]--;
376 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
377 ALOGD("%s: snd_device(%d: %s)", __func__,
378 snd_device, device_table[snd_device]);
379 audio_route_reset_path(adev->audio_route, device_table[snd_device]);
380 if (update_mixer)
381 audio_route_update_mixer(adev->audio_route);
382 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800383 return 0;
384}
385
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700386static void check_usecases_codec_backend(struct audio_device *adev,
387 struct audio_usecase *uc_info,
388 snd_device_t snd_device)
389{
390 struct listnode *node;
391 struct audio_usecase *usecase;
392 bool switch_device[AUDIO_USECASE_MAX];
393 int i, num_uc_to_switch = 0;
394
395 /*
396 * This function is to make sure that all the usecases that are active on
397 * the hardware codec backend are always routed to any one device that is
398 * handled by the hardware codec.
399 * For example, if low-latency and deep-buffer usecases are currently active
400 * on speaker and out_set_parameters(headset) is received on low-latency
401 * output, then we have to make sure deep-buffer is also switched to headset,
402 * because of the limitation that both the devices cannot be enabled
403 * at the same time as they share the same backend.
404 */
405 /* Disable all the usecases on the shared backend other than the
406 specified usecase */
407 for (i = 0; i < AUDIO_USECASE_MAX; i++)
408 switch_device[i] = false;
409
410 list_for_each(node, &adev->usecase_list) {
411 usecase = node_to_item(node, struct audio_usecase, list);
412 if (usecase->type != PCM_CAPTURE &&
413 usecase != uc_info &&
414 usecase->out_snd_device != snd_device &&
415 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
416 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
417 __func__, use_case_table[usecase->id],
418 device_table[usecase->out_snd_device]);
419 disable_audio_route(adev, usecase, false);
420 switch_device[usecase->id] = true;
421 num_uc_to_switch++;
422 }
423 }
424
425 if (num_uc_to_switch) {
426 /* Make sure all the streams are de-routed before disabling the device */
427 audio_route_update_mixer(adev->audio_route);
428
429 list_for_each(node, &adev->usecase_list) {
430 usecase = node_to_item(node, struct audio_usecase, list);
431 if (switch_device[usecase->id]) {
432 disable_snd_device(adev, usecase->out_snd_device, false);
433 enable_snd_device(adev, snd_device, false);
434 }
435 }
436
437 /* Make sure new snd device is enabled before re-routing the streams */
438 audio_route_update_mixer(adev->audio_route);
439
440 /* Re-route all the usecases on the shared backend other than the
441 specified usecase to new snd devices */
442 list_for_each(node, &adev->usecase_list) {
443 usecase = node_to_item(node, struct audio_usecase, list);
444 /* Update the out_snd_device only before enabling the audio route */
445 if (switch_device[usecase->id] ) {
446 usecase->out_snd_device = snd_device;
447 enable_audio_route(adev, usecase, false);
448 }
449 }
450
451 audio_route_update_mixer(adev->audio_route);
452 }
453}
454
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700455static void check_and_route_capture_usecases(struct audio_device *adev,
456 struct audio_usecase *uc_info,
457 snd_device_t snd_device)
458{
459 struct listnode *node;
460 struct audio_usecase *usecase;
461 bool switch_device[AUDIO_USECASE_MAX];
462 int i, num_uc_to_switch = 0;
463
464 /*
465 * This function is to make sure that all the active capture usecases
466 * are always routed to the same input sound device.
467 * For example, if audio-record and voice-call usecases are currently
468 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
469 * is received for voice call then we have to make sure that audio-record
470 * usecase is also switched to earpiece i.e. voice-dmic-ef,
471 * because of the limitation that two devices cannot be enabled
472 * at the same time if they share the same backend.
473 */
474 for (i = 0; i < AUDIO_USECASE_MAX; i++)
475 switch_device[i] = false;
476
477 list_for_each(node, &adev->usecase_list) {
478 usecase = node_to_item(node, struct audio_usecase, list);
479 if (usecase->type != PCM_PLAYBACK &&
480 usecase != uc_info &&
481 usecase->in_snd_device != snd_device) {
482 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
483 __func__, use_case_table[usecase->id],
484 device_table[usecase->in_snd_device]);
485 disable_audio_route(adev, usecase, false);
486 switch_device[usecase->id] = true;
487 num_uc_to_switch++;
488 }
489 }
490
491 if (num_uc_to_switch) {
492 /* Make sure all the streams are de-routed before disabling the device */
493 audio_route_update_mixer(adev->audio_route);
494
495 list_for_each(node, &adev->usecase_list) {
496 usecase = node_to_item(node, struct audio_usecase, list);
497 if (switch_device[usecase->id]) {
498 disable_snd_device(adev, usecase->in_snd_device, false);
499 enable_snd_device(adev, snd_device, false);
500 }
501 }
502
503 /* Make sure new snd device is enabled before re-routing the streams */
504 audio_route_update_mixer(adev->audio_route);
505
506 /* Re-route all the usecases on the shared backend other than the
507 specified usecase to new snd devices */
508 list_for_each(node, &adev->usecase_list) {
509 usecase = node_to_item(node, struct audio_usecase, list);
510 /* Update the in_snd_device only before enabling the audio route */
511 if (switch_device[usecase->id] ) {
512 usecase->in_snd_device = snd_device;
513 enable_audio_route(adev, usecase, false);
514 }
515 }
516
517 audio_route_update_mixer(adev->audio_route);
518 }
519}
520
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -0700521static int set_echo_reference(struct mixer *mixer, const char* ec_ref)
522{
523 struct mixer_ctl *ctl;
524 const char *mixer_ctl_name = "EC_REF_RX";
525
526 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
527 if (!ctl) {
528 ALOGE("%s: Could not get ctl for mixer cmd - %s",
529 __func__, mixer_ctl_name);
530 return -EINVAL;
531 }
532 ALOGV("Setting EC Reference: %s", ec_ref);
533 mixer_ctl_set_enum_by_string(ctl, ec_ref);
534 return 0;
535}
536
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800537static int set_hdmi_channels(struct mixer *mixer,
538 int channel_count)
539{
540 struct mixer_ctl *ctl;
541 const char *channel_cnt_str = NULL;
542 const char *mixer_ctl_name = "HDMI_RX Channels";
543 switch (channel_count) {
544 case 8:
545 channel_cnt_str = "Eight"; break;
546 case 7:
547 channel_cnt_str = "Seven"; break;
548 case 6:
549 channel_cnt_str = "Six"; break;
550 case 5:
551 channel_cnt_str = "Five"; break;
552 case 4:
553 channel_cnt_str = "Four"; break;
554 case 3:
555 channel_cnt_str = "Three"; break;
556 default:
557 channel_cnt_str = "Two"; break;
558 }
559 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
560 if (!ctl) {
561 ALOGE("%s: Could not get ctl for mixer cmd - %s",
562 __func__, mixer_ctl_name);
563 return -EINVAL;
564 }
565 ALOGV("HDMI channel count: %s", channel_cnt_str);
566 mixer_ctl_set_enum_by_string(ctl, channel_cnt_str);
567 return 0;
568}
569
570/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700571static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800572{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700573 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800574 int channels = edid_get_max_channels();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800575
576 switch (channels) {
577 /*
578 * Do not handle stereo output in Multi-channel cases
579 * Stereo case is handled in normal playback path
580 */
581 case 6:
582 ALOGV("%s: HDMI supports 5.1", __func__);
583 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
584 break;
585 case 8:
586 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
587 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
588 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
589 break;
590 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700591 ALOGE("HDMI does not support multi channel playback");
592 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800593 break;
594 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700595 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800596}
597
Ravi Kumar Alamanda87f6ee02013-05-07 14:20:11 -0700598static int set_voice_volume(struct mixer *mixer,
599 int volume)
600{
601 struct mixer_ctl *ctl;
602 const char *mixer_ctl_name = "Voice Rx Volume";
sangwoofbf5d512013-05-20 11:38:45 +0900603
604 // Voice volume levels are mapped to adsp volume levels as follows.
605 // 100 -> 5, 80 -> 4, 60 -> 3, 40 -> 2, 20 -> 1 0 -> 0
606 // But this values don't changed in kernel. So, below change is need.
607 volume = volume / 20;
608
Ravi Kumar Alamanda87f6ee02013-05-07 14:20:11 -0700609 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
610 if (!ctl) {
611 ALOGE("%s: Could not get ctl for mixer cmd - %s",
612 __func__, mixer_ctl_name);
613 return -EINVAL;
614 }
615 ALOGV("Setting voice volume: %d", volume);
616 mixer_ctl_set_value(ctl, 0, volume);
617 return 0;
618}
619
620static int set_mic_mute(struct mixer *mixer,
621 int mute)
622{
623 struct mixer_ctl *ctl;
624 const char *mixer_ctl_name = "Voice Tx Mute";
625 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
626 if (!ctl) {
627 ALOGE("%s: Could not get ctl for mixer cmd - %s",
628 __func__, mixer_ctl_name);
629 return -EINVAL;
630 }
631 ALOGV("Setting mic mute: %d", mute);
632 mixer_ctl_set_value(ctl, 0, mute);
633 return 0;
634}
635
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700636static snd_device_t get_output_snd_device(struct audio_device *adev,
637 audio_devices_t devices)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800638{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700639 audio_mode_t mode = adev->mode;
640 snd_device_t snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800641
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800642 ALOGV("%s: enter: output devices(%#x)", __func__, devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800643 if (devices == AUDIO_DEVICE_NONE ||
644 devices & AUDIO_DEVICE_BIT_IN) {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800645 ALOGV("%s: Invalid output devices (%#x)", __func__, devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800646 goto exit;
647 }
648
649 if (mode == AUDIO_MODE_IN_CALL) {
650 if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
651 devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800652 if (adev->tty_mode == TTY_MODE_FULL)
653 snd_device = SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES;
654 else if (adev->tty_mode == TTY_MODE_VCO)
655 snd_device = SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES;
656 else if (adev->tty_mode == TTY_MODE_HCO)
657 snd_device = SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET;
658 else
659 snd_device = SND_DEVICE_OUT_VOICE_HEADPHONES;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800660 } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
661 snd_device = SND_DEVICE_OUT_BT_SCO;
662 } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
663 snd_device = SND_DEVICE_OUT_VOICE_SPEAKER;
664 } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
Ravi Kumar Alamanda37718842013-02-22 18:44:45 -0800665 if (is_operator_tmus())
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800666 snd_device = SND_DEVICE_OUT_VOICE_HANDSET_TMUS;
667 else
668 snd_device = SND_DEVICE_OUT_HANDSET;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800669 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800670 if (snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800671 goto exit;
672 }
673 }
674
675 if (popcount(devices) == 2) {
676 if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
677 AUDIO_DEVICE_OUT_SPEAKER)) {
678 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
679 } else if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADSET |
680 AUDIO_DEVICE_OUT_SPEAKER)) {
681 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
682 } else if (devices == (AUDIO_DEVICE_OUT_AUX_DIGITAL |
683 AUDIO_DEVICE_OUT_SPEAKER)) {
684 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HDMI;
685 } else {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800686 ALOGE("%s: Invalid combo device(%#x)", __func__, devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800687 goto exit;
688 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800689 if (snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800690 goto exit;
691 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800692 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800693
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800694 if (popcount(devices) != 1) {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800695 ALOGE("%s: Invalid output devices(%#x)", __func__, devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800696 goto exit;
697 }
698
699 if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
700 devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
701 snd_device = SND_DEVICE_OUT_HEADPHONES;
702 } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
Jean-Michel Trivic56336b2013-05-24 16:55:17 -0700703 if (adev->speaker_lr_swap) {
704 snd_device = SND_DEVICE_OUT_SPEAKER_REVERSE;
705 } else {
706 snd_device = SND_DEVICE_OUT_SPEAKER;
707 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800708 } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
709 snd_device = SND_DEVICE_OUT_BT_SCO;
710 } else if (devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
711 snd_device = SND_DEVICE_OUT_HDMI ;
712 } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
713 snd_device = SND_DEVICE_OUT_HANDSET;
714 } else {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800715 ALOGE("%s: Unknown device(s) %#x", __func__, devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800716 }
717exit:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800718 ALOGV("%s: exit: snd_device(%s)", __func__, device_table[snd_device]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800719 return snd_device;
720}
721
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700722static snd_device_t get_input_snd_device(struct audio_device *adev,
723 audio_devices_t out_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800724{
Eric Laurentc8400632013-02-14 19:04:54 -0800725 audio_source_t source = (adev->active_input == NULL) ?
726 AUDIO_SOURCE_DEFAULT : adev->active_input->source;
727
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800728 audio_mode_t mode = adev->mode;
Eric Laurentc8400632013-02-14 19:04:54 -0800729 audio_devices_t in_device = ((adev->active_input == NULL) ?
730 AUDIO_DEVICE_NONE : adev->active_input->device)
731 & ~AUDIO_DEVICE_BIT_IN;
732 audio_channel_mask_t channel_mask = (adev->active_input == NULL) ?
733 AUDIO_CHANNEL_IN_MONO : adev->active_input->channel_mask;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800734 snd_device_t snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800735
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800736 ALOGV("%s: enter: out_device(%#x) in_device(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800737 __func__, out_device, in_device);
738 if (mode == AUDIO_MODE_IN_CALL) {
739 if (out_device == AUDIO_DEVICE_NONE) {
740 ALOGE("%s: No output device set for voice call", __func__);
741 goto exit;
742 }
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800743 if (adev->tty_mode != TTY_MODE_OFF) {
744 if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
745 out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
746 switch (adev->tty_mode) {
747 case TTY_MODE_FULL:
748 snd_device = SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC;
749 break;
750 case TTY_MODE_VCO:
751 snd_device = SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC;
752 break;
753 case TTY_MODE_HCO:
754 snd_device = SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC;
755 break;
756 default:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800757 ALOGE("%s: Invalid TTY mode (%#x)", __func__, adev->tty_mode);
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800758 }
759 goto exit;
760 }
761 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800762 if (out_device & AUDIO_DEVICE_OUT_EARPIECE ||
763 out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800764 if (adev->mic_type_analog || adev->fluence_in_voice_call == false) {
765 snd_device = SND_DEVICE_IN_HANDSET_MIC;
766 } else {
767 if (adev->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
Ravi Kumar Alamanda37718842013-02-22 18:44:45 -0800768 if (is_operator_tmus())
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800769 snd_device = SND_DEVICE_IN_VOICE_DMIC_EF_TMUS;
770 else
771 snd_device = SND_DEVICE_IN_VOICE_DMIC_EF;
772 } else if(adev->dualmic_config == DUALMIC_CONFIG_BROADSIDE)
773 snd_device = SND_DEVICE_IN_VOICE_DMIC_BS;
774 else
775 snd_device = SND_DEVICE_IN_HANDSET_MIC;
776 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800777 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
778 snd_device = SND_DEVICE_IN_VOICE_HEADSET_MIC;
779 } else if (out_device & AUDIO_DEVICE_OUT_ALL_SCO) {
780 snd_device = SND_DEVICE_IN_BT_SCO_MIC ;
781 } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -0800782 if (adev->fluence_in_voice_call && adev->fluence_in_spkr_mode &&
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800783 adev->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
784 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF;
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -0800785 } else if (adev->fluence_in_voice_call && adev->fluence_in_spkr_mode &&
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800786 adev->dualmic_config == DUALMIC_CONFIG_BROADSIDE) {
787 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS;
788 } else {
789 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC;
790 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800791 }
792 } else if (source == AUDIO_SOURCE_CAMCORDER) {
793 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC ||
794 in_device & AUDIO_DEVICE_IN_BACK_MIC) {
795 snd_device = SND_DEVICE_IN_CAMCORDER_MIC;
796 }
797 } else if (source == AUDIO_SOURCE_VOICE_RECOGNITION) {
798 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
Eric Laurentc8400632013-02-14 19:04:54 -0800799 if (adev->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
800 if (channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK)
801 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_EF;
802 else if (adev->fluence_in_voice_rec)
803 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE;
Eric Laurentc8400632013-02-14 19:04:54 -0800804 } else if (adev->dualmic_config == DUALMIC_CONFIG_BROADSIDE) {
805 if (channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK)
806 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_BS;
807 else if (adev->fluence_in_voice_rec)
808 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE;
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -0700809 }
810
811 if (snd_device == SND_DEVICE_NONE) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800812 snd_device = SND_DEVICE_IN_VOICE_REC_MIC;
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -0700813 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800814 }
Ravi Kumar Alamanda8455fa72013-02-19 14:53:19 -0800815 } else if (source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
816 if (out_device & AUDIO_DEVICE_OUT_SPEAKER)
817 in_device = AUDIO_DEVICE_IN_BACK_MIC;
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -0700818 if (adev->active_input) {
819 if (adev->active_input->enable_aec) {
820 if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
821 if (adev->mic_type_analog)
822 snd_device = SND_DEVICE_IN_HANDSET_MIC_AEC;
823 else
824 snd_device = SND_DEVICE_IN_SPEAKER_MIC_AEC;
825 } else if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
826 snd_device = SND_DEVICE_IN_HANDSET_MIC_AEC;
827 } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
828 snd_device = SND_DEVICE_IN_HEADSET_MIC_AEC;
829 }
830 set_echo_reference(adev->mixer, "SLIM_RX");
831 } else
832 set_echo_reference(adev->mixer, "NONE");
833 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800834 } else if (source == AUDIO_SOURCE_DEFAULT) {
835 goto exit;
836 }
837
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -0700838
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800839 if (snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800840 goto exit;
841 }
842
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800843 if (in_device != AUDIO_DEVICE_NONE &&
Ravi Kumar Alamanda8455fa72013-02-19 14:53:19 -0800844 !(in_device & AUDIO_DEVICE_IN_VOICE_CALL) &&
845 !(in_device & AUDIO_DEVICE_IN_COMMUNICATION)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800846 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800847 snd_device = SND_DEVICE_IN_HANDSET_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800848 } else if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800849 if (adev->mic_type_analog)
850 snd_device = SND_DEVICE_IN_HANDSET_MIC;
851 else
852 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800853 } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
854 snd_device = SND_DEVICE_IN_HEADSET_MIC;
855 } else if (in_device & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
856 snd_device = SND_DEVICE_IN_BT_SCO_MIC ;
857 } else if (in_device & AUDIO_DEVICE_IN_AUX_DIGITAL) {
858 snd_device = SND_DEVICE_IN_HDMI_MIC;
859 } else {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800860 ALOGE("%s: Unknown input device(s) %#x", __func__, in_device);
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800861 ALOGW("%s: Using default handset-mic", __func__);
862 snd_device = SND_DEVICE_IN_HANDSET_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800863 }
864 } else {
865 if (out_device & AUDIO_DEVICE_OUT_EARPIECE) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800866 snd_device = SND_DEVICE_IN_HANDSET_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800867 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
868 snd_device = SND_DEVICE_IN_HEADSET_MIC;
869 } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
870 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
871 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800872 snd_device = SND_DEVICE_IN_HANDSET_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800873 } else if (out_device & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800874 snd_device = SND_DEVICE_IN_BT_SCO_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800875 } else if (out_device & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
876 snd_device = SND_DEVICE_IN_HDMI_MIC;
877 } else {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800878 ALOGE("%s: Unknown output device(s) %#x", __func__, out_device);
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800879 ALOGW("%s: Using default handset-mic", __func__);
880 snd_device = SND_DEVICE_IN_HANDSET_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800881 }
882 }
883exit:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800884 ALOGV("%s: exit: in_snd_device(%s)", __func__, device_table[snd_device]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800885 return snd_device;
886}
887
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700888static struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
889 audio_usecase_t uc_id)
890{
891 struct audio_usecase *usecase;
892 struct listnode *node;
893
894 list_for_each(node, &adev->usecase_list) {
895 usecase = node_to_item(node, struct audio_usecase, list);
896 if (usecase->id == uc_id)
897 return usecase;
898 }
899 return NULL;
900}
901
902static int select_devices(struct audio_device *adev,
903 audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800904{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800905 snd_device_t out_snd_device = SND_DEVICE_NONE;
906 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700907 struct audio_usecase *usecase = NULL;
908 struct audio_usecase *vc_usecase = NULL;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800909 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800910 int acdb_rx_id, acdb_tx_id;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700911 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800912
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700913 usecase = get_usecase_from_list(adev, uc_id);
914 if (usecase == NULL) {
915 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
916 return -EINVAL;
917 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800918
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700919 if (usecase->type == VOICE_CALL) {
920 out_snd_device = get_output_snd_device(adev, usecase->stream.out->devices);
921 in_snd_device = get_input_snd_device(adev, usecase->stream.out->devices);
922 usecase->devices = usecase->stream.out->devices;
923 } else {
924 /*
925 * If the voice call is active, use the sound devices of voice call usecase
926 * so that it would not result any device switch. All the usecases will
927 * be switched to new device when select_devices() is called for voice call
928 * usecase. This is to avoid switching devices for voice call when
929 * check_usecases_codec_backend() is called below.
930 */
931 if (adev->in_call) {
932 vc_usecase = get_usecase_from_list(adev, USECASE_VOICE_CALL);
933 if (vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
934 in_snd_device = vc_usecase->in_snd_device;
935 out_snd_device = vc_usecase->out_snd_device;
936 }
937 }
938 if (usecase->type == PCM_PLAYBACK) {
939 usecase->devices = usecase->stream.out->devices;
940 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700941 if (out_snd_device == SND_DEVICE_NONE) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700942 out_snd_device = get_output_snd_device(adev,
943 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700944 if (usecase->stream.out == adev->primary_output &&
945 adev->active_input &&
946 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
947 select_devices(adev, adev->active_input->usecase);
948 }
949 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700950 } else if (usecase->type == PCM_CAPTURE) {
951 usecase->devices = usecase->stream.in->device;
952 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700953 if (in_snd_device == SND_DEVICE_NONE) {
954 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
955 adev->primary_output && !adev->primary_output->standby) {
956 in_snd_device = get_input_snd_device(adev,
957 adev->primary_output->devices);
958 } else {
959 in_snd_device = get_input_snd_device(adev, AUDIO_DEVICE_NONE);
960 }
961 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700962 }
963 }
964
965 if (out_snd_device == usecase->out_snd_device &&
966 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800967 return 0;
968 }
969
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800970 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
971 out_snd_device, device_table[out_snd_device],
972 in_snd_device, device_table[in_snd_device]);
973
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800974 /*
975 * Limitation: While in call, to do a device switch we need to disable
976 * and enable both RX and TX devices though one of them is same as current
977 * device.
978 */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700979 if (usecase->type == VOICE_CALL && adev->csd_client != NULL &&
980 usecase->in_snd_device != SND_DEVICE_NONE &&
981 usecase->out_snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800982 /* This must be called before disabling the mixer controls on APQ side */
983 if (adev->csd_disable_device == NULL) {
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -0800984 ALOGE("%s: dlsym error for csd_client_disable_device", __func__);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800985 } else {
986 status = adev->csd_disable_device();
987 if (status < 0) {
988 ALOGE("%s: csd_client_disable_device, failed, error %d",
989 __func__, status);
990 }
991 }
992 }
993
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700994 /* Disable current sound devices */
995 if (usecase->out_snd_device != SND_DEVICE_NONE) {
996 disable_audio_route(adev, usecase, true);
997 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800998 }
999
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001000 if (usecase->in_snd_device != SND_DEVICE_NONE) {
1001 disable_audio_route(adev, usecase, true);
1002 disable_snd_device(adev, usecase->in_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001003 }
1004
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001005 /* Enable new sound devices */
1006 if (out_snd_device != SND_DEVICE_NONE) {
1007 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
1008 check_usecases_codec_backend(adev, usecase, out_snd_device);
1009 enable_snd_device(adev, out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001010 }
1011
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001012 if (in_snd_device != SND_DEVICE_NONE) {
1013 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001014 enable_snd_device(adev, in_snd_device, false);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -07001015 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001016
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001017 if (usecase->type == VOICE_CALL && adev->csd_client) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08001018 if (adev->csd_enable_device == NULL) {
1019 ALOGE("%s: dlsym error for csd_client_enable_device",
1020 __func__);
1021 } else {
1022 acdb_rx_id = get_acdb_device_id(out_snd_device);
1023 acdb_tx_id = get_acdb_device_id(in_snd_device);
1024
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001025 if (acdb_rx_id > 0 || acdb_tx_id > 0) {
1026 status = adev->csd_enable_device(acdb_rx_id, acdb_tx_id,
1027 adev->acdb_settings);
1028 if (status < 0) {
1029 ALOGE("%s: csd_client_enable_device, failed, error %d",
1030 __func__, status);
1031 }
1032 } else {
1033 ALOGE("%s: Incorrect ACDB IDs (rx: %d tx: %d)", __func__,
1034 acdb_rx_id, acdb_tx_id);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08001035 }
1036 }
sangwoofbf5d512013-05-20 11:38:45 +09001037 } else if (usecase->type == VOICE_CALL) {
1038 if (adev->acdb_send_voice_cal == NULL) {
1039 ALOGE("%s: dlsym error for acdb_send_voice_call", __func__);
1040 } else {
1041 acdb_rx_id = get_acdb_device_id(out_snd_device);
1042 acdb_tx_id = get_acdb_device_id(in_snd_device);
1043
1044 if (acdb_rx_id > 0 && acdb_tx_id > 0)
1045 adev->acdb_send_voice_cal(acdb_rx_id, acdb_tx_id);
1046 else
1047 ALOGE("%s: Incorrect ACDB IDs (rx: %d tx: %d)", __func__,
1048 acdb_rx_id, acdb_tx_id);
1049 }
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08001050 }
1051
sangwoo170731f2013-06-08 15:36:36 +09001052 audio_route_update_mixer(adev->audio_route);
1053
1054 usecase->in_snd_device = in_snd_device;
1055 usecase->out_snd_device = out_snd_device;
1056
1057 enable_audio_route(adev, usecase, true);
1058
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001059 return status;
1060}
1061
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001062static int stop_input_stream(struct stream_in *in)
1063{
1064 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001065 struct audio_usecase *uc_info;
1066 struct audio_device *adev = in->dev;
1067
Eric Laurentc8400632013-02-14 19:04:54 -08001068 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001069
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001070 ALOGD("%s: enter: usecase(%d: %s)", __func__,
1071 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001072 uc_info = get_usecase_from_list(adev, in->usecase);
1073 if (uc_info == NULL) {
1074 ALOGE("%s: Could not find the usecase (%d) in the list",
1075 __func__, in->usecase);
1076 return -EINVAL;
1077 }
1078
Eric Laurent150dbfe2013-02-27 14:31:02 -08001079 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001080 disable_audio_route(adev, uc_info, true);
1081
1082 /* 2. Disable the tx device */
1083 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001084
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001085 list_remove(&uc_info->list);
1086 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001087
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001088 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001089 return ret;
1090}
1091
1092int start_input_stream(struct stream_in *in)
1093{
1094 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -08001095 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001096 struct audio_usecase *uc_info;
1097 struct audio_device *adev = in->dev;
1098
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001099 ALOGD("%s: enter: usecase(%d)", __func__, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001100 in->pcm_device_id = get_pcm_device_id(adev->audio_route,
1101 in->usecase,
1102 PCM_CAPTURE);
1103 if (in->pcm_device_id < 0) {
1104 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
1105 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -08001106 ret = -EINVAL;
1107 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001108 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001109
1110 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001111 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1112 uc_info->id = in->usecase;
1113 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001114 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001115 uc_info->devices = in->device;
1116 uc_info->in_snd_device = SND_DEVICE_NONE;
1117 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001118
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001119 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001120 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001121
Eric Laurentc8400632013-02-14 19:04:54 -08001122 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
1123 __func__, SOUND_CARD, in->pcm_device_id, in->config.channels);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001124 in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id,
1125 PCM_IN, &in->config);
1126 if (in->pcm && !pcm_is_ready(in->pcm)) {
1127 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
1128 pcm_close(in->pcm);
1129 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -08001130 ret = -EIO;
1131 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001132 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001133 ALOGD("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -08001134 return ret;
1135
1136error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001137 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -08001138
1139error_config:
1140 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001141 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -08001142
1143 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001144}
1145
1146static int stop_output_stream(struct stream_out *out)
1147{
1148 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001149 struct audio_usecase *uc_info;
1150 struct audio_device *adev = out->dev;
1151
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001152 ALOGD("%s: enter: usecase(%d: %s)", __func__,
1153 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001154 uc_info = get_usecase_from_list(adev, out->usecase);
1155 if (uc_info == NULL) {
1156 ALOGE("%s: Could not find the usecase (%d) in the list",
1157 __func__, out->usecase);
1158 return -EINVAL;
1159 }
1160
Eric Laurent150dbfe2013-02-27 14:31:02 -08001161 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001162 disable_audio_route(adev, uc_info, true);
1163
1164 /* 2. Disable the rx device */
1165 disable_snd_device(adev, uc_info->out_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001166
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001167 list_remove(&uc_info->list);
1168 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001169
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001170 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001171 return ret;
1172}
1173
1174int start_output_stream(struct stream_out *out)
1175{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001176 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001177 struct audio_usecase *uc_info;
1178 struct audio_device *adev = out->dev;
1179
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001180 ALOGD("%s: enter: usecase(%d: %s) devices(%#x)",
1181 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001182 out->pcm_device_id = get_pcm_device_id(adev->audio_route,
1183 out->usecase,
1184 PCM_PLAYBACK);
1185 if (out->pcm_device_id < 0) {
1186 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1187 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001188 ret = -EINVAL;
1189 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001190 }
1191
1192 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1193 uc_info->id = out->usecase;
1194 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001195 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001196 uc_info->devices = out->devices;
1197 uc_info->in_snd_device = SND_DEVICE_NONE;
1198 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001199
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001200 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001201
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001202 select_devices(adev, out->usecase);
1203
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001204 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
1205 __func__, 0, out->pcm_device_id);
1206 out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id,
1207 PCM_OUT, &out->config);
1208 if (out->pcm && !pcm_is_ready(out->pcm)) {
1209 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1210 pcm_close(out->pcm);
1211 out->pcm = NULL;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001212 ret = -EIO;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001213 goto error_pcm_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001214 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001215 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001216 return 0;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001217error_pcm_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001218 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001219error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001220 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001221}
1222
1223static int stop_voice_call(struct audio_device *adev)
1224{
1225 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001226 struct audio_usecase *uc_info;
1227
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001228 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001229 adev->in_call = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001230 if (adev->csd_client) {
1231 if (adev->csd_stop_voice == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08001232 ALOGE("dlsym error for csd_client_disable_device");
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001233 } else {
1234 ret = adev->csd_stop_voice();
1235 if (ret < 0) {
1236 ALOGE("%s: csd_client error %d\n", __func__, ret);
1237 }
1238 }
1239 }
1240
1241 /* 1. Close the PCM devices */
1242 if (adev->voice_call_rx) {
1243 pcm_close(adev->voice_call_rx);
1244 adev->voice_call_rx = NULL;
1245 }
1246 if (adev->voice_call_tx) {
1247 pcm_close(adev->voice_call_tx);
1248 adev->voice_call_tx = NULL;
1249 }
1250
1251 uc_info = get_usecase_from_list(adev, USECASE_VOICE_CALL);
1252 if (uc_info == NULL) {
1253 ALOGE("%s: Could not find the usecase (%d) in the list",
1254 __func__, USECASE_VOICE_CALL);
1255 return -EINVAL;
1256 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001257
1258 /* 2. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001259 disable_audio_route(adev, uc_info, true);
1260
1261 /* 3. Disable the rx and tx devices */
1262 disable_snd_device(adev, uc_info->out_snd_device, false);
1263 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001264
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001265 list_remove(&uc_info->list);
1266 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001267
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001268 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001269 return ret;
1270}
1271
1272static int start_voice_call(struct audio_device *adev)
1273{
1274 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001275 struct audio_usecase *uc_info;
1276 int pcm_dev_rx_id, pcm_dev_tx_id;
1277
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001278 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001279
1280 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1281 uc_info->id = USECASE_VOICE_CALL;
1282 uc_info->type = VOICE_CALL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001283 uc_info->stream.out = adev->primary_output;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001284 uc_info->devices = adev->primary_output->devices;
1285 uc_info->in_snd_device = SND_DEVICE_NONE;
1286 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001287
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001288 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001289
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001290 select_devices(adev, USECASE_VOICE_CALL);
1291
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001292 pcm_dev_rx_id = get_pcm_device_id(adev->audio_route, uc_info->id,
1293 PCM_PLAYBACK);
1294 pcm_dev_tx_id = get_pcm_device_id(adev->audio_route, uc_info->id,
1295 PCM_CAPTURE);
1296
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001297 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
1298 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
1299 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001300 ret = -EIO;
1301 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001302 }
1303
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001304 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001305 __func__, SOUND_CARD, pcm_dev_rx_id);
1306 adev->voice_call_rx = pcm_open(SOUND_CARD,
1307 pcm_dev_rx_id,
1308 PCM_OUT, &pcm_config_voice_call);
1309 if (adev->voice_call_rx && !pcm_is_ready(adev->voice_call_rx)) {
1310 ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_rx));
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001311 ret = -EIO;
1312 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001313 }
1314
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001315 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001316 __func__, SOUND_CARD, pcm_dev_tx_id);
1317 adev->voice_call_tx = pcm_open(SOUND_CARD,
1318 pcm_dev_tx_id,
1319 PCM_IN, &pcm_config_voice_call);
1320 if (adev->voice_call_tx && !pcm_is_ready(adev->voice_call_tx)) {
1321 ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_tx));
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001322 ret = -EIO;
1323 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001324 }
1325 pcm_start(adev->voice_call_rx);
1326 pcm_start(adev->voice_call_tx);
1327
1328 if (adev->csd_client) {
1329 if (adev->csd_start_voice == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08001330 ALOGE("dlsym error for csd_client_start_voice");
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001331 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001332 } else {
1333 ret = adev->csd_start_voice();
1334 if (ret < 0) {
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001335 ALOGE("%s: csd_start_voice error %d\n", __func__, ret);
1336 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001337 }
1338 }
1339 }
1340
1341 adev->in_call = true;
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001342 return 0;
1343
1344error_start_voice:
1345 stop_voice_call(adev);
1346
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001347 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001348 return ret;
1349}
1350
1351static int check_input_parameters(uint32_t sample_rate,
1352 audio_format_t format,
1353 int channel_count)
1354{
1355 if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
1356
1357 if ((channel_count < 1) || (channel_count > 2)) return -EINVAL;
1358
1359 switch (sample_rate) {
1360 case 8000:
1361 case 11025:
1362 case 12000:
1363 case 16000:
1364 case 22050:
1365 case 24000:
1366 case 32000:
1367 case 44100:
1368 case 48000:
1369 break;
1370 default:
1371 return -EINVAL;
1372 }
1373
1374 return 0;
1375}
1376
1377static size_t get_input_buffer_size(uint32_t sample_rate,
1378 audio_format_t format,
1379 int channel_count)
1380{
1381 size_t size = 0;
1382
1383 if (check_input_parameters(sample_rate, format, channel_count) != 0) return 0;
1384
1385 if (sample_rate == 8000 || sample_rate == 16000 || sample_rate == 32000) {
1386 size = (sample_rate * 20) / 1000;
1387 } else if (sample_rate == 11025 || sample_rate == 12000) {
1388 size = 256;
1389 } else if (sample_rate == 22050 || sample_rate == 24000) {
1390 size = 512;
1391 } else if (sample_rate == 44100 || sample_rate == 48000) {
1392 size = 1024;
1393 }
1394
1395 return size * sizeof(short) * channel_count;
1396}
1397
1398static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1399{
1400 struct stream_out *out = (struct stream_out *)stream;
1401
1402 return out->config.rate;
1403}
1404
1405static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1406{
1407 return -ENOSYS;
1408}
1409
1410static size_t out_get_buffer_size(const struct audio_stream *stream)
1411{
1412 struct stream_out *out = (struct stream_out *)stream;
1413
1414 return out->config.period_size * audio_stream_frame_size(stream);
1415}
1416
1417static uint32_t out_get_channels(const struct audio_stream *stream)
1418{
1419 struct stream_out *out = (struct stream_out *)stream;
1420
1421 return out->channel_mask;
1422}
1423
1424static audio_format_t out_get_format(const struct audio_stream *stream)
1425{
1426 return AUDIO_FORMAT_PCM_16_BIT;
1427}
1428
1429static int out_set_format(struct audio_stream *stream, audio_format_t format)
1430{
1431 return -ENOSYS;
1432}
1433
1434static int out_standby(struct audio_stream *stream)
1435{
1436 struct stream_out *out = (struct stream_out *)stream;
1437 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001438 ALOGD("%s: enter: usecase(%d: %s)", __func__,
1439 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001440 pthread_mutex_lock(&out->lock);
1441
1442 if (!out->standby) {
1443 out->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001444 if (out->pcm) {
1445 pcm_close(out->pcm);
1446 out->pcm = NULL;
1447 }
1448 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001449 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001450 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001451 }
1452 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001453 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001454 return 0;
1455}
1456
1457static int out_dump(const struct audio_stream *stream, int fd)
1458{
1459 return 0;
1460}
1461
1462static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1463{
1464 struct stream_out *out = (struct stream_out *)stream;
1465 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001466 struct audio_usecase *usecase;
1467 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001468 struct str_parms *parms;
1469 char value[32];
1470 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001471 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001472
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001473 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
1474 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001475 parms = str_parms_create_str(kvpairs);
1476 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1477 if (ret >= 0) {
1478 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001479 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001480 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001481
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001482 /*
1483 * When HDMI cable is unplugged the music playback is paused and
1484 * the policy manager sends routing=0. But the audioflinger
1485 * continues to write data until standby time (3sec).
1486 * As the HDMI core is turned off, the write gets blocked.
1487 * Avoid this by routing audio to speaker until standby.
1488 */
1489 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1490 val == AUDIO_DEVICE_NONE) {
1491 val = AUDIO_DEVICE_OUT_SPEAKER;
1492 }
1493
1494 /*
1495 * select_devices() call below switches all the usecases on the same
1496 * backend to the new device. Refer to check_usecases_codec_backend() in
1497 * the select_devices(). But how do we undo this?
1498 *
1499 * For example, music playback is active on headset (deep-buffer usecase)
1500 * and if we go to ringtones and select a ringtone, low-latency usecase
1501 * will be started on headset+speaker. As we can't enable headset+speaker
1502 * and headset devices at the same time, select_devices() switches the music
1503 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1504 * So when the ringtone playback is completed, how do we undo the same?
1505 *
1506 * We are relying on the out_set_parameters() call on deep-buffer output,
1507 * once the ringtone playback is ended.
1508 * NOTE: We should not check if the current devices are same as new devices.
1509 * Because select_devices() must be called to switch back the music
1510 * playback to headset.
1511 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001512 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001513 out->devices = val;
1514
1515 if (!out->standby)
1516 select_devices(adev, out->usecase);
1517
1518 if ((adev->mode == AUDIO_MODE_IN_CALL) && !adev->in_call &&
1519 (out == adev->primary_output)) {
1520 start_voice_call(adev);
1521 } else if ((adev->mode == AUDIO_MODE_IN_CALL) && adev->in_call &&
1522 (out == adev->primary_output)) {
1523 select_devices(adev, USECASE_VOICE_CALL);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001524 }
1525 }
1526
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001527 if ((adev->mode != AUDIO_MODE_IN_CALL) && adev->in_call &&
1528 (out == adev->primary_output)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001529 stop_voice_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001530 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001531
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001532 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001533 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001534 }
1535 str_parms_destroy(parms);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001536 ALOGD("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001537 return ret;
1538}
1539
1540static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1541{
1542 struct stream_out *out = (struct stream_out *)stream;
1543 struct str_parms *query = str_parms_create_str(keys);
1544 char *str;
1545 char value[256];
1546 struct str_parms *reply = str_parms_create();
1547 size_t i, j;
1548 int ret;
1549 bool first = true;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001550 ALOGD("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001551 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1552 if (ret >= 0) {
1553 value[0] = '\0';
1554 i = 0;
1555 while (out->supported_channel_masks[i] != 0) {
1556 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1557 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1558 if (!first) {
1559 strcat(value, "|");
1560 }
1561 strcat(value, out_channels_name_to_enum_table[j].name);
1562 first = false;
1563 break;
1564 }
1565 }
1566 i++;
1567 }
1568 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1569 str = str_parms_to_str(reply);
1570 } else {
1571 str = strdup(keys);
1572 }
1573 str_parms_destroy(query);
1574 str_parms_destroy(reply);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001575 ALOGD("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001576 return str;
1577}
1578
1579static uint32_t out_get_latency(const struct audio_stream_out *stream)
1580{
1581 struct stream_out *out = (struct stream_out *)stream;
1582
1583 return (out->config.period_count * out->config.period_size * 1000) / (out->config.rate);
1584}
1585
1586static int out_set_volume(struct audio_stream_out *stream, float left,
1587 float right)
1588{
Eric Laurenta9024de2013-04-04 09:19:12 -07001589 struct stream_out *out = (struct stream_out *)stream;
1590 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1591 /* only take left channel into account: the API is for stereo anyway */
1592 out->muted = (left == 0.0f);
1593 return 0;
1594 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001595 return -ENOSYS;
1596}
1597
1598static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1599 size_t bytes)
1600{
1601 struct stream_out *out = (struct stream_out *)stream;
1602 struct audio_device *adev = out->dev;
1603 int i, ret = -1;
1604
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001605 pthread_mutex_lock(&out->lock);
1606 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001607 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001608 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001609 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001610 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001611 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001612 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001613 goto exit;
1614 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001615 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001616
1617 if (out->pcm) {
Eric Laurenta9024de2013-04-04 09:19:12 -07001618 if (out->muted)
1619 memset((void *)buffer, 0, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001620 //ALOGV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1621 ret = pcm_write(out->pcm, (void *)buffer, bytes);
1622 }
1623
1624exit:
1625 pthread_mutex_unlock(&out->lock);
1626
1627 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001628 if (out->pcm)
1629 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001630 out_standby(&out->stream.common);
1631 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1632 out_get_sample_rate(&out->stream.common));
1633 }
1634 return bytes;
1635}
1636
1637static int out_get_render_position(const struct audio_stream_out *stream,
1638 uint32_t *dsp_frames)
1639{
1640 return -EINVAL;
1641}
1642
1643static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1644{
1645 return 0;
1646}
1647
1648static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1649{
1650 return 0;
1651}
1652
1653static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1654 int64_t *timestamp)
1655{
1656 return -EINVAL;
1657}
1658
1659/** audio_stream_in implementation **/
1660static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1661{
1662 struct stream_in *in = (struct stream_in *)stream;
1663
1664 return in->config.rate;
1665}
1666
1667static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1668{
1669 return -ENOSYS;
1670}
1671
1672static size_t in_get_buffer_size(const struct audio_stream *stream)
1673{
1674 struct stream_in *in = (struct stream_in *)stream;
1675
1676 return in->config.period_size * audio_stream_frame_size(stream);
1677}
1678
1679static uint32_t in_get_channels(const struct audio_stream *stream)
1680{
1681 struct stream_in *in = (struct stream_in *)stream;
1682
1683 return in->channel_mask;
1684}
1685
1686static audio_format_t in_get_format(const struct audio_stream *stream)
1687{
1688 return AUDIO_FORMAT_PCM_16_BIT;
1689}
1690
1691static int in_set_format(struct audio_stream *stream, audio_format_t format)
1692{
1693 return -ENOSYS;
1694}
1695
1696static int in_standby(struct audio_stream *stream)
1697{
1698 struct stream_in *in = (struct stream_in *)stream;
1699 struct audio_device *adev = in->dev;
1700 int status = 0;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001701 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001702 pthread_mutex_lock(&in->lock);
1703 if (!in->standby) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001704 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001705 if (in->pcm) {
1706 pcm_close(in->pcm);
1707 in->pcm = NULL;
1708 }
1709 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001710 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001711 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001712 }
1713 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001714 ALOGD("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001715 return status;
1716}
1717
1718static int in_dump(const struct audio_stream *stream, int fd)
1719{
1720 return 0;
1721}
1722
1723static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1724{
1725 struct stream_in *in = (struct stream_in *)stream;
1726 struct audio_device *adev = in->dev;
1727 struct str_parms *parms;
1728 char *str;
1729 char value[32];
1730 int ret, val = 0;
1731
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001732 ALOGD("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001733 parms = str_parms_create_str(kvpairs);
1734
1735 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1736
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001737 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001738 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001739 if (ret >= 0) {
1740 val = atoi(value);
1741 /* no audio source uses val == 0 */
1742 if ((in->source != val) && (val != 0)) {
1743 in->source = val;
1744 }
1745 }
1746
1747 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1748 if (ret >= 0) {
1749 val = atoi(value);
1750 if ((in->device != val) && (val != 0)) {
1751 in->device = val;
1752 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001753 if (!in->standby)
1754 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001755 }
1756 }
1757
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001758 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001759 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001760
1761 str_parms_destroy(parms);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001762 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001763 return ret;
1764}
1765
1766static char* in_get_parameters(const struct audio_stream *stream,
1767 const char *keys)
1768{
1769 return strdup("");
1770}
1771
1772static int in_set_gain(struct audio_stream_in *stream, float gain)
1773{
1774 return 0;
1775}
1776
1777static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1778 size_t bytes)
1779{
1780 struct stream_in *in = (struct stream_in *)stream;
1781 struct audio_device *adev = in->dev;
1782 int i, ret = -1;
1783
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001784 pthread_mutex_lock(&in->lock);
1785 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001786 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001787 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001788 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001789 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001790 goto exit;
1791 }
1792 in->standby = 0;
1793 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001794
1795 if (in->pcm) {
1796 ret = pcm_read(in->pcm, buffer, bytes);
1797 }
1798
1799 /*
1800 * Instead of writing zeroes here, we could trust the hardware
1801 * to always provide zeroes when muted.
1802 */
1803 if (ret == 0 && adev->mic_mute)
1804 memset(buffer, 0, bytes);
1805
1806exit:
1807 pthread_mutex_unlock(&in->lock);
1808
1809 if (ret != 0) {
1810 in_standby(&in->stream.common);
1811 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1812 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1813 in_get_sample_rate(&in->stream.common));
1814 }
1815 return bytes;
1816}
1817
1818static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1819{
1820 return 0;
1821}
1822
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001823static int add_remove_audio_effect(const struct audio_stream *stream,
1824 effect_handle_t effect,
1825 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001826{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001827 struct stream_in *in = (struct stream_in *)stream;
1828 int status = 0;
1829 effect_descriptor_t desc;
1830
1831 status = (*effect)->get_descriptor(effect, &desc);
1832 if (status != 0)
1833 return status;
1834
1835 pthread_mutex_lock(&in->lock);
1836 pthread_mutex_lock(&in->dev->lock);
1837 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1838 in->enable_aec != enable &&
1839 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1840 in->enable_aec = enable;
1841 if (!in->standby)
1842 select_devices(in->dev, in->usecase);
1843 }
1844 pthread_mutex_unlock(&in->dev->lock);
1845 pthread_mutex_unlock(&in->lock);
1846
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001847 return 0;
1848}
1849
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001850static int in_add_audio_effect(const struct audio_stream *stream,
1851 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001852{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001853 ALOGD("%s: effect %p", __func__, effect);
1854 return add_remove_audio_effect(stream, effect, true);
1855}
1856
1857static int in_remove_audio_effect(const struct audio_stream *stream,
1858 effect_handle_t effect)
1859{
1860 ALOGD("%s: effect %p", __func__, effect);
1861 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001862}
1863
1864static int adev_open_output_stream(struct audio_hw_device *dev,
1865 audio_io_handle_t handle,
1866 audio_devices_t devices,
1867 audio_output_flags_t flags,
1868 struct audio_config *config,
1869 struct audio_stream_out **stream_out)
1870{
1871 struct audio_device *adev = (struct audio_device *)dev;
1872 struct stream_out *out;
1873 int i, ret;
1874
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001875 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001876 __func__, config->sample_rate, config->channel_mask, devices, flags);
1877 *stream_out = NULL;
1878 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1879
1880 if (devices == AUDIO_DEVICE_NONE)
1881 devices = AUDIO_DEVICE_OUT_SPEAKER;
1882
1883 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
1884 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1885 out->flags = flags;
1886 out->devices = devices;
1887
1888 /* Init use case and pcm_config */
1889 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
1890 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001891 pthread_mutex_lock(&adev->lock);
1892 ret = read_hdmi_channel_masks(out);
1893 pthread_mutex_unlock(&adev->lock);
1894 if (ret != 0) {
1895 /* If HDMI does not support multi channel playback, set the default */
1896 out->config.channels = popcount(out->channel_mask);
1897 set_hdmi_channels(adev->mixer, out->config.channels);
1898 goto error_open;
1899 }
1900
1901 if (config->sample_rate == 0)
1902 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1903 if (config->channel_mask == 0)
1904 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1905
1906 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001907 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
1908 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001909 out->config.rate = config->sample_rate;
1910 out->config.channels = popcount(out->channel_mask);
1911 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
1912 set_hdmi_channels(adev->mixer, out->config.channels);
1913 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
1914 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
1915 out->config = pcm_config_deep_buffer;
1916 } else {
1917 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
1918 out->config = pcm_config_low_latency;
1919 }
1920
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001921 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1922 if(adev->primary_output == NULL)
1923 adev->primary_output = out;
1924 else {
1925 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001926 ret = -EEXIST;
1927 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001928 }
1929 }
1930
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001931 /* Check if this usecase is already existing */
1932 pthread_mutex_lock(&adev->lock);
1933 if (get_usecase_from_list(adev, out->usecase) != NULL) {
1934 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001935 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001936 ret = -EEXIST;
1937 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001938 }
1939 pthread_mutex_unlock(&adev->lock);
1940
1941 out->stream.common.get_sample_rate = out_get_sample_rate;
1942 out->stream.common.set_sample_rate = out_set_sample_rate;
1943 out->stream.common.get_buffer_size = out_get_buffer_size;
1944 out->stream.common.get_channels = out_get_channels;
1945 out->stream.common.get_format = out_get_format;
1946 out->stream.common.set_format = out_set_format;
1947 out->stream.common.standby = out_standby;
1948 out->stream.common.dump = out_dump;
1949 out->stream.common.set_parameters = out_set_parameters;
1950 out->stream.common.get_parameters = out_get_parameters;
1951 out->stream.common.add_audio_effect = out_add_audio_effect;
1952 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1953 out->stream.get_latency = out_get_latency;
1954 out->stream.set_volume = out_set_volume;
1955 out->stream.write = out_write;
1956 out->stream.get_render_position = out_get_render_position;
1957 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
1958
1959 out->dev = adev;
1960 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07001961 /* out->muted = false; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001962
1963 config->format = out->stream.common.get_format(&out->stream.common);
1964 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
1965 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
1966
1967 *stream_out = &out->stream;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001968 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001969 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001970
1971error_open:
1972 free(out);
1973 *stream_out = NULL;
1974 ALOGD("%s: exit: ret %d", __func__, ret);
1975 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001976}
1977
1978static void adev_close_output_stream(struct audio_hw_device *dev,
1979 struct audio_stream_out *stream)
1980{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001981 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001982 out_standby(&stream->common);
1983 free(stream);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001984 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001985}
1986
1987static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
1988{
1989 struct audio_device *adev = (struct audio_device *)dev;
1990 struct str_parms *parms;
1991 char *str;
1992 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07001993 int val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001994 int ret;
1995
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001996 ALOGD("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001997
1998 parms = str_parms_create_str(kvpairs);
1999 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value));
2000 if (ret >= 0) {
2001 int tty_mode;
2002
2003 if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0)
2004 tty_mode = TTY_MODE_OFF;
2005 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0)
2006 tty_mode = TTY_MODE_VCO;
2007 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0)
2008 tty_mode = TTY_MODE_HCO;
2009 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0)
2010 tty_mode = TTY_MODE_FULL;
2011 else
2012 return -EINVAL;
2013
2014 pthread_mutex_lock(&adev->lock);
2015 if (tty_mode != adev->tty_mode) {
2016 adev->tty_mode = tty_mode;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002017 adev->acdb_settings = (adev->acdb_settings & TTY_MODE_CLEAR) | tty_mode;
2018 if (adev->in_call)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002019 select_devices(adev, USECASE_VOICE_CALL);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002020 }
2021 pthread_mutex_unlock(&adev->lock);
2022 }
2023
2024 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2025 if (ret >= 0) {
2026 /* When set to false, HAL should disable EC and NS
2027 * But it is currently not supported.
2028 */
2029 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2030 adev->bluetooth_nrec = true;
2031 else
2032 adev->bluetooth_nrec = false;
2033 }
2034
2035 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2036 if (ret >= 0) {
2037 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2038 adev->screen_off = false;
2039 else
2040 adev->screen_off = true;
2041 }
2042
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002043 ret = str_parms_get_int(parms, "rotation", &val);
2044 if (ret >= 0) {
2045 bool reverse_speakers = false;
2046 switch(val) {
2047 // FIXME: note that the code below assumes that the speakers are in the correct placement
2048 // relative to the user when the device is rotated 90deg from its default rotation. This
2049 // assumption is device-specific, not platform-specific like this code.
2050 case 270:
2051 reverse_speakers = true;
2052 break;
2053 case 0:
2054 case 90:
2055 case 180:
2056 break;
2057 default:
2058 ALOGE("%s: unexpected rotation of %d", __func__, val);
2059 }
2060 pthread_mutex_lock(&adev->lock);
2061 if (adev->speaker_lr_swap != reverse_speakers) {
2062 adev->speaker_lr_swap = reverse_speakers;
2063 // only update the selected device if there is active pcm playback
2064 struct audio_usecase *usecase;
2065 struct listnode *node;
2066 list_for_each(node, &adev->usecase_list) {
2067 usecase = node_to_item(node, struct audio_usecase, list);
2068 if (usecase->type == PCM_PLAYBACK) {
2069 select_devices(adev, usecase->id);
2070 break;
2071 }
2072 }
2073 }
2074 pthread_mutex_unlock(&adev->lock);
2075 }
2076
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002077 str_parms_destroy(parms);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002078 ALOGD("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002079 return ret;
2080}
2081
2082static char* adev_get_parameters(const struct audio_hw_device *dev,
2083 const char *keys)
2084{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002085 return strdup("");
2086}
2087
2088static int adev_init_check(const struct audio_hw_device *dev)
2089{
2090 return 0;
2091}
2092
2093static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2094{
2095 struct audio_device *adev = (struct audio_device *)dev;
2096 int vol, err = 0;
2097
2098 pthread_mutex_lock(&adev->lock);
2099 adev->voice_volume = volume;
2100 if (adev->mode == AUDIO_MODE_IN_CALL) {
2101 if (volume < 0.0) {
2102 volume = 0.0;
2103 } else if (volume > 1.0) {
2104 volume = 1.0;
2105 }
2106
2107 vol = lrint(volume * 100.0);
2108
2109 // Voice volume levels from android are mapped to driver volume levels as follows.
2110 // 0 -> 5, 20 -> 4, 40 ->3, 60 -> 2, 80 -> 1, 100 -> 0
2111 // So adjust the volume to get the correct volume index in driver
2112 vol = 100 - vol;
Ravi Kumar Alamanda87f6ee02013-05-07 14:20:11 -07002113#ifdef MSM8974
2114 set_voice_volume(adev->mixer, vol);
2115#else
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002116 if (adev->csd_client) {
2117 if (adev->csd_volume == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002118 ALOGE("%s: dlsym error for csd_client_volume", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002119 } else {
2120 err = adev->csd_volume(vol);
2121 if (err < 0) {
2122 ALOGE("%s: csd_client error %d", __func__, err);
2123 }
2124 }
2125 } else {
2126 ALOGE("%s: No CSD Client present", __func__);
2127 }
Ravi Kumar Alamanda87f6ee02013-05-07 14:20:11 -07002128#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002129 }
2130 pthread_mutex_unlock(&adev->lock);
2131 return err;
2132}
2133
2134static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
2135{
2136 return -ENOSYS;
2137}
2138
2139static int adev_get_master_volume(struct audio_hw_device *dev,
2140 float *volume)
2141{
2142 return -ENOSYS;
2143}
2144
2145static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
2146{
2147 return -ENOSYS;
2148}
2149
2150static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
2151{
2152 return -ENOSYS;
2153}
2154
2155static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2156{
2157 struct audio_device *adev = (struct audio_device *)dev;
2158
2159 pthread_mutex_lock(&adev->lock);
2160 if (adev->mode != mode) {
2161 adev->mode = mode;
2162 }
2163 pthread_mutex_unlock(&adev->lock);
2164 return 0;
2165}
2166
2167static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2168{
2169 struct audio_device *adev = (struct audio_device *)dev;
2170 int err = 0;
2171
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002172 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002173 adev->mic_mute = state;
2174 if (adev->mode == AUDIO_MODE_IN_CALL) {
Ravi Kumar Alamanda87f6ee02013-05-07 14:20:11 -07002175#ifdef MSM8974
2176 set_mic_mute(adev->mixer, state);
2177#else
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002178 if (adev->csd_client) {
2179 if (adev->csd_mic_mute == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002180 ALOGE("%s: dlsym error for csd_mic_mute", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002181 } else {
2182 err = adev->csd_mic_mute(state);
2183 if (err < 0) {
2184 ALOGE("%s: csd_client error %d", __func__, err);
2185 }
2186 }
2187 } else {
2188 ALOGE("%s: No CSD Client present", __func__);
2189 }
Ravi Kumar Alamanda87f6ee02013-05-07 14:20:11 -07002190#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002191 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002192 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002193 return err;
2194}
2195
2196static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2197{
2198 struct audio_device *adev = (struct audio_device *)dev;
2199
2200 *state = adev->mic_mute;
2201
2202 return 0;
2203}
2204
2205static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
2206 const struct audio_config *config)
2207{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002208 int channel_count = popcount(config->channel_mask);
2209
2210 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
2211}
2212
2213static int adev_open_input_stream(struct audio_hw_device *dev,
2214 audio_io_handle_t handle,
2215 audio_devices_t devices,
2216 struct audio_config *config,
2217 struct audio_stream_in **stream_in)
2218{
2219 struct audio_device *adev = (struct audio_device *)dev;
2220 struct stream_in *in;
2221 int ret, buffer_size, frame_size;
2222 int channel_count = popcount(config->channel_mask);
2223
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002224 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002225 *stream_in = NULL;
2226 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2227 return -EINVAL;
2228
2229 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
2230
2231 in->stream.common.get_sample_rate = in_get_sample_rate;
2232 in->stream.common.set_sample_rate = in_set_sample_rate;
2233 in->stream.common.get_buffer_size = in_get_buffer_size;
2234 in->stream.common.get_channels = in_get_channels;
2235 in->stream.common.get_format = in_get_format;
2236 in->stream.common.set_format = in_set_format;
2237 in->stream.common.standby = in_standby;
2238 in->stream.common.dump = in_dump;
2239 in->stream.common.set_parameters = in_set_parameters;
2240 in->stream.common.get_parameters = in_get_parameters;
2241 in->stream.common.add_audio_effect = in_add_audio_effect;
2242 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2243 in->stream.set_gain = in_set_gain;
2244 in->stream.read = in_read;
2245 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2246
2247 in->device = devices;
2248 in->source = AUDIO_SOURCE_DEFAULT;
2249 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002250 in->standby = 1;
2251 in->channel_mask = config->channel_mask;
2252
2253 /* Update config params with the requested sample rate and channels */
2254 in->usecase = USECASE_AUDIO_RECORD;
2255 in->config = pcm_config_audio_capture;
2256 in->config.channels = channel_count;
2257 in->config.rate = config->sample_rate;
2258
2259 frame_size = audio_stream_frame_size((struct audio_stream *)in);
2260 buffer_size = get_input_buffer_size(config->sample_rate,
2261 config->format,
2262 channel_count);
2263 in->config.period_size = buffer_size / frame_size;
2264
2265 *stream_in = &in->stream;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002266 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002267 return 0;
2268
2269err_open:
2270 free(in);
2271 *stream_in = NULL;
2272 return ret;
2273}
2274
2275static void adev_close_input_stream(struct audio_hw_device *dev,
2276 struct audio_stream_in *stream)
2277{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002278 ALOGD("%s", __func__);
2279
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002280 in_standby(&stream->common);
2281 free(stream);
2282
2283 return;
2284}
2285
2286static int adev_dump(const audio_hw_device_t *device, int fd)
2287{
2288 return 0;
2289}
2290
2291static int adev_close(hw_device_t *device)
2292{
2293 struct audio_device *adev = (struct audio_device *)device;
2294 audio_route_free(adev->audio_route);
2295 free(device);
2296 return 0;
2297}
2298
2299static void init_platform_data(struct audio_device *adev)
2300{
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002301 char platform[PROPERTY_VALUE_MAX];
2302 char baseband[PROPERTY_VALUE_MAX];
2303 char value[PROPERTY_VALUE_MAX];
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002304
2305 adev->dualmic_config = DUALMIC_CONFIG_NONE;
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -08002306 adev->fluence_in_spkr_mode = false;
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002307 adev->fluence_in_voice_call = false;
2308 adev->fluence_in_voice_rec = false;
2309 adev->mic_type_analog = false;
2310
2311 property_get("persist.audio.handset.mic.type",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002312 if (!strcmp("analog", value))
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002313 adev->mic_type_analog = true;
2314
2315 property_get("persist.audio.dualmic.config",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002316 if (!strcmp("broadside", value)) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002317 adev->dualmic_config = DUALMIC_CONFIG_BROADSIDE;
2318 adev->acdb_settings |= DMIC_FLAG;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002319 } else if (!strcmp("endfire", value)) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002320 adev->dualmic_config = DUALMIC_CONFIG_ENDFIRE;
2321 adev->acdb_settings |= DMIC_FLAG;
2322 }
2323
2324 if (adev->dualmic_config != DUALMIC_CONFIG_NONE) {
2325 property_get("persist.audio.fluence.voicecall",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002326 if (!strcmp("true", value)) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002327 adev->fluence_in_voice_call = true;
2328 }
2329
2330 property_get("persist.audio.fluence.voicerec",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002331 if (!strcmp("true", value)) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002332 adev->fluence_in_voice_rec = true;
2333 }
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -08002334
2335 property_get("persist.audio.fluence.speaker",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002336 if (!strcmp("true", value)) {
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -08002337 adev->fluence_in_spkr_mode = true;
2338 }
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002339 }
2340
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002341 adev->acdb_handle = dlopen(LIB_ACDB_LOADER, RTLD_NOW);
2342 if (adev->acdb_handle == NULL) {
2343 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_ACDB_LOADER);
2344 } else {
2345 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_ACDB_LOADER);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002346 adev->acdb_deallocate = (acdb_deallocate_t)dlsym(adev->acdb_handle,
2347 "acdb_loader_deallocate_ACDB");
2348 adev->acdb_send_audio_cal = (acdb_send_audio_cal_t)dlsym(adev->acdb_handle,
2349 "acdb_loader_send_audio_cal");
2350 adev->acdb_send_voice_cal = (acdb_send_voice_cal_t)dlsym(adev->acdb_handle,
2351 "acdb_loader_send_voice_cal");
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002352 adev->acdb_init = (acdb_init_t)dlsym(adev->acdb_handle,
2353 "acdb_loader_init_ACDB");
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002354 if (adev->acdb_init == NULL)
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002355 ALOGE("%s: dlsym error %s for acdb_loader_init_ACDB", __func__, dlerror());
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002356 else
2357 adev->acdb_init();
2358 }
2359
2360 /* If platform is Fusion3, load CSD Client specific symbols
2361 * Voice call is handled by MDM and apps processor talks to
2362 * MDM through CSD Client
2363 */
2364 property_get("ro.board.platform", platform, "");
2365 property_get("ro.baseband", baseband, "");
2366 if (!strcmp("msm8960", platform) && !strcmp("mdm", baseband)) {
2367 adev->csd_client = dlopen(LIB_CSD_CLIENT, RTLD_NOW);
2368 if (adev->csd_client == NULL)
2369 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_CSD_CLIENT);
2370 }
2371
2372 if (adev->csd_client) {
2373 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_CSD_CLIENT);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002374 adev->csd_client_deinit = (csd_client_deinit_t)dlsym(adev->csd_client,
2375 "csd_client_deinit");
2376 adev->csd_disable_device = (csd_disable_device_t)dlsym(adev->csd_client,
2377 "csd_client_disable_device");
2378 adev->csd_enable_device = (csd_enable_device_t)dlsym(adev->csd_client,
2379 "csd_client_enable_device");
2380 adev->csd_start_voice = (csd_start_voice_t)dlsym(adev->csd_client,
2381 "csd_client_start_voice");
2382 adev->csd_stop_voice = (csd_stop_voice_t)dlsym(adev->csd_client,
2383 "csd_client_stop_voice");
2384 adev->csd_volume = (csd_volume_t)dlsym(adev->csd_client,
2385 "csd_client_volume");
2386 adev->csd_mic_mute = (csd_mic_mute_t)dlsym(adev->csd_client,
2387 "csd_client_mic_mute");
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002388 adev->csd_client_init = (csd_client_init_t)dlsym(adev->csd_client,
2389 "csd_client_init");
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002390
2391 if (adev->csd_client_init == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002392 ALOGE("%s: dlsym error %s for csd_client_init", __func__, dlerror());
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002393 } else {
2394 adev->csd_client_init();
2395 }
2396 }
2397}
2398
2399static int adev_open(const hw_module_t *module, const char *name,
2400 hw_device_t **device)
2401{
2402 struct audio_device *adev;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002403 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002404
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002405 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002406 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2407
2408 adev = calloc(1, sizeof(struct audio_device));
2409
2410 adev->mixer = mixer_open(MIXER_CARD);
2411 if (!adev->mixer) {
2412 ALOGE("Unable to open the mixer, aborting.");
2413 return -ENOSYS;
2414 }
2415
2416 adev->audio_route = audio_route_init(MIXER_CARD, MIXER_XML_PATH);
2417 if (!adev->audio_route) {
2418 free(adev);
2419 ALOGE("%s: Failed to init audio route controls, aborting.", __func__);
2420 *device = NULL;
2421 return -EINVAL;
2422 }
2423
2424 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2425 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2426 adev->device.common.module = (struct hw_module_t *)module;
2427 adev->device.common.close = adev_close;
2428
2429 adev->device.init_check = adev_init_check;
2430 adev->device.set_voice_volume = adev_set_voice_volume;
2431 adev->device.set_master_volume = adev_set_master_volume;
2432 adev->device.get_master_volume = adev_get_master_volume;
2433 adev->device.set_master_mute = adev_set_master_mute;
2434 adev->device.get_master_mute = adev_get_master_mute;
2435 adev->device.set_mode = adev_set_mode;
2436 adev->device.set_mic_mute = adev_set_mic_mute;
2437 adev->device.get_mic_mute = adev_get_mic_mute;
2438 adev->device.set_parameters = adev_set_parameters;
2439 adev->device.get_parameters = adev_get_parameters;
2440 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2441 adev->device.open_output_stream = adev_open_output_stream;
2442 adev->device.close_output_stream = adev_close_output_stream;
2443 adev->device.open_input_stream = adev_open_input_stream;
2444 adev->device.close_input_stream = adev_close_input_stream;
2445 adev->device.dump = adev_dump;
2446
2447 /* Set the default route before the PCM stream is opened */
2448 pthread_mutex_lock(&adev->lock);
2449 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002450 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002451 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002452 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002453 adev->voice_call_rx = NULL;
2454 adev->voice_call_tx = NULL;
2455 adev->voice_volume = 1.0f;
2456 adev->tty_mode = TTY_MODE_OFF;
2457 adev->bluetooth_nrec = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002458 adev->in_call = false;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002459 adev->acdb_settings = TTY_MODE_OFF;
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002460 adev->speaker_lr_swap = false;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002461 for (i = 0; i < SND_DEVICE_MAX; i++) {
2462 adev->snd_dev_ref_cnt[i] = 0;
2463 }
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002464 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002465 pthread_mutex_unlock(&adev->lock);
2466
2467 /* Loads platform specific libraries dynamically */
2468 init_platform_data(adev);
2469
2470 *device = &adev->device.common;
2471
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002472 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002473 return 0;
2474}
2475
2476static struct hw_module_methods_t hal_module_methods = {
2477 .open = adev_open,
2478};
2479
2480struct audio_module HAL_MODULE_INFO_SYM = {
2481 .common = {
2482 .tag = HARDWARE_MODULE_TAG,
2483 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2484 .hal_api_version = HARDWARE_HAL_API_VERSION,
2485 .id = AUDIO_HARDWARE_MODULE_ID,
2486 .name = "QCOM Audio HAL",
2487 .author = "Code Aurora Forum",
2488 .methods = &hal_module_methods,
2489 },
2490};