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