1 /* 2 ** Copyright 2008, 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 #include <math.h> 18 19 //#define LOG_NDEBUG 0 20 #define LOG_TAG "AudioHardwareQSD" 21 #include <utils/Log.h> 22 #include <utils/String8.h> 23 #include <hardware_legacy/power.h> 24 25 #include <stdio.h> 26 #include <unistd.h> 27 #include <sys/ioctl.h> 28 #include <sys/types.h> 29 #include <sys/stat.h> 30 #include <dlfcn.h> 31 #include <fcntl.h> 32 33 #include <cutils/properties.h> // for property_get for the voice recognition mode switch 34 35 // hardware specific functions 36 37 #include "AudioHardware.h" 38 #include <media/AudioRecord.h> 39 40 extern "C" { 41 #include "msm_audio.h" 42 #include <linux/a1026.h> 43 #include <linux/tpa2018d1.h> 44 } 45 46 #define LOG_SND_RPC 0 // Set to 1 to log sound RPC's 47 #define TX_PATH (1) 48 49 static const uint32_t SND_DEVICE_CURRENT = 256; 50 static const uint32_t SND_DEVICE_HANDSET = 0; 51 static const uint32_t SND_DEVICE_SPEAKER = 1; 52 static const uint32_t SND_DEVICE_BT = 3; 53 static const uint32_t SND_DEVICE_CARKIT = 4; 54 static const uint32_t SND_DEVICE_BT_EC_OFF = 45; 55 static const uint32_t SND_DEVICE_HEADSET = 2; 56 static const uint32_t SND_DEVICE_HEADSET_AND_SPEAKER = 10; 57 static const uint32_t SND_DEVICE_FM_HEADSET = 9; 58 static const uint32_t SND_DEVICE_FM_SPEAKER = 11; 59 static const uint32_t SND_DEVICE_NO_MIC_HEADSET = 8; 60 static const uint32_t SND_DEVICE_TTY_FULL = 5; 61 static const uint32_t SND_DEVICE_TTY_VCO = 6; 62 static const uint32_t SND_DEVICE_TTY_HCO = 7; 63 static const uint32_t SND_DEVICE_HANDSET_BACK_MIC = 20; 64 static const uint32_t SND_DEVICE_SPEAKER_BACK_MIC = 21; 65 static const uint32_t SND_DEVICE_NO_MIC_HEADSET_BACK_MIC = 28; 66 static const uint32_t SND_DEVICE_HEADSET_AND_SPEAKER_BACK_MIC = 30; 67 namespace android { 68 static int support_a1026 = 1; 69 static bool support_tpa2018d1 = true; 70 static int fd_a1026 = -1; 71 static int old_pathid = -1; 72 static int new_pathid = -1; 73 static int curr_out_device = -1; 74 static int curr_mic_device = -1; 75 static int voice_started = 0; 76 static int fd_fm_device = -1; 77 static int stream_volume = -300; 78 // use VR mode on inputs: 1 == VR mode enabled when selected, 0 = VR mode disabled when selected 79 static int vr_mode_enabled; 80 static bool vr_mode_change = false; 81 static int vr_uses_ns = 0; 82 static int alt_enable = 0; 83 static int hac_enable = 0; 84 // enable or disable 2-mic noise suppression in call on receiver mode 85 static int enable1026 = 1; 86 //FIXME add new settings in A1026 driver for an incall no ns mode, based on the current vr no ns 87 #define A1026_PATH_INCALL_NO_NS_RECEIVER A1026_PATH_VR_NO_NS_RECEIVER 88 89 int errCount = 0; 90 static void * acoustic; 91 const uint32_t AudioHardware::inputSamplingRates[] = { 92 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000 93 }; 94 95 // ID string for audio wakelock 96 static const char kOutputWakelockStr[] = "AudioHardwareQSDOut"; 97 static const char kInputWakelockStr[] = "AudioHardwareQSDIn"; 98 99 // ---------------------------------------------------------------------------- 100 101 AudioHardware::AudioHardware() : 102 mA1026Init(false), mInit(false), mMicMute(true), 103 mBluetoothNrec(true), 104 mHACSetting(false), 105 mBluetoothIdTx(0), mBluetoothIdRx(0), 106 mOutput(0), 107 mNoiseSuppressionState(A1026_NS_STATE_AUTO), 108 mVoiceVolume(VOICE_VOLUME_MAX), mTTYMode(TTY_MODE_OFF) 109 { 110 int (*snd_get_num)(); 111 int (*snd_get_bt_endpoint)(msm_bt_endpoint *); 112 int (*set_acoustic_parameters)(); 113 int (*set_tpa2018d1_parameters)(); 114 115 struct msm_bt_endpoint *ept; 116 117 doA1026_init(); 118 119 acoustic =:: dlopen("/system/lib/libhtc_acoustic.so", RTLD_NOW); 120 if (acoustic == NULL ) { 121 LOGE("Could not open libhtc_acoustic.so"); 122 /* this is not really an error on non-htc devices... */ 123 mNumBTEndpoints = 0; 124 mInit = true; 125 return; 126 } 127 set_acoustic_parameters = (int (*)(void))::dlsym(acoustic, "set_acoustic_parameters"); 128 if ((*set_acoustic_parameters) == 0 ) { 129 LOGE("Could not open set_acoustic_parameters()"); 130 return; 131 } 132 133 set_tpa2018d1_parameters = (int (*)(void))::dlsym(acoustic, "set_tpa2018d1_parameters"); 134 if ((*set_tpa2018d1_parameters) == 0) { 135 LOGI("set_tpa2018d1_parameters() not present"); 136 support_tpa2018d1 = false; 137 } 138 139 int rc = set_acoustic_parameters(); 140 if (rc < 0) { 141 LOGE("Could not set acoustic parameters to share memory: %d", rc); 142 } 143 144 if (support_tpa2018d1) { 145 rc = set_tpa2018d1_parameters(); 146 if (rc < 0) { 147 support_tpa2018d1 = false; 148 LOGE("speaker amplifier tpa2018 is not supported\n"); 149 } 150 } 151 152 snd_get_num = (int (*)(void))::dlsym(acoustic, "snd_get_num"); 153 if ((*snd_get_num) == 0 ) { 154 LOGE("Could not open snd_get_num()"); 155 } 156 157 mNumBTEndpoints = snd_get_num(); 158 LOGD("mNumBTEndpoints = %d", mNumBTEndpoints); 159 mBTEndpoints = new msm_bt_endpoint[mNumBTEndpoints]; 160 mInit = true; 161 LOGV("constructed %d SND endpoints)", mNumBTEndpoints); 162 ept = mBTEndpoints; 163 snd_get_bt_endpoint = (int (*)(msm_bt_endpoint *))::dlsym(acoustic, "snd_get_bt_endpoint"); 164 if ((*snd_get_bt_endpoint) == 0 ) { 165 LOGE("Could not open snd_get_bt_endpoint()"); 166 return; 167 } 168 snd_get_bt_endpoint(mBTEndpoints); 169 170 for (int i = 0; i < mNumBTEndpoints; i++) { 171 LOGE("BT name %s (tx,rx)=(%d,%d)", mBTEndpoints[i].name, mBTEndpoints[i].tx, mBTEndpoints[i].rx); 172 } 173 174 // reset voice mode in case media_server crashed and restarted while in call 175 int fd = open("/dev/msm_audio_ctl", O_RDWR); 176 if (fd >= 0) { 177 ioctl(fd, AUDIO_STOP_VOICE, NULL); 178 close(fd); 179 } 180 181 vr_mode_change = false; 182 vr_mode_enabled = 0; 183 enable1026 = 1; 184 char value[PROPERTY_VALUE_MAX]; 185 // Check the system property to enable or not the special recording modes 186 property_get("media.a1026.enableA1026", value, "1"); 187 enable1026 = atoi(value); 188 LOGV("Enable mode selection for A1026 is %d", enable1026); 189 // Check the system property for which VR mode to use 190 property_get("media.a1026.nsForVoiceRec", value, "0"); 191 vr_uses_ns = atoi(value); 192 LOGV("Using Noise Suppression for Voice Rec is %d", vr_uses_ns); 193 194 // Check the system property for enable or not the ALT function 195 property_get("htc.audio.alt.enable", value, "0"); 196 alt_enable = atoi(value); 197 LOGV("Enable ALT function: %d", alt_enable); 198 199 // Check the system property for enable or not the HAC function 200 property_get("htc.audio.hac.enable", value, "0"); 201 hac_enable = atoi(value); 202 LOGV("Enable HAC function: %d", hac_enable); 203 204 mInit = true; 205 } 206 207 AudioHardware::~AudioHardware() 208 { 209 for (size_t index = 0; index < mInputs.size(); index++) { 210 closeInputStream((AudioStreamIn*)mInputs[index]); 211 } 212 mInputs.clear(); 213 closeOutputStream((AudioStreamOut*)mOutput); 214 mInit = false; 215 } 216 217 status_t AudioHardware::initCheck() 218 { 219 return mInit ? NO_ERROR : NO_INIT; 220 } 221 222 AudioStreamOut* AudioHardware::openOutputStream( 223 uint32_t devices, int *format, uint32_t *channels, uint32_t *sampleRate, status_t *status) 224 { 225 { // scope for the lock 226 Mutex::Autolock lock(mLock); 227 228 // only one output stream allowed 229 if (mOutput) { 230 if (status) { 231 *status = INVALID_OPERATION; 232 } 233 return 0; 234 } 235 236 // create new output stream 237 AudioStreamOutMSM72xx* out = new AudioStreamOutMSM72xx(); 238 status_t lStatus = out->set(this, devices, format, channels, sampleRate); 239 if (status) { 240 *status = lStatus; 241 } 242 if (lStatus == NO_ERROR) { 243 mOutput = out; 244 } else { 245 delete out; 246 } 247 } 248 return mOutput; 249 } 250 251 void AudioHardware::closeOutputStream(AudioStreamOut* out) { 252 Mutex::Autolock lock(mLock); 253 if (mOutput == 0 || mOutput != out) { 254 LOGW("Attempt to close invalid output stream"); 255 } 256 else { 257 delete mOutput; 258 mOutput = 0; 259 } 260 } 261 262 AudioStreamIn* AudioHardware::openInputStream( 263 uint32_t devices, int *format, uint32_t *channels, uint32_t *sampleRate, status_t *status, 264 AudioSystem::audio_in_acoustics acoustic_flags) 265 { 266 // check for valid input source 267 if (!AudioSystem::isInputDevice((AudioSystem::audio_devices)devices)) { 268 return 0; 269 } 270 271 mLock.lock(); 272 273 AudioStreamInMSM72xx* in = new AudioStreamInMSM72xx(); 274 status_t lStatus = in->set(this, devices, format, channels, sampleRate, acoustic_flags); 275 if (status) { 276 *status = lStatus; 277 } 278 if (lStatus != NO_ERROR) { 279 mLock.unlock(); 280 delete in; 281 return 0; 282 } 283 284 mInputs.add(in); 285 mLock.unlock(); 286 287 return in; 288 } 289 290 void AudioHardware::closeInputStream(AudioStreamIn* in) { 291 Mutex::Autolock lock(mLock); 292 293 ssize_t index = mInputs.indexOf((AudioStreamInMSM72xx *)in); 294 if (index < 0) { 295 LOGW("Attempt to close invalid input stream"); 296 } else { 297 mLock.unlock(); 298 delete mInputs[index]; 299 mLock.lock(); 300 mInputs.removeAt(index); 301 } 302 } 303 304 status_t AudioHardware::setMode(int mode) 305 { 306 // VR mode is never used in a call and must be cleared when entering the IN_CALL mode 307 if (mode == AudioSystem::MODE_IN_CALL) { 308 vr_mode_enabled = 0; 309 } 310 311 if (support_tpa2018d1) 312 do_tpa2018_control(mode); 313 314 int prevMode = mMode; 315 status_t status = AudioHardwareBase::setMode(mode); 316 if (status == NO_ERROR) { 317 // make sure that doAudioRouteOrMute() is called by doRouting() 318 // when entering or exiting in call mode even if the new device 319 // selected is the same as current one. 320 if (((prevMode != AudioSystem::MODE_IN_CALL) && (mMode == AudioSystem::MODE_IN_CALL)) || 321 ((prevMode == AudioSystem::MODE_IN_CALL) && (mMode != AudioSystem::MODE_IN_CALL))) { 322 clearCurDevice(); 323 } 324 } 325 326 return status; 327 } 328 329 bool AudioHardware::checkOutputStandby() 330 { 331 if (mOutput) 332 if (!mOutput->checkStandby()) 333 return false; 334 335 return true; 336 } 337 static status_t set_mic_mute(bool _mute) 338 { 339 uint32_t mute = _mute; 340 int fd = -1; 341 fd = open("/dev/msm_audio_ctl", O_RDWR); 342 if (fd < 0) { 343 LOGE("Cannot open msm_audio_ctl device\n"); 344 return -1; 345 } 346 LOGD("Setting mic mute to %d\n", mute); 347 if (ioctl(fd, AUDIO_SET_MUTE, &mute)) { 348 LOGE("Cannot set mic mute on current device\n"); 349 close(fd); 350 return -1; 351 } 352 close(fd); 353 return NO_ERROR; 354 } 355 356 status_t AudioHardware::setMicMute(bool state) 357 { 358 Mutex::Autolock lock(mLock); 359 return setMicMute_nosync(state); 360 } 361 362 // always call with mutex held 363 status_t AudioHardware::setMicMute_nosync(bool state) 364 { 365 if (mMicMute != state) { 366 mMicMute = state; 367 return set_mic_mute(mMicMute); //always set current TX device 368 } 369 return NO_ERROR; 370 } 371 372 status_t AudioHardware::getMicMute(bool* state) 373 { 374 *state = mMicMute; 375 return NO_ERROR; 376 } 377 378 status_t AudioHardware::setParameters(const String8& keyValuePairs) 379 { 380 AudioParameter param = AudioParameter(keyValuePairs); 381 String8 value; 382 String8 key; 383 const char BT_NREC_KEY[] = "bt_headset_nrec"; 384 const char BT_NAME_KEY[] = "bt_headset_name"; 385 const char HAC_KEY[] = "HACSetting"; 386 const char BT_NREC_VALUE_ON[] = "on"; 387 const char HAC_VALUE_ON[] = "ON"; 388 389 390 LOGV("setParameters() %s", keyValuePairs.string()); 391 392 if (keyValuePairs.length() == 0) return BAD_VALUE; 393 394 if(hac_enable) { 395 key = String8(HAC_KEY); 396 if (param.get(key, value) == NO_ERROR) { 397 if (value == HAC_VALUE_ON) { 398 mHACSetting = true; 399 LOGD("Enable HAC"); 400 } else { 401 mHACSetting = false; 402 LOGD("Disable HAC"); 403 } 404 } 405 } 406 407 key = String8(BT_NREC_KEY); 408 if (param.get(key, value) == NO_ERROR) { 409 if (value == BT_NREC_VALUE_ON) { 410 mBluetoothNrec = true; 411 } else { 412 mBluetoothNrec = false; 413 LOGI("Turning noise reduction and echo cancellation off for BT " 414 "headset"); 415 } 416 } 417 key = String8(BT_NAME_KEY); 418 if (param.get(key, value) == NO_ERROR) { 419 mBluetoothIdTx = 0; 420 mBluetoothIdRx = 0; 421 for (int i = 0; i < mNumBTEndpoints; i++) { 422 if (!strcasecmp(value.string(), mBTEndpoints[i].name)) { 423 mBluetoothIdTx = mBTEndpoints[i].tx; 424 mBluetoothIdRx = mBTEndpoints[i].rx; 425 LOGI("Using custom acoustic parameters for %s", value.string()); 426 break; 427 } 428 } 429 if (mBluetoothIdTx == 0) { 430 LOGI("Using default acoustic parameters " 431 "(%s not in acoustic database)", value.string()); 432 } 433 doRouting(); 434 } 435 key = String8("noise_suppression"); 436 if (param.get(key, value) == NO_ERROR) { 437 if (support_a1026 == 1) { 438 int noiseSuppressionState; 439 if (value == "off") { 440 noiseSuppressionState = A1026_NS_STATE_OFF; 441 } else if (value == "auto") { 442 noiseSuppressionState = A1026_NS_STATE_AUTO; 443 } else if (value == "far_talk") { 444 noiseSuppressionState = A1026_NS_STATE_FT; 445 } else if (value == "close_talk") { 446 noiseSuppressionState = A1026_NS_STATE_CT; 447 } else { 448 return BAD_VALUE; 449 } 450 451 if (noiseSuppressionState != mNoiseSuppressionState) { 452 if (!mA1026Init) { 453 LOGW("Audience A1026 not initialized.\n"); 454 return INVALID_OPERATION; 455 } 456 457 mA1026Lock.lock(); 458 if (fd_a1026 < 0) { 459 fd_a1026 = open("/dev/audience_a1026", O_RDWR); 460 if (fd_a1026 < 0) { 461 LOGE("Cannot open audience_a1026 device (%d)\n", fd_a1026); 462 mA1026Lock.unlock(); 463 return -1; 464 } 465 } 466 LOGV("Setting noise suppression %s", value.string()); 467 468 int rc = ioctl(fd_a1026, A1026_SET_NS_STATE, &noiseSuppressionState); 469 if (!rc) { 470 mNoiseSuppressionState = noiseSuppressionState; 471 } else { 472 LOGE("Failed to set noise suppression %s", value.string()); 473 } 474 close(fd_a1026); 475 fd_a1026 = -1; 476 mA1026Lock.unlock(); 477 } 478 } else { 479 return INVALID_OPERATION; 480 } 481 } 482 483 key = String8("tty_mode"); 484 if (param.get(key, value) == NO_ERROR) { 485 int ttyMode; 486 if (value == "tty_off") { 487 ttyMode = TTY_MODE_OFF; 488 } else if (value == "tty_full") { 489 ttyMode = TTY_MODE_FULL; 490 } else if (value == "tty_vco") { 491 ttyMode = TTY_MODE_VCO; 492 } else if (value == "tty_hco") { 493 ttyMode = TTY_MODE_HCO; 494 } else { 495 return BAD_VALUE; 496 } 497 498 if (ttyMode != mTTYMode) { 499 LOGV("new tty mode %d", ttyMode); 500 mTTYMode = ttyMode; 501 doRouting(); 502 } 503 } 504 505 return NO_ERROR; 506 } 507 508 String8 AudioHardware::getParameters(const String8& keys) 509 { 510 AudioParameter request = AudioParameter(keys); 511 AudioParameter reply = AudioParameter(); 512 String8 value; 513 String8 key; 514 515 LOGV("getParameters() %s", keys.string()); 516 517 key = "noise_suppression"; 518 if (request.get(key, value) == NO_ERROR) { 519 switch(mNoiseSuppressionState) { 520 case A1026_NS_STATE_OFF: 521 value = "off"; 522 break; 523 case A1026_NS_STATE_AUTO: 524 value = "auto"; 525 break; 526 case A1026_NS_STATE_FT: 527 value = "far_talk"; 528 break; 529 case A1026_NS_STATE_CT: 530 value = "close_talk"; 531 break; 532 } 533 reply.add(key, value); 534 } 535 536 return reply.toString(); 537 } 538 539 540 static unsigned calculate_audpre_table_index(unsigned index) 541 { 542 switch (index) { 543 case 48000: return SAMP_RATE_INDX_48000; 544 case 44100: return SAMP_RATE_INDX_44100; 545 case 32000: return SAMP_RATE_INDX_32000; 546 case 24000: return SAMP_RATE_INDX_24000; 547 case 22050: return SAMP_RATE_INDX_22050; 548 case 16000: return SAMP_RATE_INDX_16000; 549 case 12000: return SAMP_RATE_INDX_12000; 550 case 11025: return SAMP_RATE_INDX_11025; 551 case 8000: return SAMP_RATE_INDX_8000; 552 default: return -1; 553 } 554 } 555 556 size_t AudioHardware::getBufferSize(uint32_t sampleRate, int channelCount) 557 { 558 size_t bufSize; 559 560 if (sampleRate < 11025) { 561 bufSize = 256; 562 } else if (sampleRate < 22050) { 563 bufSize = 512; 564 } else if (sampleRate < 32000) { 565 bufSize = 768; 566 } else if (sampleRate < 44100) { 567 bufSize = 1024; 568 } else { 569 bufSize = 1536; 570 } 571 572 return bufSize*channelCount; 573 } 574 575 576 size_t AudioHardware::getInputBufferSize(uint32_t sampleRate, int format, int channelCount) 577 { 578 if (format != AudioSystem::PCM_16_BIT) { 579 LOGW("getInputBufferSize bad format: %d", format); 580 return 0; 581 } 582 if (channelCount < 1 || channelCount > 2) { 583 LOGW("getInputBufferSize bad channel count: %d", channelCount); 584 return 0; 585 } 586 if (sampleRate < 8000 || sampleRate > 48000) { 587 LOGW("getInputBufferSize bad sample rate: %d", sampleRate); 588 return 0; 589 } 590 591 return getBufferSize(sampleRate, channelCount); 592 } 593 594 static status_t set_volume_rpc(uint32_t volume) 595 { 596 int fd = -1; 597 fd = open("/dev/msm_audio_ctl", O_RDWR); 598 if (fd < 0) { 599 LOGE("Cannot open msm_audio_ctl device\n"); 600 return -1; 601 } 602 volume *= 20; //percentage 603 LOGD("Setting in-call volume to %d\n", volume); 604 if (ioctl(fd, AUDIO_SET_VOLUME, &volume)) { 605 LOGW("Cannot set volume on current device\n"); 606 } 607 close(fd); 608 return NO_ERROR; 609 } 610 611 status_t AudioHardware::setVoiceVolume(float v) 612 { 613 if (v < 0.0) { 614 LOGW("setVoiceVolume(%f) under 0.0, assuming 0.0\n", v); 615 v = 0.0; 616 } else if (v > 1.0) { 617 LOGW("setVoiceVolume(%f) over 1.0, assuming 1.0\n", v); 618 v = 1.0; 619 } 620 621 int vol = lrint(v * VOICE_VOLUME_MAX); 622 623 Mutex::Autolock lock(mLock); 624 if (mHACSetting && hac_enable && mCurSndDevice == (int) SND_DEVICE_HANDSET) { 625 LOGD("HAC enable: Setting in-call volume to maximum.\n"); 626 set_volume_rpc(VOICE_VOLUME_MAX); 627 } else { 628 LOGD("setVoiceVolume(%f)\n", v); 629 LOGI("Setting in-call volume to %d (available range is 0 to %d)\n", vol, VOICE_VOLUME_MAX); 630 set_volume_rpc(vol); //always set current device 631 } 632 mVoiceVolume = vol; 633 return NO_ERROR; 634 } 635 636 status_t AudioHardware::setMasterVolume(float v) 637 { 638 LOGI("Set master volume to %f.\n", v); 639 // We return an error code here to let the audioflinger do in-software 640 // volume on top of the maximum volume that we set through the SND API. 641 // return error - software mixer will handle it 642 return -1; 643 } 644 645 static status_t do_route_audio_dev_ctrl(uint32_t device, bool inCall, uint32_t rx_acdb_id, uint32_t tx_acdb_id) 646 { 647 uint32_t out_device = 0, mic_device = 0; 648 uint32_t path[2]; 649 int fd = 0; 650 651 if (device == SND_DEVICE_CURRENT) 652 goto Incall; 653 654 // hack -- kernel needs to put these in include file 655 LOGD("Switching audio device to "); 656 if (device == SND_DEVICE_HANDSET) { 657 out_device = HANDSET_SPKR; 658 mic_device = HANDSET_MIC; 659 LOGD("Handset"); 660 } else if ((device == SND_DEVICE_BT) || (device == SND_DEVICE_BT_EC_OFF)) { 661 out_device = BT_SCO_SPKR; 662 mic_device = BT_SCO_MIC; 663 LOGD("BT Headset"); 664 } else if (device == SND_DEVICE_SPEAKER || 665 device == SND_DEVICE_SPEAKER_BACK_MIC) { 666 out_device = SPKR_PHONE_MONO; 667 mic_device = SPKR_PHONE_MIC; 668 LOGD("Speakerphone"); 669 } else if (device == SND_DEVICE_HEADSET) { 670 out_device = HEADSET_SPKR_STEREO; 671 mic_device = HEADSET_MIC; 672 LOGD("Stereo Headset"); 673 } else if (device == SND_DEVICE_HEADSET_AND_SPEAKER) { 674 out_device = SPKR_PHONE_HEADSET_STEREO; 675 mic_device = HEADSET_MIC; 676 LOGD("Stereo Headset + Speaker"); 677 } else if (device == SND_DEVICE_HEADSET_AND_SPEAKER_BACK_MIC) { 678 out_device = SPKR_PHONE_HEADSET_STEREO; 679 mic_device = SPKR_PHONE_MIC; 680 LOGD("Stereo Headset + Speaker and back mic"); 681 } else if (device == SND_DEVICE_NO_MIC_HEADSET) { 682 out_device = HEADSET_SPKR_STEREO; 683 mic_device = HANDSET_MIC; 684 LOGD("No microphone Wired Headset"); 685 } else if (device == SND_DEVICE_NO_MIC_HEADSET_BACK_MIC) { 686 out_device = HEADSET_SPKR_STEREO; 687 mic_device = SPKR_PHONE_MIC; 688 LOGD("No microphone Wired Headset and back mic"); 689 } else if (device == SND_DEVICE_HANDSET_BACK_MIC) { 690 out_device = HANDSET_SPKR; 691 mic_device = SPKR_PHONE_MIC; 692 LOGD("Handset and back mic"); 693 } else if (device == SND_DEVICE_FM_HEADSET) { 694 out_device = FM_HEADSET; 695 mic_device = HEADSET_MIC; 696 LOGD("Stereo FM headset"); 697 } else if (device == SND_DEVICE_FM_SPEAKER) { 698 out_device = FM_SPKR; 699 mic_device = HEADSET_MIC; 700 LOGD("Stereo FM speaker"); 701 } else if (device == SND_DEVICE_CARKIT) { 702 out_device = BT_SCO_SPKR; 703 mic_device = BT_SCO_MIC; 704 LOGD("Carkit"); 705 } else if (device == SND_DEVICE_TTY_FULL) { 706 out_device = TTY_HEADSET_SPKR; 707 mic_device = TTY_HEADSET_MIC; 708 LOGD("TTY FULL headset"); 709 } else if (device == SND_DEVICE_TTY_VCO) { 710 out_device = TTY_HEADSET_SPKR; 711 mic_device = SPKR_PHONE_MIC; 712 LOGD("TTY VCO headset"); 713 } else if (device == SND_DEVICE_TTY_HCO) { 714 out_device = SPKR_PHONE_MONO; 715 mic_device = TTY_HEADSET_MIC; 716 LOGD("TTY HCO headset"); 717 } else { 718 LOGE("unknown device %d", device); 719 return -1; 720 } 721 722 #if 0 //Add for FM support 723 if (out_device == FM_HEADSET || 724 out_device == FM_SPKR) { 725 if (fd_fm_device < 0) { 726 fd_fm_device = open("/dev/msm_htc_fm", O_RDWR); 727 if (fd_fm_device < 0) { 728 LOGE("Cannot open msm_htc_fm device"); 729 return -1; 730 } 731 LOGD("Opened msm_htc_fm for FM radio"); 732 } 733 } else if (fd_fm_device >= 0) { 734 close(fd_fm_device); 735 fd_fm_device = -1; 736 LOGD("Closed msm_htc_fm after FM radio"); 737 } 738 #endif 739 740 fd = open("/dev/msm_audio_ctl", O_RDWR); 741 if (fd < 0) { 742 LOGE("Cannot open msm_audio_ctl"); 743 return -1; 744 } 745 path[0] = out_device; 746 path[1] = rx_acdb_id; 747 if (ioctl(fd, AUDIO_SWITCH_DEVICE, &path)) { 748 LOGE("Cannot switch audio device"); 749 close(fd); 750 return -1; 751 } 752 path[0] = mic_device; 753 path[1] = tx_acdb_id; 754 if (ioctl(fd, AUDIO_SWITCH_DEVICE, &path)) { 755 LOGE("Cannot switch mic device"); 756 close(fd); 757 return -1; 758 } 759 curr_out_device = out_device; 760 curr_mic_device = mic_device; 761 762 Incall: 763 if (inCall == true && !voice_started) { 764 if (fd < 0) { 765 fd = open("/dev/msm_audio_ctl", O_RDWR); 766 767 if (fd < 0) { 768 LOGE("Cannot open msm_audio_ctl"); 769 return -1; 770 } 771 } 772 path[0] = rx_acdb_id; 773 path[1] = tx_acdb_id; 774 if (ioctl(fd, AUDIO_START_VOICE, &path)) { 775 LOGE("Cannot start voice"); 776 close(fd); 777 return -1; 778 } 779 LOGD("Voice Started!!"); 780 voice_started = 1; 781 } 782 else if (inCall == false && voice_started) { 783 if (fd < 0) { 784 fd = open("/dev/msm_audio_ctl", O_RDWR); 785 786 if (fd < 0) { 787 LOGE("Cannot open msm_audio_ctl"); 788 return -1; 789 } 790 } 791 if (ioctl(fd, AUDIO_STOP_VOICE, NULL)) { 792 LOGE("Cannot stop voice"); 793 close(fd); 794 return -1; 795 } 796 LOGD("Voice Stopped!!"); 797 voice_started = 0; 798 } 799 800 close(fd); 801 return NO_ERROR; 802 } 803 804 805 // always call with mutex held 806 status_t AudioHardware::doAudioRouteOrMute(uint32_t device) 807 { 808 uint32_t rx_acdb_id = 0; 809 uint32_t tx_acdb_id = 0; 810 811 if (support_a1026 == 1) 812 doAudience_A1026_Control(mMode, mRecordState, device); 813 814 if (device == (uint32_t)SND_DEVICE_BT) { 815 if (!mBluetoothNrec) { 816 device = SND_DEVICE_BT_EC_OFF; 817 } 818 } 819 820 821 if (device == (int) SND_DEVICE_BT 822 || device == (int) SND_DEVICE_BT_EC_OFF) { 823 if (mBluetoothIdTx != 0) { 824 rx_acdb_id = mBluetoothIdRx; 825 tx_acdb_id = mBluetoothIdTx; 826 } else { 827 /* use default BT entry defined in AudioBTID.csv */ 828 rx_acdb_id = mBTEndpoints[0].rx; 829 tx_acdb_id = mBTEndpoints[0].tx; 830 LOGD("Update ACDB ID to default BT setting\n"); 831 } 832 } else if (device == (int) SND_DEVICE_CARKIT) { 833 if (mBluetoothIdTx != 0) { 834 rx_acdb_id = mBluetoothIdRx; 835 tx_acdb_id = mBluetoothIdTx; 836 } else { 837 /* use default carkit entry defined in AudioBTID.csv */ 838 rx_acdb_id = mBTEndpoints[1].rx; 839 tx_acdb_id = mBTEndpoints[1].tx; 840 LOGD("Update ACDB ID to default carkit setting"); 841 } 842 } else if (mMode == AudioSystem::MODE_IN_CALL 843 && hac_enable && mHACSetting && 844 device == (int) SND_DEVICE_HANDSET) { 845 LOGE("Update acdb id to hac profile."); 846 rx_acdb_id = ACDB_ID_HAC_HANDSET_SPKR; 847 tx_acdb_id = ACDB_ID_HAC_HANDSET_MIC; 848 } else { 849 if (!checkOutputStandby() || mMode != AudioSystem::MODE_IN_CALL) 850 rx_acdb_id = getACDB(MOD_PLAY, device); 851 if (mRecordState) 852 tx_acdb_id = getACDB(MOD_REC, device); 853 } 854 LOGV("doAudioRouteOrMute: rx acdb %d, tx acdb %d\n", rx_acdb_id, tx_acdb_id); 855 856 return do_route_audio_dev_ctrl(device, mMode == AudioSystem::MODE_IN_CALL, rx_acdb_id, tx_acdb_id); 857 } 858 859 status_t AudioHardware::get_mMode(void) 860 { 861 return mMode; 862 } 863 864 status_t AudioHardware::get_mRoutes(void) 865 { 866 return mRoutes[mMode]; 867 } 868 869 status_t AudioHardware::set_mRecordState(bool onoff) 870 { 871 mRecordState = onoff; 872 return 0; 873 } 874 875 status_t AudioHardware::get_batt_temp(int *batt_temp) 876 { 877 int fd, len; 878 const char *fn = 879 "/sys/devices/platform/ds2784-battery/power_supply/battery/temp"; 880 char get_batt_temp[6] = { 0 }; 881 882 if ((fd = open(fn, O_RDONLY)) < 0) { 883 LOGE("%s: cannot open %s: %s\n", __FUNCTION__, fn, strerror(errno)); 884 return UNKNOWN_ERROR; 885 } 886 887 if ((len = read(fd, get_batt_temp, sizeof(get_batt_temp))) <= 1) { 888 LOGE("read battery temp fail: %s\n", strerror(errno)); 889 close(fd); 890 return BAD_VALUE; 891 } 892 893 *batt_temp = strtol(get_batt_temp, NULL, 10); 894 close(fd); 895 return NO_ERROR; 896 } 897 898 /* 899 * Note: upon exiting doA1026_init(), fd_a1026 will be -1 900 */ 901 status_t AudioHardware::doA1026_init(void) 902 { 903 struct a1026img fwimg; 904 char char_tmp = 0; 905 unsigned char local_vpimg_buf[A1026_MAX_FW_SIZE], *ptr = local_vpimg_buf; 906 int rc = 0, fw_fd = -1; 907 ssize_t nr; 908 size_t remaining; 909 struct stat fw_stat; 910 911 static const char *const fn = "/system/etc/vpimg"; 912 static const char *const path = "/dev/audience_a1026"; 913 914 if (fd_a1026 < 0) 915 fd_a1026 = open(path, O_RDWR | O_NONBLOCK, 0); 916 917 if (fd_a1026 < 0) { 918 LOGE("Cannot open %s %d\n", path, fd_a1026); 919 support_a1026 = 0; 920 goto open_drv_err; 921 } 922 923 fw_fd = open(fn, O_RDONLY); 924 if (fw_fd < 0) { 925 LOGE("Fail to open %s\n", fn); 926 goto ld_img_error; 927 } else LOGI("open %s success\n", fn); 928 929 rc = fstat(fw_fd, &fw_stat); 930 if (rc < 0) { 931 LOGE("Cannot stat file %s: %s\n", fn, strerror(errno)); 932 goto ld_img_error; 933 } 934 935 remaining = (int)fw_stat.st_size; 936 937 LOGI("Firmware %s size %d\n", fn, remaining); 938 939 if (remaining > sizeof(local_vpimg_buf)) { 940 LOGE("File %s size %d exceeds internal limit %d\n", 941 fn, remaining, sizeof(local_vpimg_buf)); 942 goto ld_img_error; 943 } 944 945 while (remaining) { 946 nr = read(fw_fd, ptr, remaining); 947 if (nr < 0) { 948 LOGE("Error reading firmware: %s\n", strerror(errno)); 949 goto ld_img_error; 950 } 951 else if (!nr) { 952 if (remaining) 953 LOGW("EOF reading firmware %s while %d bytes remain\n", 954 fn, remaining); 955 break; 956 } 957 remaining -= nr; 958 ptr += nr; 959 } 960 961 close (fw_fd); 962 fw_fd = -1; 963 964 fwimg.buf = local_vpimg_buf; 965 fwimg.img_size = (int)(fw_stat.st_size - remaining); 966 LOGI("Total %d bytes put to user space buffer.\n", fwimg.img_size); 967 968 rc = ioctl(fd_a1026, A1026_BOOTUP_INIT, &fwimg); 969 if (!rc) { 970 LOGI("audience_a1026 init OK\n"); 971 mA1026Init = 1; 972 } else 973 LOGE("audience_a1026 init failed\n"); 974 975 ld_img_error: 976 if (fw_fd >= 0) 977 close(fw_fd); 978 close(fd_a1026); 979 open_drv_err: 980 fd_a1026 = -1; 981 return rc; 982 } 983 984 status_t AudioHardware::get_snd_dev(void) 985 { 986 Mutex::Autolock lock(mLock); 987 return mCurSndDevice; 988 } 989 990 uint32_t AudioHardware::getACDB(int mode, int device) 991 { 992 uint32_t acdb_id = 0; 993 int batt_temp = 0; 994 if (mMode == AudioSystem::MODE_IN_CALL) { 995 LOGD("skip update ACDB due to in-call"); 996 return 0; 997 } 998 999 if (mode == MOD_PLAY) { 1000 switch (device) { 1001 case SND_DEVICE_HEADSET: 1002 case SND_DEVICE_NO_MIC_HEADSET: 1003 case SND_DEVICE_NO_MIC_HEADSET_BACK_MIC: 1004 case SND_DEVICE_FM_HEADSET: 1005 acdb_id = ACDB_ID_HEADSET_PLAYBACK; 1006 break; 1007 case SND_DEVICE_SPEAKER: 1008 case SND_DEVICE_FM_SPEAKER: 1009 case SND_DEVICE_SPEAKER_BACK_MIC: 1010 acdb_id = ACDB_ID_SPKR_PLAYBACK; 1011 if(alt_enable) { 1012 LOGD("Enable ALT for speaker\n"); 1013 if (get_batt_temp(&batt_temp) == NO_ERROR) { 1014 if (batt_temp < 50) 1015 acdb_id = ACDB_ID_ALT_SPKR_PLAYBACK; 1016 LOGD("ALT batt temp = %d\n", batt_temp); 1017 } 1018 } 1019 break; 1020 case SND_DEVICE_HEADSET_AND_SPEAKER: 1021 case SND_DEVICE_HEADSET_AND_SPEAKER_BACK_MIC: 1022 acdb_id = ACDB_ID_HEADSET_RINGTONE_PLAYBACK; 1023 break; 1024 default: 1025 break; 1026 } 1027 } else if (mode == MOD_REC) { 1028 switch (device) { 1029 case SND_DEVICE_HEADSET: 1030 case SND_DEVICE_FM_HEADSET: 1031 case SND_DEVICE_FM_SPEAKER: 1032 case SND_DEVICE_HEADSET_AND_SPEAKER: 1033 acdb_id = ACDB_ID_EXT_MIC_REC; 1034 break; 1035 case SND_DEVICE_HANDSET: 1036 case SND_DEVICE_NO_MIC_HEADSET: 1037 case SND_DEVICE_SPEAKER: 1038 if (vr_mode_enabled == 0) { 1039 acdb_id = ACDB_ID_INT_MIC_REC; 1040 } else { 1041 acdb_id = ACDB_ID_INT_MIC_VR; 1042 } 1043 break; 1044 case SND_DEVICE_SPEAKER_BACK_MIC: 1045 case SND_DEVICE_NO_MIC_HEADSET_BACK_MIC: 1046 case SND_DEVICE_HANDSET_BACK_MIC: 1047 case SND_DEVICE_HEADSET_AND_SPEAKER_BACK_MIC: 1048 acdb_id = ACDB_ID_CAMCORDER; 1049 break; 1050 default: 1051 break; 1052 } 1053 } 1054 LOGV("getACDB, return ID %d\n", acdb_id); 1055 return acdb_id; 1056 } 1057 1058 status_t AudioHardware::do_tpa2018_control(int mode) 1059 { 1060 if (curr_out_device == HANDSET_SPKR || 1061 curr_out_device == SPKR_PHONE_MONO || 1062 curr_out_device == HEADSET_SPKR_STEREO || 1063 curr_out_device == SPKR_PHONE_HEADSET_STEREO || 1064 curr_out_device == FM_SPKR) { 1065 1066 int fd, rc; 1067 int retry = 3; 1068 1069 switch (mode) { 1070 case AudioSystem::MODE_NORMAL: 1071 mode = TPA2018_MODE_PLAYBACK; 1072 break; 1073 case AudioSystem::MODE_RINGTONE: 1074 mode = TPA2018_MODE_RINGTONE; 1075 break; 1076 case AudioSystem::MODE_IN_CALL: 1077 mode = TPA2018_MODE_VOICE_CALL; 1078 break; 1079 default: 1080 return 0; 1081 } 1082 1083 fd = open("/dev/tpa2018d1", O_RDWR); 1084 if (fd < 0) { 1085 LOGE("can't open /dev/tpa2018d1 %d", fd); 1086 return -1; 1087 } 1088 1089 do { 1090 rc = ioctl(fd, TPA2018_SET_MODE, &mode); 1091 if (!rc) 1092 break; 1093 } while (--retry); 1094 1095 if (rc < 0) { 1096 LOGE("ioctl TPA2018_SET_MODE failed: %s", strerror(errno)); 1097 } else 1098 LOGD("Update TPA2018_SET_MODE to mode %d success", mode); 1099 1100 close(fd); 1101 } 1102 return 0; 1103 } 1104 1105 status_t AudioHardware::doAudience_A1026_Control(int Mode, bool Record, uint32_t Routes) 1106 { 1107 int rc = 0; 1108 int retry = 4; 1109 1110 if (!mA1026Init) { 1111 LOGW("Audience A1026 not initialized.\n"); 1112 return NO_INIT; 1113 } 1114 1115 mA1026Lock.lock(); 1116 if (fd_a1026 < 0) { 1117 fd_a1026 = open("/dev/audience_a1026", O_RDWR); 1118 if (fd_a1026 < 0) { 1119 LOGE("Cannot open audience_a1026 device (%d)\n", fd_a1026); 1120 mA1026Lock.unlock(); 1121 return -1; 1122 } 1123 } 1124 1125 if ((Mode < AudioSystem::MODE_CURRENT) || (Mode >= AudioSystem::NUM_MODES)) { 1126 LOGW("Illegal value: doAudience_A1026_Control(%d, %u, %u)", Mode, Record, Routes); 1127 mA1026Lock.unlock(); 1128 return BAD_VALUE; 1129 } 1130 1131 if (Mode == AudioSystem::MODE_IN_CALL) { 1132 if (Record == 1) { 1133 switch (Routes) { 1134 case SND_DEVICE_HANDSET: 1135 case SND_DEVICE_NO_MIC_HEADSET: 1136 //TODO: what do we do for camcorder when in call? 1137 case SND_DEVICE_NO_MIC_HEADSET_BACK_MIC: 1138 case SND_DEVICE_HANDSET_BACK_MIC: 1139 case SND_DEVICE_TTY_VCO: 1140 if (enable1026) { 1141 new_pathid = A1026_PATH_INCALL_RECEIVER; 1142 LOGV("A1026 control: new path is A1026_PATH_INCALL_RECEIVER"); 1143 } else { 1144 new_pathid = A1026_PATH_INCALL_NO_NS_RECEIVER; 1145 LOGV("A1026 control: new path is A1026_PATH_INCALL_NO_NS_RECEIVER"); 1146 } 1147 break; 1148 case SND_DEVICE_HEADSET: 1149 case SND_DEVICE_HEADSET_AND_SPEAKER: 1150 case SND_DEVICE_FM_HEADSET: 1151 case SND_DEVICE_FM_SPEAKER: 1152 case SND_DEVICE_HEADSET_AND_SPEAKER_BACK_MIC: 1153 new_pathid = A1026_PATH_INCALL_HEADSET; 1154 LOGV("A1026 control: new path is A1026_PATH_INCALL_HEADSET"); 1155 break; 1156 case SND_DEVICE_SPEAKER: 1157 //TODO: what do we do for camcorder when in call? 1158 case SND_DEVICE_SPEAKER_BACK_MIC: 1159 new_pathid = A1026_PATH_INCALL_SPEAKER; 1160 LOGV("A1026 control: new path is A1026_PATH_INCALL_SPEAKER"); 1161 break; 1162 case SND_DEVICE_BT: 1163 case SND_DEVICE_BT_EC_OFF: 1164 case SND_DEVICE_CARKIT: 1165 new_pathid = A1026_PATH_INCALL_BT; 1166 LOGV("A1026 control: new path is A1026_PATH_INCALL_BT"); 1167 break; 1168 case SND_DEVICE_TTY_HCO: 1169 case SND_DEVICE_TTY_FULL: 1170 new_pathid = A1026_PATH_INCALL_TTY; 1171 LOGV("A1026 control: new path is A1026_PATH_INCALL_TTY"); 1172 break; 1173 default: 1174 break; 1175 } 1176 } else { 1177 switch (Routes) { 1178 case SND_DEVICE_HANDSET: 1179 case SND_DEVICE_NO_MIC_HEADSET: 1180 case SND_DEVICE_TTY_VCO: 1181 if (enable1026) { 1182 new_pathid = A1026_PATH_INCALL_RECEIVER; /* NS CT mode, Dual MIC */ 1183 LOGV("A1026 control: new path is A1026_PATH_INCALL_RECEIVER"); 1184 } else { 1185 new_pathid = A1026_PATH_INCALL_NO_NS_RECEIVER; 1186 LOGV("A1026 control: new path is A1026_PATH_INCALL_NO_NS_RECEIVER"); 1187 } 1188 break; 1189 case SND_DEVICE_HEADSET: 1190 case SND_DEVICE_HEADSET_AND_SPEAKER: 1191 case SND_DEVICE_FM_HEADSET: 1192 case SND_DEVICE_FM_SPEAKER: 1193 new_pathid = A1026_PATH_INCALL_HEADSET; /* NS disable, Headset MIC */ 1194 LOGV("A1026 control: new path is A1026_PATH_INCALL_HEADSET"); 1195 break; 1196 case SND_DEVICE_SPEAKER: 1197 new_pathid = A1026_PATH_INCALL_SPEAKER; /* NS FT mode, Main MIC */ 1198 LOGV("A1026 control: new path is A1026_PATH_INCALL_SPEAKER"); 1199 break; 1200 case SND_DEVICE_BT: 1201 case SND_DEVICE_BT_EC_OFF: 1202 case SND_DEVICE_CARKIT: 1203 new_pathid = A1026_PATH_INCALL_BT; /* QCOM NS, BT MIC */ 1204 LOGV("A1026 control: new path is A1026_PATH_INCALL_BT"); 1205 break; 1206 case SND_DEVICE_TTY_HCO: 1207 case SND_DEVICE_TTY_FULL: 1208 new_pathid = A1026_PATH_INCALL_TTY; 1209 LOGV("A1026 control: new path is A1026_PATH_INCALL_TTY"); 1210 break; 1211 default: 1212 break; 1213 } 1214 } 1215 } else if (Record == 1) { 1216 switch (Routes) { 1217 case SND_DEVICE_SPEAKER: 1218 // default output is speaker, recording from phone mic, user RECEIVER configuration 1219 case SND_DEVICE_HANDSET: 1220 case SND_DEVICE_NO_MIC_HEADSET: 1221 if (vr_mode_enabled) { 1222 if (vr_uses_ns) { 1223 new_pathid = A1026_PATH_VR_NS_RECEIVER; 1224 LOGV("A1026 control: new path is A1026_PATH_VR_NS_RECEIVER"); 1225 } else { 1226 new_pathid = A1026_PATH_VR_NO_NS_RECEIVER; 1227 LOGV("A1026 control: new path is A1026_PATH_VR_NO_NS_RECEIVER"); 1228 } 1229 } else { 1230 new_pathid = A1026_PATH_RECORD_RECEIVER; /* INT-MIC Recording: NS disable, Main MIC */ 1231 LOGV("A1026 control: new path is A1026_PATH_RECORD_RECEIVER"); 1232 } 1233 break; 1234 case SND_DEVICE_HEADSET: 1235 case SND_DEVICE_HEADSET_AND_SPEAKER: 1236 case SND_DEVICE_FM_HEADSET: 1237 case SND_DEVICE_FM_SPEAKER: 1238 if (vr_mode_enabled) { 1239 if (vr_uses_ns) { 1240 new_pathid = A1026_PATH_VR_NS_HEADSET; 1241 LOGV("A1026 control: new path is A1026_PATH_VR_NS_HEADSET"); 1242 } else { 1243 new_pathid = A1026_PATH_VR_NO_NS_HEADSET; 1244 LOGV("A1026 control: new path is A1026_PATH_VR_NO_NS_HEADSET"); 1245 } 1246 } else { 1247 new_pathid = A1026_PATH_RECORD_HEADSET; /* EXT-MIC Recording: NS disable, Headset MIC */ 1248 LOGV("A1026 control: new path is A1026_PATH_RECORD_HEADSET"); 1249 } 1250 break; 1251 case SND_DEVICE_SPEAKER_BACK_MIC: 1252 case SND_DEVICE_NO_MIC_HEADSET_BACK_MIC: 1253 case SND_DEVICE_HANDSET_BACK_MIC: 1254 case SND_DEVICE_HEADSET_AND_SPEAKER_BACK_MIC: 1255 new_pathid = A1026_PATH_CAMCORDER; /* CAM-Coder: NS FT mode, Back MIC */ 1256 LOGV("A1026 control: new path is A1026_PATH_CAMCORDER"); 1257 break; 1258 case SND_DEVICE_BT: 1259 case SND_DEVICE_BT_EC_OFF: 1260 case SND_DEVICE_CARKIT: 1261 if (vr_mode_enabled) { 1262 if (vr_uses_ns) { 1263 new_pathid = A1026_PATH_VR_NS_BT; 1264 LOGV("A1026 control: new path is A1026_PATH_VR_NS_BT"); 1265 } else { 1266 new_pathid = A1026_PATH_VR_NO_NS_BT; 1267 LOGV("A1026 control: new path is A1026_PATH_VR_NO_NS_BT"); 1268 } 1269 } else { 1270 new_pathid = A1026_PATH_RECORD_BT; /* BT MIC */ 1271 LOGV("A1026 control: new path is A1026_PATH_RECORD_BT"); 1272 } 1273 break; 1274 default: 1275 break; 1276 } 1277 } 1278 else { 1279 switch (Routes) { 1280 case SND_DEVICE_BT: 1281 case SND_DEVICE_BT_EC_OFF: 1282 case SND_DEVICE_CARKIT: 1283 new_pathid = A1026_PATH_RECORD_BT; /* BT MIC */ 1284 LOGV("A1026 control: new path is A1026_PATH_RECORD_BT"); 1285 break; 1286 default: 1287 new_pathid = A1026_PATH_SUSPEND; 1288 break; 1289 } 1290 } 1291 1292 if (old_pathid != new_pathid) { 1293 //LOGI("A1026: do ioctl(A1026_SET_CONFIG) to %d\n", new_pathid); 1294 do { 1295 rc = ioctl(fd_a1026, A1026_SET_CONFIG, &new_pathid); 1296 if (!rc) { 1297 old_pathid = new_pathid; 1298 break; 1299 } 1300 } while (--retry); 1301 1302 if (rc < 0) { 1303 LOGE("A1026 do hard reset to recover from error!\n"); 1304 rc = doA1026_init(); /* A1026 needs to do hard reset! */ 1305 if (!rc) { 1306 /* after doA1026_init(), fd_a1026 is -1*/ 1307 fd_a1026 = open("/dev/audience_a1026", O_RDWR); 1308 if (fd_a1026 < 0) { 1309 LOGE("A1026 Fatal Error: unable to open A1026 after hard reset\n"); 1310 } else { 1311 rc = ioctl(fd_a1026, A1026_SET_CONFIG, &new_pathid); 1312 if (!rc) { 1313 old_pathid = new_pathid; 1314 } else { 1315 LOGE("A1026 Fatal Error: unable to A1026_SET_CONFIG after hard reset\n"); 1316 } 1317 } 1318 } else 1319 LOGE("A1026 Fatal Error: Re-init A1026 Failed\n"); 1320 } 1321 } 1322 1323 if (fd_a1026 >= 0) { 1324 close(fd_a1026); 1325 } 1326 fd_a1026 = -1; 1327 mA1026Lock.unlock(); 1328 1329 return rc; 1330 } 1331 1332 1333 status_t AudioHardware::doRouting() 1334 { 1335 Mutex::Autolock lock(mLock); 1336 uint32_t outputDevices = mOutput->devices(); 1337 status_t ret = NO_ERROR; 1338 AudioStreamInMSM72xx *input = getActiveInput_l(); 1339 uint32_t inputDevice = (input == NULL) ? 0 : input->devices(); 1340 int sndDevice = -1; 1341 1342 if (mMode == AudioSystem::MODE_IN_CALL && mTTYMode != TTY_MODE_OFF) { 1343 if ((outputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADSET) || 1344 (outputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADPHONE)) { 1345 switch (mTTYMode) { 1346 case TTY_MODE_FULL: 1347 sndDevice = SND_DEVICE_TTY_FULL; 1348 break; 1349 case TTY_MODE_VCO: 1350 sndDevice = SND_DEVICE_TTY_VCO; 1351 break; 1352 case TTY_MODE_HCO: 1353 sndDevice = SND_DEVICE_TTY_HCO; 1354 break; 1355 } 1356 } 1357 } 1358 1359 if (sndDevice == -1 && inputDevice != 0) { 1360 LOGI("do input routing device %x\n", inputDevice); 1361 if (inputDevice & AudioSystem::DEVICE_IN_BLUETOOTH_SCO_HEADSET) { 1362 LOGI("Routing audio to Bluetooth PCM\n"); 1363 sndDevice = SND_DEVICE_BT; 1364 } else if (inputDevice & AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_CARKIT) { 1365 LOGI("Routing audio to Bluetooth car kit\n"); 1366 sndDevice = SND_DEVICE_CARKIT; 1367 } else if (inputDevice & AudioSystem::DEVICE_IN_WIRED_HEADSET) { 1368 if ((outputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADSET) && 1369 (outputDevices & AudioSystem::DEVICE_OUT_SPEAKER)) { 1370 LOGI("Routing audio to Wired Headset and Speaker\n"); 1371 sndDevice = SND_DEVICE_HEADSET_AND_SPEAKER; 1372 } else { 1373 LOGI("Routing audio to Wired Headset\n"); 1374 sndDevice = SND_DEVICE_HEADSET; 1375 } 1376 } else if (inputDevice & AudioSystem::DEVICE_IN_BACK_MIC) { 1377 if (outputDevices & (AudioSystem:: DEVICE_OUT_WIRED_HEADSET) && 1378 (outputDevices & AudioSystem:: DEVICE_OUT_SPEAKER)) { 1379 LOGI("Routing audio to Wired Headset and Speaker with back mic\n"); 1380 sndDevice = SND_DEVICE_HEADSET_AND_SPEAKER_BACK_MIC; 1381 } else if (outputDevices & AudioSystem::DEVICE_OUT_SPEAKER) { 1382 LOGI("Routing audio to Speakerphone with back mic\n"); 1383 sndDevice = SND_DEVICE_SPEAKER_BACK_MIC; 1384 } else if (outputDevices == AudioSystem::DEVICE_OUT_EARPIECE) { 1385 LOGI("Routing audio to Handset with back mic\n"); 1386 sndDevice = SND_DEVICE_HANDSET_BACK_MIC; 1387 } else { 1388 LOGI("Routing audio to Headset with back mic\n"); 1389 sndDevice = SND_DEVICE_NO_MIC_HEADSET_BACK_MIC; 1390 } 1391 } else { 1392 if (outputDevices & AudioSystem::DEVICE_OUT_SPEAKER) { 1393 LOGI("Routing audio to Speakerphone\n"); 1394 sndDevice = SND_DEVICE_SPEAKER; 1395 } else if (outputDevices == AudioSystem::DEVICE_OUT_WIRED_HEADPHONE) { 1396 LOGI("Routing audio to Speakerphone\n"); 1397 sndDevice = SND_DEVICE_NO_MIC_HEADSET; 1398 } else { 1399 LOGI("Routing audio to Handset\n"); 1400 sndDevice = SND_DEVICE_HANDSET; 1401 } 1402 } 1403 } 1404 // if inputDevice == 0, restore output routing 1405 1406 if (sndDevice == -1) { 1407 if (outputDevices & (outputDevices - 1)) { 1408 if ((outputDevices & AudioSystem::DEVICE_OUT_SPEAKER) == 0) { 1409 LOGW("Hardware does not support requested route combination (%#X)," 1410 " picking closest possible route...", outputDevices); 1411 } 1412 } 1413 1414 if (outputDevices & 1415 (AudioSystem::DEVICE_OUT_BLUETOOTH_SCO | AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_HEADSET)) { 1416 LOGI("Routing audio to Bluetooth PCM\n"); 1417 sndDevice = SND_DEVICE_BT; 1418 } else if (outputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_CARKIT) { 1419 LOGI("Routing audio to Bluetooth PCM\n"); 1420 sndDevice = SND_DEVICE_CARKIT; 1421 } else if ((outputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADSET) && 1422 (outputDevices & AudioSystem::DEVICE_OUT_SPEAKER)) { 1423 LOGI("Routing audio to Wired Headset and Speaker\n"); 1424 sndDevice = SND_DEVICE_HEADSET_AND_SPEAKER; 1425 } else if (outputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADPHONE) { 1426 if (outputDevices & AudioSystem::DEVICE_OUT_SPEAKER) { 1427 LOGI("Routing audio to No microphone Wired Headset and Speaker (%d,%x)\n", mMode, outputDevices); 1428 sndDevice = SND_DEVICE_HEADSET_AND_SPEAKER; 1429 } else { 1430 LOGI("Routing audio to No microphone Wired Headset (%d,%x)\n", mMode, outputDevices); 1431 sndDevice = SND_DEVICE_NO_MIC_HEADSET; 1432 } 1433 } else if (outputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADSET) { 1434 LOGI("Routing audio to Wired Headset\n"); 1435 sndDevice = SND_DEVICE_HEADSET; 1436 } else if (outputDevices & AudioSystem::DEVICE_OUT_SPEAKER) { 1437 LOGI("Routing audio to Speakerphone\n"); 1438 sndDevice = SND_DEVICE_SPEAKER; 1439 } else { 1440 LOGI("Routing audio to Handset\n"); 1441 sndDevice = SND_DEVICE_HANDSET; 1442 } 1443 } 1444 1445 if ((vr_mode_change) || (sndDevice != -1 && sndDevice != mCurSndDevice)) { 1446 ret = doAudioRouteOrMute(sndDevice); 1447 mCurSndDevice = sndDevice; 1448 if (mMode == AudioSystem::MODE_IN_CALL) { 1449 if (mHACSetting && hac_enable && mCurSndDevice == (int) SND_DEVICE_HANDSET) { 1450 LOGD("HAC enable: Setting in-call volume to maximum.\n"); 1451 set_volume_rpc(VOICE_VOLUME_MAX); 1452 } else { 1453 set_volume_rpc(mVoiceVolume); 1454 } 1455 } 1456 } 1457 1458 return ret; 1459 } 1460 1461 status_t AudioHardware::checkMicMute() 1462 { 1463 Mutex::Autolock lock(mLock); 1464 if (mMode != AudioSystem::MODE_IN_CALL) { 1465 setMicMute_nosync(true); 1466 } 1467 1468 return NO_ERROR; 1469 } 1470 1471 status_t AudioHardware::dumpInternals(int fd, const Vector<String16>& args) 1472 { 1473 const size_t SIZE = 256; 1474 char buffer[SIZE]; 1475 String8 result; 1476 result.append("AudioHardware::dumpInternals\n"); 1477 snprintf(buffer, SIZE, "\tmInit: %s\n", mInit? "true": "false"); 1478 result.append(buffer); 1479 snprintf(buffer, SIZE, "\tmMicMute: %s\n", mMicMute? "true": "false"); 1480 result.append(buffer); 1481 snprintf(buffer, SIZE, "\tmBluetoothNrec: %s\n", mBluetoothNrec? "true": "false"); 1482 result.append(buffer); 1483 snprintf(buffer, SIZE, "\tmBluetoothIdtx: %d\n", mBluetoothIdTx); 1484 result.append(buffer); 1485 snprintf(buffer, SIZE, "\tmBluetoothIdrx: %d\n", mBluetoothIdRx); 1486 result.append(buffer); 1487 ::write(fd, result.string(), result.size()); 1488 return NO_ERROR; 1489 } 1490 1491 status_t AudioHardware::dump(int fd, const Vector<String16>& args) 1492 { 1493 dumpInternals(fd, args); 1494 for (size_t index = 0; index < mInputs.size(); index++) { 1495 mInputs[index]->dump(fd, args); 1496 } 1497 1498 if (mOutput) { 1499 mOutput->dump(fd, args); 1500 } 1501 return NO_ERROR; 1502 } 1503 1504 uint32_t AudioHardware::getInputSampleRate(uint32_t sampleRate) 1505 { 1506 uint32_t i; 1507 uint32_t prevDelta; 1508 uint32_t delta; 1509 1510 for (i = 0, prevDelta = 0xFFFFFFFF; i < sizeof(inputSamplingRates)/sizeof(uint32_t); i++, prevDelta = delta) { 1511 delta = abs(sampleRate - inputSamplingRates[i]); 1512 if (delta > prevDelta) break; 1513 } 1514 // i is always > 0 here 1515 return inputSamplingRates[i-1]; 1516 } 1517 1518 // getActiveInput_l() must be called with mLock held 1519 AudioHardware::AudioStreamInMSM72xx *AudioHardware::getActiveInput_l() 1520 { 1521 for (size_t i = 0; i < mInputs.size(); i++) { 1522 // return first input found not being in standby mode 1523 // as only one input can be in this state 1524 if (mInputs[i]->state() > AudioStreamInMSM72xx::AUDIO_INPUT_CLOSED) { 1525 return mInputs[i]; 1526 } 1527 } 1528 1529 return NULL; 1530 } 1531 // ---------------------------------------------------------------------------- 1532 1533 AudioHardware::AudioStreamOutMSM72xx::AudioStreamOutMSM72xx() : 1534 mHardware(0), mFd(-1), mStartCount(0), mRetryCount(0), mStandby(true), 1535 mDevices(0), mChannels(AUDIO_HW_OUT_CHANNELS), mSampleRate(AUDIO_HW_OUT_SAMPLERATE), 1536 mBufferSize(AUDIO_HW_OUT_BUFSZ) 1537 { 1538 } 1539 1540 status_t AudioHardware::AudioStreamOutMSM72xx::set( 1541 AudioHardware* hw, uint32_t devices, int *pFormat, uint32_t *pChannels, uint32_t *pRate) 1542 { 1543 int lFormat = pFormat ? *pFormat : 0; 1544 uint32_t lChannels = pChannels ? *pChannels : 0; 1545 uint32_t lRate = pRate ? *pRate : 0; 1546 1547 mHardware = hw; 1548 mDevices = devices; 1549 1550 // fix up defaults 1551 if (lFormat == 0) lFormat = format(); 1552 if (lChannels == 0) lChannels = channels(); 1553 if (lRate == 0) lRate = sampleRate(); 1554 1555 // check values 1556 if ((lFormat != format()) || 1557 (lChannels != channels()) || 1558 (lRate != sampleRate())) { 1559 if (pFormat) *pFormat = format(); 1560 if (pChannels) *pChannels = channels(); 1561 if (pRate) *pRate = sampleRate(); 1562 return BAD_VALUE; 1563 } 1564 1565 if (pFormat) *pFormat = lFormat; 1566 if (pChannels) *pChannels = lChannels; 1567 if (pRate) *pRate = lRate; 1568 1569 mChannels = lChannels; 1570 mSampleRate = lRate; 1571 mBufferSize = hw->getBufferSize(lRate, AudioSystem::popCount(lChannels)); 1572 1573 return NO_ERROR; 1574 } 1575 1576 AudioHardware::AudioStreamOutMSM72xx::~AudioStreamOutMSM72xx() 1577 { 1578 standby(); 1579 } 1580 1581 ssize_t AudioHardware::AudioStreamOutMSM72xx::write(const void* buffer, size_t bytes) 1582 { 1583 // LOGD("AudioStreamOutMSM72xx::write(%p, %u)", buffer, bytes); 1584 status_t status = NO_INIT; 1585 size_t count = bytes; 1586 const uint8_t* p = static_cast<const uint8_t*>(buffer); 1587 1588 if (mStandby) { 1589 1590 // open driver 1591 LOGV("open pcm_out driver"); 1592 status = ::open("/dev/msm_pcm_out", O_RDWR); 1593 if (status < 0) { 1594 if (errCount++ < 10) { 1595 LOGE("Cannot open /dev/msm_pcm_out errno: %d", errno); 1596 } 1597 goto Error; 1598 } 1599 mFd = status; 1600 1601 // configuration 1602 LOGV("get config"); 1603 struct msm_audio_config config; 1604 status = ioctl(mFd, AUDIO_GET_CONFIG, &config); 1605 if (status < 0) { 1606 LOGE("Cannot read pcm_out config"); 1607 goto Error; 1608 } 1609 1610 LOGV("set pcm_out config"); 1611 config.channel_count = AudioSystem::popCount(channels()); 1612 config.sample_rate = mSampleRate; 1613 config.buffer_size = mBufferSize; 1614 config.buffer_count = AUDIO_HW_NUM_OUT_BUF; 1615 config.codec_type = CODEC_TYPE_PCM; 1616 status = ioctl(mFd, AUDIO_SET_CONFIG, &config); 1617 if (status < 0) { 1618 LOGE("Cannot set config"); 1619 goto Error; 1620 } 1621 1622 LOGV("buffer_size: %u", config.buffer_size); 1623 LOGV("buffer_count: %u", config.buffer_count); 1624 LOGV("channel_count: %u", config.channel_count); 1625 LOGV("sample_rate: %u", config.sample_rate); 1626 1627 uint32_t acdb_id = mHardware->getACDB(MOD_PLAY, mHardware->get_snd_dev()); 1628 status = ioctl(mFd, AUDIO_START, &acdb_id); 1629 if (status < 0) { 1630 LOGE("Cannot start pcm playback"); 1631 goto Error; 1632 } 1633 1634 status = ioctl(mFd, AUDIO_SET_VOLUME, &stream_volume); 1635 if (status < 0) { 1636 LOGE("Cannot start pcm playback"); 1637 goto Error; 1638 } 1639 1640 LOGV("acquire output wakelock"); 1641 acquire_wake_lock(PARTIAL_WAKE_LOCK, kOutputWakelockStr); 1642 mStandby = false; 1643 } 1644 1645 while (count) { 1646 ssize_t written = ::write(mFd, p, count); 1647 if (written >= 0) { 1648 count -= written; 1649 p += written; 1650 } else { 1651 if (errno != EAGAIN) return written; 1652 mRetryCount++; 1653 LOGW("EAGAIN - retry"); 1654 } 1655 } 1656 1657 return bytes; 1658 1659 Error: 1660 if (mFd >= 0) { 1661 ::close(mFd); 1662 mFd = -1; 1663 } 1664 // Simulate audio output timing in case of error 1665 usleep(bytes * 1000000 / frameSize() / sampleRate()); 1666 1667 return status; 1668 } 1669 1670 status_t AudioHardware::AudioStreamOutMSM72xx::standby() 1671 { 1672 status_t status = NO_ERROR; 1673 if (!mStandby && mFd >= 0) { 1674 ::close(mFd); 1675 mFd = -1; 1676 LOGV("release output wakelock"); 1677 release_wake_lock(kOutputWakelockStr); 1678 } 1679 mStandby = true; 1680 LOGI("AudioHardware pcm playback is going to standby."); 1681 return status; 1682 } 1683 1684 status_t AudioHardware::AudioStreamOutMSM72xx::dump(int fd, const Vector<String16>& args) 1685 { 1686 const size_t SIZE = 256; 1687 char buffer[SIZE]; 1688 String8 result; 1689 result.append("AudioStreamOutMSM72xx::dump\n"); 1690 snprintf(buffer, SIZE, "\tsample rate: %d\n", sampleRate()); 1691 result.append(buffer); 1692 snprintf(buffer, SIZE, "\tbuffer size: %d\n", bufferSize()); 1693 result.append(buffer); 1694 snprintf(buffer, SIZE, "\tchannels: %d\n", channels()); 1695 result.append(buffer); 1696 snprintf(buffer, SIZE, "\tformat: %d\n", format()); 1697 result.append(buffer); 1698 snprintf(buffer, SIZE, "\tmHardware: %p\n", mHardware); 1699 result.append(buffer); 1700 snprintf(buffer, SIZE, "\tmFd: %d\n", mFd); 1701 result.append(buffer); 1702 snprintf(buffer, SIZE, "\tmStartCount: %d\n", mStartCount); 1703 result.append(buffer); 1704 snprintf(buffer, SIZE, "\tmRetryCount: %d\n", mRetryCount); 1705 result.append(buffer); 1706 snprintf(buffer, SIZE, "\tmStandby: %s\n", mStandby? "true": "false"); 1707 result.append(buffer); 1708 ::write(fd, result.string(), result.size()); 1709 return NO_ERROR; 1710 } 1711 1712 bool AudioHardware::AudioStreamOutMSM72xx::checkStandby() 1713 { 1714 return mStandby; 1715 } 1716 1717 1718 status_t AudioHardware::AudioStreamOutMSM72xx::setParameters(const String8& keyValuePairs) 1719 { 1720 AudioParameter param = AudioParameter(keyValuePairs); 1721 String8 key = String8(AudioParameter::keyRouting); 1722 status_t status = NO_ERROR; 1723 int device; 1724 LOGV("AudioStreamOutMSM72xx::setParameters() %s", keyValuePairs.string()); 1725 1726 if (param.getInt(key, device) == NO_ERROR) { 1727 mDevices = device; 1728 LOGV("set output routing %x", mDevices); 1729 status = mHardware->doRouting(); 1730 param.remove(key); 1731 } 1732 1733 if (param.size()) { 1734 status = BAD_VALUE; 1735 } 1736 return status; 1737 } 1738 1739 String8 AudioHardware::AudioStreamOutMSM72xx::getParameters(const String8& keys) 1740 { 1741 AudioParameter param = AudioParameter(keys); 1742 String8 value; 1743 String8 key = String8(AudioParameter::keyRouting); 1744 1745 if (param.get(key, value) == NO_ERROR) { 1746 LOGV("get routing %x", mDevices); 1747 param.addInt(key, (int)mDevices); 1748 } 1749 1750 LOGV("AudioStreamOutMSM72xx::getParameters() %s", param.toString().string()); 1751 return param.toString(); 1752 } 1753 1754 status_t AudioHardware::AudioStreamOutMSM72xx::getRenderPosition(uint32_t *dspFrames) 1755 { 1756 //TODO: enable when supported by driver 1757 return INVALID_OPERATION; 1758 } 1759 1760 // ---------------------------------------------------------------------------- 1761 1762 AudioHardware::AudioStreamInMSM72xx::AudioStreamInMSM72xx() : 1763 mHardware(0), mFd(-1), mState(AUDIO_INPUT_CLOSED), mRetryCount(0), 1764 mFormat(AUDIO_HW_IN_FORMAT), mChannels(AUDIO_HW_IN_CHANNELS), 1765 mSampleRate(AUDIO_HW_IN_SAMPLERATE), mBufferSize(AUDIO_HW_IN_BUFSZ), 1766 mAcoustics((AudioSystem::audio_in_acoustics)0), mDevices(0) 1767 { 1768 } 1769 1770 status_t AudioHardware::AudioStreamInMSM72xx::set( 1771 AudioHardware* hw, uint32_t devices, int *pFormat, uint32_t *pChannels, uint32_t *pRate, 1772 AudioSystem::audio_in_acoustics acoustic_flags) 1773 { 1774 if (pFormat == 0 || *pFormat != AUDIO_HW_IN_FORMAT) { 1775 *pFormat = AUDIO_HW_IN_FORMAT; 1776 return BAD_VALUE; 1777 } 1778 if (pRate == 0) { 1779 return BAD_VALUE; 1780 } 1781 uint32_t rate = hw->getInputSampleRate(*pRate); 1782 if (rate != *pRate) { 1783 *pRate = rate; 1784 return BAD_VALUE; 1785 } 1786 1787 if (pChannels == 0 || (*pChannels != AudioSystem::CHANNEL_IN_MONO && 1788 *pChannels != AudioSystem::CHANNEL_IN_STEREO)) { 1789 *pChannels = AUDIO_HW_IN_CHANNELS; 1790 return BAD_VALUE; 1791 } 1792 1793 mHardware = hw; 1794 1795 LOGV("AudioStreamInMSM72xx::set(%d, %d, %u)", *pFormat, *pChannels, *pRate); 1796 if (mFd >= 0) { 1797 LOGE("Audio record already open"); 1798 return -EPERM; 1799 } 1800 1801 // open audio input device 1802 status_t status = ::open("/dev/msm_pcm_in", O_RDWR); 1803 if (status < 0) { 1804 LOGE("Cannot open /dev/msm_pcm_in errno: %d", errno); 1805 goto Error; 1806 } 1807 mFd = status; 1808 mBufferSize = hw->getBufferSize(*pRate, AudioSystem::popCount(*pChannels)); 1809 1810 // configuration 1811 LOGV("get config"); 1812 struct msm_audio_config config; 1813 status = ioctl(mFd, AUDIO_GET_CONFIG, &config); 1814 if (status < 0) { 1815 LOGE("Cannot read config"); 1816 goto Error; 1817 } 1818 1819 LOGV("set config"); 1820 config.channel_count = AudioSystem::popCount(*pChannels); 1821 config.sample_rate = *pRate; 1822 config.buffer_size = mBufferSize; 1823 config.buffer_count = 2; 1824 config.codec_type = CODEC_TYPE_PCM; 1825 status = ioctl(mFd, AUDIO_SET_CONFIG, &config); 1826 if (status < 0) { 1827 LOGE("Cannot set config"); 1828 if (ioctl(mFd, AUDIO_GET_CONFIG, &config) == 0) { 1829 if (config.channel_count == 1) { 1830 *pChannels = AudioSystem::CHANNEL_IN_MONO; 1831 } else { 1832 *pChannels = AudioSystem::CHANNEL_IN_STEREO; 1833 } 1834 *pRate = config.sample_rate; 1835 } 1836 goto Error; 1837 } 1838 1839 LOGV("confirm config"); 1840 status = ioctl(mFd, AUDIO_GET_CONFIG, &config); 1841 if (status < 0) { 1842 LOGE("Cannot read config"); 1843 goto Error; 1844 } 1845 LOGV("buffer_size: %u", config.buffer_size); 1846 LOGV("buffer_count: %u", config.buffer_count); 1847 LOGV("channel_count: %u", config.channel_count); 1848 LOGV("sample_rate: %u", config.sample_rate); 1849 1850 mDevices = devices; 1851 mFormat = AUDIO_HW_IN_FORMAT; 1852 mChannels = *pChannels; 1853 mSampleRate = config.sample_rate; 1854 mBufferSize = config.buffer_size; 1855 1856 //mHardware->setMicMute_nosync(false); 1857 mState = AUDIO_INPUT_OPENED; 1858 1859 return NO_ERROR; 1860 1861 Error: 1862 if (mFd >= 0) { 1863 ::close(mFd); 1864 mFd = -1; 1865 } 1866 return status; 1867 } 1868 1869 AudioHardware::AudioStreamInMSM72xx::~AudioStreamInMSM72xx() 1870 { 1871 LOGV("AudioStreamInMSM72xx destructor"); 1872 standby(); 1873 } 1874 1875 ssize_t AudioHardware::AudioStreamInMSM72xx::read( void* buffer, ssize_t bytes) 1876 { 1877 LOGV("AudioStreamInMSM72xx::read(%p, %ld)", buffer, bytes); 1878 if (!mHardware) return -1; 1879 1880 size_t count = bytes; 1881 uint8_t* p = static_cast<uint8_t*>(buffer); 1882 1883 if (mState < AUDIO_INPUT_OPENED) { 1884 Mutex::Autolock lock(mHardware->mLock); 1885 if (set(mHardware, mDevices, &mFormat, &mChannels, &mSampleRate, mAcoustics) != NO_ERROR) { 1886 return -1; 1887 } 1888 } 1889 1890 if (mState < AUDIO_INPUT_STARTED) { 1891 mState = AUDIO_INPUT_STARTED; 1892 mHardware->set_mRecordState(1); 1893 // make sure a1026 config is re-applied even is input device is not changed 1894 mHardware->clearCurDevice(); 1895 mHardware->doRouting(); 1896 1897 LOGV("acquire input wakelock"); 1898 acquire_wake_lock(PARTIAL_WAKE_LOCK, kInputWakelockStr); 1899 uint32_t acdb_id = mHardware->getACDB(MOD_REC, mHardware->get_snd_dev()); 1900 if (ioctl(mFd, AUDIO_START, &acdb_id)) { 1901 LOGE("Error starting record"); 1902 standby(); 1903 return -1; 1904 } 1905 } 1906 1907 while (count) { 1908 ssize_t bytesRead = ::read(mFd, buffer, count); 1909 if (bytesRead >= 0) { 1910 count -= bytesRead; 1911 p += bytesRead; 1912 } else { 1913 if (errno != EAGAIN) return bytesRead; 1914 mRetryCount++; 1915 LOGW("EAGAIN - retrying"); 1916 } 1917 } 1918 return bytes; 1919 } 1920 1921 status_t AudioHardware::AudioStreamInMSM72xx::standby() 1922 { 1923 if (mState > AUDIO_INPUT_CLOSED) { 1924 if (mFd >= 0) { 1925 ::close(mFd); 1926 mFd = -1; 1927 } 1928 mState = AUDIO_INPUT_CLOSED; 1929 LOGV("release input wakelock"); 1930 release_wake_lock(kInputWakelockStr); 1931 } 1932 1933 if (!mHardware) return -1; 1934 1935 mHardware->set_mRecordState(0); 1936 // make sure a1026 config is re-applied even is input device is not changed 1937 mHardware->clearCurDevice(); 1938 mHardware->doRouting(); 1939 1940 LOGI("AudioHardware PCM record is going to standby."); 1941 return NO_ERROR; 1942 } 1943 1944 status_t AudioHardware::AudioStreamInMSM72xx::dump(int fd, const Vector<String16>& args) 1945 { 1946 const size_t SIZE = 256; 1947 char buffer[SIZE]; 1948 String8 result; 1949 result.append("AudioStreamInMSM72xx::dump\n"); 1950 snprintf(buffer, SIZE, "\tsample rate: %d\n", sampleRate()); 1951 result.append(buffer); 1952 snprintf(buffer, SIZE, "\tbuffer size: %d\n", bufferSize()); 1953 result.append(buffer); 1954 snprintf(buffer, SIZE, "\tchannels: %d\n", channels()); 1955 result.append(buffer); 1956 snprintf(buffer, SIZE, "\tformat: %d\n", format()); 1957 result.append(buffer); 1958 snprintf(buffer, SIZE, "\tmHardware: %p\n", mHardware); 1959 result.append(buffer); 1960 snprintf(buffer, SIZE, "\tmFd count: %d\n", mFd); 1961 result.append(buffer); 1962 snprintf(buffer, SIZE, "\tmState: %d\n", mState); 1963 result.append(buffer); 1964 snprintf(buffer, SIZE, "\tmRetryCount: %d\n", mRetryCount); 1965 result.append(buffer); 1966 ::write(fd, result.string(), result.size()); 1967 return NO_ERROR; 1968 } 1969 1970 status_t AudioHardware::AudioStreamInMSM72xx::setParameters(const String8& keyValuePairs) 1971 { 1972 AudioParameter param = AudioParameter(keyValuePairs); 1973 status_t status = NO_ERROR; 1974 int device; 1975 String8 key = String8(KEY_A1026_VR_MODE); 1976 int enabled; 1977 LOGV("AudioStreamInMSM72xx::setParameters() %s", keyValuePairs.string()); 1978 1979 // reading voice recognition mode parameter 1980 if (param.getInt(key, enabled) == NO_ERROR) { 1981 LOGV("set vr_mode_enabled to %d", enabled); 1982 vr_mode_change = (vr_mode_enabled != enabled); 1983 vr_mode_enabled = enabled; 1984 param.remove(key); 1985 } 1986 1987 // reading routing parameter 1988 key = String8(AudioParameter::keyRouting); 1989 if (param.getInt(key, device) == NO_ERROR) { 1990 LOGV("set input routing %x", device); 1991 if (device & (device - 1)) { 1992 status = BAD_VALUE; 1993 } else { 1994 mDevices = device; 1995 status = mHardware->doRouting(); 1996 } 1997 param.remove(key); 1998 } 1999 2000 if (param.size()) { 2001 status = BAD_VALUE; 2002 } 2003 return status; 2004 } 2005 2006 String8 AudioHardware::AudioStreamInMSM72xx::getParameters(const String8& keys) 2007 { 2008 AudioParameter param = AudioParameter(keys); 2009 String8 value; 2010 String8 key = String8(AudioParameter::keyRouting); 2011 2012 if (param.get(key, value) == NO_ERROR) { 2013 LOGV("get routing %x", mDevices); 2014 param.addInt(key, (int)mDevices); 2015 } 2016 2017 LOGV("AudioStreamInMSM72xx::getParameters() %s", param.toString().string()); 2018 return param.toString(); 2019 } 2020 2021 // ---------------------------------------------------------------------------- 2022 2023 extern "C" AudioHardwareInterface* createAudioHardware(void) { 2024 return new AudioHardware(); 2025 } 2026 2027 }; // namespace android 2028