blob: e80f88f6d385c89f4bed9c6fee45a045e859a5ce [file] [log] [blame]
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001/*
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07002 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
3 * Not a Contribution.
4 *
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005 * Copyright (C) 2013 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#define LOG_TAG "audio_hw_primary"
21/*#define LOG_NDEBUG 0*/
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -080022#define LOG_NDDEBUG 0
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080023
24#include <errno.h>
25#include <pthread.h>
26#include <stdint.h>
27#include <sys/time.h>
28#include <stdlib.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080029#include <math.h>
30
31#include <cutils/log.h>
32#include <cutils/str_parms.h>
33#include <cutils/properties.h>
34
Eric Laurentb23d5282013-05-14 15:27:20 -070035#include <hardware/audio_effect.h>
36#include <audio_effects/effect_aec.h>
37#include <audio_effects/effect_ns.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080038#include "audio_hw.h"
Eric Laurentb23d5282013-05-14 15:27:20 -070039#include "platform_api.h"
40#include <platform.h>
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -070041#include "audio_extn.h"
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080042
Eric Laurentb23d5282013-05-14 15:27:20 -070043struct pcm_config pcm_config_deep_buffer = {
44 .channels = 2,
45 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
46 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
47 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
48 .format = PCM_FORMAT_S16_LE,
49 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
50 .stop_threshold = INT_MAX,
51 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
52};
53
54struct pcm_config pcm_config_low_latency = {
55 .channels = 2,
56 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
57 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
58 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
59 .format = PCM_FORMAT_S16_LE,
60 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
61 .stop_threshold = INT_MAX,
62 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
63};
64
65struct pcm_config pcm_config_hdmi_multi = {
66 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
67 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
68 .period_size = HDMI_MULTI_PERIOD_SIZE,
69 .period_count = HDMI_MULTI_PERIOD_COUNT,
70 .format = PCM_FORMAT_S16_LE,
71 .start_threshold = 0,
72 .stop_threshold = INT_MAX,
73 .avail_min = 0,
74};
75
76struct pcm_config pcm_config_audio_capture = {
77 .channels = 2,
Eric Laurentb23d5282013-05-14 15:27:20 -070078 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
79 .format = PCM_FORMAT_S16_LE,
80};
81
82struct pcm_config pcm_config_voice_call = {
83 .channels = 1,
84 .rate = 8000,
85 .period_size = 160,
86 .period_count = 2,
87 .format = PCM_FORMAT_S16_LE,
88};
89
90static const char * const use_case_table[AUDIO_USECASE_MAX] = {
91 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
92 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
93 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
94 [USECASE_AUDIO_RECORD] = "audio-record",
95 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
96 [USECASE_VOICE_CALL] = "voice-call",
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070097 [USECASE_AUDIO_PLAYBACK_FM] = "play-fm",
Eric Laurentb23d5282013-05-14 15:27:20 -070098};
99
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800100
101#define STRING_TO_ENUM(string) { #string, string }
102
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800103struct string_to_enum {
104 const char *name;
105 uint32_t value;
106};
107
108static const struct string_to_enum out_channels_name_to_enum_table[] = {
109 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
110 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
111 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
112};
113
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700114static struct audio_device *adev = NULL;
115static pthread_mutex_t adev_init_lock;
116static bool is_adev_initialised = false;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800117
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700118static int enable_audio_route(struct audio_device *adev,
119 struct audio_usecase *usecase,
120 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800121{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700122 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700123 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800124
125 if (usecase == NULL)
126 return -EINVAL;
127
128 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
129
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800130 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700131 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800132 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700133 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800134
135 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700136 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700137 ALOGV("%s: apply mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700138 audio_route_apply_path(adev->audio_route, mixer_path);
139 if (update_mixer)
140 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800141
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800142 ALOGV("%s: exit", __func__);
143 return 0;
144}
145
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700146int disable_audio_route(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700147 struct audio_usecase *usecase,
148 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800149{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700150 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700151 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800152
153 if (usecase == NULL)
154 return -EINVAL;
155
156 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700157 if (usecase->type == PCM_CAPTURE)
158 snd_device = usecase->in_snd_device;
159 else
160 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800161 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700162 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700163 ALOGV("%s: reset mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700164 audio_route_reset_path(adev->audio_route, mixer_path);
165 if (update_mixer)
166 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800167
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800168 ALOGV("%s: exit", __func__);
169 return 0;
170}
171
172static int enable_snd_device(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700173 snd_device_t snd_device,
174 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800175{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800176 if (snd_device < SND_DEVICE_MIN ||
177 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800178 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800179 return -EINVAL;
180 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700181
182 adev->snd_dev_ref_cnt[snd_device]++;
183 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700184 ALOGV("%s: snd_device(%d: %s) is already active",
Eric Laurentb23d5282013-05-14 15:27:20 -0700185 __func__, snd_device, platform_get_snd_device_name(snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700186 return 0;
187 }
188
Eric Laurentb23d5282013-05-14 15:27:20 -0700189 if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700190 adev->snd_dev_ref_cnt[snd_device]--;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800191 return -EINVAL;
192 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800193
Eric Laurent994a6932013-07-17 11:51:42 -0700194 ALOGV("%s: snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700195 snd_device, platform_get_snd_device_name(snd_device));
196 audio_route_apply_path(adev->audio_route, platform_get_snd_device_name(snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700197 if (update_mixer)
198 audio_route_update_mixer(adev->audio_route);
199
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800200 return 0;
201}
202
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700203int disable_snd_device(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700204 snd_device_t snd_device,
205 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800206{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800207 if (snd_device < SND_DEVICE_MIN ||
208 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800209 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800210 return -EINVAL;
211 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700212 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
213 ALOGE("%s: device ref cnt is already 0", __func__);
214 return -EINVAL;
215 }
216 adev->snd_dev_ref_cnt[snd_device]--;
217 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Eric Laurent994a6932013-07-17 11:51:42 -0700218 ALOGV("%s: snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700219 snd_device, platform_get_snd_device_name(snd_device));
220 audio_route_reset_path(adev->audio_route, platform_get_snd_device_name(snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700221 if (update_mixer)
222 audio_route_update_mixer(adev->audio_route);
223 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800224 return 0;
225}
226
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700227static void check_usecases_codec_backend(struct audio_device *adev,
228 struct audio_usecase *uc_info,
229 snd_device_t snd_device)
230{
231 struct listnode *node;
232 struct audio_usecase *usecase;
233 bool switch_device[AUDIO_USECASE_MAX];
234 int i, num_uc_to_switch = 0;
235
236 /*
237 * This function is to make sure that all the usecases that are active on
238 * the hardware codec backend are always routed to any one device that is
239 * handled by the hardware codec.
240 * For example, if low-latency and deep-buffer usecases are currently active
241 * on speaker and out_set_parameters(headset) is received on low-latency
242 * output, then we have to make sure deep-buffer is also switched to headset,
243 * because of the limitation that both the devices cannot be enabled
244 * at the same time as they share the same backend.
245 */
246 /* Disable all the usecases on the shared backend other than the
247 specified usecase */
248 for (i = 0; i < AUDIO_USECASE_MAX; i++)
249 switch_device[i] = false;
250
251 list_for_each(node, &adev->usecase_list) {
252 usecase = node_to_item(node, struct audio_usecase, list);
253 if (usecase->type != PCM_CAPTURE &&
254 usecase != uc_info &&
255 usecase->out_snd_device != snd_device &&
256 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
257 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
258 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700259 platform_get_snd_device_name(usecase->out_snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700260 disable_audio_route(adev, usecase, false);
261 switch_device[usecase->id] = true;
262 num_uc_to_switch++;
263 }
264 }
265
266 if (num_uc_to_switch) {
267 /* Make sure all the streams are de-routed before disabling the device */
268 audio_route_update_mixer(adev->audio_route);
269
270 list_for_each(node, &adev->usecase_list) {
271 usecase = node_to_item(node, struct audio_usecase, list);
272 if (switch_device[usecase->id]) {
273 disable_snd_device(adev, usecase->out_snd_device, false);
274 enable_snd_device(adev, snd_device, false);
275 }
276 }
277
278 /* Make sure new snd device is enabled before re-routing the streams */
279 audio_route_update_mixer(adev->audio_route);
280
281 /* Re-route all the usecases on the shared backend other than the
282 specified usecase to new snd devices */
283 list_for_each(node, &adev->usecase_list) {
284 usecase = node_to_item(node, struct audio_usecase, list);
285 /* Update the out_snd_device only before enabling the audio route */
286 if (switch_device[usecase->id] ) {
287 usecase->out_snd_device = snd_device;
288 enable_audio_route(adev, usecase, false);
289 }
290 }
291
292 audio_route_update_mixer(adev->audio_route);
293 }
294}
295
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700296static void check_and_route_capture_usecases(struct audio_device *adev,
297 struct audio_usecase *uc_info,
298 snd_device_t snd_device)
299{
300 struct listnode *node;
301 struct audio_usecase *usecase;
302 bool switch_device[AUDIO_USECASE_MAX];
303 int i, num_uc_to_switch = 0;
304
305 /*
306 * This function is to make sure that all the active capture usecases
307 * are always routed to the same input sound device.
308 * For example, if audio-record and voice-call usecases are currently
309 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
310 * is received for voice call then we have to make sure that audio-record
311 * usecase is also switched to earpiece i.e. voice-dmic-ef,
312 * because of the limitation that two devices cannot be enabled
313 * at the same time if they share the same backend.
314 */
315 for (i = 0; i < AUDIO_USECASE_MAX; i++)
316 switch_device[i] = false;
317
318 list_for_each(node, &adev->usecase_list) {
319 usecase = node_to_item(node, struct audio_usecase, list);
320 if (usecase->type != PCM_PLAYBACK &&
321 usecase != uc_info &&
322 usecase->in_snd_device != snd_device) {
323 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
324 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700325 platform_get_snd_device_name(usecase->in_snd_device));
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700326 disable_audio_route(adev, usecase, false);
327 switch_device[usecase->id] = true;
328 num_uc_to_switch++;
329 }
330 }
331
332 if (num_uc_to_switch) {
333 /* Make sure all the streams are de-routed before disabling the device */
334 audio_route_update_mixer(adev->audio_route);
335
336 list_for_each(node, &adev->usecase_list) {
337 usecase = node_to_item(node, struct audio_usecase, list);
338 if (switch_device[usecase->id]) {
339 disable_snd_device(adev, usecase->in_snd_device, false);
340 enable_snd_device(adev, snd_device, false);
341 }
342 }
343
344 /* Make sure new snd device is enabled before re-routing the streams */
345 audio_route_update_mixer(adev->audio_route);
346
347 /* Re-route all the usecases on the shared backend other than the
348 specified usecase to new snd devices */
349 list_for_each(node, &adev->usecase_list) {
350 usecase = node_to_item(node, struct audio_usecase, list);
351 /* Update the in_snd_device only before enabling the audio route */
352 if (switch_device[usecase->id] ) {
353 usecase->in_snd_device = snd_device;
354 enable_audio_route(adev, usecase, false);
355 }
356 }
357
358 audio_route_update_mixer(adev->audio_route);
359 }
360}
361
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800362
363/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700364static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800365{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700366 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700367 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800368
369 switch (channels) {
370 /*
371 * Do not handle stereo output in Multi-channel cases
372 * Stereo case is handled in normal playback path
373 */
374 case 6:
375 ALOGV("%s: HDMI supports 5.1", __func__);
376 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
377 break;
378 case 8:
379 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
380 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
381 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
382 break;
383 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700384 ALOGE("HDMI does not support multi channel playback");
385 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800386 break;
387 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700388 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800389}
390
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700391struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700392 audio_usecase_t uc_id)
393{
394 struct audio_usecase *usecase;
395 struct listnode *node;
396
397 list_for_each(node, &adev->usecase_list) {
398 usecase = node_to_item(node, struct audio_usecase, list);
399 if (usecase->id == uc_id)
400 return usecase;
401 }
402 return NULL;
403}
404
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700405int select_devices(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700406 audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800407{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800408 snd_device_t out_snd_device = SND_DEVICE_NONE;
409 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700410 struct audio_usecase *usecase = NULL;
411 struct audio_usecase *vc_usecase = NULL;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800412 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700413 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800414
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700415 usecase = get_usecase_from_list(adev, uc_id);
416 if (usecase == NULL) {
417 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
418 return -EINVAL;
419 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800420
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700421 if (usecase->type == VOICE_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700422 out_snd_device = platform_get_output_snd_device(adev->platform,
423 usecase->stream.out->devices);
424 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700425 usecase->devices = usecase->stream.out->devices;
426 } else {
427 /*
428 * If the voice call is active, use the sound devices of voice call usecase
429 * so that it would not result any device switch. All the usecases will
430 * be switched to new device when select_devices() is called for voice call
431 * usecase. This is to avoid switching devices for voice call when
432 * check_usecases_codec_backend() is called below.
433 */
434 if (adev->in_call) {
435 vc_usecase = get_usecase_from_list(adev, USECASE_VOICE_CALL);
436 if (vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
437 in_snd_device = vc_usecase->in_snd_device;
438 out_snd_device = vc_usecase->out_snd_device;
439 }
440 }
441 if (usecase->type == PCM_PLAYBACK) {
442 usecase->devices = usecase->stream.out->devices;
443 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700444 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700445 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700446 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700447 if (usecase->stream.out == adev->primary_output &&
448 adev->active_input &&
449 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
450 select_devices(adev, adev->active_input->usecase);
451 }
452 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700453 } else if (usecase->type == PCM_CAPTURE) {
454 usecase->devices = usecase->stream.in->device;
455 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700456 if (in_snd_device == SND_DEVICE_NONE) {
457 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
458 adev->primary_output && !adev->primary_output->standby) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700459 in_snd_device = platform_get_input_snd_device(adev->platform,
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700460 adev->primary_output->devices);
461 } else {
Eric Laurentb23d5282013-05-14 15:27:20 -0700462 in_snd_device = platform_get_input_snd_device(adev->platform,
463 AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700464 }
465 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700466 }
467 }
468
469 if (out_snd_device == usecase->out_snd_device &&
470 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800471 return 0;
472 }
473
sangwoobc677242013-08-08 16:53:43 +0900474 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700475 out_snd_device, platform_get_snd_device_name(out_snd_device),
476 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800477
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800478 /*
479 * Limitation: While in call, to do a device switch we need to disable
480 * and enable both RX and TX devices though one of them is same as current
481 * device.
482 */
Eric Laurentb23d5282013-05-14 15:27:20 -0700483 if (usecase->type == VOICE_CALL) {
484 status = platform_switch_voice_call_device_pre(adev->platform);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800485 }
486
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700487 /* Disable current sound devices */
488 if (usecase->out_snd_device != SND_DEVICE_NONE) {
489 disable_audio_route(adev, usecase, true);
490 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800491 }
492
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700493 if (usecase->in_snd_device != SND_DEVICE_NONE) {
494 disable_audio_route(adev, usecase, true);
495 disable_snd_device(adev, usecase->in_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800496 }
497
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700498 /* Enable new sound devices */
499 if (out_snd_device != SND_DEVICE_NONE) {
500 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
501 check_usecases_codec_backend(adev, usecase, out_snd_device);
502 enable_snd_device(adev, out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800503 }
504
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700505 if (in_snd_device != SND_DEVICE_NONE) {
506 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700507 enable_snd_device(adev, in_snd_device, false);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700508 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700509
Eric Laurentb23d5282013-05-14 15:27:20 -0700510 if (usecase->type == VOICE_CALL)
511 status = platform_switch_voice_call_device_post(adev->platform,
512 out_snd_device,
513 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800514
sangwoo170731f2013-06-08 15:36:36 +0900515 audio_route_update_mixer(adev->audio_route);
516
517 usecase->in_snd_device = in_snd_device;
518 usecase->out_snd_device = out_snd_device;
519
520 enable_audio_route(adev, usecase, true);
521
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800522 return status;
523}
524
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800525static int stop_input_stream(struct stream_in *in)
526{
527 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800528 struct audio_usecase *uc_info;
529 struct audio_device *adev = in->dev;
530
Eric Laurentc8400632013-02-14 19:04:54 -0800531 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800532
Eric Laurent994a6932013-07-17 11:51:42 -0700533 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700534 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800535 uc_info = get_usecase_from_list(adev, in->usecase);
536 if (uc_info == NULL) {
537 ALOGE("%s: Could not find the usecase (%d) in the list",
538 __func__, in->usecase);
539 return -EINVAL;
540 }
541
Eric Laurent150dbfe2013-02-27 14:31:02 -0800542 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700543 disable_audio_route(adev, uc_info, true);
544
545 /* 2. Disable the tx device */
546 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800547
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800548 list_remove(&uc_info->list);
549 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800550
Eric Laurent994a6932013-07-17 11:51:42 -0700551 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800552 return ret;
553}
554
555int start_input_stream(struct stream_in *in)
556{
557 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800558 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800559 struct audio_usecase *uc_info;
560 struct audio_device *adev = in->dev;
561
Eric Laurent994a6932013-07-17 11:51:42 -0700562 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
Eric Laurentb23d5282013-05-14 15:27:20 -0700563 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800564 if (in->pcm_device_id < 0) {
565 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
566 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800567 ret = -EINVAL;
568 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800569 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700570
571 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800572 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
573 uc_info->id = in->usecase;
574 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800575 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700576 uc_info->devices = in->device;
577 uc_info->in_snd_device = SND_DEVICE_NONE;
578 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800579
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800580 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700581 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800582
Eric Laurentc8400632013-02-14 19:04:54 -0800583 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
584 __func__, SOUND_CARD, in->pcm_device_id, in->config.channels);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800585 in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id,
586 PCM_IN, &in->config);
587 if (in->pcm && !pcm_is_ready(in->pcm)) {
588 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
589 pcm_close(in->pcm);
590 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800591 ret = -EIO;
592 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800593 }
Eric Laurent994a6932013-07-17 11:51:42 -0700594 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800595 return ret;
596
597error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800598 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800599
600error_config:
601 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700602 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800603
604 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800605}
606
607static int stop_output_stream(struct stream_out *out)
608{
609 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800610 struct audio_usecase *uc_info;
611 struct audio_device *adev = out->dev;
612
Eric Laurent994a6932013-07-17 11:51:42 -0700613 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700614 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800615 uc_info = get_usecase_from_list(adev, out->usecase);
616 if (uc_info == NULL) {
617 ALOGE("%s: Could not find the usecase (%d) in the list",
618 __func__, out->usecase);
619 return -EINVAL;
620 }
621
Eric Laurent150dbfe2013-02-27 14:31:02 -0800622 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700623 disable_audio_route(adev, uc_info, true);
624
625 /* 2. Disable the rx device */
626 disable_snd_device(adev, uc_info->out_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800627
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800628 list_remove(&uc_info->list);
629 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800630
Eric Laurent994a6932013-07-17 11:51:42 -0700631 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800632 return ret;
633}
634
635int start_output_stream(struct stream_out *out)
636{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800637 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800638 struct audio_usecase *uc_info;
639 struct audio_device *adev = out->dev;
640
Eric Laurent994a6932013-07-17 11:51:42 -0700641 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700642 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Eric Laurentb23d5282013-05-14 15:27:20 -0700643 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800644 if (out->pcm_device_id < 0) {
645 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
646 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800647 ret = -EINVAL;
648 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800649 }
650
651 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
652 uc_info->id = out->usecase;
653 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800654 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700655 uc_info->devices = out->devices;
656 uc_info->in_snd_device = SND_DEVICE_NONE;
657 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800658
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800659 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800660
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700661 select_devices(adev, out->usecase);
662
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800663 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
664 __func__, 0, out->pcm_device_id);
665 out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id,
666 PCM_OUT, &out->config);
667 if (out->pcm && !pcm_is_ready(out->pcm)) {
668 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
669 pcm_close(out->pcm);
670 out->pcm = NULL;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800671 ret = -EIO;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700672 goto error_pcm_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800673 }
Eric Laurent994a6932013-07-17 11:51:42 -0700674 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800675 return 0;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700676error_pcm_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800677 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800678error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800679 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800680}
681
682static int stop_voice_call(struct audio_device *adev)
683{
684 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800685 struct audio_usecase *uc_info;
686
Eric Laurent994a6932013-07-17 11:51:42 -0700687 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -0800688 adev->in_call = false;
Eric Laurentb23d5282013-05-14 15:27:20 -0700689
690 ret = platform_stop_voice_call(adev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800691
692 /* 1. Close the PCM devices */
693 if (adev->voice_call_rx) {
694 pcm_close(adev->voice_call_rx);
695 adev->voice_call_rx = NULL;
696 }
697 if (adev->voice_call_tx) {
698 pcm_close(adev->voice_call_tx);
699 adev->voice_call_tx = NULL;
700 }
701
702 uc_info = get_usecase_from_list(adev, USECASE_VOICE_CALL);
703 if (uc_info == NULL) {
704 ALOGE("%s: Could not find the usecase (%d) in the list",
705 __func__, USECASE_VOICE_CALL);
706 return -EINVAL;
707 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800708
709 /* 2. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700710 disable_audio_route(adev, uc_info, true);
711
712 /* 3. Disable the rx and tx devices */
713 disable_snd_device(adev, uc_info->out_snd_device, false);
714 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800715
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800716 list_remove(&uc_info->list);
717 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800718
Eric Laurent994a6932013-07-17 11:51:42 -0700719 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800720 return ret;
721}
722
723static int start_voice_call(struct audio_device *adev)
724{
725 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800726 struct audio_usecase *uc_info;
727 int pcm_dev_rx_id, pcm_dev_tx_id;
728
Eric Laurent994a6932013-07-17 11:51:42 -0700729 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800730
731 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
732 uc_info->id = USECASE_VOICE_CALL;
733 uc_info->type = VOICE_CALL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800734 uc_info->stream.out = adev->primary_output;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700735 uc_info->devices = adev->primary_output->devices;
736 uc_info->in_snd_device = SND_DEVICE_NONE;
737 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800738
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800739 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800740
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700741 select_devices(adev, USECASE_VOICE_CALL);
742
Eric Laurentb23d5282013-05-14 15:27:20 -0700743 pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK);
744 pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800745
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800746 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
747 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
748 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -0800749 ret = -EIO;
750 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800751 }
752
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -0800753 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800754 __func__, SOUND_CARD, pcm_dev_rx_id);
755 adev->voice_call_rx = pcm_open(SOUND_CARD,
756 pcm_dev_rx_id,
757 PCM_OUT, &pcm_config_voice_call);
758 if (adev->voice_call_rx && !pcm_is_ready(adev->voice_call_rx)) {
759 ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_rx));
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -0800760 ret = -EIO;
761 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800762 }
763
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -0800764 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800765 __func__, SOUND_CARD, pcm_dev_tx_id);
766 adev->voice_call_tx = pcm_open(SOUND_CARD,
767 pcm_dev_tx_id,
768 PCM_IN, &pcm_config_voice_call);
769 if (adev->voice_call_tx && !pcm_is_ready(adev->voice_call_tx)) {
770 ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_tx));
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -0800771 ret = -EIO;
772 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800773 }
774 pcm_start(adev->voice_call_rx);
775 pcm_start(adev->voice_call_tx);
776
Eric Laurentb23d5282013-05-14 15:27:20 -0700777 ret = platform_start_voice_call(adev->platform);
778 if (ret < 0) {
779 ALOGE("%s: platform_start_voice_call error %d\n", __func__, ret);
780 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800781 }
782
783 adev->in_call = true;
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -0800784 return 0;
785
786error_start_voice:
787 stop_voice_call(adev);
788
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800789 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800790 return ret;
791}
792
793static int check_input_parameters(uint32_t sample_rate,
794 audio_format_t format,
795 int channel_count)
796{
797 if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
798
799 if ((channel_count < 1) || (channel_count > 2)) return -EINVAL;
800
801 switch (sample_rate) {
802 case 8000:
803 case 11025:
804 case 12000:
805 case 16000:
806 case 22050:
807 case 24000:
808 case 32000:
809 case 44100:
810 case 48000:
811 break;
812 default:
813 return -EINVAL;
814 }
815
816 return 0;
817}
818
819static size_t get_input_buffer_size(uint32_t sample_rate,
820 audio_format_t format,
821 int channel_count)
822{
823 size_t size = 0;
824
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -0700825 if (check_input_parameters(sample_rate, format, channel_count) != 0)
826 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800827
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -0700828 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
829 /* ToDo: should use frame_size computed based on the format and
830 channel_count here. */
831 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800832
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -0700833 /* make sure the size is multiple of 64 */
834 size += 0x3f;
835 size &= ~0x3f;
836
837 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800838}
839
840static uint32_t out_get_sample_rate(const struct audio_stream *stream)
841{
842 struct stream_out *out = (struct stream_out *)stream;
843
844 return out->config.rate;
845}
846
847static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
848{
849 return -ENOSYS;
850}
851
852static size_t out_get_buffer_size(const struct audio_stream *stream)
853{
854 struct stream_out *out = (struct stream_out *)stream;
855
856 return out->config.period_size * audio_stream_frame_size(stream);
857}
858
859static uint32_t out_get_channels(const struct audio_stream *stream)
860{
861 struct stream_out *out = (struct stream_out *)stream;
862
863 return out->channel_mask;
864}
865
866static audio_format_t out_get_format(const struct audio_stream *stream)
867{
868 return AUDIO_FORMAT_PCM_16_BIT;
869}
870
871static int out_set_format(struct audio_stream *stream, audio_format_t format)
872{
873 return -ENOSYS;
874}
875
876static int out_standby(struct audio_stream *stream)
877{
878 struct stream_out *out = (struct stream_out *)stream;
879 struct audio_device *adev = out->dev;
Eric Laurent994a6932013-07-17 11:51:42 -0700880 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700881 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800882 pthread_mutex_lock(&out->lock);
883
884 if (!out->standby) {
885 out->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -0800886 if (out->pcm) {
887 pcm_close(out->pcm);
888 out->pcm = NULL;
889 }
890 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800891 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -0800892 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800893 }
894 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -0700895 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800896 return 0;
897}
898
899static int out_dump(const struct audio_stream *stream, int fd)
900{
901 return 0;
902}
903
904static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
905{
906 struct stream_out *out = (struct stream_out *)stream;
907 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800908 struct audio_usecase *usecase;
909 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800910 struct str_parms *parms;
911 char value[32];
912 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800913 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800914
sangwoobc677242013-08-08 16:53:43 +0900915 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700916 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800917 parms = str_parms_create_str(kvpairs);
918 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
919 if (ret >= 0) {
920 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800921 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -0800922 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800923
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700924 /*
925 * When HDMI cable is unplugged the music playback is paused and
926 * the policy manager sends routing=0. But the audioflinger
927 * continues to write data until standby time (3sec).
928 * As the HDMI core is turned off, the write gets blocked.
929 * Avoid this by routing audio to speaker until standby.
930 */
931 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
932 val == AUDIO_DEVICE_NONE) {
933 val = AUDIO_DEVICE_OUT_SPEAKER;
934 }
935
936 /*
937 * select_devices() call below switches all the usecases on the same
938 * backend to the new device. Refer to check_usecases_codec_backend() in
939 * the select_devices(). But how do we undo this?
940 *
941 * For example, music playback is active on headset (deep-buffer usecase)
942 * and if we go to ringtones and select a ringtone, low-latency usecase
943 * will be started on headset+speaker. As we can't enable headset+speaker
944 * and headset devices at the same time, select_devices() switches the music
945 * playback to headset+speaker while starting low-lateny usecase for ringtone.
946 * So when the ringtone playback is completed, how do we undo the same?
947 *
948 * We are relying on the out_set_parameters() call on deep-buffer output,
949 * once the ringtone playback is ended.
950 * NOTE: We should not check if the current devices are same as new devices.
951 * Because select_devices() must be called to switch back the music
952 * playback to headset.
953 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800954 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700955 out->devices = val;
956
957 if (!out->standby)
958 select_devices(adev, out->usecase);
959
960 if ((adev->mode == AUDIO_MODE_IN_CALL) && !adev->in_call &&
961 (out == adev->primary_output)) {
962 start_voice_call(adev);
963 } else if ((adev->mode == AUDIO_MODE_IN_CALL) && adev->in_call &&
964 (out == adev->primary_output)) {
965 select_devices(adev, USECASE_VOICE_CALL);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800966 }
967 }
968
Ravi Kumar Alamanda60caf202013-10-02 09:56:18 -0700969 if ((adev->mode == AUDIO_MODE_NORMAL) && adev->in_call &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700970 (out == adev->primary_output)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800971 stop_voice_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800972 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800973
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800974 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -0800975 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800976 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700977
978 if (out == adev->primary_output) {
979 pthread_mutex_lock(&adev->lock);
980 audio_extn_set_parameters(adev, parms);
981 pthread_mutex_unlock(&adev->lock);
982 }
983
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800984 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -0700985 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800986 return ret;
987}
988
989static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
990{
991 struct stream_out *out = (struct stream_out *)stream;
992 struct str_parms *query = str_parms_create_str(keys);
993 char *str;
994 char value[256];
995 struct str_parms *reply = str_parms_create();
996 size_t i, j;
997 int ret;
998 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -0700999 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001000 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1001 if (ret >= 0) {
1002 value[0] = '\0';
1003 i = 0;
1004 while (out->supported_channel_masks[i] != 0) {
1005 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1006 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1007 if (!first) {
1008 strcat(value, "|");
1009 }
1010 strcat(value, out_channels_name_to_enum_table[j].name);
1011 first = false;
1012 break;
1013 }
1014 }
1015 i++;
1016 }
1017 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1018 str = str_parms_to_str(reply);
1019 } else {
1020 str = strdup(keys);
1021 }
1022 str_parms_destroy(query);
1023 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001024 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001025 return str;
1026}
1027
1028static uint32_t out_get_latency(const struct audio_stream_out *stream)
1029{
1030 struct stream_out *out = (struct stream_out *)stream;
1031
1032 return (out->config.period_count * out->config.period_size * 1000) / (out->config.rate);
1033}
1034
1035static int out_set_volume(struct audio_stream_out *stream, float left,
1036 float right)
1037{
Eric Laurenta9024de2013-04-04 09:19:12 -07001038 struct stream_out *out = (struct stream_out *)stream;
1039 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1040 /* only take left channel into account: the API is for stereo anyway */
1041 out->muted = (left == 0.0f);
1042 return 0;
1043 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001044 return -ENOSYS;
1045}
1046
1047static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1048 size_t bytes)
1049{
1050 struct stream_out *out = (struct stream_out *)stream;
1051 struct audio_device *adev = out->dev;
1052 int i, ret = -1;
1053
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001054 pthread_mutex_lock(&out->lock);
1055 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001056 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001057 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001058 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001059 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001060 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001061 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001062 goto exit;
1063 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001064 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001065
1066 if (out->pcm) {
Eric Laurenta9024de2013-04-04 09:19:12 -07001067 if (out->muted)
1068 memset((void *)buffer, 0, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001069 //ALOGV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1070 ret = pcm_write(out->pcm, (void *)buffer, bytes);
1071 }
1072
1073exit:
1074 pthread_mutex_unlock(&out->lock);
1075
1076 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001077 if (out->pcm)
1078 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001079 out_standby(&out->stream.common);
1080 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1081 out_get_sample_rate(&out->stream.common));
1082 }
1083 return bytes;
1084}
1085
1086static int out_get_render_position(const struct audio_stream_out *stream,
1087 uint32_t *dsp_frames)
1088{
1089 return -EINVAL;
1090}
1091
1092static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1093{
1094 return 0;
1095}
1096
1097static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1098{
1099 return 0;
1100}
1101
1102static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1103 int64_t *timestamp)
1104{
1105 return -EINVAL;
1106}
1107
1108/** audio_stream_in implementation **/
1109static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1110{
1111 struct stream_in *in = (struct stream_in *)stream;
1112
1113 return in->config.rate;
1114}
1115
1116static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1117{
1118 return -ENOSYS;
1119}
1120
1121static size_t in_get_buffer_size(const struct audio_stream *stream)
1122{
1123 struct stream_in *in = (struct stream_in *)stream;
1124
1125 return in->config.period_size * audio_stream_frame_size(stream);
1126}
1127
1128static uint32_t in_get_channels(const struct audio_stream *stream)
1129{
1130 struct stream_in *in = (struct stream_in *)stream;
1131
1132 return in->channel_mask;
1133}
1134
1135static audio_format_t in_get_format(const struct audio_stream *stream)
1136{
1137 return AUDIO_FORMAT_PCM_16_BIT;
1138}
1139
1140static int in_set_format(struct audio_stream *stream, audio_format_t format)
1141{
1142 return -ENOSYS;
1143}
1144
1145static int in_standby(struct audio_stream *stream)
1146{
1147 struct stream_in *in = (struct stream_in *)stream;
1148 struct audio_device *adev = in->dev;
1149 int status = 0;
Eric Laurent994a6932013-07-17 11:51:42 -07001150 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001151 pthread_mutex_lock(&in->lock);
1152 if (!in->standby) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001153 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001154 if (in->pcm) {
1155 pcm_close(in->pcm);
1156 in->pcm = NULL;
1157 }
1158 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001159 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001160 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001161 }
1162 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001163 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001164 return status;
1165}
1166
1167static int in_dump(const struct audio_stream *stream, int fd)
1168{
1169 return 0;
1170}
1171
1172static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1173{
1174 struct stream_in *in = (struct stream_in *)stream;
1175 struct audio_device *adev = in->dev;
1176 struct str_parms *parms;
1177 char *str;
1178 char value[32];
1179 int ret, val = 0;
1180
Eric Laurent994a6932013-07-17 11:51:42 -07001181 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001182 parms = str_parms_create_str(kvpairs);
1183
1184 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1185
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001186 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001187 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001188 if (ret >= 0) {
1189 val = atoi(value);
1190 /* no audio source uses val == 0 */
1191 if ((in->source != val) && (val != 0)) {
1192 in->source = val;
1193 }
1194 }
1195
1196 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1197 if (ret >= 0) {
1198 val = atoi(value);
1199 if ((in->device != val) && (val != 0)) {
1200 in->device = val;
1201 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001202 if (!in->standby)
1203 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001204 }
1205 }
1206
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001207 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001208 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001209
1210 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001211 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001212 return ret;
1213}
1214
1215static char* in_get_parameters(const struct audio_stream *stream,
1216 const char *keys)
1217{
1218 return strdup("");
1219}
1220
1221static int in_set_gain(struct audio_stream_in *stream, float gain)
1222{
1223 return 0;
1224}
1225
1226static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1227 size_t bytes)
1228{
1229 struct stream_in *in = (struct stream_in *)stream;
1230 struct audio_device *adev = in->dev;
1231 int i, ret = -1;
1232
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001233 pthread_mutex_lock(&in->lock);
1234 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001235 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001236 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001237 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001238 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001239 goto exit;
1240 }
1241 in->standby = 0;
1242 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001243
1244 if (in->pcm) {
1245 ret = pcm_read(in->pcm, buffer, bytes);
1246 }
1247
1248 /*
1249 * Instead of writing zeroes here, we could trust the hardware
1250 * to always provide zeroes when muted.
1251 */
1252 if (ret == 0 && adev->mic_mute)
1253 memset(buffer, 0, bytes);
1254
1255exit:
1256 pthread_mutex_unlock(&in->lock);
1257
1258 if (ret != 0) {
1259 in_standby(&in->stream.common);
1260 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1261 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1262 in_get_sample_rate(&in->stream.common));
1263 }
1264 return bytes;
1265}
1266
1267static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1268{
1269 return 0;
1270}
1271
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001272static int add_remove_audio_effect(const struct audio_stream *stream,
1273 effect_handle_t effect,
1274 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001275{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001276 struct stream_in *in = (struct stream_in *)stream;
1277 int status = 0;
1278 effect_descriptor_t desc;
1279
1280 status = (*effect)->get_descriptor(effect, &desc);
1281 if (status != 0)
1282 return status;
1283
1284 pthread_mutex_lock(&in->lock);
1285 pthread_mutex_lock(&in->dev->lock);
1286 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1287 in->enable_aec != enable &&
1288 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1289 in->enable_aec = enable;
1290 if (!in->standby)
1291 select_devices(in->dev, in->usecase);
1292 }
1293 pthread_mutex_unlock(&in->dev->lock);
1294 pthread_mutex_unlock(&in->lock);
1295
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001296 return 0;
1297}
1298
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001299static int in_add_audio_effect(const struct audio_stream *stream,
1300 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001301{
Eric Laurent994a6932013-07-17 11:51:42 -07001302 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001303 return add_remove_audio_effect(stream, effect, true);
1304}
1305
1306static int in_remove_audio_effect(const struct audio_stream *stream,
1307 effect_handle_t effect)
1308{
Eric Laurent994a6932013-07-17 11:51:42 -07001309 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001310 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001311}
1312
1313static int adev_open_output_stream(struct audio_hw_device *dev,
1314 audio_io_handle_t handle,
1315 audio_devices_t devices,
1316 audio_output_flags_t flags,
1317 struct audio_config *config,
1318 struct audio_stream_out **stream_out)
1319{
1320 struct audio_device *adev = (struct audio_device *)dev;
1321 struct stream_out *out;
1322 int i, ret;
1323
Eric Laurent994a6932013-07-17 11:51:42 -07001324 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001325 __func__, config->sample_rate, config->channel_mask, devices, flags);
1326 *stream_out = NULL;
1327 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1328
1329 if (devices == AUDIO_DEVICE_NONE)
1330 devices = AUDIO_DEVICE_OUT_SPEAKER;
1331
1332 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
1333 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1334 out->flags = flags;
1335 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001336 out->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001337
1338 /* Init use case and pcm_config */
1339 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
1340 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001341 pthread_mutex_lock(&adev->lock);
1342 ret = read_hdmi_channel_masks(out);
1343 pthread_mutex_unlock(&adev->lock);
1344 if (ret != 0) {
1345 /* If HDMI does not support multi channel playback, set the default */
1346 out->config.channels = popcount(out->channel_mask);
Eric Laurentb23d5282013-05-14 15:27:20 -07001347 platform_set_hdmi_channels(adev->platform, out->config.channels);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001348 goto error_open;
1349 }
1350
1351 if (config->sample_rate == 0)
1352 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1353 if (config->channel_mask == 0)
1354 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1355
1356 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001357 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
1358 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001359 out->config.rate = config->sample_rate;
1360 out->config.channels = popcount(out->channel_mask);
1361 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Eric Laurentb23d5282013-05-14 15:27:20 -07001362 platform_set_hdmi_channels(adev->platform, out->config.channels);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001363 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
1364 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
1365 out->config = pcm_config_deep_buffer;
1366 } else {
1367 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
1368 out->config = pcm_config_low_latency;
1369 }
1370
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001371 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1372 if(adev->primary_output == NULL)
1373 adev->primary_output = out;
1374 else {
1375 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001376 ret = -EEXIST;
1377 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001378 }
1379 }
1380
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001381 /* Check if this usecase is already existing */
1382 pthread_mutex_lock(&adev->lock);
1383 if (get_usecase_from_list(adev, out->usecase) != NULL) {
1384 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001385 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001386 ret = -EEXIST;
1387 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001388 }
1389 pthread_mutex_unlock(&adev->lock);
1390
1391 out->stream.common.get_sample_rate = out_get_sample_rate;
1392 out->stream.common.set_sample_rate = out_set_sample_rate;
1393 out->stream.common.get_buffer_size = out_get_buffer_size;
1394 out->stream.common.get_channels = out_get_channels;
1395 out->stream.common.get_format = out_get_format;
1396 out->stream.common.set_format = out_set_format;
1397 out->stream.common.standby = out_standby;
1398 out->stream.common.dump = out_dump;
1399 out->stream.common.set_parameters = out_set_parameters;
1400 out->stream.common.get_parameters = out_get_parameters;
1401 out->stream.common.add_audio_effect = out_add_audio_effect;
1402 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1403 out->stream.get_latency = out_get_latency;
1404 out->stream.set_volume = out_set_volume;
1405 out->stream.write = out_write;
1406 out->stream.get_render_position = out_get_render_position;
1407 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
1408
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001409 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07001410 /* out->muted = false; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001411
1412 config->format = out->stream.common.get_format(&out->stream.common);
1413 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
1414 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
1415
1416 *stream_out = &out->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07001417 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001418 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001419
1420error_open:
1421 free(out);
1422 *stream_out = NULL;
1423 ALOGD("%s: exit: ret %d", __func__, ret);
1424 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001425}
1426
1427static void adev_close_output_stream(struct audio_hw_device *dev,
1428 struct audio_stream_out *stream)
1429{
Eric Laurent994a6932013-07-17 11:51:42 -07001430 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001431 out_standby(&stream->common);
1432 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07001433 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001434}
1435
1436static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
1437{
1438 struct audio_device *adev = (struct audio_device *)dev;
1439 struct str_parms *parms;
1440 char *str;
1441 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07001442 int val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001443 int ret;
1444
Eric Laurent994a6932013-07-17 11:51:42 -07001445 ALOGV("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001446
1447 parms = str_parms_create_str(kvpairs);
1448 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value));
1449 if (ret >= 0) {
1450 int tty_mode;
1451
1452 if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0)
1453 tty_mode = TTY_MODE_OFF;
1454 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0)
1455 tty_mode = TTY_MODE_VCO;
1456 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0)
1457 tty_mode = TTY_MODE_HCO;
1458 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0)
1459 tty_mode = TTY_MODE_FULL;
1460 else
1461 return -EINVAL;
1462
1463 pthread_mutex_lock(&adev->lock);
1464 if (tty_mode != adev->tty_mode) {
1465 adev->tty_mode = tty_mode;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08001466 adev->acdb_settings = (adev->acdb_settings & TTY_MODE_CLEAR) | tty_mode;
1467 if (adev->in_call)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001468 select_devices(adev, USECASE_VOICE_CALL);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001469 }
1470 pthread_mutex_unlock(&adev->lock);
1471 }
1472
1473 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
1474 if (ret >= 0) {
1475 /* When set to false, HAL should disable EC and NS
1476 * But it is currently not supported.
1477 */
1478 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
1479 adev->bluetooth_nrec = true;
1480 else
1481 adev->bluetooth_nrec = false;
1482 }
1483
1484 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
1485 if (ret >= 0) {
1486 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
1487 adev->screen_off = false;
1488 else
1489 adev->screen_off = true;
1490 }
1491
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07001492 ret = str_parms_get_int(parms, "rotation", &val);
1493 if (ret >= 0) {
1494 bool reverse_speakers = false;
1495 switch(val) {
1496 // FIXME: note that the code below assumes that the speakers are in the correct placement
1497 // relative to the user when the device is rotated 90deg from its default rotation. This
1498 // assumption is device-specific, not platform-specific like this code.
1499 case 270:
1500 reverse_speakers = true;
1501 break;
1502 case 0:
1503 case 90:
1504 case 180:
1505 break;
1506 default:
1507 ALOGE("%s: unexpected rotation of %d", __func__, val);
1508 }
1509 pthread_mutex_lock(&adev->lock);
1510 if (adev->speaker_lr_swap != reverse_speakers) {
1511 adev->speaker_lr_swap = reverse_speakers;
1512 // only update the selected device if there is active pcm playback
1513 struct audio_usecase *usecase;
1514 struct listnode *node;
1515 list_for_each(node, &adev->usecase_list) {
1516 usecase = node_to_item(node, struct audio_usecase, list);
1517 if (usecase->type == PCM_PLAYBACK) {
1518 select_devices(adev, usecase->id);
1519 break;
1520 }
1521 }
1522 }
1523 pthread_mutex_unlock(&adev->lock);
1524 }
1525
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07001526 audio_extn_set_parameters(adev, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001527 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001528 ALOGV("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001529 return ret;
1530}
1531
1532static char* adev_get_parameters(const struct audio_hw_device *dev,
1533 const char *keys)
1534{
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07001535 return audio_extn_get_parameters(dev, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001536}
1537
1538static int adev_init_check(const struct audio_hw_device *dev)
1539{
1540 return 0;
1541}
1542
1543static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
1544{
1545 struct audio_device *adev = (struct audio_device *)dev;
1546 int vol, err = 0;
1547
1548 pthread_mutex_lock(&adev->lock);
1549 adev->voice_volume = volume;
1550 if (adev->mode == AUDIO_MODE_IN_CALL) {
1551 if (volume < 0.0) {
1552 volume = 0.0;
1553 } else if (volume > 1.0) {
1554 volume = 1.0;
1555 }
1556
1557 vol = lrint(volume * 100.0);
1558
1559 // Voice volume levels from android are mapped to driver volume levels as follows.
1560 // 0 -> 5, 20 -> 4, 40 ->3, 60 -> 2, 80 -> 1, 100 -> 0
1561 // So adjust the volume to get the correct volume index in driver
1562 vol = 100 - vol;
Eric Laurentb23d5282013-05-14 15:27:20 -07001563
1564 err = platform_set_voice_volume(adev->platform, vol);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001565 }
1566 pthread_mutex_unlock(&adev->lock);
1567 return err;
1568}
1569
1570static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
1571{
1572 return -ENOSYS;
1573}
1574
1575static int adev_get_master_volume(struct audio_hw_device *dev,
1576 float *volume)
1577{
1578 return -ENOSYS;
1579}
1580
1581static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
1582{
1583 return -ENOSYS;
1584}
1585
1586static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
1587{
1588 return -ENOSYS;
1589}
1590
1591static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
1592{
1593 struct audio_device *adev = (struct audio_device *)dev;
1594
1595 pthread_mutex_lock(&adev->lock);
1596 if (adev->mode != mode) {
1597 adev->mode = mode;
1598 }
1599 pthread_mutex_unlock(&adev->lock);
1600 return 0;
1601}
1602
1603static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
1604{
1605 struct audio_device *adev = (struct audio_device *)dev;
1606 int err = 0;
1607
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001608 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001609 adev->mic_mute = state;
Eric Laurentb23d5282013-05-14 15:27:20 -07001610
1611 err = platform_set_mic_mute(adev->platform, state);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001612 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001613 return err;
1614}
1615
1616static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1617{
1618 struct audio_device *adev = (struct audio_device *)dev;
1619
1620 *state = adev->mic_mute;
1621
1622 return 0;
1623}
1624
1625static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
1626 const struct audio_config *config)
1627{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001628 int channel_count = popcount(config->channel_mask);
1629
1630 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
1631}
1632
1633static int adev_open_input_stream(struct audio_hw_device *dev,
1634 audio_io_handle_t handle,
1635 audio_devices_t devices,
1636 struct audio_config *config,
1637 struct audio_stream_in **stream_in)
1638{
1639 struct audio_device *adev = (struct audio_device *)dev;
1640 struct stream_in *in;
1641 int ret, buffer_size, frame_size;
1642 int channel_count = popcount(config->channel_mask);
1643
Eric Laurent994a6932013-07-17 11:51:42 -07001644 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001645 *stream_in = NULL;
1646 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
1647 return -EINVAL;
1648
1649 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
1650
1651 in->stream.common.get_sample_rate = in_get_sample_rate;
1652 in->stream.common.set_sample_rate = in_set_sample_rate;
1653 in->stream.common.get_buffer_size = in_get_buffer_size;
1654 in->stream.common.get_channels = in_get_channels;
1655 in->stream.common.get_format = in_get_format;
1656 in->stream.common.set_format = in_set_format;
1657 in->stream.common.standby = in_standby;
1658 in->stream.common.dump = in_dump;
1659 in->stream.common.set_parameters = in_set_parameters;
1660 in->stream.common.get_parameters = in_get_parameters;
1661 in->stream.common.add_audio_effect = in_add_audio_effect;
1662 in->stream.common.remove_audio_effect = in_remove_audio_effect;
1663 in->stream.set_gain = in_set_gain;
1664 in->stream.read = in_read;
1665 in->stream.get_input_frames_lost = in_get_input_frames_lost;
1666
1667 in->device = devices;
1668 in->source = AUDIO_SOURCE_DEFAULT;
1669 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001670 in->standby = 1;
1671 in->channel_mask = config->channel_mask;
1672
1673 /* Update config params with the requested sample rate and channels */
1674 in->usecase = USECASE_AUDIO_RECORD;
1675 in->config = pcm_config_audio_capture;
1676 in->config.channels = channel_count;
1677 in->config.rate = config->sample_rate;
1678
1679 frame_size = audio_stream_frame_size((struct audio_stream *)in);
1680 buffer_size = get_input_buffer_size(config->sample_rate,
1681 config->format,
1682 channel_count);
1683 in->config.period_size = buffer_size / frame_size;
1684
1685 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07001686 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001687 return 0;
1688
1689err_open:
1690 free(in);
1691 *stream_in = NULL;
1692 return ret;
1693}
1694
1695static void adev_close_input_stream(struct audio_hw_device *dev,
1696 struct audio_stream_in *stream)
1697{
Eric Laurent994a6932013-07-17 11:51:42 -07001698 ALOGV("%s", __func__);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001699
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001700 in_standby(&stream->common);
1701 free(stream);
1702
1703 return;
1704}
1705
1706static int adev_dump(const audio_hw_device_t *device, int fd)
1707{
1708 return 0;
1709}
1710
1711static int adev_close(hw_device_t *device)
1712{
1713 struct audio_device *adev = (struct audio_device *)device;
1714 audio_route_free(adev->audio_route);
Eric Laurentb23d5282013-05-14 15:27:20 -07001715 free(adev->snd_dev_ref_cnt);
1716 platform_deinit(adev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001717 free(device);
1718 return 0;
1719}
1720
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001721static int adev_open(const hw_module_t *module, const char *name,
1722 hw_device_t **device)
1723{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001724 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001725
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001726 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001727 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
1728
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001729 pthread_mutex_lock(&adev_init_lock);
1730 if (is_adev_initialised == true){
1731 *device = &adev->device.common;
1732 ALOGD("%s: returning existing instance of adev", __func__);
1733 ALOGD("%s: exit", __func__);
1734 pthread_mutex_unlock(&adev_init_lock);
1735 return 0;
1736 }
1737
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001738 adev = calloc(1, sizeof(struct audio_device));
1739
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001740 adev->device.common.tag = HARDWARE_DEVICE_TAG;
1741 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
1742 adev->device.common.module = (struct hw_module_t *)module;
1743 adev->device.common.close = adev_close;
1744
1745 adev->device.init_check = adev_init_check;
1746 adev->device.set_voice_volume = adev_set_voice_volume;
1747 adev->device.set_master_volume = adev_set_master_volume;
1748 adev->device.get_master_volume = adev_get_master_volume;
1749 adev->device.set_master_mute = adev_set_master_mute;
1750 adev->device.get_master_mute = adev_get_master_mute;
1751 adev->device.set_mode = adev_set_mode;
1752 adev->device.set_mic_mute = adev_set_mic_mute;
1753 adev->device.get_mic_mute = adev_get_mic_mute;
1754 adev->device.set_parameters = adev_set_parameters;
1755 adev->device.get_parameters = adev_get_parameters;
1756 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
1757 adev->device.open_output_stream = adev_open_output_stream;
1758 adev->device.close_output_stream = adev_close_output_stream;
1759 adev->device.open_input_stream = adev_open_input_stream;
1760 adev->device.close_input_stream = adev_close_input_stream;
1761 adev->device.dump = adev_dump;
1762
1763 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001764 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08001765 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001766 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001767 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001768 adev->voice_call_rx = NULL;
1769 adev->voice_call_tx = NULL;
1770 adev->voice_volume = 1.0f;
1771 adev->tty_mode = TTY_MODE_OFF;
1772 adev->bluetooth_nrec = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001773 adev->in_call = false;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08001774 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurentb23d5282013-05-14 15:27:20 -07001775 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001776 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001777
1778 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07001779 adev->platform = platform_init(adev);
1780 if (!adev->platform) {
1781 free(adev->snd_dev_ref_cnt);
1782 free(adev);
1783 ALOGE("%s: Failed to init platform data, aborting.", __func__);
1784 *device = NULL;
1785 return -EINVAL;
1786 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001787 *device = &adev->device.common;
1788
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001789 /* update init flag*/
1790 is_adev_initialised = true;
1791 pthread_mutex_unlock(&adev_init_lock);
1792
Eric Laurent994a6932013-07-17 11:51:42 -07001793 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001794 return 0;
1795}
1796
1797static struct hw_module_methods_t hal_module_methods = {
1798 .open = adev_open,
1799};
1800
1801struct audio_module HAL_MODULE_INFO_SYM = {
1802 .common = {
1803 .tag = HARDWARE_MODULE_TAG,
1804 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1805 .hal_api_version = HARDWARE_HAL_API_VERSION,
1806 .id = AUDIO_HARDWARE_MODULE_ID,
1807 .name = "QCOM Audio HAL",
1808 .author = "Code Aurora Forum",
1809 .methods = &hal_module_methods,
1810 },
1811};