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#ifndef ANDROID_AUDIO_HARDWARE_H
18#define ANDROID_AUDIO_HARDWARE_H
19
20#include <stdint.h>
21#include <sys/types.h>
22
23#include <utils/threads.h>
24#include <utils/SortedVector.h>
25
26#include <hardware_legacy/AudioHardwareBase.h>
27
28extern "C" {
29#include <linux/msm_audio.h>
30}
31
32namespace android {
33
34// ----------------------------------------------------------------------------
35// Kernel driver interface
36//
37
38#define SAMP_RATE_INDX_8000	0
39#define SAMP_RATE_INDX_11025	1
40#define SAMP_RATE_INDX_12000	2
41#define SAMP_RATE_INDX_16000	3
42#define SAMP_RATE_INDX_22050	4
43#define SAMP_RATE_INDX_24000	5
44#define SAMP_RATE_INDX_32000	6
45#define SAMP_RATE_INDX_44100	7
46#define SAMP_RATE_INDX_48000	8
47
48#define EQ_MAX_BAND_NUM 12
49
50#define ADRC_ENABLE  0x0001
51#define ADRC_DISABLE 0x0000
52#define EQ_ENABLE    0x0002
53#define EQ_DISABLE   0x0000
54#define RX_IIR_ENABLE   0x0004
55#define RX_IIR_DISABLE  0x0000
56
57struct eq_filter_type {
58    int16_t gain;
59    uint16_t freq;
60    uint16_t type;
61    uint16_t qf;
62};
63
64struct eqalizer {
65    uint16_t bands;
66    uint16_t params[132];
67};
68
69struct rx_iir_filter {
70    uint16_t num_bands;
71    uint16_t iir_params[48];
72};
73
74struct msm_audio_config {
75    uint32_t buffer_size;
76    uint32_t buffer_count;
77    uint32_t channel_count;
78    uint32_t sample_rate;
79    uint32_t codec_type;
80    uint32_t unused[3];
81};
82
83struct msm_audio_stats {
84    uint32_t out_bytes;
85    uint32_t unused[3];
86};
87
88#define CODEC_TYPE_PCM 0
89#define AUDIO_HW_NUM_OUT_BUF 2  // Number of buffers in audio driver for output
90// TODO: determine actual audio DSP and hardware latency
91#define AUDIO_HW_OUT_LATENCY_MS 0  // Additionnal latency introduced by audio DSP and hardware in ms
92
93#define AUDIO_HW_IN_SAMPLERATE 8000                 // Default audio input sample rate
94#define AUDIO_HW_IN_CHANNELS (AudioSystem::CHANNEL_IN_MONO) // Default audio input channel mask
95#define AUDIO_HW_IN_BUFFERSIZE 2048                 // Default audio input buffer size
96#define AUDIO_HW_IN_FORMAT (AudioSystem::PCM_16_BIT)  // Default audio input sample format
97// ----------------------------------------------------------------------------
98
99
100class AudioHardware : public  AudioHardwareBase
101{
102    class AudioStreamOutMSM72xx;
103    class AudioStreamInMSM72xx;
104
105public:
106                        AudioHardware();
107    virtual             ~AudioHardware();
108    virtual status_t    initCheck();
109
110    virtual status_t    setVoiceVolume(float volume);
111    virtual status_t    setMasterVolume(float volume);
112
113    virtual status_t    setMode(int mode);
114
115    // mic mute
116    virtual status_t    setMicMute(bool state);
117    virtual status_t    getMicMute(bool* state);
118
119    virtual status_t    setParameters(const String8& keyValuePairs);
120    virtual String8     getParameters(const String8& keys);
121
122    // create I/O streams
123    virtual AudioStreamOut* openOutputStream(
124                                uint32_t devices,
125                                int *format=0,
126                                uint32_t *channels=0,
127                                uint32_t *sampleRate=0,
128                                status_t *status=0);
129
130    virtual AudioStreamIn* openInputStream(
131
132                                uint32_t devices,
133                                int *format,
134                                uint32_t *channels,
135                                uint32_t *sampleRate,
136                                status_t *status,
137                                AudioSystem::audio_in_acoustics acoustics);
138
139    virtual    void        closeOutputStream(AudioStreamOut* out);
140    virtual    void        closeInputStream(AudioStreamIn* in);
141
142    virtual    size_t      getInputBufferSize(uint32_t sampleRate, int format, int channelCount);
143               void        clearCurDevice() { mCurSndDevice = -1; }
144
145protected:
146    virtual status_t    dump(int fd, const Vector<String16>& args);
147
148private:
149
150    status_t    doAudioRouteOrMute(uint32_t device);
151    status_t    setMicMute_nosync(bool state);
152    status_t    checkMicMute();
153    status_t    dumpInternals(int fd, const Vector<String16>& args);
154    uint32_t    getInputSampleRate(uint32_t sampleRate);
155    bool        checkOutputStandby();
156    status_t    doRouting();
157    AudioStreamInMSM72xx*   getActiveInput_l();
158
159    class AudioStreamOutMSM72xx : public AudioStreamOut {
160    public:
161                            AudioStreamOutMSM72xx();
162        virtual             ~AudioStreamOutMSM72xx();
163                status_t    set(AudioHardware* mHardware,
164                                uint32_t devices,
165                                int *pFormat,
166                                uint32_t *pChannels,
167                                uint32_t *pRate);
168        virtual uint32_t    sampleRate() const { return 44100; }
169        // must be 32-bit aligned - driver only seems to like 4800
170        virtual size_t      bufferSize() const { return 4800; }
171        virtual uint32_t    channels() const { return AudioSystem::CHANNEL_OUT_STEREO; }
172        virtual int         format() const { return AudioSystem::PCM_16_BIT; }
173        virtual uint32_t    latency() const { return (1000*AUDIO_HW_NUM_OUT_BUF*(bufferSize()/frameSize()))/sampleRate()+AUDIO_HW_OUT_LATENCY_MS; }
174        virtual status_t    setVolume(float left, float right) { return INVALID_OPERATION; }
175        virtual ssize_t     write(const void* buffer, size_t bytes);
176        virtual status_t    standby();
177        virtual status_t    dump(int fd, const Vector<String16>& args);
178                bool        checkStandby();
179        virtual status_t    setParameters(const String8& keyValuePairs);
180        virtual String8     getParameters(const String8& keys);
181                uint32_t    devices() { return mDevices; }
182        virtual status_t    getRenderPosition(uint32_t *dspFrames);
183
184    private:
185                AudioHardware* mHardware;
186                int         mFd;
187                int         mStartCount;
188                int         mRetryCount;
189                bool        mStandby;
190                uint32_t    mDevices;
191    };
192
193    class AudioStreamInMSM72xx : public AudioStreamIn {
194    public:
195        enum input_state {
196            AUDIO_INPUT_CLOSED,
197            AUDIO_INPUT_OPENED,
198            AUDIO_INPUT_STARTED
199        };
200
201                            AudioStreamInMSM72xx();
202        virtual             ~AudioStreamInMSM72xx();
203                status_t    set(AudioHardware* mHardware,
204                                uint32_t devices,
205                                int *pFormat,
206                                uint32_t *pChannels,
207                                uint32_t *pRate,
208                                AudioSystem::audio_in_acoustics acoustics);
209        virtual size_t      bufferSize() const { return mBufferSize; }
210        virtual uint32_t    channels() const { return mChannels; }
211        virtual int         format() const { return mFormat; }
212        virtual uint32_t    sampleRate() const { return mSampleRate; }
213        virtual status_t    setGain(float gain) { return INVALID_OPERATION; }
214        virtual ssize_t     read(void* buffer, ssize_t bytes);
215        virtual status_t    dump(int fd, const Vector<String16>& args);
216        virtual status_t    standby();
217        virtual status_t    setParameters(const String8& keyValuePairs);
218        virtual String8     getParameters(const String8& keys);
219        virtual unsigned int  getInputFramesLost() const { return 0; }
220                uint32_t    devices() { return mDevices; }
221                int         state() const { return mState; }
222
223    private:
224                AudioHardware* mHardware;
225                int         mFd;
226                int         mState;
227                int         mRetryCount;
228                int         mFormat;
229                uint32_t    mChannels;
230                uint32_t    mSampleRate;
231                size_t      mBufferSize;
232                AudioSystem::audio_in_acoustics mAcoustics;
233                uint32_t    mDevices;
234    };
235
236            static const uint32_t inputSamplingRates[];
237            bool        mInit;
238            bool        mMicMute;
239            bool        mBluetoothNrec;
240            uint32_t    mBluetoothId;
241            AudioStreamOutMSM72xx*  mOutput;
242            SortedVector <AudioStreamInMSM72xx*>   mInputs;
243
244            msm_snd_endpoint *mSndEndpoints;
245            int mNumSndEndpoints;
246            int mCurSndDevice;
247
248     friend class AudioStreamInMSM72xx;
249            Mutex       mLock;
250
251            int SND_DEVICE_CURRENT;
252            int SND_DEVICE_HANDSET;
253            int SND_DEVICE_SPEAKER;
254            int SND_DEVICE_HEADSET;
255            int SND_DEVICE_BT;
256            int SND_DEVICE_CARKIT;
257            int SND_DEVICE_TTY_FULL;
258            int SND_DEVICE_TTY_VCO;
259            int SND_DEVICE_TTY_HCO;
260            int SND_DEVICE_NO_MIC_HEADSET;
261            int SND_DEVICE_FM_HEADSET;
262            int SND_DEVICE_HEADSET_AND_SPEAKER;
263            int SND_DEVICE_FM_SPEAKER;
264            int SND_DEVICE_BT_EC_OFF;
265};
266
267// ----------------------------------------------------------------------------
268
269}; // namespace android
270
271#endif // ANDROID_AUDIO_HARDWARE_MSM72XX_H
272