blob: 3155ab652bd47fbbde3a6ab20132884c1bf4ff7b [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
Eric Laurentb23d5282013-05-14 15:27:20 -070082static const char * const use_case_table[AUDIO_USECASE_MAX] = {
83 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
84 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
85 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
86 [USECASE_AUDIO_RECORD] = "audio-record",
87 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070088 [USECASE_AUDIO_PLAYBACK_FM] = "play-fm",
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -070089 [USECASE_VOICE_CALL] = "voice-call",
90 [USECASE_VOICE2_CALL] = "voice2-call",
91 [USECASE_VOLTE_CALL] = "volte-call",
92 [USECASE_QCHAT_CALL] = "qchat-call",
Eric Laurentb23d5282013-05-14 15:27:20 -070093};
94
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080095
96#define STRING_TO_ENUM(string) { #string, string }
97
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080098struct string_to_enum {
99 const char *name;
100 uint32_t value;
101};
102
103static const struct string_to_enum out_channels_name_to_enum_table[] = {
104 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
105 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
106 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
107};
108
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700109static struct audio_device *adev = NULL;
110static pthread_mutex_t adev_init_lock;
111static bool is_adev_initialised = false;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800112
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700113static int enable_audio_route(struct audio_device *adev,
114 struct audio_usecase *usecase,
115 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800116{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700117 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700118 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800119
120 if (usecase == NULL)
121 return -EINVAL;
122
123 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
124
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800125 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700126 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800127 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700128 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800129
130 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700131 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700132 ALOGV("%s: apply mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700133 audio_route_apply_path(adev->audio_route, mixer_path);
134 if (update_mixer)
135 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800136
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800137 ALOGV("%s: exit", __func__);
138 return 0;
139}
140
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700141int disable_audio_route(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700142 struct audio_usecase *usecase,
143 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800144{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700145 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700146 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800147
148 if (usecase == NULL)
149 return -EINVAL;
150
151 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700152 if (usecase->type == PCM_CAPTURE)
153 snd_device = usecase->in_snd_device;
154 else
155 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800156 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700157 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700158 ALOGV("%s: reset mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700159 audio_route_reset_path(adev->audio_route, mixer_path);
160 if (update_mixer)
161 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800162
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800163 ALOGV("%s: exit", __func__);
164 return 0;
165}
166
167static int enable_snd_device(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700168 snd_device_t snd_device,
169 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800170{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800171 if (snd_device < SND_DEVICE_MIN ||
172 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800173 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800174 return -EINVAL;
175 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700176
177 adev->snd_dev_ref_cnt[snd_device]++;
178 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700179 ALOGV("%s: snd_device(%d: %s) is already active",
Eric Laurentb23d5282013-05-14 15:27:20 -0700180 __func__, snd_device, platform_get_snd_device_name(snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700181 return 0;
182 }
183
Eric Laurentb23d5282013-05-14 15:27:20 -0700184 if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700185 adev->snd_dev_ref_cnt[snd_device]--;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800186 return -EINVAL;
187 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800188
Eric Laurent994a6932013-07-17 11:51:42 -0700189 ALOGV("%s: snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700190 snd_device, platform_get_snd_device_name(snd_device));
191 audio_route_apply_path(adev->audio_route, platform_get_snd_device_name(snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700192 if (update_mixer)
193 audio_route_update_mixer(adev->audio_route);
194
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800195 return 0;
196}
197
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700198int disable_snd_device(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700199 snd_device_t snd_device,
200 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800201{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800202 if (snd_device < SND_DEVICE_MIN ||
203 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800204 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800205 return -EINVAL;
206 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700207 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
208 ALOGE("%s: device ref cnt is already 0", __func__);
209 return -EINVAL;
210 }
211 adev->snd_dev_ref_cnt[snd_device]--;
212 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Eric Laurent994a6932013-07-17 11:51:42 -0700213 ALOGV("%s: snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700214 snd_device, platform_get_snd_device_name(snd_device));
215 audio_route_reset_path(adev->audio_route, platform_get_snd_device_name(snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700216 if (update_mixer)
217 audio_route_update_mixer(adev->audio_route);
218 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800219 return 0;
220}
221
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700222static void check_usecases_codec_backend(struct audio_device *adev,
223 struct audio_usecase *uc_info,
224 snd_device_t snd_device)
225{
226 struct listnode *node;
227 struct audio_usecase *usecase;
228 bool switch_device[AUDIO_USECASE_MAX];
229 int i, num_uc_to_switch = 0;
230
231 /*
232 * This function is to make sure that all the usecases that are active on
233 * the hardware codec backend are always routed to any one device that is
234 * handled by the hardware codec.
235 * For example, if low-latency and deep-buffer usecases are currently active
236 * on speaker and out_set_parameters(headset) is received on low-latency
237 * output, then we have to make sure deep-buffer is also switched to headset,
238 * because of the limitation that both the devices cannot be enabled
239 * at the same time as they share the same backend.
240 */
241 /* Disable all the usecases on the shared backend other than the
242 specified usecase */
243 for (i = 0; i < AUDIO_USECASE_MAX; i++)
244 switch_device[i] = false;
245
246 list_for_each(node, &adev->usecase_list) {
247 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700248 if (usecase->type == PCM_PLAYBACK &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700249 usecase != uc_info &&
250 usecase->out_snd_device != snd_device &&
251 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
252 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
253 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700254 platform_get_snd_device_name(usecase->out_snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700255 disable_audio_route(adev, usecase, false);
256 switch_device[usecase->id] = true;
257 num_uc_to_switch++;
258 }
259 }
260
261 if (num_uc_to_switch) {
262 /* Make sure all the streams are de-routed before disabling the device */
263 audio_route_update_mixer(adev->audio_route);
264
265 list_for_each(node, &adev->usecase_list) {
266 usecase = node_to_item(node, struct audio_usecase, list);
267 if (switch_device[usecase->id]) {
268 disable_snd_device(adev, usecase->out_snd_device, false);
269 enable_snd_device(adev, snd_device, false);
270 }
271 }
272
273 /* Make sure new snd device is enabled before re-routing the streams */
274 audio_route_update_mixer(adev->audio_route);
275
276 /* Re-route all the usecases on the shared backend other than the
277 specified usecase to new snd devices */
278 list_for_each(node, &adev->usecase_list) {
279 usecase = node_to_item(node, struct audio_usecase, list);
280 /* Update the out_snd_device only before enabling the audio route */
281 if (switch_device[usecase->id] ) {
282 usecase->out_snd_device = snd_device;
283 enable_audio_route(adev, usecase, false);
284 }
285 }
286
287 audio_route_update_mixer(adev->audio_route);
288 }
289}
290
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700291static void check_and_route_capture_usecases(struct audio_device *adev,
292 struct audio_usecase *uc_info,
293 snd_device_t snd_device)
294{
295 struct listnode *node;
296 struct audio_usecase *usecase;
297 bool switch_device[AUDIO_USECASE_MAX];
298 int i, num_uc_to_switch = 0;
299
300 /*
301 * This function is to make sure that all the active capture usecases
302 * are always routed to the same input sound device.
303 * For example, if audio-record and voice-call usecases are currently
304 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
305 * is received for voice call then we have to make sure that audio-record
306 * usecase is also switched to earpiece i.e. voice-dmic-ef,
307 * because of the limitation that two devices cannot be enabled
308 * at the same time if they share the same backend.
309 */
310 for (i = 0; i < AUDIO_USECASE_MAX; i++)
311 switch_device[i] = false;
312
313 list_for_each(node, &adev->usecase_list) {
314 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700315 if (usecase->type == PCM_CAPTURE &&
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700316 usecase != uc_info &&
317 usecase->in_snd_device != snd_device) {
318 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
319 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700320 platform_get_snd_device_name(usecase->in_snd_device));
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700321 disable_audio_route(adev, usecase, false);
322 switch_device[usecase->id] = true;
323 num_uc_to_switch++;
324 }
325 }
326
327 if (num_uc_to_switch) {
328 /* Make sure all the streams are de-routed before disabling the device */
329 audio_route_update_mixer(adev->audio_route);
330
331 list_for_each(node, &adev->usecase_list) {
332 usecase = node_to_item(node, struct audio_usecase, list);
333 if (switch_device[usecase->id]) {
334 disable_snd_device(adev, usecase->in_snd_device, false);
335 enable_snd_device(adev, snd_device, false);
336 }
337 }
338
339 /* Make sure new snd device is enabled before re-routing the streams */
340 audio_route_update_mixer(adev->audio_route);
341
342 /* Re-route all the usecases on the shared backend other than the
343 specified usecase to new snd devices */
344 list_for_each(node, &adev->usecase_list) {
345 usecase = node_to_item(node, struct audio_usecase, list);
346 /* Update the in_snd_device only before enabling the audio route */
347 if (switch_device[usecase->id] ) {
348 usecase->in_snd_device = snd_device;
349 enable_audio_route(adev, usecase, false);
350 }
351 }
352
353 audio_route_update_mixer(adev->audio_route);
354 }
355}
356
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700357static int disable_all_usecases_of_type(struct audio_device *adev,
358 usecase_type_t usecase_type,
359 bool update_mixer)
360{
361 struct audio_usecase *usecase;
362 struct listnode *node;
363 int ret = 0;
364
365 list_for_each(node, &adev->usecase_list) {
366 usecase = node_to_item(node, struct audio_usecase, list);
367 if (usecase->type == usecase_type) {
368 ALOGV("%s: usecase id %d", __func__, usecase->id);
369 ret = disable_audio_route(adev, usecase, update_mixer);
370 if (ret) {
371 ALOGE("%s: Failed to disable usecase id %d",
372 __func__, usecase->id);
373 }
374 }
375 }
376
377 return ret;
378}
379
380static int enable_all_usecases_of_type(struct audio_device *adev,
381 usecase_type_t usecase_type,
382 bool update_mixer)
383{
384 struct audio_usecase *usecase;
385 struct listnode *node;
386 int ret = 0;
387
388 list_for_each(node, &adev->usecase_list) {
389 usecase = node_to_item(node, struct audio_usecase, list);
390 if (usecase->type == usecase_type) {
391 ALOGV("%s: usecase id %d", __func__, usecase->id);
392 ret = enable_audio_route(adev, usecase, update_mixer);
393 if (ret) {
394 ALOGE("%s: Failed to enable usecase id %d",
395 __func__, usecase->id);
396 }
397 }
398 }
399
400 return ret;
401}
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800402
403/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700404static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800405{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700406 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700407 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800408
409 switch (channels) {
410 /*
411 * Do not handle stereo output in Multi-channel cases
412 * Stereo case is handled in normal playback path
413 */
414 case 6:
415 ALOGV("%s: HDMI supports 5.1", __func__);
416 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
417 break;
418 case 8:
419 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
420 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
421 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
422 break;
423 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700424 ALOGE("HDMI does not support multi channel playback");
425 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800426 break;
427 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700428 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800429}
430
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700431static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
432{
433 struct audio_usecase *usecase;
434 struct listnode *node;
435
436 list_for_each(node, &adev->usecase_list) {
437 usecase = node_to_item(node, struct audio_usecase, list);
438 if (usecase->type == VOICE_CALL) {
439 ALOGV("%s: usecase id %d", __func__, usecase->id);
440 return usecase->id;
441 }
442 }
443 return USECASE_INVALID;
444}
445
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700446struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700447 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700448{
449 struct audio_usecase *usecase;
450 struct listnode *node;
451
452 list_for_each(node, &adev->usecase_list) {
453 usecase = node_to_item(node, struct audio_usecase, list);
454 if (usecase->id == uc_id)
455 return usecase;
456 }
457 return NULL;
458}
459
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700460int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800461{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800462 snd_device_t out_snd_device = SND_DEVICE_NONE;
463 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700464 struct audio_usecase *usecase = NULL;
465 struct audio_usecase *vc_usecase = NULL;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800466 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700467 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800468
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700469 usecase = get_usecase_from_list(adev, uc_id);
470 if (usecase == NULL) {
471 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
472 return -EINVAL;
473 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800474
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700475 if (usecase->type == VOICE_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700476 out_snd_device = platform_get_output_snd_device(adev->platform,
477 usecase->stream.out->devices);
478 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700479 usecase->devices = usecase->stream.out->devices;
480 } else {
481 /*
482 * If the voice call is active, use the sound devices of voice call usecase
483 * so that it would not result any device switch. All the usecases will
484 * be switched to new device when select_devices() is called for voice call
485 * usecase. This is to avoid switching devices for voice call when
486 * check_usecases_codec_backend() is called below.
487 */
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700488 if (voice_is_in_call(adev)) {
489 vc_usecase = get_usecase_from_list(adev,
490 get_voice_usecase_id_from_list(adev));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700491 if (vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
492 in_snd_device = vc_usecase->in_snd_device;
493 out_snd_device = vc_usecase->out_snd_device;
494 }
495 }
496 if (usecase->type == PCM_PLAYBACK) {
497 usecase->devices = usecase->stream.out->devices;
498 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700499 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700500 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700501 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700502 if (usecase->stream.out == adev->primary_output &&
503 adev->active_input &&
504 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
505 select_devices(adev, adev->active_input->usecase);
506 }
507 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700508 } else if (usecase->type == PCM_CAPTURE) {
509 usecase->devices = usecase->stream.in->device;
510 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700511 if (in_snd_device == SND_DEVICE_NONE) {
512 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
513 adev->primary_output && !adev->primary_output->standby) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700514 in_snd_device = platform_get_input_snd_device(adev->platform,
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700515 adev->primary_output->devices);
516 } else {
Eric Laurentb23d5282013-05-14 15:27:20 -0700517 in_snd_device = platform_get_input_snd_device(adev->platform,
518 AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700519 }
520 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700521 }
522 }
523
524 if (out_snd_device == usecase->out_snd_device &&
525 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800526 return 0;
527 }
528
sangwoobc677242013-08-08 16:53:43 +0900529 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700530 out_snd_device, platform_get_snd_device_name(out_snd_device),
531 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800532
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800533 /*
534 * Limitation: While in call, to do a device switch we need to disable
535 * and enable both RX and TX devices though one of them is same as current
536 * device.
537 */
Eric Laurentb23d5282013-05-14 15:27:20 -0700538 if (usecase->type == VOICE_CALL) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700539 disable_all_usecases_of_type(adev, VOICE_CALL, true);
Eric Laurentb23d5282013-05-14 15:27:20 -0700540 status = platform_switch_voice_call_device_pre(adev->platform);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800541 }
542
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700543 /* Disable current sound devices */
544 if (usecase->out_snd_device != SND_DEVICE_NONE) {
545 disable_audio_route(adev, usecase, true);
546 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800547 }
548
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700549 if (usecase->in_snd_device != SND_DEVICE_NONE) {
550 disable_audio_route(adev, usecase, true);
551 disable_snd_device(adev, usecase->in_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800552 }
553
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700554 /* Enable new sound devices */
555 if (out_snd_device != SND_DEVICE_NONE) {
556 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
557 check_usecases_codec_backend(adev, usecase, out_snd_device);
558 enable_snd_device(adev, out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800559 }
560
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700561 if (in_snd_device != SND_DEVICE_NONE) {
562 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700563 enable_snd_device(adev, in_snd_device, false);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700564 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700565
Eric Laurentb23d5282013-05-14 15:27:20 -0700566 if (usecase->type == VOICE_CALL)
567 status = platform_switch_voice_call_device_post(adev->platform,
568 out_snd_device,
569 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800570
sangwoo170731f2013-06-08 15:36:36 +0900571 audio_route_update_mixer(adev->audio_route);
572
573 usecase->in_snd_device = in_snd_device;
574 usecase->out_snd_device = out_snd_device;
575
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700576 if (usecase->type == VOICE_CALL)
577 enable_all_usecases_of_type(adev, VOICE_CALL, true);
578 else
579 enable_audio_route(adev, usecase, true);
sangwoo170731f2013-06-08 15:36:36 +0900580
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800581 return status;
582}
583
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800584static int stop_input_stream(struct stream_in *in)
585{
586 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800587 struct audio_usecase *uc_info;
588 struct audio_device *adev = in->dev;
589
Eric Laurentc8400632013-02-14 19:04:54 -0800590 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800591
Eric Laurent994a6932013-07-17 11:51:42 -0700592 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700593 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800594 uc_info = get_usecase_from_list(adev, in->usecase);
595 if (uc_info == NULL) {
596 ALOGE("%s: Could not find the usecase (%d) in the list",
597 __func__, in->usecase);
598 return -EINVAL;
599 }
600
Eric Laurent150dbfe2013-02-27 14:31:02 -0800601 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700602 disable_audio_route(adev, uc_info, true);
603
604 /* 2. Disable the tx device */
605 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800606
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800607 list_remove(&uc_info->list);
608 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800609
Eric Laurent994a6932013-07-17 11:51:42 -0700610 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800611 return ret;
612}
613
614int start_input_stream(struct stream_in *in)
615{
616 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800617 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800618 struct audio_usecase *uc_info;
619 struct audio_device *adev = in->dev;
620
Eric Laurent994a6932013-07-17 11:51:42 -0700621 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
Eric Laurentb23d5282013-05-14 15:27:20 -0700622 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800623 if (in->pcm_device_id < 0) {
624 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
625 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800626 ret = -EINVAL;
627 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800628 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700629
630 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800631 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
632 uc_info->id = in->usecase;
633 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800634 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700635 uc_info->devices = in->device;
636 uc_info->in_snd_device = SND_DEVICE_NONE;
637 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800638
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800639 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700640 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800641
Eric Laurentc8400632013-02-14 19:04:54 -0800642 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
643 __func__, SOUND_CARD, in->pcm_device_id, in->config.channels);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800644 in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id,
645 PCM_IN, &in->config);
646 if (in->pcm && !pcm_is_ready(in->pcm)) {
647 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
648 pcm_close(in->pcm);
649 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800650 ret = -EIO;
651 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800652 }
Eric Laurent994a6932013-07-17 11:51:42 -0700653 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800654 return ret;
655
656error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800657 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800658
659error_config:
660 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700661 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800662
663 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800664}
665
666static int stop_output_stream(struct stream_out *out)
667{
668 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800669 struct audio_usecase *uc_info;
670 struct audio_device *adev = out->dev;
671
Eric Laurent994a6932013-07-17 11:51:42 -0700672 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700673 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800674 uc_info = get_usecase_from_list(adev, out->usecase);
675 if (uc_info == NULL) {
676 ALOGE("%s: Could not find the usecase (%d) in the list",
677 __func__, out->usecase);
678 return -EINVAL;
679 }
680
Eric Laurent150dbfe2013-02-27 14:31:02 -0800681 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700682 disable_audio_route(adev, uc_info, true);
683
684 /* 2. Disable the rx device */
685 disable_snd_device(adev, uc_info->out_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800686
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800687 list_remove(&uc_info->list);
688 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800689
Eric Laurent994a6932013-07-17 11:51:42 -0700690 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800691 return ret;
692}
693
694int start_output_stream(struct stream_out *out)
695{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800696 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800697 struct audio_usecase *uc_info;
698 struct audio_device *adev = out->dev;
699
Eric Laurent994a6932013-07-17 11:51:42 -0700700 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700701 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Eric Laurentb23d5282013-05-14 15:27:20 -0700702 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800703 if (out->pcm_device_id < 0) {
704 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
705 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800706 ret = -EINVAL;
707 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800708 }
709
710 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
711 uc_info->id = out->usecase;
712 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800713 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700714 uc_info->devices = out->devices;
715 uc_info->in_snd_device = SND_DEVICE_NONE;
716 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800717
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800718 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800719
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700720 select_devices(adev, out->usecase);
721
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800722 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
723 __func__, 0, out->pcm_device_id);
724 out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id,
725 PCM_OUT, &out->config);
726 if (out->pcm && !pcm_is_ready(out->pcm)) {
727 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
728 pcm_close(out->pcm);
729 out->pcm = NULL;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800730 ret = -EIO;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700731 goto error_pcm_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800732 }
Eric Laurent994a6932013-07-17 11:51:42 -0700733 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800734 return 0;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700735error_pcm_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800736 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800737error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800738 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800739}
740
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800741static int check_input_parameters(uint32_t sample_rate,
742 audio_format_t format,
743 int channel_count)
744{
745 if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
746
747 if ((channel_count < 1) || (channel_count > 2)) return -EINVAL;
748
749 switch (sample_rate) {
750 case 8000:
751 case 11025:
752 case 12000:
753 case 16000:
754 case 22050:
755 case 24000:
756 case 32000:
757 case 44100:
758 case 48000:
759 break;
760 default:
761 return -EINVAL;
762 }
763
764 return 0;
765}
766
767static size_t get_input_buffer_size(uint32_t sample_rate,
768 audio_format_t format,
769 int channel_count)
770{
771 size_t size = 0;
772
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -0700773 if (check_input_parameters(sample_rate, format, channel_count) != 0)
774 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800775
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -0700776 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
777 /* ToDo: should use frame_size computed based on the format and
778 channel_count here. */
779 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800780
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -0700781 /* make sure the size is multiple of 64 */
782 size += 0x3f;
783 size &= ~0x3f;
784
785 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800786}
787
788static uint32_t out_get_sample_rate(const struct audio_stream *stream)
789{
790 struct stream_out *out = (struct stream_out *)stream;
791
792 return out->config.rate;
793}
794
795static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
796{
797 return -ENOSYS;
798}
799
800static size_t out_get_buffer_size(const struct audio_stream *stream)
801{
802 struct stream_out *out = (struct stream_out *)stream;
803
804 return out->config.period_size * audio_stream_frame_size(stream);
805}
806
807static uint32_t out_get_channels(const struct audio_stream *stream)
808{
809 struct stream_out *out = (struct stream_out *)stream;
810
811 return out->channel_mask;
812}
813
814static audio_format_t out_get_format(const struct audio_stream *stream)
815{
816 return AUDIO_FORMAT_PCM_16_BIT;
817}
818
819static int out_set_format(struct audio_stream *stream, audio_format_t format)
820{
821 return -ENOSYS;
822}
823
824static int out_standby(struct audio_stream *stream)
825{
826 struct stream_out *out = (struct stream_out *)stream;
827 struct audio_device *adev = out->dev;
Eric Laurent994a6932013-07-17 11:51:42 -0700828 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700829 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800830 pthread_mutex_lock(&out->lock);
831
832 if (!out->standby) {
833 out->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -0800834 if (out->pcm) {
835 pcm_close(out->pcm);
836 out->pcm = NULL;
837 }
838 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800839 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -0800840 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800841 }
842 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -0700843 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800844 return 0;
845}
846
847static int out_dump(const struct audio_stream *stream, int fd)
848{
849 return 0;
850}
851
852static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
853{
854 struct stream_out *out = (struct stream_out *)stream;
855 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800856 struct audio_usecase *usecase;
857 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800858 struct str_parms *parms;
859 char value[32];
860 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800861 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800862
sangwoobc677242013-08-08 16:53:43 +0900863 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700864 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800865 parms = str_parms_create_str(kvpairs);
866 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
867 if (ret >= 0) {
868 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800869 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -0800870 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800871
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700872 /*
873 * When HDMI cable is unplugged the music playback is paused and
874 * the policy manager sends routing=0. But the audioflinger
875 * continues to write data until standby time (3sec).
876 * As the HDMI core is turned off, the write gets blocked.
877 * Avoid this by routing audio to speaker until standby.
878 */
879 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
880 val == AUDIO_DEVICE_NONE) {
881 val = AUDIO_DEVICE_OUT_SPEAKER;
882 }
883
884 /*
885 * select_devices() call below switches all the usecases on the same
886 * backend to the new device. Refer to check_usecases_codec_backend() in
887 * the select_devices(). But how do we undo this?
888 *
889 * For example, music playback is active on headset (deep-buffer usecase)
890 * and if we go to ringtones and select a ringtone, low-latency usecase
891 * will be started on headset+speaker. As we can't enable headset+speaker
892 * and headset devices at the same time, select_devices() switches the music
893 * playback to headset+speaker while starting low-lateny usecase for ringtone.
894 * So when the ringtone playback is completed, how do we undo the same?
895 *
896 * We are relying on the out_set_parameters() call on deep-buffer output,
897 * once the ringtone playback is ended.
898 * NOTE: We should not check if the current devices are same as new devices.
899 * Because select_devices() must be called to switch back the music
900 * playback to headset.
901 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800902 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700903 out->devices = val;
904
905 if (!out->standby)
906 select_devices(adev, out->usecase);
907
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700908 if ((adev->mode == AUDIO_MODE_IN_CALL) &&
909 !voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700910 (out == adev->primary_output)) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700911 voice_start_call(adev);
912 } else if ((adev->mode == AUDIO_MODE_IN_CALL) &&
913 voice_is_in_call(adev) &&
914 (out == adev->primary_output)) {
915 select_devices(adev, get_voice_usecase_id_from_list(adev));
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800916 }
917 }
918
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700919 if ((adev->mode == AUDIO_MODE_NORMAL) &&
920 voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700921 (out == adev->primary_output)) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700922 voice_stop_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800923 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800924
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800925 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -0800926 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800927 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700928
929 if (out == adev->primary_output) {
930 pthread_mutex_lock(&adev->lock);
931 audio_extn_set_parameters(adev, parms);
932 pthread_mutex_unlock(&adev->lock);
933 }
934
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800935 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -0700936 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800937 return ret;
938}
939
940static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
941{
942 struct stream_out *out = (struct stream_out *)stream;
943 struct str_parms *query = str_parms_create_str(keys);
944 char *str;
945 char value[256];
946 struct str_parms *reply = str_parms_create();
947 size_t i, j;
948 int ret;
949 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -0700950 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800951 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
952 if (ret >= 0) {
953 value[0] = '\0';
954 i = 0;
955 while (out->supported_channel_masks[i] != 0) {
956 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
957 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
958 if (!first) {
959 strcat(value, "|");
960 }
961 strcat(value, out_channels_name_to_enum_table[j].name);
962 first = false;
963 break;
964 }
965 }
966 i++;
967 }
968 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
969 str = str_parms_to_str(reply);
970 } else {
971 str = strdup(keys);
972 }
973 str_parms_destroy(query);
974 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -0700975 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800976 return str;
977}
978
979static uint32_t out_get_latency(const struct audio_stream_out *stream)
980{
981 struct stream_out *out = (struct stream_out *)stream;
982
983 return (out->config.period_count * out->config.period_size * 1000) / (out->config.rate);
984}
985
986static int out_set_volume(struct audio_stream_out *stream, float left,
987 float right)
988{
Eric Laurenta9024de2013-04-04 09:19:12 -0700989 struct stream_out *out = (struct stream_out *)stream;
990 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
991 /* only take left channel into account: the API is for stereo anyway */
992 out->muted = (left == 0.0f);
993 return 0;
994 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800995 return -ENOSYS;
996}
997
998static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
999 size_t bytes)
1000{
1001 struct stream_out *out = (struct stream_out *)stream;
1002 struct audio_device *adev = out->dev;
1003 int i, ret = -1;
1004
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001005 pthread_mutex_lock(&out->lock);
1006 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001007 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001008 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001009 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001010 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001011 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001012 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001013 goto exit;
1014 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001015 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001016
1017 if (out->pcm) {
Eric Laurenta9024de2013-04-04 09:19:12 -07001018 if (out->muted)
1019 memset((void *)buffer, 0, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001020 //ALOGV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1021 ret = pcm_write(out->pcm, (void *)buffer, bytes);
1022 }
1023
1024exit:
1025 pthread_mutex_unlock(&out->lock);
1026
1027 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001028 if (out->pcm)
1029 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001030 out_standby(&out->stream.common);
1031 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1032 out_get_sample_rate(&out->stream.common));
1033 }
1034 return bytes;
1035}
1036
1037static int out_get_render_position(const struct audio_stream_out *stream,
1038 uint32_t *dsp_frames)
1039{
1040 return -EINVAL;
1041}
1042
1043static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1044{
1045 return 0;
1046}
1047
1048static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1049{
1050 return 0;
1051}
1052
1053static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1054 int64_t *timestamp)
1055{
1056 return -EINVAL;
1057}
1058
1059/** audio_stream_in implementation **/
1060static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1061{
1062 struct stream_in *in = (struct stream_in *)stream;
1063
1064 return in->config.rate;
1065}
1066
1067static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1068{
1069 return -ENOSYS;
1070}
1071
1072static size_t in_get_buffer_size(const struct audio_stream *stream)
1073{
1074 struct stream_in *in = (struct stream_in *)stream;
1075
1076 return in->config.period_size * audio_stream_frame_size(stream);
1077}
1078
1079static uint32_t in_get_channels(const struct audio_stream *stream)
1080{
1081 struct stream_in *in = (struct stream_in *)stream;
1082
1083 return in->channel_mask;
1084}
1085
1086static audio_format_t in_get_format(const struct audio_stream *stream)
1087{
1088 return AUDIO_FORMAT_PCM_16_BIT;
1089}
1090
1091static int in_set_format(struct audio_stream *stream, audio_format_t format)
1092{
1093 return -ENOSYS;
1094}
1095
1096static int in_standby(struct audio_stream *stream)
1097{
1098 struct stream_in *in = (struct stream_in *)stream;
1099 struct audio_device *adev = in->dev;
1100 int status = 0;
Eric Laurent994a6932013-07-17 11:51:42 -07001101 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001102 pthread_mutex_lock(&in->lock);
1103 if (!in->standby) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001104 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001105 if (in->pcm) {
1106 pcm_close(in->pcm);
1107 in->pcm = NULL;
1108 }
1109 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001110 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001111 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001112 }
1113 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001114 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001115 return status;
1116}
1117
1118static int in_dump(const struct audio_stream *stream, int fd)
1119{
1120 return 0;
1121}
1122
1123static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1124{
1125 struct stream_in *in = (struct stream_in *)stream;
1126 struct audio_device *adev = in->dev;
1127 struct str_parms *parms;
1128 char *str;
1129 char value[32];
1130 int ret, val = 0;
1131
Eric Laurent994a6932013-07-17 11:51:42 -07001132 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001133 parms = str_parms_create_str(kvpairs);
1134
1135 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1136
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001137 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001138 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001139 if (ret >= 0) {
1140 val = atoi(value);
1141 /* no audio source uses val == 0 */
1142 if ((in->source != val) && (val != 0)) {
1143 in->source = val;
1144 }
1145 }
1146
1147 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1148 if (ret >= 0) {
1149 val = atoi(value);
1150 if ((in->device != val) && (val != 0)) {
1151 in->device = val;
1152 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001153 if (!in->standby)
1154 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001155 }
1156 }
1157
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001158 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001159 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001160
1161 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001162 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001163 return ret;
1164}
1165
1166static char* in_get_parameters(const struct audio_stream *stream,
1167 const char *keys)
1168{
1169 return strdup("");
1170}
1171
1172static int in_set_gain(struct audio_stream_in *stream, float gain)
1173{
1174 return 0;
1175}
1176
1177static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1178 size_t bytes)
1179{
1180 struct stream_in *in = (struct stream_in *)stream;
1181 struct audio_device *adev = in->dev;
1182 int i, ret = -1;
1183
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001184 pthread_mutex_lock(&in->lock);
1185 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001186 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001187 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001188 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001189 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001190 goto exit;
1191 }
1192 in->standby = 0;
1193 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001194
1195 if (in->pcm) {
1196 ret = pcm_read(in->pcm, buffer, bytes);
1197 }
1198
1199 /*
1200 * Instead of writing zeroes here, we could trust the hardware
1201 * to always provide zeroes when muted.
1202 */
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001203 if (ret == 0 && voice_get_mic_mute(adev))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001204 memset(buffer, 0, bytes);
1205
1206exit:
1207 pthread_mutex_unlock(&in->lock);
1208
1209 if (ret != 0) {
1210 in_standby(&in->stream.common);
1211 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1212 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1213 in_get_sample_rate(&in->stream.common));
1214 }
1215 return bytes;
1216}
1217
1218static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1219{
1220 return 0;
1221}
1222
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001223static int add_remove_audio_effect(const struct audio_stream *stream,
1224 effect_handle_t effect,
1225 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001226{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001227 struct stream_in *in = (struct stream_in *)stream;
1228 int status = 0;
1229 effect_descriptor_t desc;
1230
1231 status = (*effect)->get_descriptor(effect, &desc);
1232 if (status != 0)
1233 return status;
1234
1235 pthread_mutex_lock(&in->lock);
1236 pthread_mutex_lock(&in->dev->lock);
1237 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1238 in->enable_aec != enable &&
1239 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1240 in->enable_aec = enable;
1241 if (!in->standby)
1242 select_devices(in->dev, in->usecase);
1243 }
1244 pthread_mutex_unlock(&in->dev->lock);
1245 pthread_mutex_unlock(&in->lock);
1246
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001247 return 0;
1248}
1249
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001250static int in_add_audio_effect(const struct audio_stream *stream,
1251 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001252{
Eric Laurent994a6932013-07-17 11:51:42 -07001253 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001254 return add_remove_audio_effect(stream, effect, true);
1255}
1256
1257static int in_remove_audio_effect(const struct audio_stream *stream,
1258 effect_handle_t effect)
1259{
Eric Laurent994a6932013-07-17 11:51:42 -07001260 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001261 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001262}
1263
1264static int adev_open_output_stream(struct audio_hw_device *dev,
1265 audio_io_handle_t handle,
1266 audio_devices_t devices,
1267 audio_output_flags_t flags,
1268 struct audio_config *config,
1269 struct audio_stream_out **stream_out)
1270{
1271 struct audio_device *adev = (struct audio_device *)dev;
1272 struct stream_out *out;
1273 int i, ret;
1274
Eric Laurent994a6932013-07-17 11:51:42 -07001275 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001276 __func__, config->sample_rate, config->channel_mask, devices, flags);
1277 *stream_out = NULL;
1278 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1279
1280 if (devices == AUDIO_DEVICE_NONE)
1281 devices = AUDIO_DEVICE_OUT_SPEAKER;
1282
1283 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
1284 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1285 out->flags = flags;
1286 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001287 out->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001288
1289 /* Init use case and pcm_config */
1290 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
1291 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001292 pthread_mutex_lock(&adev->lock);
1293 ret = read_hdmi_channel_masks(out);
1294 pthread_mutex_unlock(&adev->lock);
1295 if (ret != 0) {
1296 /* If HDMI does not support multi channel playback, set the default */
1297 out->config.channels = popcount(out->channel_mask);
Eric Laurentb23d5282013-05-14 15:27:20 -07001298 platform_set_hdmi_channels(adev->platform, out->config.channels);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001299 goto error_open;
1300 }
1301
1302 if (config->sample_rate == 0)
1303 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1304 if (config->channel_mask == 0)
1305 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1306
1307 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001308 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
1309 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001310 out->config.rate = config->sample_rate;
1311 out->config.channels = popcount(out->channel_mask);
1312 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Eric Laurentb23d5282013-05-14 15:27:20 -07001313 platform_set_hdmi_channels(adev->platform, out->config.channels);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001314 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
1315 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
1316 out->config = pcm_config_deep_buffer;
1317 } else {
1318 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
1319 out->config = pcm_config_low_latency;
1320 }
1321
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001322 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1323 if(adev->primary_output == NULL)
1324 adev->primary_output = out;
1325 else {
1326 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001327 ret = -EEXIST;
1328 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001329 }
1330 }
1331
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001332 /* Check if this usecase is already existing */
1333 pthread_mutex_lock(&adev->lock);
1334 if (get_usecase_from_list(adev, out->usecase) != NULL) {
1335 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001336 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001337 ret = -EEXIST;
1338 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001339 }
1340 pthread_mutex_unlock(&adev->lock);
1341
1342 out->stream.common.get_sample_rate = out_get_sample_rate;
1343 out->stream.common.set_sample_rate = out_set_sample_rate;
1344 out->stream.common.get_buffer_size = out_get_buffer_size;
1345 out->stream.common.get_channels = out_get_channels;
1346 out->stream.common.get_format = out_get_format;
1347 out->stream.common.set_format = out_set_format;
1348 out->stream.common.standby = out_standby;
1349 out->stream.common.dump = out_dump;
1350 out->stream.common.set_parameters = out_set_parameters;
1351 out->stream.common.get_parameters = out_get_parameters;
1352 out->stream.common.add_audio_effect = out_add_audio_effect;
1353 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1354 out->stream.get_latency = out_get_latency;
1355 out->stream.set_volume = out_set_volume;
1356 out->stream.write = out_write;
1357 out->stream.get_render_position = out_get_render_position;
1358 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
1359
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001360 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07001361 /* out->muted = false; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001362
1363 config->format = out->stream.common.get_format(&out->stream.common);
1364 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
1365 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
1366
1367 *stream_out = &out->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07001368 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001369 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001370
1371error_open:
1372 free(out);
1373 *stream_out = NULL;
1374 ALOGD("%s: exit: ret %d", __func__, ret);
1375 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001376}
1377
1378static void adev_close_output_stream(struct audio_hw_device *dev,
1379 struct audio_stream_out *stream)
1380{
Eric Laurent994a6932013-07-17 11:51:42 -07001381 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001382 out_standby(&stream->common);
1383 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07001384 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001385}
1386
1387static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
1388{
1389 struct audio_device *adev = (struct audio_device *)dev;
1390 struct str_parms *parms;
1391 char *str;
1392 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07001393 int val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001394 int ret;
1395
Eric Laurent994a6932013-07-17 11:51:42 -07001396 ALOGV("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001397 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001398
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001399 voice_set_parameters(adev, parms);
1400 platform_set_parameters(adev->platform, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001401
1402 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
1403 if (ret >= 0) {
1404 /* When set to false, HAL should disable EC and NS
1405 * But it is currently not supported.
1406 */
1407 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
1408 adev->bluetooth_nrec = true;
1409 else
1410 adev->bluetooth_nrec = false;
1411 }
1412
1413 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
1414 if (ret >= 0) {
1415 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
1416 adev->screen_off = false;
1417 else
1418 adev->screen_off = true;
1419 }
1420
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07001421 ret = str_parms_get_int(parms, "rotation", &val);
1422 if (ret >= 0) {
1423 bool reverse_speakers = false;
1424 switch(val) {
1425 // FIXME: note that the code below assumes that the speakers are in the correct placement
1426 // relative to the user when the device is rotated 90deg from its default rotation. This
1427 // assumption is device-specific, not platform-specific like this code.
1428 case 270:
1429 reverse_speakers = true;
1430 break;
1431 case 0:
1432 case 90:
1433 case 180:
1434 break;
1435 default:
1436 ALOGE("%s: unexpected rotation of %d", __func__, val);
1437 }
1438 pthread_mutex_lock(&adev->lock);
1439 if (adev->speaker_lr_swap != reverse_speakers) {
1440 adev->speaker_lr_swap = reverse_speakers;
1441 // only update the selected device if there is active pcm playback
1442 struct audio_usecase *usecase;
1443 struct listnode *node;
1444 list_for_each(node, &adev->usecase_list) {
1445 usecase = node_to_item(node, struct audio_usecase, list);
1446 if (usecase->type == PCM_PLAYBACK) {
1447 select_devices(adev, usecase->id);
1448 break;
1449 }
1450 }
1451 }
1452 pthread_mutex_unlock(&adev->lock);
1453 }
1454
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07001455 audio_extn_set_parameters(adev, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001456 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001457 ALOGV("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001458 return ret;
1459}
1460
1461static char* adev_get_parameters(const struct audio_hw_device *dev,
1462 const char *keys)
1463{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001464 struct audio_device *adev = (struct audio_device *)dev;
1465 struct str_parms *reply = str_parms_create();
1466 struct str_parms *query = str_parms_create_str(keys);
1467 char *str;
1468
1469 audio_extn_get_parameters(adev, query, reply);
1470 platform_get_parameters(adev->platform, query, reply);
1471 str = str_parms_to_str(reply);
1472 str_parms_destroy(query);
1473 str_parms_destroy(reply);
1474
1475 ALOGV("%s: exit: returns - %s", __func__, str);
1476 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001477}
1478
1479static int adev_init_check(const struct audio_hw_device *dev)
1480{
1481 return 0;
1482}
1483
1484static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
1485{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001486 return voice_set_volume((struct audio_device *)dev, volume);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001487}
1488
1489static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
1490{
1491 return -ENOSYS;
1492}
1493
1494static int adev_get_master_volume(struct audio_hw_device *dev,
1495 float *volume)
1496{
1497 return -ENOSYS;
1498}
1499
1500static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
1501{
1502 return -ENOSYS;
1503}
1504
1505static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
1506{
1507 return -ENOSYS;
1508}
1509
1510static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
1511{
1512 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001513 pthread_mutex_lock(&adev->lock);
1514 if (adev->mode != mode) {
1515 adev->mode = mode;
1516 }
1517 pthread_mutex_unlock(&adev->lock);
1518 return 0;
1519}
1520
1521static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
1522{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001523 return voice_set_mic_mute((struct audio_device *)dev, state);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001524}
1525
1526static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1527{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001528 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001529 return 0;
1530}
1531
1532static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
1533 const struct audio_config *config)
1534{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001535 int channel_count = popcount(config->channel_mask);
1536
1537 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
1538}
1539
1540static int adev_open_input_stream(struct audio_hw_device *dev,
1541 audio_io_handle_t handle,
1542 audio_devices_t devices,
1543 struct audio_config *config,
1544 struct audio_stream_in **stream_in)
1545{
1546 struct audio_device *adev = (struct audio_device *)dev;
1547 struct stream_in *in;
1548 int ret, buffer_size, frame_size;
1549 int channel_count = popcount(config->channel_mask);
1550
Eric Laurent994a6932013-07-17 11:51:42 -07001551 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001552 *stream_in = NULL;
1553 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
1554 return -EINVAL;
1555
1556 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
1557
1558 in->stream.common.get_sample_rate = in_get_sample_rate;
1559 in->stream.common.set_sample_rate = in_set_sample_rate;
1560 in->stream.common.get_buffer_size = in_get_buffer_size;
1561 in->stream.common.get_channels = in_get_channels;
1562 in->stream.common.get_format = in_get_format;
1563 in->stream.common.set_format = in_set_format;
1564 in->stream.common.standby = in_standby;
1565 in->stream.common.dump = in_dump;
1566 in->stream.common.set_parameters = in_set_parameters;
1567 in->stream.common.get_parameters = in_get_parameters;
1568 in->stream.common.add_audio_effect = in_add_audio_effect;
1569 in->stream.common.remove_audio_effect = in_remove_audio_effect;
1570 in->stream.set_gain = in_set_gain;
1571 in->stream.read = in_read;
1572 in->stream.get_input_frames_lost = in_get_input_frames_lost;
1573
1574 in->device = devices;
1575 in->source = AUDIO_SOURCE_DEFAULT;
1576 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001577 in->standby = 1;
1578 in->channel_mask = config->channel_mask;
1579
1580 /* Update config params with the requested sample rate and channels */
1581 in->usecase = USECASE_AUDIO_RECORD;
1582 in->config = pcm_config_audio_capture;
1583 in->config.channels = channel_count;
1584 in->config.rate = config->sample_rate;
1585
1586 frame_size = audio_stream_frame_size((struct audio_stream *)in);
1587 buffer_size = get_input_buffer_size(config->sample_rate,
1588 config->format,
1589 channel_count);
1590 in->config.period_size = buffer_size / frame_size;
1591
1592 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07001593 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001594 return 0;
1595
1596err_open:
1597 free(in);
1598 *stream_in = NULL;
1599 return ret;
1600}
1601
1602static void adev_close_input_stream(struct audio_hw_device *dev,
1603 struct audio_stream_in *stream)
1604{
Eric Laurent994a6932013-07-17 11:51:42 -07001605 ALOGV("%s", __func__);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001606
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001607 in_standby(&stream->common);
1608 free(stream);
1609
1610 return;
1611}
1612
1613static int adev_dump(const audio_hw_device_t *device, int fd)
1614{
1615 return 0;
1616}
1617
1618static int adev_close(hw_device_t *device)
1619{
1620 struct audio_device *adev = (struct audio_device *)device;
1621 audio_route_free(adev->audio_route);
Eric Laurentb23d5282013-05-14 15:27:20 -07001622 free(adev->snd_dev_ref_cnt);
1623 platform_deinit(adev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001624 free(device);
1625 return 0;
1626}
1627
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001628static int adev_open(const hw_module_t *module, const char *name,
1629 hw_device_t **device)
1630{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001631 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001632
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001633 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001634 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
1635
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001636 pthread_mutex_lock(&adev_init_lock);
1637 if (is_adev_initialised == true){
1638 *device = &adev->device.common;
1639 ALOGD("%s: returning existing instance of adev", __func__);
1640 ALOGD("%s: exit", __func__);
1641 pthread_mutex_unlock(&adev_init_lock);
1642 return 0;
1643 }
1644
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001645 adev = calloc(1, sizeof(struct audio_device));
1646
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001647 adev->device.common.tag = HARDWARE_DEVICE_TAG;
1648 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
1649 adev->device.common.module = (struct hw_module_t *)module;
1650 adev->device.common.close = adev_close;
1651
1652 adev->device.init_check = adev_init_check;
1653 adev->device.set_voice_volume = adev_set_voice_volume;
1654 adev->device.set_master_volume = adev_set_master_volume;
1655 adev->device.get_master_volume = adev_get_master_volume;
1656 adev->device.set_master_mute = adev_set_master_mute;
1657 adev->device.get_master_mute = adev_get_master_mute;
1658 adev->device.set_mode = adev_set_mode;
1659 adev->device.set_mic_mute = adev_set_mic_mute;
1660 adev->device.get_mic_mute = adev_get_mic_mute;
1661 adev->device.set_parameters = adev_set_parameters;
1662 adev->device.get_parameters = adev_get_parameters;
1663 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
1664 adev->device.open_output_stream = adev_open_output_stream;
1665 adev->device.close_output_stream = adev_close_output_stream;
1666 adev->device.open_input_stream = adev_open_input_stream;
1667 adev->device.close_input_stream = adev_close_input_stream;
1668 adev->device.dump = adev_dump;
1669
1670 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001671 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08001672 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001673 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001674 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001675 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08001676 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurentb23d5282013-05-14 15:27:20 -07001677 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001678 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001679 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001680
1681 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07001682 adev->platform = platform_init(adev);
1683 if (!adev->platform) {
1684 free(adev->snd_dev_ref_cnt);
1685 free(adev);
1686 ALOGE("%s: Failed to init platform data, aborting.", __func__);
1687 *device = NULL;
1688 return -EINVAL;
1689 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001690 *device = &adev->device.common;
1691
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001692 /* update init flag*/
1693 is_adev_initialised = true;
1694 pthread_mutex_unlock(&adev_init_lock);
1695
Eric Laurent994a6932013-07-17 11:51:42 -07001696 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001697 return 0;
1698}
1699
1700static struct hw_module_methods_t hal_module_methods = {
1701 .open = adev_open,
1702};
1703
1704struct audio_module HAL_MODULE_INFO_SYM = {
1705 .common = {
1706 .tag = HARDWARE_MODULE_TAG,
1707 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1708 .hal_api_version = HARDWARE_HAL_API_VERSION,
1709 .id = AUDIO_HARDWARE_MODULE_ID,
1710 .name = "QCOM Audio HAL",
1711 .author = "Code Aurora Forum",
1712 .methods = &hal_module_methods,
1713 },
1714};