blob: 7a65925bc8c691a279d6dfb971d3c31c8343b000 [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
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700189 /* start usb playback thread */
190 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
191 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
192 audio_extn_usb_start_playback(adev);
193
194 /* start usb capture thread */
195 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
196 audio_extn_usb_start_capture(adev);
197
Eric Laurentb23d5282013-05-14 15:27:20 -0700198 if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700199 adev->snd_dev_ref_cnt[snd_device]--;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800200 return -EINVAL;
201 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800202
Eric Laurent994a6932013-07-17 11:51:42 -0700203 ALOGV("%s: snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700204 snd_device, platform_get_snd_device_name(snd_device));
205 audio_route_apply_path(adev->audio_route, platform_get_snd_device_name(snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700206 if (update_mixer)
207 audio_route_update_mixer(adev->audio_route);
208
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800209 return 0;
210}
211
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700212int disable_snd_device(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700213 snd_device_t snd_device,
214 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800215{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800216 if (snd_device < SND_DEVICE_MIN ||
217 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800218 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800219 return -EINVAL;
220 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700221 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
222 ALOGE("%s: device ref cnt is already 0", __func__);
223 return -EINVAL;
224 }
225 adev->snd_dev_ref_cnt[snd_device]--;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700226
227 /* exit usb play back thread */
228 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
229 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
230 audio_extn_usb_stop_playback();
231
232 /* exit usb capture thread */
233 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
234 audio_extn_usb_stop_capture(adev);
235
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700236 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Eric Laurent994a6932013-07-17 11:51:42 -0700237 ALOGV("%s: snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700238 snd_device, platform_get_snd_device_name(snd_device));
239 audio_route_reset_path(adev->audio_route, platform_get_snd_device_name(snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700240 if (update_mixer)
241 audio_route_update_mixer(adev->audio_route);
242 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800243 return 0;
244}
245
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700246static void check_usecases_codec_backend(struct audio_device *adev,
247 struct audio_usecase *uc_info,
248 snd_device_t snd_device)
249{
250 struct listnode *node;
251 struct audio_usecase *usecase;
252 bool switch_device[AUDIO_USECASE_MAX];
253 int i, num_uc_to_switch = 0;
254
255 /*
256 * This function is to make sure that all the usecases that are active on
257 * the hardware codec backend are always routed to any one device that is
258 * handled by the hardware codec.
259 * For example, if low-latency and deep-buffer usecases are currently active
260 * on speaker and out_set_parameters(headset) is received on low-latency
261 * output, then we have to make sure deep-buffer is also switched to headset,
262 * because of the limitation that both the devices cannot be enabled
263 * at the same time as they share the same backend.
264 */
265 /* Disable all the usecases on the shared backend other than the
266 specified usecase */
267 for (i = 0; i < AUDIO_USECASE_MAX; i++)
268 switch_device[i] = false;
269
270 list_for_each(node, &adev->usecase_list) {
271 usecase = node_to_item(node, struct audio_usecase, list);
272 if (usecase->type != PCM_CAPTURE &&
273 usecase != uc_info &&
274 usecase->out_snd_device != snd_device &&
275 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
276 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
277 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700278 platform_get_snd_device_name(usecase->out_snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700279 disable_audio_route(adev, usecase, false);
280 switch_device[usecase->id] = true;
281 num_uc_to_switch++;
282 }
283 }
284
285 if (num_uc_to_switch) {
286 /* Make sure all the streams are de-routed before disabling the device */
287 audio_route_update_mixer(adev->audio_route);
288
289 list_for_each(node, &adev->usecase_list) {
290 usecase = node_to_item(node, struct audio_usecase, list);
291 if (switch_device[usecase->id]) {
292 disable_snd_device(adev, usecase->out_snd_device, false);
293 enable_snd_device(adev, snd_device, false);
294 }
295 }
296
297 /* Make sure new snd device is enabled before re-routing the streams */
298 audio_route_update_mixer(adev->audio_route);
299
300 /* Re-route all the usecases on the shared backend other than the
301 specified usecase to new snd devices */
302 list_for_each(node, &adev->usecase_list) {
303 usecase = node_to_item(node, struct audio_usecase, list);
304 /* Update the out_snd_device only before enabling the audio route */
305 if (switch_device[usecase->id] ) {
306 usecase->out_snd_device = snd_device;
307 enable_audio_route(adev, usecase, false);
308 }
309 }
310
311 audio_route_update_mixer(adev->audio_route);
312 }
313}
314
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700315static void check_and_route_capture_usecases(struct audio_device *adev,
316 struct audio_usecase *uc_info,
317 snd_device_t snd_device)
318{
319 struct listnode *node;
320 struct audio_usecase *usecase;
321 bool switch_device[AUDIO_USECASE_MAX];
322 int i, num_uc_to_switch = 0;
323
324 /*
325 * This function is to make sure that all the active capture usecases
326 * are always routed to the same input sound device.
327 * For example, if audio-record and voice-call usecases are currently
328 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
329 * is received for voice call then we have to make sure that audio-record
330 * usecase is also switched to earpiece i.e. voice-dmic-ef,
331 * because of the limitation that two devices cannot be enabled
332 * at the same time if they share the same backend.
333 */
334 for (i = 0; i < AUDIO_USECASE_MAX; i++)
335 switch_device[i] = false;
336
337 list_for_each(node, &adev->usecase_list) {
338 usecase = node_to_item(node, struct audio_usecase, list);
339 if (usecase->type != PCM_PLAYBACK &&
340 usecase != uc_info &&
341 usecase->in_snd_device != snd_device) {
342 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
343 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700344 platform_get_snd_device_name(usecase->in_snd_device));
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700345 disable_audio_route(adev, usecase, false);
346 switch_device[usecase->id] = true;
347 num_uc_to_switch++;
348 }
349 }
350
351 if (num_uc_to_switch) {
352 /* Make sure all the streams are de-routed before disabling the device */
353 audio_route_update_mixer(adev->audio_route);
354
355 list_for_each(node, &adev->usecase_list) {
356 usecase = node_to_item(node, struct audio_usecase, list);
357 if (switch_device[usecase->id]) {
358 disable_snd_device(adev, usecase->in_snd_device, false);
359 enable_snd_device(adev, snd_device, false);
360 }
361 }
362
363 /* Make sure new snd device is enabled before re-routing the streams */
364 audio_route_update_mixer(adev->audio_route);
365
366 /* Re-route all the usecases on the shared backend other than the
367 specified usecase to new snd devices */
368 list_for_each(node, &adev->usecase_list) {
369 usecase = node_to_item(node, struct audio_usecase, list);
370 /* Update the in_snd_device only before enabling the audio route */
371 if (switch_device[usecase->id] ) {
372 usecase->in_snd_device = snd_device;
373 enable_audio_route(adev, usecase, false);
374 }
375 }
376
377 audio_route_update_mixer(adev->audio_route);
378 }
379}
380
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800381
382/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700383static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800384{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700385 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700386 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800387
388 switch (channels) {
389 /*
390 * Do not handle stereo output in Multi-channel cases
391 * Stereo case is handled in normal playback path
392 */
393 case 6:
394 ALOGV("%s: HDMI supports 5.1", __func__);
395 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
396 break;
397 case 8:
398 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
399 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
400 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
401 break;
402 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700403 ALOGE("HDMI does not support multi channel playback");
404 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800405 break;
406 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700407 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800408}
409
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700410struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700411 audio_usecase_t uc_id)
412{
413 struct audio_usecase *usecase;
414 struct listnode *node;
415
416 list_for_each(node, &adev->usecase_list) {
417 usecase = node_to_item(node, struct audio_usecase, list);
418 if (usecase->id == uc_id)
419 return usecase;
420 }
421 return NULL;
422}
423
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700424int select_devices(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700425 audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800426{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800427 snd_device_t out_snd_device = SND_DEVICE_NONE;
428 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700429 struct audio_usecase *usecase = NULL;
430 struct audio_usecase *vc_usecase = NULL;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800431 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700432 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800433
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700434 usecase = get_usecase_from_list(adev, uc_id);
435 if (usecase == NULL) {
436 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
437 return -EINVAL;
438 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800439
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700440 if (usecase->type == VOICE_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700441 out_snd_device = platform_get_output_snd_device(adev->platform,
442 usecase->stream.out->devices);
443 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700444 usecase->devices = usecase->stream.out->devices;
445 } else {
446 /*
447 * If the voice call is active, use the sound devices of voice call usecase
448 * so that it would not result any device switch. All the usecases will
449 * be switched to new device when select_devices() is called for voice call
450 * usecase. This is to avoid switching devices for voice call when
451 * check_usecases_codec_backend() is called below.
452 */
453 if (adev->in_call) {
454 vc_usecase = get_usecase_from_list(adev, USECASE_VOICE_CALL);
455 if (vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
456 in_snd_device = vc_usecase->in_snd_device;
457 out_snd_device = vc_usecase->out_snd_device;
458 }
459 }
460 if (usecase->type == PCM_PLAYBACK) {
461 usecase->devices = usecase->stream.out->devices;
462 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700463 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700464 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700465 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700466 if (usecase->stream.out == adev->primary_output &&
467 adev->active_input &&
468 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
469 select_devices(adev, adev->active_input->usecase);
470 }
471 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700472 } else if (usecase->type == PCM_CAPTURE) {
473 usecase->devices = usecase->stream.in->device;
474 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700475 if (in_snd_device == SND_DEVICE_NONE) {
476 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
477 adev->primary_output && !adev->primary_output->standby) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700478 in_snd_device = platform_get_input_snd_device(adev->platform,
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700479 adev->primary_output->devices);
480 } else {
Eric Laurentb23d5282013-05-14 15:27:20 -0700481 in_snd_device = platform_get_input_snd_device(adev->platform,
482 AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700483 }
484 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700485 }
486 }
487
488 if (out_snd_device == usecase->out_snd_device &&
489 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800490 return 0;
491 }
492
sangwoobc677242013-08-08 16:53:43 +0900493 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700494 out_snd_device, platform_get_snd_device_name(out_snd_device),
495 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800496
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800497 /*
498 * Limitation: While in call, to do a device switch we need to disable
499 * and enable both RX and TX devices though one of them is same as current
500 * device.
501 */
Eric Laurentb23d5282013-05-14 15:27:20 -0700502 if (usecase->type == VOICE_CALL) {
503 status = platform_switch_voice_call_device_pre(adev->platform);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800504 }
505
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700506 /* Disable current sound devices */
507 if (usecase->out_snd_device != SND_DEVICE_NONE) {
508 disable_audio_route(adev, usecase, true);
509 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800510 }
511
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700512 if (usecase->in_snd_device != SND_DEVICE_NONE) {
513 disable_audio_route(adev, usecase, true);
514 disable_snd_device(adev, usecase->in_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800515 }
516
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700517 /* Enable new sound devices */
518 if (out_snd_device != SND_DEVICE_NONE) {
519 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
520 check_usecases_codec_backend(adev, usecase, out_snd_device);
521 enable_snd_device(adev, out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800522 }
523
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700524 if (in_snd_device != SND_DEVICE_NONE) {
525 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700526 enable_snd_device(adev, in_snd_device, false);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700527 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700528
Eric Laurentb23d5282013-05-14 15:27:20 -0700529 if (usecase->type == VOICE_CALL)
530 status = platform_switch_voice_call_device_post(adev->platform,
531 out_snd_device,
532 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800533
sangwoo170731f2013-06-08 15:36:36 +0900534 audio_route_update_mixer(adev->audio_route);
535
536 usecase->in_snd_device = in_snd_device;
537 usecase->out_snd_device = out_snd_device;
538
539 enable_audio_route(adev, usecase, true);
540
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800541 return status;
542}
543
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800544static int stop_input_stream(struct stream_in *in)
545{
546 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800547 struct audio_usecase *uc_info;
548 struct audio_device *adev = in->dev;
549
Eric Laurentc8400632013-02-14 19:04:54 -0800550 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800551
Eric Laurent994a6932013-07-17 11:51:42 -0700552 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700553 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800554 uc_info = get_usecase_from_list(adev, in->usecase);
555 if (uc_info == NULL) {
556 ALOGE("%s: Could not find the usecase (%d) in the list",
557 __func__, in->usecase);
558 return -EINVAL;
559 }
560
Eric Laurent150dbfe2013-02-27 14:31:02 -0800561 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700562 disable_audio_route(adev, uc_info, true);
563
564 /* 2. Disable the tx device */
565 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800566
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800567 list_remove(&uc_info->list);
568 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800569
Eric Laurent994a6932013-07-17 11:51:42 -0700570 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800571 return ret;
572}
573
574int start_input_stream(struct stream_in *in)
575{
576 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800577 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800578 struct audio_usecase *uc_info;
579 struct audio_device *adev = in->dev;
580
Eric Laurent994a6932013-07-17 11:51:42 -0700581 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
Eric Laurentb23d5282013-05-14 15:27:20 -0700582 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800583 if (in->pcm_device_id < 0) {
584 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
585 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800586 ret = -EINVAL;
587 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800588 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700589
590 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800591 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
592 uc_info->id = in->usecase;
593 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800594 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700595 uc_info->devices = in->device;
596 uc_info->in_snd_device = SND_DEVICE_NONE;
597 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800598
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800599 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700600 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800601
Eric Laurentc8400632013-02-14 19:04:54 -0800602 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
603 __func__, SOUND_CARD, in->pcm_device_id, in->config.channels);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800604 in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id,
605 PCM_IN, &in->config);
606 if (in->pcm && !pcm_is_ready(in->pcm)) {
607 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
608 pcm_close(in->pcm);
609 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800610 ret = -EIO;
611 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800612 }
Eric Laurent994a6932013-07-17 11:51:42 -0700613 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800614 return ret;
615
616error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800617 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800618
619error_config:
620 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700621 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800622
623 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800624}
625
626static int stop_output_stream(struct stream_out *out)
627{
628 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800629 struct audio_usecase *uc_info;
630 struct audio_device *adev = out->dev;
631
Eric Laurent994a6932013-07-17 11:51:42 -0700632 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700633 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800634 uc_info = get_usecase_from_list(adev, out->usecase);
635 if (uc_info == NULL) {
636 ALOGE("%s: Could not find the usecase (%d) in the list",
637 __func__, out->usecase);
638 return -EINVAL;
639 }
640
Eric Laurent150dbfe2013-02-27 14:31:02 -0800641 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700642 disable_audio_route(adev, uc_info, true);
643
644 /* 2. Disable the rx device */
645 disable_snd_device(adev, uc_info->out_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800646
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800647 list_remove(&uc_info->list);
648 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800649
Eric Laurent994a6932013-07-17 11:51:42 -0700650 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800651 return ret;
652}
653
654int start_output_stream(struct stream_out *out)
655{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800656 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800657 struct audio_usecase *uc_info;
658 struct audio_device *adev = out->dev;
659
Eric Laurent994a6932013-07-17 11:51:42 -0700660 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700661 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Eric Laurentb23d5282013-05-14 15:27:20 -0700662 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800663 if (out->pcm_device_id < 0) {
664 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
665 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800666 ret = -EINVAL;
667 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800668 }
669
670 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
671 uc_info->id = out->usecase;
672 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800673 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700674 uc_info->devices = out->devices;
675 uc_info->in_snd_device = SND_DEVICE_NONE;
676 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800677
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800678 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800679
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700680 select_devices(adev, out->usecase);
681
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800682 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
683 __func__, 0, out->pcm_device_id);
684 out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id,
685 PCM_OUT, &out->config);
686 if (out->pcm && !pcm_is_ready(out->pcm)) {
687 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
688 pcm_close(out->pcm);
689 out->pcm = NULL;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800690 ret = -EIO;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700691 goto error_pcm_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800692 }
Eric Laurent994a6932013-07-17 11:51:42 -0700693 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800694 return 0;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700695error_pcm_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800696 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800697error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800698 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800699}
700
701static int stop_voice_call(struct audio_device *adev)
702{
703 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800704 struct audio_usecase *uc_info;
705
Eric Laurent994a6932013-07-17 11:51:42 -0700706 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -0800707 adev->in_call = false;
Eric Laurentb23d5282013-05-14 15:27:20 -0700708
709 ret = platform_stop_voice_call(adev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800710
711 /* 1. Close the PCM devices */
712 if (adev->voice_call_rx) {
713 pcm_close(adev->voice_call_rx);
714 adev->voice_call_rx = NULL;
715 }
716 if (adev->voice_call_tx) {
717 pcm_close(adev->voice_call_tx);
718 adev->voice_call_tx = NULL;
719 }
720
721 uc_info = get_usecase_from_list(adev, USECASE_VOICE_CALL);
722 if (uc_info == NULL) {
723 ALOGE("%s: Could not find the usecase (%d) in the list",
724 __func__, USECASE_VOICE_CALL);
725 return -EINVAL;
726 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800727
728 /* 2. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700729 disable_audio_route(adev, uc_info, true);
730
731 /* 3. Disable the rx and tx devices */
732 disable_snd_device(adev, uc_info->out_snd_device, false);
733 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800734
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800735 list_remove(&uc_info->list);
736 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800737
Eric Laurent994a6932013-07-17 11:51:42 -0700738 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800739 return ret;
740}
741
742static int start_voice_call(struct audio_device *adev)
743{
744 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800745 struct audio_usecase *uc_info;
746 int pcm_dev_rx_id, pcm_dev_tx_id;
747
Eric Laurent994a6932013-07-17 11:51:42 -0700748 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800749
750 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
751 uc_info->id = USECASE_VOICE_CALL;
752 uc_info->type = VOICE_CALL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800753 uc_info->stream.out = adev->primary_output;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700754 uc_info->devices = adev->primary_output->devices;
755 uc_info->in_snd_device = SND_DEVICE_NONE;
756 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800757
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800758 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800759
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700760 select_devices(adev, USECASE_VOICE_CALL);
761
Eric Laurentb23d5282013-05-14 15:27:20 -0700762 pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK);
763 pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800764
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800765 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
766 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
767 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -0800768 ret = -EIO;
769 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800770 }
771
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -0800772 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800773 __func__, SOUND_CARD, pcm_dev_rx_id);
774 adev->voice_call_rx = pcm_open(SOUND_CARD,
775 pcm_dev_rx_id,
776 PCM_OUT, &pcm_config_voice_call);
777 if (adev->voice_call_rx && !pcm_is_ready(adev->voice_call_rx)) {
778 ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_rx));
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -0800779 ret = -EIO;
780 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800781 }
782
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -0800783 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800784 __func__, SOUND_CARD, pcm_dev_tx_id);
785 adev->voice_call_tx = pcm_open(SOUND_CARD,
786 pcm_dev_tx_id,
787 PCM_IN, &pcm_config_voice_call);
788 if (adev->voice_call_tx && !pcm_is_ready(adev->voice_call_tx)) {
789 ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_tx));
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -0800790 ret = -EIO;
791 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800792 }
793 pcm_start(adev->voice_call_rx);
794 pcm_start(adev->voice_call_tx);
795
Eric Laurentb23d5282013-05-14 15:27:20 -0700796 ret = platform_start_voice_call(adev->platform);
797 if (ret < 0) {
798 ALOGE("%s: platform_start_voice_call error %d\n", __func__, ret);
799 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800800 }
801
802 adev->in_call = true;
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -0800803 return 0;
804
805error_start_voice:
806 stop_voice_call(adev);
807
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800808 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800809 return ret;
810}
811
812static int check_input_parameters(uint32_t sample_rate,
813 audio_format_t format,
814 int channel_count)
815{
816 if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
817
818 if ((channel_count < 1) || (channel_count > 2)) return -EINVAL;
819
820 switch (sample_rate) {
821 case 8000:
822 case 11025:
823 case 12000:
824 case 16000:
825 case 22050:
826 case 24000:
827 case 32000:
828 case 44100:
829 case 48000:
830 break;
831 default:
832 return -EINVAL;
833 }
834
835 return 0;
836}
837
838static size_t get_input_buffer_size(uint32_t sample_rate,
839 audio_format_t format,
840 int channel_count)
841{
842 size_t size = 0;
843
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -0700844 if (check_input_parameters(sample_rate, format, channel_count) != 0)
845 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800846
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -0700847 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
848 /* ToDo: should use frame_size computed based on the format and
849 channel_count here. */
850 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800851
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -0700852 /* make sure the size is multiple of 64 */
853 size += 0x3f;
854 size &= ~0x3f;
855
856 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800857}
858
859static uint32_t out_get_sample_rate(const struct audio_stream *stream)
860{
861 struct stream_out *out = (struct stream_out *)stream;
862
863 return out->config.rate;
864}
865
866static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
867{
868 return -ENOSYS;
869}
870
871static size_t out_get_buffer_size(const struct audio_stream *stream)
872{
873 struct stream_out *out = (struct stream_out *)stream;
874
875 return out->config.period_size * audio_stream_frame_size(stream);
876}
877
878static uint32_t out_get_channels(const struct audio_stream *stream)
879{
880 struct stream_out *out = (struct stream_out *)stream;
881
882 return out->channel_mask;
883}
884
885static audio_format_t out_get_format(const struct audio_stream *stream)
886{
887 return AUDIO_FORMAT_PCM_16_BIT;
888}
889
890static int out_set_format(struct audio_stream *stream, audio_format_t format)
891{
892 return -ENOSYS;
893}
894
895static int out_standby(struct audio_stream *stream)
896{
897 struct stream_out *out = (struct stream_out *)stream;
898 struct audio_device *adev = out->dev;
Eric Laurent994a6932013-07-17 11:51:42 -0700899 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700900 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800901 pthread_mutex_lock(&out->lock);
902
903 if (!out->standby) {
904 out->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -0800905 if (out->pcm) {
906 pcm_close(out->pcm);
907 out->pcm = NULL;
908 }
909 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800910 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -0800911 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800912 }
913 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -0700914 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800915 return 0;
916}
917
918static int out_dump(const struct audio_stream *stream, int fd)
919{
920 return 0;
921}
922
923static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
924{
925 struct stream_out *out = (struct stream_out *)stream;
926 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800927 struct audio_usecase *usecase;
928 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800929 struct str_parms *parms;
930 char value[32];
931 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800932 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800933
sangwoobc677242013-08-08 16:53:43 +0900934 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700935 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800936 parms = str_parms_create_str(kvpairs);
937 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
938 if (ret >= 0) {
939 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800940 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -0800941 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800942
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700943 /*
944 * When HDMI cable is unplugged the music playback is paused and
945 * the policy manager sends routing=0. But the audioflinger
946 * continues to write data until standby time (3sec).
947 * As the HDMI core is turned off, the write gets blocked.
948 * Avoid this by routing audio to speaker until standby.
949 */
950 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
951 val == AUDIO_DEVICE_NONE) {
952 val = AUDIO_DEVICE_OUT_SPEAKER;
953 }
954
955 /*
956 * select_devices() call below switches all the usecases on the same
957 * backend to the new device. Refer to check_usecases_codec_backend() in
958 * the select_devices(). But how do we undo this?
959 *
960 * For example, music playback is active on headset (deep-buffer usecase)
961 * and if we go to ringtones and select a ringtone, low-latency usecase
962 * will be started on headset+speaker. As we can't enable headset+speaker
963 * and headset devices at the same time, select_devices() switches the music
964 * playback to headset+speaker while starting low-lateny usecase for ringtone.
965 * So when the ringtone playback is completed, how do we undo the same?
966 *
967 * We are relying on the out_set_parameters() call on deep-buffer output,
968 * once the ringtone playback is ended.
969 * NOTE: We should not check if the current devices are same as new devices.
970 * Because select_devices() must be called to switch back the music
971 * playback to headset.
972 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800973 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700974 out->devices = val;
975
976 if (!out->standby)
977 select_devices(adev, out->usecase);
978
979 if ((adev->mode == AUDIO_MODE_IN_CALL) && !adev->in_call &&
980 (out == adev->primary_output)) {
981 start_voice_call(adev);
982 } else if ((adev->mode == AUDIO_MODE_IN_CALL) && adev->in_call &&
983 (out == adev->primary_output)) {
984 select_devices(adev, USECASE_VOICE_CALL);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800985 }
986 }
987
Ravi Kumar Alamanda60caf202013-10-02 09:56:18 -0700988 if ((adev->mode == AUDIO_MODE_NORMAL) && adev->in_call &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700989 (out == adev->primary_output)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800990 stop_voice_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800991 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800992
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800993 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -0800994 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800995 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700996
997 if (out == adev->primary_output) {
998 pthread_mutex_lock(&adev->lock);
999 audio_extn_set_parameters(adev, parms);
1000 pthread_mutex_unlock(&adev->lock);
1001 }
1002
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001003 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001004 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001005 return ret;
1006}
1007
1008static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1009{
1010 struct stream_out *out = (struct stream_out *)stream;
1011 struct str_parms *query = str_parms_create_str(keys);
1012 char *str;
1013 char value[256];
1014 struct str_parms *reply = str_parms_create();
1015 size_t i, j;
1016 int ret;
1017 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -07001018 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001019 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1020 if (ret >= 0) {
1021 value[0] = '\0';
1022 i = 0;
1023 while (out->supported_channel_masks[i] != 0) {
1024 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1025 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1026 if (!first) {
1027 strcat(value, "|");
1028 }
1029 strcat(value, out_channels_name_to_enum_table[j].name);
1030 first = false;
1031 break;
1032 }
1033 }
1034 i++;
1035 }
1036 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1037 str = str_parms_to_str(reply);
1038 } else {
1039 str = strdup(keys);
1040 }
1041 str_parms_destroy(query);
1042 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001043 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001044 return str;
1045}
1046
1047static uint32_t out_get_latency(const struct audio_stream_out *stream)
1048{
1049 struct stream_out *out = (struct stream_out *)stream;
1050
1051 return (out->config.period_count * out->config.period_size * 1000) / (out->config.rate);
1052}
1053
1054static int out_set_volume(struct audio_stream_out *stream, float left,
1055 float right)
1056{
Eric Laurenta9024de2013-04-04 09:19:12 -07001057 struct stream_out *out = (struct stream_out *)stream;
1058 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1059 /* only take left channel into account: the API is for stereo anyway */
1060 out->muted = (left == 0.0f);
1061 return 0;
1062 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001063 return -ENOSYS;
1064}
1065
1066static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1067 size_t bytes)
1068{
1069 struct stream_out *out = (struct stream_out *)stream;
1070 struct audio_device *adev = out->dev;
1071 int i, ret = -1;
1072
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001073 pthread_mutex_lock(&out->lock);
1074 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001075 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001076 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001077 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001078 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001079 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001080 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001081 goto exit;
1082 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001083 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001084
1085 if (out->pcm) {
Eric Laurenta9024de2013-04-04 09:19:12 -07001086 if (out->muted)
1087 memset((void *)buffer, 0, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001088 //ALOGV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1089 ret = pcm_write(out->pcm, (void *)buffer, bytes);
1090 }
1091
1092exit:
1093 pthread_mutex_unlock(&out->lock);
1094
1095 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001096 if (out->pcm)
1097 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001098 out_standby(&out->stream.common);
1099 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1100 out_get_sample_rate(&out->stream.common));
1101 }
1102 return bytes;
1103}
1104
1105static int out_get_render_position(const struct audio_stream_out *stream,
1106 uint32_t *dsp_frames)
1107{
1108 return -EINVAL;
1109}
1110
1111static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1112{
1113 return 0;
1114}
1115
1116static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1117{
1118 return 0;
1119}
1120
1121static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1122 int64_t *timestamp)
1123{
1124 return -EINVAL;
1125}
1126
1127/** audio_stream_in implementation **/
1128static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1129{
1130 struct stream_in *in = (struct stream_in *)stream;
1131
1132 return in->config.rate;
1133}
1134
1135static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1136{
1137 return -ENOSYS;
1138}
1139
1140static size_t in_get_buffer_size(const struct audio_stream *stream)
1141{
1142 struct stream_in *in = (struct stream_in *)stream;
1143
1144 return in->config.period_size * audio_stream_frame_size(stream);
1145}
1146
1147static uint32_t in_get_channels(const struct audio_stream *stream)
1148{
1149 struct stream_in *in = (struct stream_in *)stream;
1150
1151 return in->channel_mask;
1152}
1153
1154static audio_format_t in_get_format(const struct audio_stream *stream)
1155{
1156 return AUDIO_FORMAT_PCM_16_BIT;
1157}
1158
1159static int in_set_format(struct audio_stream *stream, audio_format_t format)
1160{
1161 return -ENOSYS;
1162}
1163
1164static int in_standby(struct audio_stream *stream)
1165{
1166 struct stream_in *in = (struct stream_in *)stream;
1167 struct audio_device *adev = in->dev;
1168 int status = 0;
Eric Laurent994a6932013-07-17 11:51:42 -07001169 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001170 pthread_mutex_lock(&in->lock);
1171 if (!in->standby) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001172 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001173 if (in->pcm) {
1174 pcm_close(in->pcm);
1175 in->pcm = NULL;
1176 }
1177 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001178 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001179 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001180 }
1181 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001182 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001183 return status;
1184}
1185
1186static int in_dump(const struct audio_stream *stream, int fd)
1187{
1188 return 0;
1189}
1190
1191static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1192{
1193 struct stream_in *in = (struct stream_in *)stream;
1194 struct audio_device *adev = in->dev;
1195 struct str_parms *parms;
1196 char *str;
1197 char value[32];
1198 int ret, val = 0;
1199
Eric Laurent994a6932013-07-17 11:51:42 -07001200 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001201 parms = str_parms_create_str(kvpairs);
1202
1203 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1204
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001205 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001206 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001207 if (ret >= 0) {
1208 val = atoi(value);
1209 /* no audio source uses val == 0 */
1210 if ((in->source != val) && (val != 0)) {
1211 in->source = val;
1212 }
1213 }
1214
1215 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1216 if (ret >= 0) {
1217 val = atoi(value);
1218 if ((in->device != val) && (val != 0)) {
1219 in->device = val;
1220 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001221 if (!in->standby)
1222 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001223 }
1224 }
1225
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001226 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001227 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001228
1229 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001230 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001231 return ret;
1232}
1233
1234static char* in_get_parameters(const struct audio_stream *stream,
1235 const char *keys)
1236{
1237 return strdup("");
1238}
1239
1240static int in_set_gain(struct audio_stream_in *stream, float gain)
1241{
1242 return 0;
1243}
1244
1245static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1246 size_t bytes)
1247{
1248 struct stream_in *in = (struct stream_in *)stream;
1249 struct audio_device *adev = in->dev;
1250 int i, ret = -1;
1251
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001252 pthread_mutex_lock(&in->lock);
1253 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001254 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001255 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001256 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001257 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001258 goto exit;
1259 }
1260 in->standby = 0;
1261 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001262
1263 if (in->pcm) {
1264 ret = pcm_read(in->pcm, buffer, bytes);
1265 }
1266
1267 /*
1268 * Instead of writing zeroes here, we could trust the hardware
1269 * to always provide zeroes when muted.
1270 */
1271 if (ret == 0 && adev->mic_mute)
1272 memset(buffer, 0, bytes);
1273
1274exit:
1275 pthread_mutex_unlock(&in->lock);
1276
1277 if (ret != 0) {
1278 in_standby(&in->stream.common);
1279 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1280 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1281 in_get_sample_rate(&in->stream.common));
1282 }
1283 return bytes;
1284}
1285
1286static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1287{
1288 return 0;
1289}
1290
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001291static int add_remove_audio_effect(const struct audio_stream *stream,
1292 effect_handle_t effect,
1293 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001294{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001295 struct stream_in *in = (struct stream_in *)stream;
1296 int status = 0;
1297 effect_descriptor_t desc;
1298
1299 status = (*effect)->get_descriptor(effect, &desc);
1300 if (status != 0)
1301 return status;
1302
1303 pthread_mutex_lock(&in->lock);
1304 pthread_mutex_lock(&in->dev->lock);
1305 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1306 in->enable_aec != enable &&
1307 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1308 in->enable_aec = enable;
1309 if (!in->standby)
1310 select_devices(in->dev, in->usecase);
1311 }
1312 pthread_mutex_unlock(&in->dev->lock);
1313 pthread_mutex_unlock(&in->lock);
1314
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001315 return 0;
1316}
1317
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001318static int in_add_audio_effect(const struct audio_stream *stream,
1319 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001320{
Eric Laurent994a6932013-07-17 11:51:42 -07001321 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001322 return add_remove_audio_effect(stream, effect, true);
1323}
1324
1325static int in_remove_audio_effect(const struct audio_stream *stream,
1326 effect_handle_t effect)
1327{
Eric Laurent994a6932013-07-17 11:51:42 -07001328 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001329 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001330}
1331
1332static int adev_open_output_stream(struct audio_hw_device *dev,
1333 audio_io_handle_t handle,
1334 audio_devices_t devices,
1335 audio_output_flags_t flags,
1336 struct audio_config *config,
1337 struct audio_stream_out **stream_out)
1338{
1339 struct audio_device *adev = (struct audio_device *)dev;
1340 struct stream_out *out;
1341 int i, ret;
1342
Eric Laurent994a6932013-07-17 11:51:42 -07001343 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001344 __func__, config->sample_rate, config->channel_mask, devices, flags);
1345 *stream_out = NULL;
1346 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1347
1348 if (devices == AUDIO_DEVICE_NONE)
1349 devices = AUDIO_DEVICE_OUT_SPEAKER;
1350
1351 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
1352 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1353 out->flags = flags;
1354 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001355 out->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001356
1357 /* Init use case and pcm_config */
1358 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
1359 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001360 pthread_mutex_lock(&adev->lock);
1361 ret = read_hdmi_channel_masks(out);
1362 pthread_mutex_unlock(&adev->lock);
1363 if (ret != 0) {
1364 /* If HDMI does not support multi channel playback, set the default */
1365 out->config.channels = popcount(out->channel_mask);
Eric Laurentb23d5282013-05-14 15:27:20 -07001366 platform_set_hdmi_channels(adev->platform, out->config.channels);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001367 goto error_open;
1368 }
1369
1370 if (config->sample_rate == 0)
1371 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1372 if (config->channel_mask == 0)
1373 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1374
1375 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001376 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
1377 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001378 out->config.rate = config->sample_rate;
1379 out->config.channels = popcount(out->channel_mask);
1380 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Eric Laurentb23d5282013-05-14 15:27:20 -07001381 platform_set_hdmi_channels(adev->platform, out->config.channels);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001382 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
1383 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
1384 out->config = pcm_config_deep_buffer;
1385 } else {
1386 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
1387 out->config = pcm_config_low_latency;
1388 }
1389
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001390 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1391 if(adev->primary_output == NULL)
1392 adev->primary_output = out;
1393 else {
1394 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001395 ret = -EEXIST;
1396 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001397 }
1398 }
1399
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001400 /* Check if this usecase is already existing */
1401 pthread_mutex_lock(&adev->lock);
1402 if (get_usecase_from_list(adev, out->usecase) != NULL) {
1403 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001404 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001405 ret = -EEXIST;
1406 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001407 }
1408 pthread_mutex_unlock(&adev->lock);
1409
1410 out->stream.common.get_sample_rate = out_get_sample_rate;
1411 out->stream.common.set_sample_rate = out_set_sample_rate;
1412 out->stream.common.get_buffer_size = out_get_buffer_size;
1413 out->stream.common.get_channels = out_get_channels;
1414 out->stream.common.get_format = out_get_format;
1415 out->stream.common.set_format = out_set_format;
1416 out->stream.common.standby = out_standby;
1417 out->stream.common.dump = out_dump;
1418 out->stream.common.set_parameters = out_set_parameters;
1419 out->stream.common.get_parameters = out_get_parameters;
1420 out->stream.common.add_audio_effect = out_add_audio_effect;
1421 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1422 out->stream.get_latency = out_get_latency;
1423 out->stream.set_volume = out_set_volume;
1424 out->stream.write = out_write;
1425 out->stream.get_render_position = out_get_render_position;
1426 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
1427
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001428 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07001429 /* out->muted = false; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001430
1431 config->format = out->stream.common.get_format(&out->stream.common);
1432 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
1433 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
1434
1435 *stream_out = &out->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07001436 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001437 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001438
1439error_open:
1440 free(out);
1441 *stream_out = NULL;
1442 ALOGD("%s: exit: ret %d", __func__, ret);
1443 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001444}
1445
1446static void adev_close_output_stream(struct audio_hw_device *dev,
1447 struct audio_stream_out *stream)
1448{
Eric Laurent994a6932013-07-17 11:51:42 -07001449 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001450 out_standby(&stream->common);
1451 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07001452 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001453}
1454
1455static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
1456{
1457 struct audio_device *adev = (struct audio_device *)dev;
1458 struct str_parms *parms;
1459 char *str;
1460 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07001461 int val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001462 int ret;
1463
Eric Laurent994a6932013-07-17 11:51:42 -07001464 ALOGV("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001465
1466 parms = str_parms_create_str(kvpairs);
1467 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value));
1468 if (ret >= 0) {
1469 int tty_mode;
1470
1471 if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0)
1472 tty_mode = TTY_MODE_OFF;
1473 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0)
1474 tty_mode = TTY_MODE_VCO;
1475 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0)
1476 tty_mode = TTY_MODE_HCO;
1477 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0)
1478 tty_mode = TTY_MODE_FULL;
1479 else
1480 return -EINVAL;
1481
1482 pthread_mutex_lock(&adev->lock);
1483 if (tty_mode != adev->tty_mode) {
1484 adev->tty_mode = tty_mode;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08001485 adev->acdb_settings = (adev->acdb_settings & TTY_MODE_CLEAR) | tty_mode;
1486 if (adev->in_call)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001487 select_devices(adev, USECASE_VOICE_CALL);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001488 }
1489 pthread_mutex_unlock(&adev->lock);
1490 }
1491
1492 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
1493 if (ret >= 0) {
1494 /* When set to false, HAL should disable EC and NS
1495 * But it is currently not supported.
1496 */
1497 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
1498 adev->bluetooth_nrec = true;
1499 else
1500 adev->bluetooth_nrec = false;
1501 }
1502
1503 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
1504 if (ret >= 0) {
1505 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
1506 adev->screen_off = false;
1507 else
1508 adev->screen_off = true;
1509 }
1510
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07001511 ret = str_parms_get_int(parms, "rotation", &val);
1512 if (ret >= 0) {
1513 bool reverse_speakers = false;
1514 switch(val) {
1515 // FIXME: note that the code below assumes that the speakers are in the correct placement
1516 // relative to the user when the device is rotated 90deg from its default rotation. This
1517 // assumption is device-specific, not platform-specific like this code.
1518 case 270:
1519 reverse_speakers = true;
1520 break;
1521 case 0:
1522 case 90:
1523 case 180:
1524 break;
1525 default:
1526 ALOGE("%s: unexpected rotation of %d", __func__, val);
1527 }
1528 pthread_mutex_lock(&adev->lock);
1529 if (adev->speaker_lr_swap != reverse_speakers) {
1530 adev->speaker_lr_swap = reverse_speakers;
1531 // only update the selected device if there is active pcm playback
1532 struct audio_usecase *usecase;
1533 struct listnode *node;
1534 list_for_each(node, &adev->usecase_list) {
1535 usecase = node_to_item(node, struct audio_usecase, list);
1536 if (usecase->type == PCM_PLAYBACK) {
1537 select_devices(adev, usecase->id);
1538 break;
1539 }
1540 }
1541 }
1542 pthread_mutex_unlock(&adev->lock);
1543 }
1544
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07001545 audio_extn_set_parameters(adev, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001546 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001547 ALOGV("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001548 return ret;
1549}
1550
1551static char* adev_get_parameters(const struct audio_hw_device *dev,
1552 const char *keys)
1553{
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07001554 return audio_extn_get_parameters(dev, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001555}
1556
1557static int adev_init_check(const struct audio_hw_device *dev)
1558{
1559 return 0;
1560}
1561
1562static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
1563{
1564 struct audio_device *adev = (struct audio_device *)dev;
1565 int vol, err = 0;
1566
1567 pthread_mutex_lock(&adev->lock);
1568 adev->voice_volume = volume;
1569 if (adev->mode == AUDIO_MODE_IN_CALL) {
1570 if (volume < 0.0) {
1571 volume = 0.0;
1572 } else if (volume > 1.0) {
1573 volume = 1.0;
1574 }
1575
1576 vol = lrint(volume * 100.0);
1577
1578 // Voice volume levels from android are mapped to driver volume levels as follows.
1579 // 0 -> 5, 20 -> 4, 40 ->3, 60 -> 2, 80 -> 1, 100 -> 0
1580 // So adjust the volume to get the correct volume index in driver
1581 vol = 100 - vol;
Eric Laurentb23d5282013-05-14 15:27:20 -07001582
1583 err = platform_set_voice_volume(adev->platform, vol);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001584 }
1585 pthread_mutex_unlock(&adev->lock);
1586 return err;
1587}
1588
1589static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
1590{
1591 return -ENOSYS;
1592}
1593
1594static int adev_get_master_volume(struct audio_hw_device *dev,
1595 float *volume)
1596{
1597 return -ENOSYS;
1598}
1599
1600static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
1601{
1602 return -ENOSYS;
1603}
1604
1605static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
1606{
1607 return -ENOSYS;
1608}
1609
1610static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
1611{
1612 struct audio_device *adev = (struct audio_device *)dev;
1613
1614 pthread_mutex_lock(&adev->lock);
1615 if (adev->mode != mode) {
1616 adev->mode = mode;
1617 }
1618 pthread_mutex_unlock(&adev->lock);
1619 return 0;
1620}
1621
1622static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
1623{
1624 struct audio_device *adev = (struct audio_device *)dev;
1625 int err = 0;
1626
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001627 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001628 adev->mic_mute = state;
Eric Laurentb23d5282013-05-14 15:27:20 -07001629
1630 err = platform_set_mic_mute(adev->platform, state);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001631 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001632 return err;
1633}
1634
1635static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1636{
1637 struct audio_device *adev = (struct audio_device *)dev;
1638
1639 *state = adev->mic_mute;
1640
1641 return 0;
1642}
1643
1644static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
1645 const struct audio_config *config)
1646{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001647 int channel_count = popcount(config->channel_mask);
1648
1649 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
1650}
1651
1652static int adev_open_input_stream(struct audio_hw_device *dev,
1653 audio_io_handle_t handle,
1654 audio_devices_t devices,
1655 struct audio_config *config,
1656 struct audio_stream_in **stream_in)
1657{
1658 struct audio_device *adev = (struct audio_device *)dev;
1659 struct stream_in *in;
1660 int ret, buffer_size, frame_size;
1661 int channel_count = popcount(config->channel_mask);
1662
Eric Laurent994a6932013-07-17 11:51:42 -07001663 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001664 *stream_in = NULL;
1665 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
1666 return -EINVAL;
1667
1668 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
1669
1670 in->stream.common.get_sample_rate = in_get_sample_rate;
1671 in->stream.common.set_sample_rate = in_set_sample_rate;
1672 in->stream.common.get_buffer_size = in_get_buffer_size;
1673 in->stream.common.get_channels = in_get_channels;
1674 in->stream.common.get_format = in_get_format;
1675 in->stream.common.set_format = in_set_format;
1676 in->stream.common.standby = in_standby;
1677 in->stream.common.dump = in_dump;
1678 in->stream.common.set_parameters = in_set_parameters;
1679 in->stream.common.get_parameters = in_get_parameters;
1680 in->stream.common.add_audio_effect = in_add_audio_effect;
1681 in->stream.common.remove_audio_effect = in_remove_audio_effect;
1682 in->stream.set_gain = in_set_gain;
1683 in->stream.read = in_read;
1684 in->stream.get_input_frames_lost = in_get_input_frames_lost;
1685
1686 in->device = devices;
1687 in->source = AUDIO_SOURCE_DEFAULT;
1688 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001689 in->standby = 1;
1690 in->channel_mask = config->channel_mask;
1691
1692 /* Update config params with the requested sample rate and channels */
1693 in->usecase = USECASE_AUDIO_RECORD;
1694 in->config = pcm_config_audio_capture;
1695 in->config.channels = channel_count;
1696 in->config.rate = config->sample_rate;
1697
1698 frame_size = audio_stream_frame_size((struct audio_stream *)in);
1699 buffer_size = get_input_buffer_size(config->sample_rate,
1700 config->format,
1701 channel_count);
1702 in->config.period_size = buffer_size / frame_size;
1703
1704 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07001705 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001706 return 0;
1707
1708err_open:
1709 free(in);
1710 *stream_in = NULL;
1711 return ret;
1712}
1713
1714static void adev_close_input_stream(struct audio_hw_device *dev,
1715 struct audio_stream_in *stream)
1716{
Eric Laurent994a6932013-07-17 11:51:42 -07001717 ALOGV("%s", __func__);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001718
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001719 in_standby(&stream->common);
1720 free(stream);
1721
1722 return;
1723}
1724
1725static int adev_dump(const audio_hw_device_t *device, int fd)
1726{
1727 return 0;
1728}
1729
1730static int adev_close(hw_device_t *device)
1731{
1732 struct audio_device *adev = (struct audio_device *)device;
1733 audio_route_free(adev->audio_route);
Eric Laurentb23d5282013-05-14 15:27:20 -07001734 free(adev->snd_dev_ref_cnt);
1735 platform_deinit(adev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001736 free(device);
1737 return 0;
1738}
1739
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001740static int adev_open(const hw_module_t *module, const char *name,
1741 hw_device_t **device)
1742{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001743 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001744
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001745 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001746 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
1747
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001748 pthread_mutex_lock(&adev_init_lock);
1749 if (is_adev_initialised == true){
1750 *device = &adev->device.common;
1751 ALOGD("%s: returning existing instance of adev", __func__);
1752 ALOGD("%s: exit", __func__);
1753 pthread_mutex_unlock(&adev_init_lock);
1754 return 0;
1755 }
1756
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001757 adev = calloc(1, sizeof(struct audio_device));
1758
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001759 adev->device.common.tag = HARDWARE_DEVICE_TAG;
1760 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
1761 adev->device.common.module = (struct hw_module_t *)module;
1762 adev->device.common.close = adev_close;
1763
1764 adev->device.init_check = adev_init_check;
1765 adev->device.set_voice_volume = adev_set_voice_volume;
1766 adev->device.set_master_volume = adev_set_master_volume;
1767 adev->device.get_master_volume = adev_get_master_volume;
1768 adev->device.set_master_mute = adev_set_master_mute;
1769 adev->device.get_master_mute = adev_get_master_mute;
1770 adev->device.set_mode = adev_set_mode;
1771 adev->device.set_mic_mute = adev_set_mic_mute;
1772 adev->device.get_mic_mute = adev_get_mic_mute;
1773 adev->device.set_parameters = adev_set_parameters;
1774 adev->device.get_parameters = adev_get_parameters;
1775 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
1776 adev->device.open_output_stream = adev_open_output_stream;
1777 adev->device.close_output_stream = adev_close_output_stream;
1778 adev->device.open_input_stream = adev_open_input_stream;
1779 adev->device.close_input_stream = adev_close_input_stream;
1780 adev->device.dump = adev_dump;
1781
1782 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001783 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08001784 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001785 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001786 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001787 adev->voice_call_rx = NULL;
1788 adev->voice_call_tx = NULL;
1789 adev->voice_volume = 1.0f;
1790 adev->tty_mode = TTY_MODE_OFF;
1791 adev->bluetooth_nrec = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001792 adev->in_call = false;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08001793 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurentb23d5282013-05-14 15:27:20 -07001794 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001795 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001796
1797 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07001798 adev->platform = platform_init(adev);
1799 if (!adev->platform) {
1800 free(adev->snd_dev_ref_cnt);
1801 free(adev);
1802 ALOGE("%s: Failed to init platform data, aborting.", __func__);
1803 *device = NULL;
1804 return -EINVAL;
1805 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001806 *device = &adev->device.common;
1807
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001808 /* update init flag*/
1809 is_adev_initialised = true;
1810 pthread_mutex_unlock(&adev_init_lock);
1811
Eric Laurent994a6932013-07-17 11:51:42 -07001812 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001813 return 0;
1814}
1815
1816static struct hw_module_methods_t hal_module_methods = {
1817 .open = adev_open,
1818};
1819
1820struct audio_module HAL_MODULE_INFO_SYM = {
1821 .common = {
1822 .tag = HARDWARE_MODULE_TAG,
1823 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1824 .hal_api_version = HARDWARE_HAL_API_VERSION,
1825 .id = AUDIO_HARDWARE_MODULE_ID,
1826 .name = "QCOM Audio HAL",
1827 .author = "Code Aurora Forum",
1828 .methods = &hal_module_methods,
1829 },
1830};