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