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