audio_hw.c revision 8455fa7d7e51680e45b6a88d28cf48074280e2f9
1/* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#define LOG_TAG "audio_hw_primary" 18/*#define LOG_NDEBUG 0*/ 19 20#include <errno.h> 21#include <pthread.h> 22#include <stdint.h> 23#include <sys/time.h> 24#include <stdlib.h> 25#include <dlfcn.h> 26#include <math.h> 27 28#include <cutils/log.h> 29#include <cutils/str_parms.h> 30#include <cutils/properties.h> 31 32#include "audio_hw.h" 33 34#define LIB_ACDB_LOADER "/system/lib/libacdbloader.so" 35#define LIB_CSD_CLIENT "/system/lib/libcsd-client.so" 36#define MIXER_XML_PATH "/system/etc/mixer_paths.xml" 37#define MIXER_CARD 0 38 39#define STRING_TO_ENUM(string) { #string, string } 40 41/* Flags used to initialize acdb_settings variable that goes to ACDB library */ 42#define DMIC_FLAG 0x00000002 43#define TTY_MODE_OFF 0x00000010 44#define TTY_MODE_FULL 0x00000020 45#define TTY_MODE_VCO 0x00000040 46#define TTY_MODE_HCO 0x00000080 47#define TTY_MODE_CLEAR 0xFFFFFF0F 48 49struct string_to_enum { 50 const char *name; 51 uint32_t value; 52}; 53 54static const struct string_to_enum out_channels_name_to_enum_table[] = { 55 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO), 56 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1), 57 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1), 58}; 59 60static const char * const use_case_table[AUDIO_USECASE_MAX] = { 61 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback", 62 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback", 63 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback", 64 [USECASE_AUDIO_RECORD] = "audio-record", 65 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record", 66 [USECASE_VOICE_CALL] = "voice-call", 67}; 68 69static const int pcm_device_table[AUDIO_USECASE_MAX][2] = { 70 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0}, 71 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {14, 14}, 72 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = {1, 1}, 73 [USECASE_AUDIO_RECORD] = {0, 0}, 74 [USECASE_AUDIO_RECORD_LOW_LATENCY] = {14, 14}, 75 [USECASE_VOICE_CALL] = {12, 12}, 76}; 77 78/* Array to store sound devices */ 79static const char * const device_table[SND_DEVICE_ALL] = { 80 /* Playback sound devices */ 81 [SND_DEVICE_OUT_HANDSET] = "handset", 82 [SND_DEVICE_OUT_SPEAKER] = "speaker", 83 [SND_DEVICE_OUT_HEADPHONES] = "headphones", 84 [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = "speaker-and-headphones", 85 [SND_DEVICE_OUT_VOICE_SPEAKER] = "voice-speaker", 86 [SND_DEVICE_OUT_VOICE_HEADPHONES] = "voice-headphones", 87 [SND_DEVICE_OUT_HDMI] = "hdmi", 88 [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = "speaker-and-hdmi", 89 [SND_DEVICE_OUT_BT_SCO] = "bt-sco-headset", 90 [SND_DEVICE_OUT_VOICE_HANDSET_TMUS] = "voice-handset-tmus", 91 [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = "voice-tty-full-headphones", 92 [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = "voice-tty-vco-headphones", 93 [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = "voice-tty-hco-handset", 94 95 /* Capture sound devices */ 96 [SND_DEVICE_IN_HANDSET_MIC] = "handset-mic", 97 [SND_DEVICE_IN_SPEAKER_MIC] = "speaker-mic", 98 [SND_DEVICE_IN_HEADSET_MIC] = "headset-mic", 99 [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = "voice-speaker-mic", 100 [SND_DEVICE_IN_VOICE_HEADSET_MIC] = "voice-headset-mic", 101 [SND_DEVICE_IN_HDMI_MIC] = "hdmi-mic", 102 [SND_DEVICE_IN_BT_SCO_MIC] = "bt-sco-mic", 103 [SND_DEVICE_IN_CAMCORDER_MIC] = "camcorder-mic", 104 [SND_DEVICE_IN_VOICE_DMIC_EF] = "voice-dmic-ef", 105 [SND_DEVICE_IN_VOICE_DMIC_BS] = "voice-dmic-bs", 106 [SND_DEVICE_IN_VOICE_DMIC_EF_TMUS] = "voice-dmic-ef-tmus", 107 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF] = "voice-speaker-dmic-ef", 108 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS] = "voice-speaker-dmic-bs", 109 [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = "voice-tty-full-headset-mic", 110 [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = "voice-tty-vco-handset-mic", 111 [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = "voice-tty-hco-headset-mic", 112 [SND_DEVICE_IN_VOICE_REC_MIC] = "voice-rec-mic", 113 [SND_DEVICE_IN_VOICE_REC_DMIC_EF] = "voice-rec-dmic-ef", 114 [SND_DEVICE_IN_VOICE_REC_DMIC_BS] = "voice-rec-dmic-bs", 115 [SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE] = "voice-rec-dmic-ef-fluence", 116 [SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE] = "voice-rec-dmic-bs-fluence", 117}; 118 119/* ACDB IDs (audio DSP path configuration IDs) for each sound device */ 120static const int acdb_device_table[SND_DEVICE_ALL] = { 121 [SND_DEVICE_OUT_HANDSET] = 7, 122 [SND_DEVICE_OUT_SPEAKER] = 14, 123 [SND_DEVICE_OUT_HEADPHONES] = 10, 124 [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = 10, 125 [SND_DEVICE_OUT_VOICE_SPEAKER] = 14, 126 [SND_DEVICE_OUT_VOICE_HEADPHONES] = 10, 127 [SND_DEVICE_OUT_HDMI] = 18, 128 [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = 14, 129 [SND_DEVICE_OUT_BT_SCO] = 22, 130 [SND_DEVICE_OUT_VOICE_HANDSET_TMUS] = 81, 131 [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = 17, 132 [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = 17, 133 [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = 37, 134 135 [SND_DEVICE_IN_HANDSET_MIC] = 4, 136 [SND_DEVICE_IN_SPEAKER_MIC] = 4, 137 [SND_DEVICE_IN_HEADSET_MIC] = 8, 138 [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = 11, 139 [SND_DEVICE_IN_VOICE_HEADSET_MIC] = 8, 140 [SND_DEVICE_IN_HDMI_MIC] = 4, 141 [SND_DEVICE_IN_BT_SCO_MIC] = 21, 142 [SND_DEVICE_IN_CAMCORDER_MIC] = 61, 143 [SND_DEVICE_IN_VOICE_DMIC_EF] = 6, 144 [SND_DEVICE_IN_VOICE_DMIC_BS] = 5, 145 [SND_DEVICE_IN_VOICE_DMIC_EF_TMUS] = 91, 146 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF] = 13, 147 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS] = 12, 148 [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = 16, 149 [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = 36, 150 [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = 16, 151 [SND_DEVICE_IN_VOICE_REC_MIC] = 62, 152 [SND_DEVICE_IN_VOICE_REC_DMIC_EF] = 62, 153 [SND_DEVICE_IN_VOICE_REC_DMIC_BS] = 62, 154 [SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE] = 62, 155 [SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE] = 62, 156}; 157 158int edid_get_max_channels(void); 159 160static int get_pcm_device_id(struct audio_route *ar, 161 audio_usecase_t usecase, 162 int device_type) 163{ 164 ALOGV("%s: enter: usecase(%d)", __func__, usecase); 165 int device_id; 166 if (device_type == PCM_PLAYBACK) 167 device_id = pcm_device_table[usecase][0]; 168 else 169 device_id = pcm_device_table[usecase][1]; 170 ALOGV("%s: exit: device_id(%d)", __func__, device_id); 171 return device_id; 172} 173 174static int get_acdb_device_id(snd_device_t snd_device) 175{ 176 ALOGV("%s: enter: snd_devie(%d)", __func__, snd_device); 177 int acdb_dev_id = acdb_device_table[snd_device]; 178 ALOGV("%s: exit: acdb_dev_id(%d)", __func__, acdb_dev_id); 179 return acdb_dev_id; 180} 181 182static void add_backend_name(char *mixer_path, 183 snd_device_t snd_device) 184{ 185 if (snd_device == SND_DEVICE_OUT_HDMI || 186 snd_device == SND_DEVICE_IN_HDMI_MIC) 187 strcat(mixer_path, " hdmi"); 188 else if(snd_device == SND_DEVICE_OUT_BT_SCO || 189 snd_device == SND_DEVICE_IN_BT_SCO_MIC) 190 strcat(mixer_path, " bt-sco"); 191 else if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_HDMI) 192 strcat(mixer_path, " speaker-and-hdmi"); 193} 194 195static int enable_audio_route(struct audio_route *ar, 196 audio_usecase_t usecase, 197 snd_device_t snd_device) 198{ 199 ALOGV("%s: enter: usecase(%d) snd_device(%d)", 200 __func__, usecase, snd_device); 201 char mixer_path[50]; 202 strcpy(mixer_path, use_case_table[usecase]); 203 add_backend_name(mixer_path, snd_device); 204 audio_route_apply_path(ar, mixer_path); 205 ALOGV("%s: exit", __func__); 206 return 0; 207} 208 209static int disable_audio_route(struct audio_route *ar, 210 audio_usecase_t usecase, 211 snd_device_t snd_device) 212{ 213 ALOGV("%s: enter: usecase(%d) snd_device(%d)", 214 __func__, usecase, snd_device); 215 char mixer_path[50]; 216 strcpy(mixer_path, use_case_table[usecase]); 217 add_backend_name(mixer_path, snd_device); 218 audio_route_reset_path(ar, mixer_path); 219 ALOGV("%s: exit", __func__); 220 return 0; 221} 222 223static int enable_snd_device(struct audio_device *adev, 224 snd_device_t snd_device) 225{ 226 int acdb_dev_id, acdb_dev_type; 227 228 ALOGV("%s: enter: snd_device(%d)", __func__, snd_device); 229 acdb_dev_id = get_acdb_device_id(snd_device); 230 if (acdb_dev_id < 0) { 231 ALOGE("%s: Could not find acdb id for device(%d)", 232 __func__, snd_device); 233 return -EINVAL; 234 } 235 if (snd_device >= SND_DEVICE_OUT_BEGIN && 236 snd_device < SND_DEVICE_OUT_END) { 237 acdb_dev_type = ACDB_DEV_TYPE_OUT; 238 } else { 239 acdb_dev_type = ACDB_DEV_TYPE_IN; 240 } 241 if (adev->acdb_send_audio_cal) { 242 ALOGV("%s: sending audio calibration for snd_device(%d) acdb_id(%d)", 243 __func__, snd_device, acdb_dev_id); 244 adev->acdb_send_audio_cal(acdb_dev_id, acdb_dev_type); 245 } else { 246 ALOGW("%s: Could find the symbol acdb_send_audio_cal from %s", 247 __func__, LIB_ACDB_LOADER); 248 } 249 250 audio_route_apply_path(adev->audio_route, device_table[snd_device]); 251 ALOGV("%s: exit", __func__); 252 return 0; 253} 254 255static int disable_snd_device(struct audio_route *ar, 256 snd_device_t snd_device) 257{ 258 ALOGV("%s: enter: snd_device(%d)", __func__, snd_device); 259 audio_route_reset_path(ar, device_table[snd_device]); 260 ALOGV("%s: exit", __func__); 261 return 0; 262} 263 264static int set_hdmi_channels(struct mixer *mixer, 265 int channel_count) 266{ 267 struct mixer_ctl *ctl; 268 const char *channel_cnt_str = NULL; 269 const char *mixer_ctl_name = "HDMI_RX Channels"; 270 switch (channel_count) { 271 case 8: 272 channel_cnt_str = "Eight"; break; 273 case 7: 274 channel_cnt_str = "Seven"; break; 275 case 6: 276 channel_cnt_str = "Six"; break; 277 case 5: 278 channel_cnt_str = "Five"; break; 279 case 4: 280 channel_cnt_str = "Four"; break; 281 case 3: 282 channel_cnt_str = "Three"; break; 283 default: 284 channel_cnt_str = "Two"; break; 285 } 286 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name); 287 if (!ctl) { 288 ALOGE("%s: Could not get ctl for mixer cmd - %s", 289 __func__, mixer_ctl_name); 290 return -EINVAL; 291 } 292 ALOGV("HDMI channel count: %s", channel_cnt_str); 293 mixer_ctl_set_enum_by_string(ctl, channel_cnt_str); 294 return 0; 295} 296 297/* must be called with hw device mutex locked */ 298static void read_hdmi_channel_masks(struct stream_out *out) 299{ 300 int channels = edid_get_max_channels(); 301 ALOGE("%s: enter", __func__); 302 303 switch (channels) { 304 /* 305 * Do not handle stereo output in Multi-channel cases 306 * Stereo case is handled in normal playback path 307 */ 308 case 6: 309 ALOGV("%s: HDMI supports 5.1", __func__); 310 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1; 311 break; 312 case 8: 313 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__); 314 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1; 315 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1; 316 break; 317 default: 318 ALOGE("Unsupported number of channels (%d)", channels); 319 break; 320 } 321 322 ALOGE("%s: exit", __func__); 323} 324 325static snd_device_t get_output_snd_device(struct audio_device *adev) 326{ 327 audio_source_t source = (adev->active_input == NULL) ? 328 AUDIO_SOURCE_DEFAULT : adev->active_input->source; 329 audio_mode_t mode = adev->mode; 330 audio_devices_t devices = adev->out_device; 331 snd_device_t snd_device = SND_DEVICE_INVALID; 332 333 ALOGV("%s: enter: output devices(0x%x)", __func__, devices); 334 if (devices == AUDIO_DEVICE_NONE || 335 devices & AUDIO_DEVICE_BIT_IN) { 336 ALOGV("%s: Invalid output devices (0x%x)", __func__, devices); 337 goto exit; 338 } 339 340 if (mode == AUDIO_MODE_IN_CALL) { 341 if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE || 342 devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) { 343 if (adev->tty_mode == TTY_MODE_FULL) 344 snd_device = SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES; 345 else if (adev->tty_mode == TTY_MODE_VCO) 346 snd_device = SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES; 347 else if (adev->tty_mode == TTY_MODE_HCO) 348 snd_device = SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET; 349 else 350 snd_device = SND_DEVICE_OUT_VOICE_HEADPHONES; 351 } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) { 352 snd_device = SND_DEVICE_OUT_BT_SCO; 353 } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) { 354 snd_device = SND_DEVICE_OUT_VOICE_SPEAKER; 355 } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) { 356 if (adev->is_tmus) 357 snd_device = SND_DEVICE_OUT_VOICE_HANDSET_TMUS; 358 else 359 snd_device = SND_DEVICE_OUT_HANDSET; 360 } 361 if (snd_device != SND_DEVICE_INVALID) { 362 goto exit; 363 } 364 } 365 366 if (popcount(devices) == 2) { 367 if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADPHONE | 368 AUDIO_DEVICE_OUT_SPEAKER)) { 369 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES; 370 } else if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADSET | 371 AUDIO_DEVICE_OUT_SPEAKER)) { 372 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES; 373 } else if (devices == (AUDIO_DEVICE_OUT_AUX_DIGITAL | 374 AUDIO_DEVICE_OUT_SPEAKER)) { 375 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HDMI; 376 } else { 377 ALOGE("%s: Invalid combo device(0x%x)", __func__, devices); 378 goto exit; 379 } 380 if (snd_device != SND_DEVICE_INVALID) { 381 goto exit; 382 } 383 } 384 if (popcount(devices) != 1) { 385 ALOGE("%s: Invalid output devices(0x%x)", __func__, devices); 386 goto exit; 387 } 388 389 if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE || 390 devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) { 391 snd_device = SND_DEVICE_OUT_HEADPHONES; 392 } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) { 393 snd_device = SND_DEVICE_OUT_SPEAKER; 394 } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) { 395 snd_device = SND_DEVICE_OUT_BT_SCO; 396 } else if (devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) { 397 snd_device = SND_DEVICE_OUT_HDMI ; 398 } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) { 399 snd_device = SND_DEVICE_OUT_HANDSET; 400 } else { 401 ALOGE("%s: Unknown device(s) 0x%x", __func__, devices); 402 } 403exit: 404 ALOGV("%s: exit: snd_device(%s)", __func__, 405 (snd_device == SND_DEVICE_INVALID) ? 406 "none" : device_table[snd_device]); 407 return snd_device; 408} 409 410static snd_device_t get_input_snd_device(struct audio_device *adev) 411{ 412 audio_source_t source = (adev->active_input == NULL) ? 413 AUDIO_SOURCE_DEFAULT : adev->active_input->source; 414 415 audio_mode_t mode = adev->mode; 416 audio_devices_t out_device = adev->out_device; 417 audio_devices_t in_device = ((adev->active_input == NULL) ? 418 AUDIO_DEVICE_NONE : adev->active_input->device) 419 & ~AUDIO_DEVICE_BIT_IN; 420 audio_channel_mask_t channel_mask = (adev->active_input == NULL) ? 421 AUDIO_CHANNEL_IN_MONO : adev->active_input->channel_mask; 422 snd_device_t snd_device = SND_DEVICE_INVALID; 423 424 ALOGV("%s: enter: out_device(0x%x) in_device(0x%x)", 425 __func__, out_device, in_device); 426 if (mode == AUDIO_MODE_IN_CALL) { 427 if (out_device == AUDIO_DEVICE_NONE) { 428 ALOGE("%s: No output device set for voice call", __func__); 429 goto exit; 430 } 431 if (adev->tty_mode != TTY_MODE_OFF) { 432 if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE || 433 out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) { 434 switch (adev->tty_mode) { 435 case TTY_MODE_FULL: 436 snd_device = SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC; 437 break; 438 case TTY_MODE_VCO: 439 snd_device = SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC; 440 break; 441 case TTY_MODE_HCO: 442 snd_device = SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC; 443 break; 444 default: 445 ALOGE("%s: Invalid TTY mode (0x%x)", __func__, adev->tty_mode); 446 } 447 goto exit; 448 } 449 } 450 if (out_device & AUDIO_DEVICE_OUT_EARPIECE || 451 out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) { 452 if (adev->mic_type_analog || adev->fluence_in_voice_call == false) { 453 snd_device = SND_DEVICE_IN_HANDSET_MIC; 454 } else { 455 if (adev->dualmic_config == DUALMIC_CONFIG_ENDFIRE) { 456 if (adev->is_tmus) 457 snd_device = SND_DEVICE_IN_VOICE_DMIC_EF_TMUS; 458 else 459 snd_device = SND_DEVICE_IN_VOICE_DMIC_EF; 460 } else if(adev->dualmic_config == DUALMIC_CONFIG_BROADSIDE) 461 snd_device = SND_DEVICE_IN_VOICE_DMIC_BS; 462 else 463 snd_device = SND_DEVICE_IN_HANDSET_MIC; 464 } 465 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) { 466 snd_device = SND_DEVICE_IN_VOICE_HEADSET_MIC; 467 } else if (out_device & AUDIO_DEVICE_OUT_ALL_SCO) { 468 snd_device = SND_DEVICE_IN_BT_SCO_MIC ; 469 } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) { 470 if (adev->fluence_in_voice_call && 471 adev->dualmic_config == DUALMIC_CONFIG_ENDFIRE) { 472 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF; 473 } else if (adev->fluence_in_voice_call && 474 adev->dualmic_config == DUALMIC_CONFIG_BROADSIDE) { 475 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS; 476 } else { 477 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC; 478 } 479 } 480 } else if (source == AUDIO_SOURCE_CAMCORDER) { 481 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC || 482 in_device & AUDIO_DEVICE_IN_BACK_MIC) { 483 snd_device = SND_DEVICE_IN_CAMCORDER_MIC; 484 } 485 } else if (source == AUDIO_SOURCE_VOICE_RECOGNITION) { 486 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) { 487 if (adev->dualmic_config == DUALMIC_CONFIG_ENDFIRE) { 488 if (channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK) 489 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_EF; 490 else if (adev->fluence_in_voice_rec) 491 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE; 492 else 493 snd_device = SND_DEVICE_IN_VOICE_REC_MIC; 494 } else if (adev->dualmic_config == DUALMIC_CONFIG_BROADSIDE) { 495 if (channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK) 496 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_BS; 497 else if (adev->fluence_in_voice_rec) 498 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE; 499 else 500 snd_device = SND_DEVICE_IN_VOICE_REC_MIC; 501 } else 502 snd_device = SND_DEVICE_IN_VOICE_REC_MIC; 503 } 504 } else if (source == AUDIO_SOURCE_VOICE_COMMUNICATION) { 505 if (out_device & AUDIO_DEVICE_OUT_SPEAKER) 506 in_device = AUDIO_DEVICE_IN_BACK_MIC; 507 } else if (source == AUDIO_SOURCE_DEFAULT) { 508 goto exit; 509 } 510 511 if (snd_device != SND_DEVICE_INVALID) { 512 goto exit; 513 } 514 515 if (in_device != AUDIO_DEVICE_NONE && 516 !(in_device & AUDIO_DEVICE_IN_VOICE_CALL) && 517 !(in_device & AUDIO_DEVICE_IN_COMMUNICATION)) { 518 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) { 519 snd_device = SND_DEVICE_IN_HANDSET_MIC; 520 } else if (in_device & AUDIO_DEVICE_IN_BACK_MIC) { 521 if (adev->mic_type_analog) 522 snd_device = SND_DEVICE_IN_HANDSET_MIC; 523 else 524 snd_device = SND_DEVICE_IN_SPEAKER_MIC; 525 } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) { 526 snd_device = SND_DEVICE_IN_HEADSET_MIC; 527 } else if (in_device & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) { 528 snd_device = SND_DEVICE_IN_BT_SCO_MIC ; 529 } else if (in_device & AUDIO_DEVICE_IN_AUX_DIGITAL) { 530 snd_device = SND_DEVICE_IN_HDMI_MIC; 531 } else { 532 ALOGE("%s: Unknown input device(s) 0x%x", __func__, in_device); 533 ALOGW("%s: Using default handset-mic", __func__); 534 snd_device = SND_DEVICE_IN_HANDSET_MIC; 535 } 536 } else { 537 if (out_device & AUDIO_DEVICE_OUT_EARPIECE) { 538 snd_device = SND_DEVICE_IN_HANDSET_MIC; 539 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) { 540 snd_device = SND_DEVICE_IN_HEADSET_MIC; 541 } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) { 542 snd_device = SND_DEVICE_IN_SPEAKER_MIC; 543 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) { 544 snd_device = SND_DEVICE_IN_HANDSET_MIC; 545 } else if (out_device & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET) { 546 snd_device = SND_DEVICE_IN_BT_SCO_MIC; 547 } else if (out_device & AUDIO_DEVICE_OUT_AUX_DIGITAL) { 548 snd_device = SND_DEVICE_IN_HDMI_MIC; 549 } else { 550 ALOGE("%s: Unknown output device(s) 0x%x", __func__, out_device); 551 ALOGW("%s: Using default handset-mic", __func__); 552 snd_device = SND_DEVICE_IN_HANDSET_MIC; 553 } 554 } 555exit: 556 ALOGV("%s: exit: in_snd_device(%s)", __func__, 557 (snd_device == SND_DEVICE_INVALID) ? 558 "none" : device_table[snd_device]); 559 return snd_device; 560} 561 562static int select_devices(struct audio_device *adev) 563{ 564 snd_device_t out_snd_device = SND_DEVICE_INVALID; 565 snd_device_t in_snd_device = SND_DEVICE_INVALID; 566 struct audio_usecase *usecase; 567 int status = 0; 568 int acdb_rx_id, acdb_tx_id; 569 bool in_call_device_switch = false; 570 571 ALOGV("%s: enter", __func__); 572 out_snd_device = get_output_snd_device(adev); 573 in_snd_device = get_input_snd_device(adev); 574 575 if (out_snd_device == adev->cur_out_snd_device && adev->out_snd_device_active && 576 in_snd_device == adev->cur_in_snd_device && adev->in_snd_device_active) { 577 ALOGV("%s: exit: snd_devices (%d and %d) are already active", 578 __func__, out_snd_device, in_snd_device); 579 return 0; 580 } 581 582 /* 583 * Limitation: While in call, to do a device switch we need to disable 584 * and enable both RX and TX devices though one of them is same as current 585 * device. 586 */ 587 if (adev->mode == AUDIO_MODE_IN_CALL && 588 adev->csd_client != NULL && 589 out_snd_device != SND_DEVICE_INVALID && 590 in_snd_device != SND_DEVICE_INVALID && 591 adev->cur_out_snd_device != SND_DEVICE_INVALID && 592 adev->cur_in_snd_device != SND_DEVICE_INVALID) { 593 in_call_device_switch = true; 594 } 595 596 if (in_call_device_switch) { 597 /* This must be called before disabling the mixer controls on APQ side */ 598 if (adev->csd_disable_device == NULL) { 599 ALOGE("%s: dlsym error for csd_client_disable_device", 600 __func__); 601 } else { 602 status = adev->csd_disable_device(); 603 if (status < 0) { 604 ALOGE("%s: csd_client_disable_device, failed, error %d", 605 __func__, status); 606 } 607 } 608 } 609 610 if ((out_snd_device != adev->cur_out_snd_device || in_call_device_switch) 611 && adev->out_snd_device_active) { 612 usecase = &adev->usecase_list; 613 while (usecase->next != NULL) { 614 usecase = usecase->next; 615 if (usecase->type == PCM_PLAYBACK || usecase->type == VOICE_CALL) { 616 disable_audio_route(adev->audio_route, usecase->id, 617 adev->cur_out_snd_device); 618 } 619 } 620 audio_route_update_mixer(adev->audio_route); 621 /* Disable current rx device */ 622 disable_snd_device(adev->audio_route, adev->cur_out_snd_device); 623 adev->out_snd_device_active = false; 624 } 625 626 if ((in_snd_device != adev->cur_in_snd_device || in_call_device_switch) 627 && adev->in_snd_device_active) { 628 usecase = &adev->usecase_list; 629 while (usecase->next != NULL) { 630 usecase = usecase->next; 631 if (usecase->type == PCM_CAPTURE) { 632 disable_audio_route(adev->audio_route, usecase->id, 633 adev->cur_in_snd_device); 634 } 635 } 636 audio_route_update_mixer(adev->audio_route); 637 /* Disable current tx device */ 638 disable_snd_device(adev->audio_route, adev->cur_in_snd_device); 639 adev->in_snd_device_active = false; 640 } 641 642 if (out_snd_device != SND_DEVICE_INVALID && !adev->out_snd_device_active) { 643 /* Enable new rx device */ 644 status = enable_snd_device(adev, out_snd_device); 645 if (status != 0) { 646 ALOGE("%s: Failed to set mixer ctls for snd_device(%d)", 647 __func__, out_snd_device); 648 return status; 649 } 650 adev->out_snd_device_active = true; 651 adev->cur_out_snd_device = out_snd_device; 652 } 653 654 if (in_snd_device != SND_DEVICE_INVALID && !adev->in_snd_device_active) { 655 /* Enable new tx device */ 656 status = enable_snd_device(adev, in_snd_device); 657 if (status != 0) { 658 ALOGE("%s: Failed to set mixer ctls for snd_device(%d)", 659 __func__, out_snd_device); 660 return status; 661 } 662 adev->in_snd_device_active = true; 663 adev->cur_in_snd_device = in_snd_device; 664 } 665 audio_route_update_mixer(adev->audio_route); 666 667 usecase = &adev->usecase_list; 668 while (usecase->next != NULL) { 669 usecase = usecase->next; 670 if (usecase->type == PCM_PLAYBACK || usecase->type == VOICE_CALL) { 671 usecase->devices = adev->out_device; /* TODO: fix device logic */ 672 status = enable_audio_route(adev->audio_route, usecase->id, 673 adev->cur_out_snd_device); 674 } else { 675 status = enable_audio_route(adev->audio_route, usecase->id, 676 adev->cur_in_snd_device); 677 } 678 } 679 audio_route_update_mixer(adev->audio_route); 680 681 if (in_call_device_switch) { 682 if (adev->csd_enable_device == NULL) { 683 ALOGE("%s: dlsym error for csd_client_enable_device", 684 __func__); 685 } else { 686 acdb_rx_id = get_acdb_device_id(out_snd_device); 687 acdb_tx_id = get_acdb_device_id(in_snd_device); 688 689 /* ToDo: To make sure acdb_settings is updated properly based on TTY mode */ 690 status = adev->csd_enable_device(acdb_rx_id, acdb_tx_id, adev->acdb_settings); 691 if (status < 0) { 692 ALOGE("%s: csd_client_enable_device, failed, error %d", 693 __func__, status); 694 } 695 } 696 } 697 698 ALOGV("%s: exit: status(%d)", __func__, status); 699 return status; 700} 701 702static void add_usecase_to_list(struct audio_device *adev, 703 struct audio_usecase *uc_info) 704{ 705 struct audio_usecase *first_entry = adev->usecase_list.next; 706 ALOGV("%s: enter: usecase(%d)", __func__, uc_info->id); 707 /* Insert the new entry on the top of the list */ 708 adev->usecase_list.next = uc_info; 709 uc_info->next = first_entry; 710 ALOGV("%s: exit", __func__); 711} 712 713static void remove_usecase_from_list(struct audio_device *adev, 714 audio_usecase_t uc_id) 715{ 716 struct audio_usecase *uc_to_remove = NULL; 717 struct audio_usecase *list_head = &adev->usecase_list; 718 ALOGV("%s: enter: usecase(%d)", __func__, uc_id); 719 while (list_head->next != NULL) { 720 if (list_head->next->id == uc_id) { 721 uc_to_remove = list_head->next; 722 list_head->next = list_head->next->next; 723 free(uc_to_remove); 724 break; 725 } 726 list_head = list_head->next; 727 } 728 ALOGV("%s: exit", __func__); 729} 730 731static struct audio_usecase *get_usecase_from_list(struct audio_device *adev, 732 audio_usecase_t uc_id) 733{ 734 struct audio_usecase *uc_info = NULL; 735 struct audio_usecase *list_head = &adev->usecase_list; 736 ALOGV("%s: enter: uc_id(%d)", __func__, uc_id); 737 while (list_head->next != NULL) { 738 list_head = list_head->next; 739 if (list_head->id == uc_id) { 740 uc_info = list_head; 741 break; 742 } 743 } 744 ALOGV("%s: exit: uc_info(%p)", __func__, uc_info); 745 return uc_info; 746} 747 748static int get_num_active_usecases(struct audio_device *adev) 749{ 750 int num_uc = 0; 751 struct audio_usecase *list_head = &adev->usecase_list; 752 while (list_head->next != NULL) { 753 num_uc++; 754 list_head = list_head->next; 755 } 756 return num_uc; 757} 758 759static audio_devices_t get_active_out_devices(struct audio_device *adev, 760 audio_usecase_t usecase) 761{ 762 audio_devices_t devices = 0; 763 struct audio_usecase *list_head = &adev->usecase_list; 764 /* Return the output devices of usecases other than given usecase */ 765 while (list_head->next != NULL) { 766 list_head = list_head->next; 767 if (list_head->type == PCM_PLAYBACK && list_head->id != usecase) { 768 devices |= list_head->devices; 769 } 770 } 771 return devices; 772} 773 774static audio_devices_t get_voice_call_out_device(struct audio_device *adev) 775{ 776 audio_devices_t devices = 0; 777 struct audio_usecase *list_head = &adev->usecase_list; 778 /* Return the output devices of usecases other than given usecase */ 779 while (list_head->next != NULL) { 780 list_head = list_head->next; 781 if (list_head->id == USECASE_VOICE_CALL) { 782 devices = list_head->devices; 783 break; 784 } 785 } 786 return devices; 787} 788 789static int stop_input_stream(struct stream_in *in) 790{ 791 int i, ret = 0; 792 snd_device_t in_snd_device; 793 struct audio_usecase *uc_info; 794 struct audio_device *adev = in->dev; 795 796 adev->active_input = NULL; 797 798 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase); 799 uc_info = get_usecase_from_list(adev, in->usecase); 800 if (uc_info == NULL) { 801 ALOGE("%s: Could not find the usecase (%d) in the list", 802 __func__, in->usecase); 803 return -EINVAL; 804 } 805 806 /* 1. Close the PCM device first */ 807 if (in->pcm) { 808 pcm_close(in->pcm); 809 in->pcm = NULL; 810 } 811 812 /* 2. Disable stream specific mixer controls */ 813 in_snd_device = adev->cur_in_snd_device; 814 disable_audio_route(adev->audio_route, in->usecase, in_snd_device); 815 audio_route_update_mixer(adev->audio_route); 816 817 remove_usecase_from_list(adev, in->usecase); 818 819 /* 3. Disable the tx device */ 820 select_devices(adev); 821 822 ALOGV("%s: exit: status(%d)", __func__, ret); 823 return ret; 824} 825 826int start_input_stream(struct stream_in *in) 827{ 828 /* 1. Enable output device and stream routing controls */ 829 int ret = 0; 830 snd_device_t in_snd_device; 831 struct audio_usecase *uc_info; 832 struct audio_device *adev = in->dev; 833 834 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase); 835 adev->active_input = in; 836 in_snd_device = get_input_snd_device(adev); 837 if (in_snd_device == SND_DEVICE_INVALID) { 838 ALOGE("%s: Could not get valid input sound device", __func__); 839 ret = -EINVAL; 840 goto error_config; 841 } 842 843 in->pcm_device_id = get_pcm_device_id(adev->audio_route, 844 in->usecase, 845 PCM_CAPTURE); 846 if (in->pcm_device_id < 0) { 847 ALOGE("%s: Could not find PCM device id for the usecase(%d)", 848 __func__, in->usecase); 849 ret = -EINVAL; 850 goto error_config; 851 } 852 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase)); 853 uc_info->id = in->usecase; 854 uc_info->type = PCM_CAPTURE; 855 uc_info->devices = in->device; 856 857 /* 1. Enable the TX device */ 858 ret = select_devices(adev); 859 if (ret) { 860 ALOGE("%s: Failed to enable device(0x%x)", 861 __func__, in->device); 862 free(uc_info); 863 goto error_config; 864 } 865 in_snd_device = adev->cur_in_snd_device; 866 867 /* 2. Enable the mixer controls for the audio route */ 868 enable_audio_route(adev->audio_route, in->usecase, in_snd_device); 869 audio_route_update_mixer(adev->audio_route); 870 871 /* 3. Add the usecase info to usecase list */ 872 add_usecase_to_list(adev, uc_info); 873 874 /* 2. Open the pcm device */ 875 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d", 876 __func__, SOUND_CARD, in->pcm_device_id, in->config.channels); 877 in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id, 878 PCM_IN, &in->config); 879 if (in->pcm && !pcm_is_ready(in->pcm)) { 880 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm)); 881 pcm_close(in->pcm); 882 in->pcm = NULL; 883 ret = -EIO; 884 goto error_open; 885 } 886 ALOGV("%s: exit", __func__); 887 return ret; 888 889error_open: 890 stop_input_stream(in); 891 892error_config: 893 adev->active_input = NULL; 894 ALOGV("%s: exit: status(%d)", __func__, ret); 895 896 return ret; 897} 898 899static int stop_output_stream(struct stream_out *out) 900{ 901 int i, ret = 0; 902 snd_device_t out_snd_device; 903 struct audio_usecase *uc_info; 904 struct audio_device *adev = out->dev; 905 906 ALOGV("%s: enter: usecase(%d)", __func__, out->usecase); 907 uc_info = get_usecase_from_list(adev, out->usecase); 908 if (uc_info == NULL) { 909 ALOGE("%s: Could not find the usecase (%d) in the list", 910 __func__, out->usecase); 911 return -EINVAL; 912 } 913 914 /* 1. Close the PCM device first */ 915 if (out->pcm) { 916 pcm_close(out->pcm); 917 out->pcm = NULL; 918 } 919 920 /* 2. Get and set stream specific mixer controls */ 921 out_snd_device = adev->cur_out_snd_device; 922 disable_audio_route(adev->audio_route, out->usecase, out_snd_device); 923 audio_route_update_mixer(adev->audio_route); 924 925 remove_usecase_from_list(adev, uc_info->id); 926 927 /* 3. Disable the rx device */ 928 adev->out_device = get_active_out_devices(adev, out->usecase); 929 adev->out_device |= get_voice_call_out_device(adev); 930 ret = select_devices(adev); 931 932 ALOGV("%s: exit: status(%d)", __func__, ret); 933 return ret; 934} 935 936int start_output_stream(struct stream_out *out) 937{ 938 int status; 939 int ret = 0; 940 snd_device_t out_snd_device; 941 struct audio_usecase *uc_info; 942 struct audio_device *adev = out->dev; 943 944 /* 1. Enable output device and stream routing controls */ 945 ALOGV("%s: enter: usecase(%d)", __func__, out->usecase); 946 adev->out_device |= out->devices; 947 out_snd_device = get_output_snd_device(adev); 948 if (out_snd_device == SND_DEVICE_INVALID) { 949 ALOGE("%s: Could not get valid output sound device", __func__); 950 /* 951 * TODO: use a single exit point to avoid duplicating code to 952 * reset output device 953 */ 954 adev->out_device = get_active_out_devices(adev, out->usecase); 955 adev->out_device |= get_voice_call_out_device(adev); 956 return -EINVAL; 957 } 958 959 out->pcm_device_id = get_pcm_device_id(adev->audio_route, 960 out->usecase, 961 PCM_PLAYBACK); 962 if (out->pcm_device_id < 0) { 963 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)", 964 __func__, out->pcm_device_id, out->usecase); 965 adev->out_device = get_active_out_devices(adev, out->usecase); 966 adev->out_device |= get_voice_call_out_device(adev); 967 return -EINVAL; 968 } 969 970 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase)); 971 uc_info->id = out->usecase; 972 uc_info->type = PCM_PLAYBACK; 973 uc_info->devices = out->devices; 974 975 ret = select_devices(adev); 976 if (ret) { 977 ALOGE("%s: Failed to enable device(0x%x)", 978 __func__, adev->out_device); 979 adev->out_device = get_active_out_devices(adev, out->usecase); 980 adev->out_device |= get_voice_call_out_device(adev); 981 free(uc_info); 982 return ret; 983 } 984 985 out_snd_device = adev->cur_out_snd_device; 986 enable_audio_route(adev->audio_route, out->usecase, out_snd_device); 987 audio_route_update_mixer(adev->audio_route); 988 989 add_usecase_to_list(adev, uc_info); 990 991 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)", 992 __func__, 0, out->pcm_device_id); 993 out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id, 994 PCM_OUT, &out->config); 995 if (out->pcm && !pcm_is_ready(out->pcm)) { 996 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm)); 997 pcm_close(out->pcm); 998 out->pcm = NULL; 999 status = -EIO; 1000 goto error; 1001 } 1002 ALOGV("%s: exit", __func__); 1003 return 0; 1004error: 1005 stop_output_stream(out); 1006 ALOGE("%s: exit: status(%d)", __func__, status); 1007 return status; 1008} 1009 1010static int stop_voice_call(struct audio_device *adev) 1011{ 1012 int i, ret = 0; 1013 snd_device_t out_snd_device; 1014 struct audio_usecase *uc_info; 1015 1016 ALOGV("%s: enter: usecase(%d)", __func__, USECASE_VOICE_CALL); 1017 if (adev->csd_client) { 1018 if (adev->csd_stop_voice == NULL) { 1019 ALOGE("dlsym error for csd_client_disable_device"); 1020 } else { 1021 ret = adev->csd_stop_voice(); 1022 if (ret < 0) { 1023 ALOGE("%s: csd_client error %d\n", __func__, ret); 1024 } 1025 } 1026 } 1027 1028 /* 1. Close the PCM devices */ 1029 if (adev->voice_call_rx) { 1030 pcm_close(adev->voice_call_rx); 1031 adev->voice_call_rx = NULL; 1032 } 1033 if (adev->voice_call_tx) { 1034 pcm_close(adev->voice_call_tx); 1035 adev->voice_call_tx = NULL; 1036 } 1037 1038 uc_info = get_usecase_from_list(adev, USECASE_VOICE_CALL); 1039 if (uc_info == NULL) { 1040 ALOGE("%s: Could not find the usecase (%d) in the list", 1041 __func__, USECASE_VOICE_CALL); 1042 return -EINVAL; 1043 } 1044 out_snd_device = adev->cur_out_snd_device; 1045 1046 /* 2. Get and set stream specific mixer controls */ 1047 /* ToDo: Status check ?*/ 1048 disable_audio_route(adev->audio_route, USECASE_VOICE_CALL, out_snd_device); 1049 audio_route_update_mixer(adev->audio_route); 1050 1051 remove_usecase_from_list(adev, uc_info->id); 1052 1053 /* 3. Disable the rx and tx devices */ 1054 ret = select_devices(adev); 1055 adev->in_call = false; 1056 1057 ALOGV("%s: exit: status(%d)", __func__, ret); 1058 return ret; 1059} 1060 1061static int start_voice_call(struct audio_device *adev) 1062{ 1063 int i, ret = 0; 1064 snd_device_t out_snd_device; 1065 struct audio_usecase *uc_info; 1066 int pcm_dev_rx_id, pcm_dev_tx_id; 1067 1068 ALOGV("%s: enter", __func__); 1069 1070 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase)); 1071 uc_info->id = USECASE_VOICE_CALL; 1072 uc_info->type = VOICE_CALL; 1073 uc_info->devices = adev->out_device; 1074 1075 ret = select_devices(adev); 1076 if (ret) { 1077 free(uc_info); 1078 return ret; 1079 } 1080 1081 /* 2. Get and set stream specific mixer controls */ 1082 out_snd_device = adev->cur_out_snd_device; 1083 /* ToDo: Status check ?*/ 1084 enable_audio_route(adev->audio_route, uc_info->id, out_snd_device); 1085 audio_route_update_mixer(adev->audio_route); 1086 1087 add_usecase_to_list(adev, uc_info); 1088 1089 pcm_dev_rx_id = get_pcm_device_id(adev->audio_route, uc_info->id, 1090 PCM_PLAYBACK); 1091 pcm_dev_tx_id = get_pcm_device_id(adev->audio_route, uc_info->id, 1092 PCM_CAPTURE); 1093 1094 /* 2. Open the pcm device */ 1095 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) { 1096 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)", 1097 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id); 1098 stop_voice_call(adev); 1099 goto error; 1100 } 1101 1102 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)", 1103 __func__, SOUND_CARD, pcm_dev_rx_id); 1104 adev->voice_call_rx = pcm_open(SOUND_CARD, 1105 pcm_dev_rx_id, 1106 PCM_OUT, &pcm_config_voice_call); 1107 if (adev->voice_call_rx && !pcm_is_ready(adev->voice_call_rx)) { 1108 ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_rx)); 1109 /* ToDo: Check return status ??*/ 1110 stop_voice_call(adev); 1111 return -EIO; 1112 } 1113 1114 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)", 1115 __func__, SOUND_CARD, pcm_dev_tx_id); 1116 adev->voice_call_tx = pcm_open(SOUND_CARD, 1117 pcm_dev_tx_id, 1118 PCM_IN, &pcm_config_voice_call); 1119 if (adev->voice_call_tx && !pcm_is_ready(adev->voice_call_tx)) { 1120 ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_tx)); 1121 /* ToDo: Check return status ??*/ 1122 stop_voice_call(adev); 1123 return -EIO; 1124 } 1125 pcm_start(adev->voice_call_rx); 1126 pcm_start(adev->voice_call_tx); 1127 1128 if (adev->csd_client) { 1129 if (adev->csd_start_voice == NULL) { 1130 ALOGE("dlsym error for csd_client_start_voice"); 1131 } else { 1132 ret = adev->csd_start_voice(); 1133 if (ret < 0) { 1134 ALOGE("%s: csd_client error %d\n", __func__, ret); 1135 } 1136 } 1137 } 1138 1139 adev->in_call = true; 1140error: 1141 ALOGV("%s: exit: status(%d)", __func__, ret); 1142 return ret; 1143} 1144 1145static int check_input_parameters(uint32_t sample_rate, 1146 audio_format_t format, 1147 int channel_count) 1148{ 1149 if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL; 1150 1151 if ((channel_count < 1) || (channel_count > 2)) return -EINVAL; 1152 1153 switch (sample_rate) { 1154 case 8000: 1155 case 11025: 1156 case 12000: 1157 case 16000: 1158 case 22050: 1159 case 24000: 1160 case 32000: 1161 case 44100: 1162 case 48000: 1163 break; 1164 default: 1165 return -EINVAL; 1166 } 1167 1168 return 0; 1169} 1170 1171static size_t get_input_buffer_size(uint32_t sample_rate, 1172 audio_format_t format, 1173 int channel_count) 1174{ 1175 size_t size = 0; 1176 1177 if (check_input_parameters(sample_rate, format, channel_count) != 0) return 0; 1178 1179 if (sample_rate == 8000 || sample_rate == 16000 || sample_rate == 32000) { 1180 size = (sample_rate * 20) / 1000; 1181 } else if (sample_rate == 11025 || sample_rate == 12000) { 1182 size = 256; 1183 } else if (sample_rate == 22050 || sample_rate == 24000) { 1184 size = 512; 1185 } else if (sample_rate == 44100 || sample_rate == 48000) { 1186 size = 1024; 1187 } 1188 1189 return size * sizeof(short) * channel_count; 1190} 1191 1192static uint32_t out_get_sample_rate(const struct audio_stream *stream) 1193{ 1194 struct stream_out *out = (struct stream_out *)stream; 1195 1196 return out->config.rate; 1197} 1198 1199static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate) 1200{ 1201 return -ENOSYS; 1202} 1203 1204static size_t out_get_buffer_size(const struct audio_stream *stream) 1205{ 1206 struct stream_out *out = (struct stream_out *)stream; 1207 1208 return out->config.period_size * audio_stream_frame_size(stream); 1209} 1210 1211static uint32_t out_get_channels(const struct audio_stream *stream) 1212{ 1213 struct stream_out *out = (struct stream_out *)stream; 1214 1215 return out->channel_mask; 1216} 1217 1218static audio_format_t out_get_format(const struct audio_stream *stream) 1219{ 1220 return AUDIO_FORMAT_PCM_16_BIT; 1221} 1222 1223static int out_set_format(struct audio_stream *stream, audio_format_t format) 1224{ 1225 return -ENOSYS; 1226} 1227 1228static int out_standby(struct audio_stream *stream) 1229{ 1230 struct stream_out *out = (struct stream_out *)stream; 1231 struct audio_device *adev = out->dev; 1232 ALOGV("%s: enter: usecase(%d)", __func__, out->usecase); 1233 pthread_mutex_lock(&out->dev->lock); 1234 pthread_mutex_lock(&out->lock); 1235 1236 if (!out->standby) { 1237 out->standby = true; 1238 stop_output_stream(out); 1239 } 1240 pthread_mutex_unlock(&out->lock); 1241 pthread_mutex_unlock(&out->dev->lock); 1242 ALOGV("%s: exit", __func__); 1243 return 0; 1244} 1245 1246static int out_dump(const struct audio_stream *stream, int fd) 1247{ 1248 return 0; 1249} 1250 1251static int out_set_parameters(struct audio_stream *stream, const char *kvpairs) 1252{ 1253 struct stream_out *out = (struct stream_out *)stream; 1254 struct audio_device *adev = out->dev; 1255 struct str_parms *parms; 1256 char value[32]; 1257 int ret, val = 0; 1258 1259 ALOGV("%s: enter: usecase(%d) kvpairs: %s", 1260 __func__, out->usecase, kvpairs); 1261 parms = str_parms_create_str(kvpairs); 1262 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value)); 1263 if (ret >= 0) { 1264 val = atoi(value); 1265 pthread_mutex_lock(&adev->lock); 1266 pthread_mutex_lock(&out->lock); 1267 1268 if (adev->mode == AUDIO_MODE_IN_CALL && !adev->in_call && (val != 0)) { 1269 adev->out_device = get_active_out_devices(adev, out->usecase) | val; 1270 out->devices = val; 1271 start_voice_call(adev); 1272 } else if (adev->mode != AUDIO_MODE_IN_CALL && adev->in_call) { 1273 if (val != 0) { 1274 adev->out_device = get_active_out_devices(adev, out->usecase) | val; 1275 out->devices = val; 1276 } 1277 stop_voice_call(adev); 1278 } else if ((adev->out_device != (audio_devices_t)val) && (val != 0)) { 1279 if (!out->standby || adev->in_call) { 1280 adev->out_device = get_active_out_devices(adev, out->usecase) | val; 1281 ret = select_devices(adev); 1282 out->devices = val; 1283 } 1284 } 1285 1286 pthread_mutex_unlock(&out->lock); 1287 pthread_mutex_unlock(&adev->lock); 1288 } 1289 str_parms_destroy(parms); 1290 ALOGV("%s: exit: code(%d)", __func__, ret); 1291 return ret; 1292} 1293 1294static char* out_get_parameters(const struct audio_stream *stream, const char *keys) 1295{ 1296 struct stream_out *out = (struct stream_out *)stream; 1297 struct str_parms *query = str_parms_create_str(keys); 1298 char *str; 1299 char value[256]; 1300 struct str_parms *reply = str_parms_create(); 1301 size_t i, j; 1302 int ret; 1303 bool first = true; 1304 ALOGV("%s: enter: keys - %s", __func__, keys); 1305 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value)); 1306 if (ret >= 0) { 1307 value[0] = '\0'; 1308 i = 0; 1309 while (out->supported_channel_masks[i] != 0) { 1310 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) { 1311 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) { 1312 if (!first) { 1313 strcat(value, "|"); 1314 } 1315 strcat(value, out_channels_name_to_enum_table[j].name); 1316 first = false; 1317 break; 1318 } 1319 } 1320 i++; 1321 } 1322 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value); 1323 str = str_parms_to_str(reply); 1324 } else { 1325 str = strdup(keys); 1326 } 1327 str_parms_destroy(query); 1328 str_parms_destroy(reply); 1329 ALOGV("%s: exit: returns - %s", __func__, str); 1330 return str; 1331} 1332 1333static uint32_t out_get_latency(const struct audio_stream_out *stream) 1334{ 1335 struct stream_out *out = (struct stream_out *)stream; 1336 1337 return (out->config.period_count * out->config.period_size * 1000) / (out->config.rate); 1338} 1339 1340static int out_set_volume(struct audio_stream_out *stream, float left, 1341 float right) 1342{ 1343 return -ENOSYS; 1344} 1345 1346static ssize_t out_write(struct audio_stream_out *stream, const void *buffer, 1347 size_t bytes) 1348{ 1349 struct stream_out *out = (struct stream_out *)stream; 1350 struct audio_device *adev = out->dev; 1351 int i, ret = -1; 1352 1353 /* acquiring hw device mutex systematically is useful if a low priority thread is waiting 1354 * on the output stream mutex - e.g. executing select_mode() while holding the hw device 1355 * mutex 1356 */ 1357 pthread_mutex_lock(&adev->lock); 1358 pthread_mutex_lock(&out->lock); 1359 if (out->standby) { 1360 ret = start_output_stream(out); 1361 if (ret != 0) { 1362 pthread_mutex_unlock(&adev->lock); 1363 goto exit; 1364 } 1365 out->standby = false; 1366 } 1367 pthread_mutex_unlock(&adev->lock); 1368 1369 if (out->pcm) { 1370 //ALOGV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes); 1371 ret = pcm_write(out->pcm, (void *)buffer, bytes); 1372 } 1373 1374exit: 1375 pthread_mutex_unlock(&out->lock); 1376 1377 if (ret != 0) { 1378 out_standby(&out->stream.common); 1379 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) / 1380 out_get_sample_rate(&out->stream.common)); 1381 } 1382 return bytes; 1383} 1384 1385static int out_get_render_position(const struct audio_stream_out *stream, 1386 uint32_t *dsp_frames) 1387{ 1388 return -EINVAL; 1389} 1390 1391static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) 1392{ 1393 return 0; 1394} 1395 1396static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) 1397{ 1398 return 0; 1399} 1400 1401static int out_get_next_write_timestamp(const struct audio_stream_out *stream, 1402 int64_t *timestamp) 1403{ 1404 return -EINVAL; 1405} 1406 1407/** audio_stream_in implementation **/ 1408static uint32_t in_get_sample_rate(const struct audio_stream *stream) 1409{ 1410 struct stream_in *in = (struct stream_in *)stream; 1411 1412 return in->config.rate; 1413} 1414 1415static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate) 1416{ 1417 return -ENOSYS; 1418} 1419 1420static size_t in_get_buffer_size(const struct audio_stream *stream) 1421{ 1422 struct stream_in *in = (struct stream_in *)stream; 1423 1424 return in->config.period_size * audio_stream_frame_size(stream); 1425} 1426 1427static uint32_t in_get_channels(const struct audio_stream *stream) 1428{ 1429 struct stream_in *in = (struct stream_in *)stream; 1430 1431 return in->channel_mask; 1432} 1433 1434static audio_format_t in_get_format(const struct audio_stream *stream) 1435{ 1436 return AUDIO_FORMAT_PCM_16_BIT; 1437} 1438 1439static int in_set_format(struct audio_stream *stream, audio_format_t format) 1440{ 1441 return -ENOSYS; 1442} 1443 1444static int in_standby(struct audio_stream *stream) 1445{ 1446 struct stream_in *in = (struct stream_in *)stream; 1447 struct audio_device *adev = in->dev; 1448 int status = 0; 1449 ALOGV("%s: enter", __func__); 1450 pthread_mutex_lock(&adev->lock); 1451 pthread_mutex_lock(&in->lock); 1452 if (!in->standby) { 1453 in->standby = true; 1454 status = stop_input_stream(in); 1455 } 1456 pthread_mutex_unlock(&in->lock); 1457 pthread_mutex_unlock(&adev->lock); 1458 ALOGV("%s: exit: status(%d)", __func__, status); 1459 return status; 1460} 1461 1462static int in_dump(const struct audio_stream *stream, int fd) 1463{ 1464 return 0; 1465} 1466 1467static int in_set_parameters(struct audio_stream *stream, const char *kvpairs) 1468{ 1469 struct stream_in *in = (struct stream_in *)stream; 1470 struct audio_device *adev = in->dev; 1471 struct str_parms *parms; 1472 char *str; 1473 char value[32]; 1474 int ret, val = 0; 1475 1476 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs); 1477 parms = str_parms_create_str(kvpairs); 1478 1479 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value)); 1480 1481 pthread_mutex_lock(&adev->lock); 1482 pthread_mutex_lock(&in->lock); 1483 if (ret >= 0) { 1484 val = atoi(value); 1485 /* no audio source uses val == 0 */ 1486 if ((in->source != val) && (val != 0)) { 1487 in->source = val; 1488 } 1489 } 1490 1491 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value)); 1492 if (ret >= 0) { 1493 val = atoi(value); 1494 if ((in->device != val) && (val != 0)) { 1495 in->device = val; 1496 /* If recording is in progress, change the tx device to new device */ 1497 if (!in->standby) { 1498 ret = select_devices(adev); 1499 } 1500 } 1501 } 1502 1503 pthread_mutex_unlock(&in->lock); 1504 pthread_mutex_unlock(&adev->lock); 1505 1506 str_parms_destroy(parms); 1507 ALOGV("%s: exit: status(%d)", __func__, ret); 1508 return ret; 1509} 1510 1511static char* in_get_parameters(const struct audio_stream *stream, 1512 const char *keys) 1513{ 1514 return strdup(""); 1515} 1516 1517static int in_set_gain(struct audio_stream_in *stream, float gain) 1518{ 1519 return 0; 1520} 1521 1522static ssize_t in_read(struct audio_stream_in *stream, void *buffer, 1523 size_t bytes) 1524{ 1525 struct stream_in *in = (struct stream_in *)stream; 1526 struct audio_device *adev = in->dev; 1527 int i, ret = -1; 1528 1529 /* acquiring hw device mutex systematically is useful if a low priority thread is waiting 1530 * on the output stream mutex - e.g. executing select_mode() while holding the hw device 1531 * mutex 1532 */ 1533 //ALOGV("%s: buffer(%p) bytes(%d)", __func__, buffer, bytes); 1534 pthread_mutex_lock(&adev->lock); 1535 pthread_mutex_lock(&in->lock); 1536 if (in->standby) { 1537 ret = start_input_stream(in); 1538 if (ret != 0) { 1539 pthread_mutex_unlock(&adev->lock); 1540 goto exit; 1541 } 1542 in->standby = 0; 1543 } 1544 pthread_mutex_unlock(&adev->lock); 1545 1546 if (in->pcm) { 1547 ret = pcm_read(in->pcm, buffer, bytes); 1548 } 1549 1550 /* 1551 * Instead of writing zeroes here, we could trust the hardware 1552 * to always provide zeroes when muted. 1553 */ 1554 if (ret == 0 && adev->mic_mute) 1555 memset(buffer, 0, bytes); 1556 1557exit: 1558 pthread_mutex_unlock(&in->lock); 1559 1560 if (ret != 0) { 1561 in_standby(&in->stream.common); 1562 ALOGV("%s: read failed - sleeping for buffer duration", __func__); 1563 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) / 1564 in_get_sample_rate(&in->stream.common)); 1565 } 1566 return bytes; 1567} 1568 1569static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream) 1570{ 1571 return 0; 1572} 1573 1574static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) 1575{ 1576 return 0; 1577} 1578 1579static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) 1580{ 1581 return 0; 1582} 1583 1584static int adev_open_output_stream(struct audio_hw_device *dev, 1585 audio_io_handle_t handle, 1586 audio_devices_t devices, 1587 audio_output_flags_t flags, 1588 struct audio_config *config, 1589 struct audio_stream_out **stream_out) 1590{ 1591 struct audio_device *adev = (struct audio_device *)dev; 1592 struct stream_out *out; 1593 int i, ret; 1594 1595 ALOGV("%s: enter: sample_rate(%d) channel_mask(0x%x) devices(0x%x) flags(0x%x)", 1596 __func__, config->sample_rate, config->channel_mask, devices, flags); 1597 *stream_out = NULL; 1598 out = (struct stream_out *)calloc(1, sizeof(struct stream_out)); 1599 1600 if (devices == AUDIO_DEVICE_NONE) 1601 devices = AUDIO_DEVICE_OUT_SPEAKER; 1602 1603 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO; 1604 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO; 1605 out->flags = flags; 1606 out->devices = devices; 1607 1608 /* Init use case and pcm_config */ 1609 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT && 1610 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) { 1611 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH; 1612 out->config = pcm_config_hdmi_multi; 1613 1614 pthread_mutex_lock(&adev->lock); 1615 read_hdmi_channel_masks(out); 1616 pthread_mutex_unlock(&adev->lock); 1617 1618 if (config->sample_rate == 0) config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE; 1619 if (config->channel_mask == 0) config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1; 1620 out->channel_mask = config->channel_mask; 1621 out->config.rate = config->sample_rate; 1622 out->config.channels = popcount(out->channel_mask); 1623 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2); 1624 set_hdmi_channels(adev->mixer, out->config.channels); 1625 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) { 1626 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER; 1627 out->config = pcm_config_deep_buffer; 1628 } else { 1629 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY; 1630 out->config = pcm_config_low_latency; 1631 } 1632 1633 /* Check if this usecase is already existing */ 1634 pthread_mutex_lock(&adev->lock); 1635 if (get_usecase_from_list(adev, out->usecase) != NULL) { 1636 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase); 1637 free(out); 1638 *stream_out = NULL; 1639 pthread_mutex_unlock(&adev->lock); 1640 return -EEXIST; 1641 } 1642 pthread_mutex_unlock(&adev->lock); 1643 1644 out->stream.common.get_sample_rate = out_get_sample_rate; 1645 out->stream.common.set_sample_rate = out_set_sample_rate; 1646 out->stream.common.get_buffer_size = out_get_buffer_size; 1647 out->stream.common.get_channels = out_get_channels; 1648 out->stream.common.get_format = out_get_format; 1649 out->stream.common.set_format = out_set_format; 1650 out->stream.common.standby = out_standby; 1651 out->stream.common.dump = out_dump; 1652 out->stream.common.set_parameters = out_set_parameters; 1653 out->stream.common.get_parameters = out_get_parameters; 1654 out->stream.common.add_audio_effect = out_add_audio_effect; 1655 out->stream.common.remove_audio_effect = out_remove_audio_effect; 1656 out->stream.get_latency = out_get_latency; 1657 out->stream.set_volume = out_set_volume; 1658 out->stream.write = out_write; 1659 out->stream.get_render_position = out_get_render_position; 1660 out->stream.get_next_write_timestamp = out_get_next_write_timestamp; 1661 1662 out->dev = adev; 1663 out->standby = 1; 1664 1665 config->format = out->stream.common.get_format(&out->stream.common); 1666 config->channel_mask = out->stream.common.get_channels(&out->stream.common); 1667 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common); 1668 1669 *stream_out = &out->stream; 1670 ALOGV("%s: exit", __func__); 1671 return 0; 1672} 1673 1674static void adev_close_output_stream(struct audio_hw_device *dev, 1675 struct audio_stream_out *stream) 1676{ 1677 ALOGV("%s: enter", __func__); 1678 out_standby(&stream->common); 1679 free(stream); 1680 ALOGV("%s: exit", __func__); 1681} 1682 1683static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs) 1684{ 1685 struct audio_device *adev = (struct audio_device *)dev; 1686 struct str_parms *parms; 1687 char *str; 1688 char value[32]; 1689 int ret; 1690 1691 ALOGV("%s: enter: %s", __func__, kvpairs); 1692 1693 parms = str_parms_create_str(kvpairs); 1694 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value)); 1695 if (ret >= 0) { 1696 int tty_mode; 1697 1698 if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0) 1699 tty_mode = TTY_MODE_OFF; 1700 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0) 1701 tty_mode = TTY_MODE_VCO; 1702 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0) 1703 tty_mode = TTY_MODE_HCO; 1704 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0) 1705 tty_mode = TTY_MODE_FULL; 1706 else 1707 return -EINVAL; 1708 1709 pthread_mutex_lock(&adev->lock); 1710 if (tty_mode != adev->tty_mode) { 1711 adev->tty_mode = tty_mode; 1712 adev->acdb_settings = (adev->acdb_settings & TTY_MODE_CLEAR) | tty_mode; 1713 if (adev->in_call) 1714 select_devices(adev); 1715 } 1716 pthread_mutex_unlock(&adev->lock); 1717 } 1718 1719 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value)); 1720 if (ret >= 0) { 1721 /* When set to false, HAL should disable EC and NS 1722 * But it is currently not supported. 1723 */ 1724 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0) 1725 adev->bluetooth_nrec = true; 1726 else 1727 adev->bluetooth_nrec = false; 1728 } 1729 1730 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value)); 1731 if (ret >= 0) { 1732 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0) 1733 adev->screen_off = false; 1734 else 1735 adev->screen_off = true; 1736 } 1737 1738 str_parms_destroy(parms); 1739 ALOGV("%s: exit with code(%d)", __func__, ret); 1740 return ret; 1741} 1742 1743static char* adev_get_parameters(const struct audio_hw_device *dev, 1744 const char *keys) 1745{ 1746 /* ToDo: Return requested params */ 1747 return strdup(""); 1748} 1749 1750static int adev_init_check(const struct audio_hw_device *dev) 1751{ 1752 return 0; 1753} 1754 1755static int adev_set_voice_volume(struct audio_hw_device *dev, float volume) 1756{ 1757 struct audio_device *adev = (struct audio_device *)dev; 1758 int vol, err = 0; 1759 1760 pthread_mutex_lock(&adev->lock); 1761 adev->voice_volume = volume; 1762 if (adev->mode == AUDIO_MODE_IN_CALL) { 1763 if (volume < 0.0) { 1764 volume = 0.0; 1765 } else if (volume > 1.0) { 1766 volume = 1.0; 1767 } 1768 1769 vol = lrint(volume * 100.0); 1770 1771 // Voice volume levels from android are mapped to driver volume levels as follows. 1772 // 0 -> 5, 20 -> 4, 40 ->3, 60 -> 2, 80 -> 1, 100 -> 0 1773 // So adjust the volume to get the correct volume index in driver 1774 vol = 100 - vol; 1775 1776 if (adev->csd_client) { 1777 if (adev->csd_volume == NULL) { 1778 ALOGE("%s: dlsym error for csd_client_volume", __func__); 1779 } else { 1780 err = adev->csd_volume(vol); 1781 if (err < 0) { 1782 ALOGE("%s: csd_client error %d", __func__, err); 1783 } 1784 } 1785 } else { 1786 ALOGE("%s: No CSD Client present", __func__); 1787 } 1788 } 1789 pthread_mutex_unlock(&adev->lock); 1790 return err; 1791} 1792 1793static int adev_set_master_volume(struct audio_hw_device *dev, float volume) 1794{ 1795 return -ENOSYS; 1796} 1797 1798static int adev_get_master_volume(struct audio_hw_device *dev, 1799 float *volume) 1800{ 1801 return -ENOSYS; 1802} 1803 1804static int adev_set_master_mute(struct audio_hw_device *dev, bool muted) 1805{ 1806 return -ENOSYS; 1807} 1808 1809static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted) 1810{ 1811 return -ENOSYS; 1812} 1813 1814static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode) 1815{ 1816 struct audio_device *adev = (struct audio_device *)dev; 1817 1818 pthread_mutex_lock(&adev->lock); 1819 if (adev->mode != mode) { 1820 adev->mode = mode; 1821 } 1822 pthread_mutex_unlock(&adev->lock); 1823 return 0; 1824} 1825 1826static int adev_set_mic_mute(struct audio_hw_device *dev, bool state) 1827{ 1828 struct audio_device *adev = (struct audio_device *)dev; 1829 int err = 0; 1830 1831 adev->mic_mute = state; 1832 if (adev->mode == AUDIO_MODE_IN_CALL) { 1833 if (adev->csd_client) { 1834 if (adev->csd_mic_mute == NULL) { 1835 ALOGE("%s: dlsym error for csd_mic_mute", __func__); 1836 } else { 1837 err = adev->csd_mic_mute(state); 1838 if (err < 0) { 1839 ALOGE("%s: csd_client error %d", __func__, err); 1840 } 1841 } 1842 } else { 1843 ALOGE("%s: No CSD Client present", __func__); 1844 } 1845 } 1846 return err; 1847} 1848 1849static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state) 1850{ 1851 struct audio_device *adev = (struct audio_device *)dev; 1852 1853 *state = adev->mic_mute; 1854 1855 return 0; 1856} 1857 1858static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev, 1859 const struct audio_config *config) 1860{ 1861 size_t size; 1862 int channel_count = popcount(config->channel_mask); 1863 1864 return get_input_buffer_size(config->sample_rate, config->format, channel_count); 1865} 1866 1867static int adev_open_input_stream(struct audio_hw_device *dev, 1868 audio_io_handle_t handle, 1869 audio_devices_t devices, 1870 struct audio_config *config, 1871 struct audio_stream_in **stream_in) 1872{ 1873 struct audio_device *adev = (struct audio_device *)dev; 1874 struct stream_in *in; 1875 int ret, buffer_size, frame_size; 1876 int channel_count = popcount(config->channel_mask); 1877 1878 ALOGV("%s: enter", __func__); 1879 *stream_in = NULL; 1880 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0) 1881 return -EINVAL; 1882 1883 in = (struct stream_in *)calloc(1, sizeof(struct stream_in)); 1884 1885 in->stream.common.get_sample_rate = in_get_sample_rate; 1886 in->stream.common.set_sample_rate = in_set_sample_rate; 1887 in->stream.common.get_buffer_size = in_get_buffer_size; 1888 in->stream.common.get_channels = in_get_channels; 1889 in->stream.common.get_format = in_get_format; 1890 in->stream.common.set_format = in_set_format; 1891 in->stream.common.standby = in_standby; 1892 in->stream.common.dump = in_dump; 1893 in->stream.common.set_parameters = in_set_parameters; 1894 in->stream.common.get_parameters = in_get_parameters; 1895 in->stream.common.add_audio_effect = in_add_audio_effect; 1896 in->stream.common.remove_audio_effect = in_remove_audio_effect; 1897 in->stream.set_gain = in_set_gain; 1898 in->stream.read = in_read; 1899 in->stream.get_input_frames_lost = in_get_input_frames_lost; 1900 1901 in->device = devices; 1902 in->source = AUDIO_SOURCE_DEFAULT; 1903 in->dev = adev; 1904 in->standby = 1; 1905 in->channel_mask = config->channel_mask; 1906 1907 /* Update config params with the requested sample rate and channels */ 1908 in->usecase = USECASE_AUDIO_RECORD; 1909 in->config = pcm_config_audio_capture; 1910 in->config.channels = channel_count; 1911 in->config.rate = config->sample_rate; 1912 1913 frame_size = audio_stream_frame_size((struct audio_stream *)in); 1914 buffer_size = get_input_buffer_size(config->sample_rate, 1915 config->format, 1916 channel_count); 1917 in->config.period_size = buffer_size / frame_size; 1918 1919 *stream_in = &in->stream; 1920 ALOGV("%s: exit", __func__); 1921 return 0; 1922 1923err_open: 1924 free(in); 1925 *stream_in = NULL; 1926 return ret; 1927} 1928 1929static void adev_close_input_stream(struct audio_hw_device *dev, 1930 struct audio_stream_in *stream) 1931{ 1932 in_standby(&stream->common); 1933 free(stream); 1934 1935 return; 1936} 1937 1938static int adev_dump(const audio_hw_device_t *device, int fd) 1939{ 1940 return 0; 1941} 1942 1943static int adev_close(hw_device_t *device) 1944{ 1945 struct audio_device *adev = (struct audio_device *)device; 1946 audio_route_free(adev->audio_route); 1947 free(device); 1948 return 0; 1949} 1950 1951static void init_platform_data(struct audio_device *adev) 1952{ 1953 char platform[PROPERTY_VALUE_MAX]; 1954 char baseband[PROPERTY_VALUE_MAX]; 1955 char value[PROPERTY_VALUE_MAX]; 1956 int mccmnc; 1957 1958 adev->dualmic_config = DUALMIC_CONFIG_NONE; 1959 adev->fluence_in_voice_call = false; 1960 adev->fluence_in_voice_rec = false; 1961 adev->mic_type_analog = false; 1962 1963 property_get("persist.audio.handset.mic.type",value,""); 1964 if (!strncmp("analog", value, 6)) 1965 adev->mic_type_analog = true; 1966 1967 property_get("persist.audio.dualmic.config",value,""); 1968 if (!strncmp("broadside", value, 9)) { 1969 adev->dualmic_config = DUALMIC_CONFIG_BROADSIDE; 1970 adev->acdb_settings |= DMIC_FLAG; 1971 } else if (!strncmp("endfire", value, 7)) { 1972 adev->dualmic_config = DUALMIC_CONFIG_ENDFIRE; 1973 adev->acdb_settings |= DMIC_FLAG; 1974 } 1975 1976 if (adev->dualmic_config != DUALMIC_CONFIG_NONE) { 1977 property_get("persist.audio.fluence.voicecall",value,""); 1978 if (!strncmp("true", value, 4)) { 1979 adev->fluence_in_voice_call = true; 1980 } 1981 1982 property_get("persist.audio.fluence.voicerec",value,""); 1983 if (!strncmp("true", value, 4)) { 1984 adev->fluence_in_voice_rec = true; 1985 } 1986 } 1987 1988 property_get("gsm.sim.operator.numeric",value,"0"); 1989 mccmnc = atoi(value); 1990 ALOGV("%s: tmus mccmnc %d", __func__, mccmnc); 1991 switch(mccmnc) { 1992 /* TMUS MCC(310), MNC(490, 260, 026) */ 1993 case 310490: 1994 case 310260: 1995 case 310026: 1996 adev->is_tmus = true; 1997 break; 1998 default: 1999 adev->is_tmus = false; 2000 break; 2001 } 2002 2003 adev->acdb_handle = dlopen(LIB_ACDB_LOADER, RTLD_NOW); 2004 if (adev->acdb_handle == NULL) { 2005 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_ACDB_LOADER); 2006 } else { 2007 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_ACDB_LOADER); 2008 adev->acdb_deallocate = (acdb_deallocate_t)dlsym(adev->acdb_handle, 2009 "acdb_loader_deallocate_ACDB"); 2010 adev->acdb_send_audio_cal = (acdb_send_audio_cal_t)dlsym(adev->acdb_handle, 2011 "acdb_loader_send_audio_cal"); 2012 adev->acdb_send_voice_cal = (acdb_send_voice_cal_t)dlsym(adev->acdb_handle, 2013 "acdb_loader_send_voice_cal"); 2014 adev->acdb_init = (acdb_init_t)dlsym(adev->acdb_handle, 2015 "acdb_loader_init_ACDB"); 2016 if (adev->acdb_init == NULL) 2017 ALOGE("%s: dlsym error %s for acdb_loader_init_ACDB", __func__, dlerror()); 2018 else 2019 adev->acdb_init(); 2020 } 2021 2022 /* If platform is Fusion3, load CSD Client specific symbols 2023 * Voice call is handled by MDM and apps processor talks to 2024 * MDM through CSD Client 2025 */ 2026 property_get("ro.board.platform", platform, ""); 2027 property_get("ro.baseband", baseband, ""); 2028 if (!strcmp("msm8960", platform) && !strcmp("mdm", baseband)) { 2029 adev->csd_client = dlopen(LIB_CSD_CLIENT, RTLD_NOW); 2030 if (adev->csd_client == NULL) 2031 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_CSD_CLIENT); 2032 } 2033 2034 if (adev->csd_client) { 2035 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_CSD_CLIENT); 2036 adev->csd_client_deinit = (csd_client_deinit_t)dlsym(adev->csd_client, 2037 "csd_client_deinit"); 2038 adev->csd_disable_device = (csd_disable_device_t)dlsym(adev->csd_client, 2039 "csd_client_disable_device"); 2040 adev->csd_enable_device = (csd_enable_device_t)dlsym(adev->csd_client, 2041 "csd_client_enable_device"); 2042 adev->csd_start_voice = (csd_start_voice_t)dlsym(adev->csd_client, 2043 "csd_client_start_voice"); 2044 adev->csd_stop_voice = (csd_stop_voice_t)dlsym(adev->csd_client, 2045 "csd_client_stop_voice"); 2046 adev->csd_volume = (csd_volume_t)dlsym(adev->csd_client, 2047 "csd_client_volume"); 2048 adev->csd_mic_mute = (csd_mic_mute_t)dlsym(adev->csd_client, 2049 "csd_client_mic_mute"); 2050 adev->csd_client_init = (csd_client_init_t)dlsym(adev->csd_client, 2051 "csd_client_init"); 2052 2053 if (adev->csd_client_init == NULL) { 2054 ALOGE("%s: dlsym error %s for csd_client_init", __func__, dlerror()); 2055 } else { 2056 adev->csd_client_init(); 2057 } 2058 } 2059} 2060 2061static int adev_open(const hw_module_t *module, const char *name, 2062 hw_device_t **device) 2063{ 2064 struct audio_device *adev; 2065 int ret; 2066 2067 ALOGV("%s: enter", __func__); 2068 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL; 2069 2070 adev = calloc(1, sizeof(struct audio_device)); 2071 2072 adev->mixer = mixer_open(MIXER_CARD); 2073 if (!adev->mixer) { 2074 ALOGE("Unable to open the mixer, aborting."); 2075 return -ENOSYS; 2076 } 2077 2078 adev->audio_route = audio_route_init(MIXER_CARD, MIXER_XML_PATH); 2079 if (!adev->audio_route) { 2080 free(adev); 2081 ALOGE("%s: Failed to init audio route controls, aborting.", __func__); 2082 *device = NULL; 2083 return -EINVAL; 2084 } 2085 2086 adev->device.common.tag = HARDWARE_DEVICE_TAG; 2087 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0; 2088 adev->device.common.module = (struct hw_module_t *)module; 2089 adev->device.common.close = adev_close; 2090 2091 adev->device.init_check = adev_init_check; 2092 adev->device.set_voice_volume = adev_set_voice_volume; 2093 adev->device.set_master_volume = adev_set_master_volume; 2094 adev->device.get_master_volume = adev_get_master_volume; 2095 adev->device.set_master_mute = adev_set_master_mute; 2096 adev->device.get_master_mute = adev_get_master_mute; 2097 adev->device.set_mode = adev_set_mode; 2098 adev->device.set_mic_mute = adev_set_mic_mute; 2099 adev->device.get_mic_mute = adev_get_mic_mute; 2100 adev->device.set_parameters = adev_set_parameters; 2101 adev->device.get_parameters = adev_get_parameters; 2102 adev->device.get_input_buffer_size = adev_get_input_buffer_size; 2103 adev->device.open_output_stream = adev_open_output_stream; 2104 adev->device.close_output_stream = adev_close_output_stream; 2105 adev->device.open_input_stream = adev_open_input_stream; 2106 adev->device.close_input_stream = adev_close_input_stream; 2107 adev->device.dump = adev_dump; 2108 2109 /* Set the default route before the PCM stream is opened */ 2110 pthread_mutex_lock(&adev->lock); 2111 adev->mode = AUDIO_MODE_NORMAL; 2112 adev->active_input = NULL; 2113 adev->out_device = AUDIO_DEVICE_NONE; 2114 adev->voice_call_rx = NULL; 2115 adev->voice_call_tx = NULL; 2116 adev->voice_volume = 1.0f; 2117 adev->tty_mode = TTY_MODE_OFF; 2118 adev->bluetooth_nrec = true; 2119 adev->cur_out_snd_device = 0; 2120 adev->cur_in_snd_device = 0; 2121 adev->out_snd_device_active = false; 2122 adev->in_snd_device_active = false; 2123 adev->usecase_list.next = NULL; 2124 adev->usecase_list.id = USECASE_INVALID; 2125 adev->in_call = false; 2126 adev->acdb_settings = TTY_MODE_OFF; 2127 pthread_mutex_unlock(&adev->lock); 2128 2129 /* Loads platform specific libraries dynamically */ 2130 init_platform_data(adev); 2131 2132 *device = &adev->device.common; 2133 2134 ALOGV("%s: exit", __func__); 2135 return 0; 2136} 2137 2138static struct hw_module_methods_t hal_module_methods = { 2139 .open = adev_open, 2140}; 2141 2142struct audio_module HAL_MODULE_INFO_SYM = { 2143 .common = { 2144 .tag = HARDWARE_MODULE_TAG, 2145 .module_api_version = AUDIO_MODULE_API_VERSION_0_1, 2146 .hal_api_version = HARDWARE_HAL_API_VERSION, 2147 .id = AUDIO_HARDWARE_MODULE_ID, 2148 .name = "QCOM Audio HAL", 2149 .author = "Code Aurora Forum", 2150 .methods = &hal_module_methods, 2151 }, 2152}; 2153