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