1/* AudioHardwareALSA.h 2 ** 3 ** Copyright 2008-2010, Wind River Systems 4 ** Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved. 5 ** 6 ** Licensed under the Apache License, Version 2.0 (the "License"); 7 ** you may not use this file except in compliance with the License. 8 ** You may obtain a copy of the License at 9 ** 10 ** http://www.apache.org/licenses/LICENSE-2.0 11 ** 12 ** Unless required by applicable law or agreed to in writing, software 13 ** distributed under the License is distributed on an "AS IS" BASIS, 14 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 ** See the License for the specific language governing permissions and 16 ** limitations under the License. 17 */ 18 19#ifndef ANDROID_AUDIO_HARDWARE_ALSA_H 20#define ANDROID_AUDIO_HARDWARE_ALSA_H 21 22#define QCOM_CSDCLIENT_ENABLED 1 23 24#include <utils/List.h> 25#include <hardware_legacy/AudioHardwareBase.h> 26 27#include <hardware_legacy/AudioHardwareInterface.h> 28#include <hardware_legacy/AudioSystemLegacy.h> 29#include <system/audio.h> 30#include <hardware/audio.h> 31#include <utils/threads.h> 32#include <dlfcn.h> 33 34#ifdef QCOM_USBAUDIO_ENABLED 35#include <AudioUsbALSA.h> 36#endif 37 38extern "C" { 39 #include <sound/asound.h> 40 #include "alsa_audio.h" 41 #include "msm8960_use_cases.h" 42} 43 44#include <hardware/hardware.h> 45 46namespace android_audio_legacy 47{ 48using android::List; 49using android::Mutex; 50class AudioHardwareALSA; 51 52/** 53 * The id of ALSA module 54 */ 55#define ALSA_HARDWARE_MODULE_ID "alsa" 56#define ALSA_HARDWARE_NAME "alsa" 57 58#define DEFAULT_SAMPLING_RATE 48000 59#define DEFAULT_CHANNEL_MODE 2 60#define VOICE_SAMPLING_RATE 8000 61#define VOICE_CHANNEL_MODE 1 62#define PLAYBACK_LATENCY 170000 63#define RECORD_LATENCY 96000 64#define VOICE_LATENCY 85333 65#define DEFAULT_BUFFER_SIZE 4096 66//4032 = 336(kernel buffer size) * 2(bytes pcm_16) * 6(number of channels) 67#define DEFAULT_MULTI_CHANNEL_BUF_SIZE 4032 68#define DEFAULT_VOICE_BUFFER_SIZE 2048 69#define PLAYBACK_LOW_LATENCY_BUFFER_SIZE 1024 70#define PLAYBACK_LOW_LATENCY 22000 71#define PLAYBACK_LOW_LATENCY_MEASURED 42000 72#define DEFAULT_IN_BUFFER_SIZE 320 73#define MIN_CAPTURE_BUFFER_SIZE_PER_CH 320 74#define MAX_CAPTURE_BUFFER_SIZE_PER_CH 2048 75#define FM_BUFFER_SIZE 1024 76 77#define VOIP_SAMPLING_RATE_8K 8000 78#define VOIP_SAMPLING_RATE_16K 16000 79#define VOIP_DEFAULT_CHANNEL_MODE 1 80#define VOIP_BUFFER_SIZE_8K 320 81#define VOIP_BUFFER_SIZE_16K 640 82#define VOIP_BUFFER_MAX_SIZE VOIP_BUFFER_SIZE_16K 83#define VOIP_PLAYBACK_LATENCY 6400 84#define VOIP_RECORD_LATENCY 6400 85 86#define MODE_IS127 0x2 87#define MODE_4GV_NB 0x3 88#define MODE_4GV_WB 0x4 89#define MODE_AMR 0x5 90#define MODE_AMR_WB 0xD 91#define MODE_PCM 0xC 92 93#define DUALMIC_KEY "dualmic_enabled" 94#define FLUENCE_KEY "fluence" 95#define ANC_KEY "anc_enabled" 96#define TTY_MODE_KEY "tty_mode" 97#define BT_SAMPLERATE_KEY "bt_samplerate" 98#define BTHEADSET_VGS "bt_headset_vgs" 99#define WIDEVOICE_KEY "wide_voice_enable" 100#define VOIPRATE_KEY "voip_rate" 101#define FENS_KEY "fens_enable" 102#define ST_KEY "st_enable" 103#define INCALLMUSIC_KEY "incall_music_enabled" 104 105#define ANC_FLAG 0x00000001 106#define DMIC_FLAG 0x00000002 107#define QMIC_FLAG 0x00000004 108#ifdef QCOM_SSR_ENABLED 109#define SSRQMIC_FLAG 0x00000008 110#endif 111 112#define TTY_OFF 0x00000010 113#define TTY_FULL 0x00000020 114#define TTY_VCO 0x00000040 115#define TTY_HCO 0x00000080 116#define TTY_CLEAR 0xFFFFFF0F 117 118#define LPA_SESSION_ID 1 119#define TUNNEL_SESSION_ID 2 120#ifdef QCOM_USBAUDIO_ENABLED 121static int USBPLAYBACKBIT_MUSIC = (1 << 0); 122static int USBPLAYBACKBIT_VOICECALL = (1 << 1); 123static int USBPLAYBACKBIT_VOIPCALL = (1 << 2); 124static int USBPLAYBACKBIT_FM = (1 << 3); 125static int USBPLAYBACKBIT_LPA = (1 << 4); 126 127static int USBRECBIT_REC = (1 << 0); 128static int USBRECBIT_VOICECALL = (1 << 1); 129static int USBRECBIT_VOIPCALL = (1 << 2); 130static int USBRECBIT_FM = (1 << 3); 131#endif 132 133#define DEVICE_SPEAKER_HEADSET "Speaker Headset" 134#define DEVICE_HEADSET "Headset" 135#define DEVICE_HEADPHONES "Headphones" 136 137#ifdef QCOM_SSR_ENABLED 138#define COEFF_ARRAY_SIZE 4 139#define FILT_SIZE ((512+1)* 6) /* # ((FFT bins)/2+1)*numOutputs */ 140#define SSR_FRAME_SIZE 512 141#define SSR_INPUT_FRAME_SIZE (SSR_FRAME_SIZE * 4) 142#define SSR_OUTPUT_FRAME_SIZE (SSR_FRAME_SIZE * 6) 143#endif 144 145#define MODE_CALL_KEY "CALL_KEY" 146 147struct alsa_device_t; 148static uint32_t FLUENCE_MODE_ENDFIRE = 0; 149static uint32_t FLUENCE_MODE_BROADSIDE = 1; 150 151enum { 152 INCALL_REC_MONO, 153 INCALL_REC_STEREO, 154}; 155 156enum audio_call_mode { 157 CS_INACTIVE = 0x0, 158 CS_ACTIVE = 0x1, 159 CS_HOLD = 0x2, 160 IMS_INACTIVE = 0x0, 161 IMS_ACTIVE = 0x10, 162 IMS_HOLD = 0x20 163}; 164 165 166struct alsa_handle_t { 167 alsa_device_t * module; 168 uint32_t devices; 169 char useCase[MAX_STR_LEN]; 170 struct pcm * handle; 171 snd_pcm_format_t format; 172 uint32_t channels; 173 uint32_t sampleRate; 174 unsigned int latency; // Delay in usec 175 unsigned int bufferSize; // Size of sample buffer 176 unsigned int periodSize; 177 bool isDeepbufferOutput; 178 struct pcm * rxHandle; 179 snd_use_case_mgr_t *ucMgr; 180}; 181 182typedef List < alsa_handle_t > ALSAHandleList; 183 184struct use_case_t { 185 char useCase[MAX_STR_LEN]; 186}; 187 188typedef List < use_case_t > ALSAUseCaseList; 189 190struct alsa_device_t { 191 hw_device_t common; 192 193 status_t (*init)(alsa_device_t *, ALSAHandleList &); 194 status_t (*open)(alsa_handle_t *); 195 status_t (*close)(alsa_handle_t *); 196 status_t (*standby)(alsa_handle_t *); 197 status_t (*route)(alsa_handle_t *, uint32_t, int); 198 status_t (*startVoiceCall)(alsa_handle_t *); 199 status_t (*startVoipCall)(alsa_handle_t *); 200 status_t (*startFm)(alsa_handle_t *); 201 void (*setVoiceVolume)(int); 202 void (*setVoipVolume)(int); 203 void (*setMicMute)(int); 204 void (*setVoipMicMute)(int); 205 void (*setVoipConfig)(int, int); 206 status_t (*setFmVolume)(int); 207 void (*setBtscoRate)(int); 208 status_t (*setLpaVolume)(int); 209 void (*enableWideVoice)(bool); 210 void (*enableFENS)(bool); 211 void (*setFlags)(uint32_t); 212 status_t (*setCompressedVolume)(int); 213 void (*enableSlowTalk)(bool); 214 void (*setVocRecMode)(uint8_t); 215 void (*setVoLTEMicMute)(int); 216 void (*setVoLTEVolume)(int); 217#ifdef SEPERATED_AUDIO_INPUT 218 void (*setInput)(int); 219#endif 220#ifdef QCOM_CSDCLIENT_ENABLED 221 void (*setCsdHandle)(void*); 222#endif 223}; 224 225// ---------------------------------------------------------------------------- 226 227class ALSAMixer 228{ 229public: 230 ALSAMixer(); 231 virtual ~ALSAMixer(); 232 233 bool isValid() { return 1;} 234 status_t setMasterVolume(float volume); 235 status_t setMasterGain(float gain); 236 237 status_t setVolume(uint32_t device, float left, float right); 238 status_t setGain(uint32_t device, float gain); 239 240 status_t setCaptureMuteState(uint32_t device, bool state); 241 status_t getCaptureMuteState(uint32_t device, bool *state); 242 status_t setPlaybackMuteState(uint32_t device, bool state); 243 status_t getPlaybackMuteState(uint32_t device, bool *state); 244 245}; 246 247class ALSAControl 248{ 249public: 250 ALSAControl(const char *device = "/dev/snd/controlC0"); 251 virtual ~ALSAControl(); 252 253 status_t get(const char *name, unsigned int &value, int index = 0); 254 status_t set(const char *name, unsigned int value, int index = -1); 255 status_t set(const char *name, const char *); 256 status_t setext(const char *name, int count, char **setValues); 257 258private: 259 struct mixer* mHandle; 260}; 261 262class ALSAStreamOps 263{ 264public: 265 ALSAStreamOps(AudioHardwareALSA *parent, alsa_handle_t *handle); 266 virtual ~ALSAStreamOps(); 267 268 status_t set(int *format, uint32_t *channels, uint32_t *rate, uint32_t device); 269 270 status_t setParameters(const String8& keyValuePairs); 271 String8 getParameters(const String8& keys); 272 273 uint32_t sampleRate() const; 274 size_t bufferSize() const; 275 int format() const; 276 uint32_t channels() const; 277 278 status_t open(int mode); 279 void close(); 280 281protected: 282 friend class AudioHardwareALSA; 283 284 AudioHardwareALSA * mParent; 285 alsa_handle_t * mHandle; 286 uint32_t mDevices; 287}; 288 289// ---------------------------------------------------------------------------- 290 291class AudioStreamOutALSA : public AudioStreamOut, public ALSAStreamOps 292{ 293public: 294 AudioStreamOutALSA(AudioHardwareALSA *parent, alsa_handle_t *handle); 295 virtual ~AudioStreamOutALSA(); 296 297 virtual uint32_t sampleRate() const 298 { 299 return ALSAStreamOps::sampleRate(); 300 } 301 302 virtual size_t bufferSize() const 303 { 304 return ALSAStreamOps::bufferSize(); 305 } 306 307 virtual uint32_t channels() const; 308 309 virtual int format() const 310 { 311 return ALSAStreamOps::format(); 312 } 313 314 virtual uint32_t latency() const; 315 316 virtual ssize_t write(const void *buffer, size_t bytes); 317 virtual status_t dump(int fd, const Vector<String16>& args); 318 319 status_t setVolume(float left, float right); 320 321 virtual status_t standby(); 322 323 virtual status_t setParameters(const String8& keyValuePairs) { 324 return ALSAStreamOps::setParameters(keyValuePairs); 325 } 326 327 virtual String8 getParameters(const String8& keys) { 328 return ALSAStreamOps::getParameters(keys); 329 } 330 331 // return the number of audio frames written by the audio dsp to DAC since 332 // the output has exited standby 333 virtual status_t getRenderPosition(uint32_t *dspFrames); 334 335 status_t open(int mode); 336 status_t close(); 337 338private: 339 uint32_t mFrameCount; 340 341protected: 342 AudioHardwareALSA * mParent; 343}; 344 345class AudioStreamInALSA : public AudioStreamIn, public ALSAStreamOps 346{ 347public: 348 AudioStreamInALSA(AudioHardwareALSA *parent, 349 alsa_handle_t *handle, 350 AudioSystem::audio_in_acoustics audio_acoustics); 351 virtual ~AudioStreamInALSA(); 352 353 virtual uint32_t sampleRate() const 354 { 355 return ALSAStreamOps::sampleRate(); 356 } 357 358 virtual size_t bufferSize() const 359 { 360 return ALSAStreamOps::bufferSize(); 361 } 362 363 virtual uint32_t channels() const 364 { 365 return ALSAStreamOps::channels(); 366 } 367 368 virtual int format() const 369 { 370 return ALSAStreamOps::format(); 371 } 372 373 virtual ssize_t read(void* buffer, ssize_t bytes); 374 virtual status_t dump(int fd, const Vector<String16>& args); 375 376 virtual status_t setGain(float gain); 377 378 virtual status_t standby(); 379 380 virtual status_t setParameters(const String8& keyValuePairs) 381 { 382 return ALSAStreamOps::setParameters(keyValuePairs); 383 } 384 385 virtual String8 getParameters(const String8& keys) 386 { 387 return ALSAStreamOps::getParameters(keys); 388 } 389 390 // Return the amount of input frames lost in the audio driver since the last call of this function. 391 // Audio driver is expected to reset the value to 0 and restart counting upon returning the current value by this function call. 392 // Such loss typically occurs when the user space process is blocked longer than the capacity of audio driver buffers. 393 // Unit: the number of input audio frames 394 virtual unsigned int getInputFramesLost() const; 395 396 virtual status_t addAudioEffect(effect_handle_t effect) 397 { 398 return BAD_VALUE; 399 } 400 401 virtual status_t removeAudioEffect(effect_handle_t effect) 402 { 403 return BAD_VALUE; 404 } 405 status_t setAcousticParams(void* params); 406 407 status_t open(int mode); 408 status_t close(); 409#ifdef QCOM_SSR_ENABLED 410 // Helper function to initialize the Surround Sound library. 411 status_t initSurroundSoundLibrary(unsigned long buffersize); 412#endif 413 414private: 415 void resetFramesLost(); 416 417#ifdef QCOM_CSDCLIENT_ENABLED 418 int start_csd_record(int); 419 int stop_csd_record(void); 420#endif 421 422 unsigned int mFramesLost; 423 AudioSystem::audio_in_acoustics mAcoustics; 424 425#ifdef QCOM_SSR_ENABLED 426 // Function to read coefficients from files. 427 status_t readCoeffsFromFile(); 428 429 FILE *mFp_4ch; 430 FILE *mFp_6ch; 431 int16_t **mRealCoeffs; 432 int16_t **mImagCoeffs; 433 void *mSurroundObj; 434 435 int16_t *mSurroundInputBuffer; 436 int16_t *mSurroundOutputBuffer; 437 int mSurroundInputBufferIdx; 438 int mSurroundOutputBufferIdx; 439#endif 440 441protected: 442 AudioHardwareALSA * mParent; 443}; 444 445class AudioHardwareALSA : public AudioHardwareBase 446{ 447public: 448 AudioHardwareALSA(); 449 virtual ~AudioHardwareALSA(); 450 451 /** 452 * check to see if the audio hardware interface has been initialized. 453 * return status based on values defined in include/utils/Errors.h 454 */ 455 virtual status_t initCheck(); 456 457 /** set the audio volume of a voice call. Range is between 0.0 and 1.0 */ 458 virtual status_t setVoiceVolume(float volume); 459 460 /** 461 * set the audio volume for all audio activities other than voice call. 462 * Range between 0.0 and 1.0. If any value other than NO_ERROR is returned, 463 * the software mixer will emulate this capability. 464 */ 465 virtual status_t setMasterVolume(float volume); 466#ifdef QCOM_FM_ENABLED 467 virtual status_t setFmVolume(float volume); 468#endif 469 /** 470 * setMode is called when the audio mode changes. NORMAL mode is for 471 * standard audio playback, RINGTONE when a ringtone is playing, and IN_CALL 472 * when a call is in progress. 473 */ 474 virtual status_t setMode(int mode); 475 476 // mic mute 477 virtual status_t setMicMute(bool state); 478 virtual status_t getMicMute(bool* state); 479 480 // set/get global audio parameters 481 virtual status_t setParameters(const String8& keyValuePairs); 482 virtual String8 getParameters(const String8& keys); 483 484 // Returns audio input buffer size according to parameters passed or 0 if one of the 485 // parameters is not supported 486 virtual size_t getInputBufferSize(uint32_t sampleRate, int format, int channels); 487 488#ifdef QCOM_TUNNEL_LPA_ENABLED 489 /** This method creates and opens the audio hardware output 490 * session for LPA */ 491 virtual AudioStreamOut* openOutputSession( 492 uint32_t devices, 493 int *format, 494 status_t *status, 495 int sessionId, 496 uint32_t samplingRate=0, 497 uint32_t channels=0); 498 virtual void closeOutputSession(AudioStreamOut* out); 499#endif 500 501 /** This method creates and opens the audio hardware output stream */ 502 virtual AudioStreamOut* openOutputStream( 503 uint32_t devices, 504 int *format=0, 505 uint32_t *channels=0, 506 uint32_t *sampleRate=0, 507 status_t *status=0); 508 virtual void closeOutputStream(AudioStreamOut* out); 509 510 /** This method creates and opens the audio hardware input stream */ 511 virtual AudioStreamIn* openInputStream( 512 uint32_t devices, 513 int *format, 514 uint32_t *channels, 515 uint32_t *sampleRate, 516 status_t *status, 517 AudioSystem::audio_in_acoustics acoustics); 518 virtual void closeInputStream(AudioStreamIn* in); 519 520 /**This method dumps the state of the audio hardware */ 521 //virtual status_t dumpState(int fd, const Vector<String16>& args); 522 523 static AudioHardwareInterface* create(); 524 525 int mode() 526 { 527 return mMode; 528 } 529 530protected: 531 virtual status_t dump(int fd, const Vector<String16>& args); 532 virtual uint32_t getVoipMode(int format); 533 void doRouting(int device); 534#ifdef QCOM_FM_ENABLED 535 void handleFm(int device); 536#endif 537#ifdef QCOM_USBAUDIO_ENABLED 538 void closeUSBPlayback(); 539 void closeUSBRecording(); 540 void closeUsbRecordingIfNothingActive(); 541 void closeUsbPlaybackIfNothingActive(); 542 void startUsbPlaybackIfNotStarted(); 543 void startUsbRecordingIfNotStarted(); 544#endif 545 546 void disableVoiceCall(char* verb, char* modifier, int mode, int device); 547 void enableVoiceCall(char* verb, char* modifier, int mode, int device); 548 bool routeVoiceCall(int device, int newMode); 549 bool routeVoLTECall(int device, int newMode); 550 friend class AudioStreamOutALSA; 551 friend class AudioStreamInALSA; 552 friend class ALSAStreamOps; 553 554 alsa_device_t * mALSADevice; 555 556 ALSAHandleList mDeviceList; 557 558#ifdef QCOM_USBAUDIO_ENABLED 559 AudioUsbALSA *mAudioUsbALSA; 560#endif 561 562 Mutex mLock; 563 564 snd_use_case_mgr_t *mUcMgr; 565 566 uint32_t mCurDevice; 567 /* The flag holds all the audio related device settings from 568 * Settings and Qualcomm Settings applications */ 569 uint32_t mDevSettingsFlag; 570 uint32_t mVoipStreamCount; 571 uint32_t mVoipBitRate; 572 uint32_t mIncallMode; 573 574 bool mMicMute; 575 int mCSCallActive; 576 int mVolteCallActive; 577 int mCallState; 578 int mIsFmActive; 579 bool mBluetoothVGS; 580 bool mFusion3Platform; 581#ifdef QCOM_USBAUDIO_ENABLED 582 int musbPlaybackState; 583 int musbRecordingState; 584#endif 585 void *mAcdbHandle; 586 void *mCsdHandle; 587}; 588 589// ---------------------------------------------------------------------------- 590 591}; // namespace android_audio_legacy 592#endif // ANDROID_AUDIO_HARDWARE_ALSA_H 593