1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #ifndef AUDIO_HOST_H
10 #define AUDIO_HOST_H
11 
12 #include "hdf_base.h"
13 #include "hdf_dlist.h"
14 #include "hdf_device_desc.h"
15 #include "hdf_log.h"
16 #include "device_resource_if.h"
17 #include "osal_mem.h"
18 #include "osal_mutex.h"
19 #include "securec.h"
20 
21 #ifdef __cplusplus
22 #if __cplusplus
23 extern "C" {
24 #endif
25 #endif /* __cplusplus */
26 
27 #define AUDIO_LIST_HEAD_INIT(name) { &(name), &(name) }
28 
29 #define AUDIO_LIST_HEAD(name) \
30     struct DListHead name = AUDIO_LIST_HEAD_INIT(name)
31 
32 enum AudioFormat {
33     AUDIO_FORMAT_TYPE_PCM_8_BIT  = 0x1u,       /**< audio 8-bit PCM */
34     AUDIO_FORMAT_TYPE_PCM_16_BIT = 0x2u,       /**< audio16-bit PCM */
35     AUDIO_FORMAT_TYPE_PCM_24_BIT = 0x3u,       /**< audio 24-bit PCM */
36     AUDIO_FORMAT_TYPE_PCM_32_BIT = 0x4u,       /**< audio 32-bit PCM */
37     AUDIO_FORMAT_TYPE_AAC_MAIN   = 0x1000001u, /**< audio AAC main */
38     AUDIO_FORMAT_TYPE_AAC_LC     = 0x1000002u, /**< audio AAC LC */
39     AUDIO_FORMAT_TYPE_AAC_LD     = 0x1000003u, /**< audio AAC LD */
40     AUDIO_FORMAT_TYPE_AAC_ELD    = 0x1000004u, /**< audio AAC ELD */
41     AUDIO_FORMAT_TYPE_AAC_HE_V1  = 0x1000005u, /**< audio AAC HE_V1 */
42     AUDIO_FORMAT_TYPE_AAC_HE_V2  = 0x1000006u, /**< audio AAC HE_V2 */
43     AUDIO_FORMAT_TYPE_G711A      = 0x2000001u, /**< audio G711A */
44     AUDIO_FORMAT_TYPE_G711U      = 0x2000002u, /**< audio G711u */
45     AUDIO_FORMAT_TYPE_G726       = 0x2000003u, /**< audio G726 */
46 };
47 
48 typedef enum {
49     AUDIO_SAMPLE_RATE_8000   = 8000,    /* 8kHz sample_rate */
50     AUDIO_SAMPLE_RATE_12000  = 12000,   /* 12kHz sample_rate */
51     AUDIO_SAMPLE_RATE_11025  = 11025,   /* 11.025kHz sample_rate */
52     AUDIO_SAMPLE_RATE_16000  = 16000,   /* 16kHz sample_rate */
53     AUDIO_SAMPLE_RATE_22050  = 22050,   /* 22.050kHz sample_rate */
54     AUDIO_SAMPLE_RATE_24000  = 24000,   /* 24kHz sample_rate */
55     AUDIO_SAMPLE_RATE_32000  = 32000,   /* 32kHz sample_rate */
56     AUDIO_SAMPLE_RATE_44100  = 44100,   /* 44.1kHz sample_rate */
57     AUDIO_SAMPLE_RATE_48000  = 48000,   /* 48kHz sample_rate */
58     AUDIO_SAMPLE_RATE_64000  = 64000,   /* 64kHz sample_rate */
59     AUDIO_SAMPLE_RATE_96000  = 96000,   /* 96kHz sample_rate */
60     AUDIO_SAMPLE_RATE_BUTT,
61 } AudioSampleRate;
62 
63 enum AuidoBitWidth {
64     BIT_WIDTH8  =  8,      /* 8 bit witdth */
65     BIT_WIDTH16 =  16,     /* 16 bit witdth */
66     BIT_WIDTH18 =  18,     /* 18 bit witdth */
67     BIT_WIDTH20 =  20,     /* 20 bit witdth */
68     BIT_WIDTH24 =  24,     /* 24 bit witdth */
69     BIT_WIDTH32 =  32,     /* 32 bit witdth */
70 };
71 
72 struct AudioConfigData {
73     const char *cardServiceName;
74     const char *codecName;
75     const char *platformName;
76     const char *cpuDaiName;
77     const char *codecDaiName;
78     const char *dspName;
79     const char *dspDaiName;
80 };
81 
82 enum AudioSapmTurnStandbyMode {
83     AUDIO_SAPM_TURN_STANDBY_LATER = 0,
84     AUDIO_SAPM_TURN_STANDBY_NOW,
85     AUDIO_SAPM_TURN_STANDBY_BUTT,
86 };
87 
88 struct AudioCard {
89     struct HdfDeviceObject *device;
90     struct AudioRuntimeDeivces *rtd;
91     struct AudioConfigData configData;
92 
93     /* Card-specific routes and components. */
94     const struct AudioSapmComponent *sapmComponents;
95     int32_t sapmComponentsNum;
96     const struct AudioSapmRoute *sapmRoutes;
97     int32_t sapmRoutesNum;
98 
99     struct DListHead list;
100     struct DListHead controls; /* all controls for this card */
101     struct DListHead components; /* all components for this card */
102     struct DListHead paths; /* all paths for this card */
103     struct DListHead sapmDirty; /* all dirty for this card */
104     enum AudioSapmTurnStandbyMode standbyMode;
105     uint64_t time;
106     bool sapmSleepState;
107     bool sapmStandbyState;
108     bool sapmMonitorState;
109     bool sapmStandbyStartTimeFlag;
110     bool sapmSleepStartTimeFlag;
111 };
112 
113 enum CriBuffStatus {
114     ENUM_CIR_BUFF_NORMAL    = -1,
115     ENUM_CIR_BUFF_FULL      = -2,
116     ENUM_CIR_BUFF_EMPTY     = -3,
117 };
118 
119 enum AudioStreamType {
120     AUDIO_CAPTURE_STREAM = 0,
121     AUDIO_RENDER_STREAM,
122 };
123 
124 enum AudioStreamDirection {
125     AUDIO_RENDER_STREAM_OUT = 0,
126     AUDIO_CAPTURE_STREAM_IN,
127 };
128 
129 enum AudioPortDirection {
130     PORT_OUT    = 0x1u, /**< Output port */
131     PORT_IN     = 0x2u, /**< Input port */
132     PORT_OUT_IN = 0x3u, /**< Input/output port, supporting both audio input and output */
133 };
134 
135 /* SoC PCM stream information */
136 struct AudioPcmStream {
137     uint64_t portDirection;
138     /* SNDRV_PCM_FMTBIT_* */
139     uint64_t formats;
140     /* SNDRV_PCM_RATE_* */
141     uint64_t rates;
142     /* min rate */
143     uint64_t rateMin;
144     /* max rate */
145     uint64_t rateMax;
146     /* min channels */
147     uint64_t channelsMin;
148     /* max channels */
149     uint64_t channelsMax;
150     /* max buffer size */
151     uint64_t bufferBytesMax;
152     /* min period size */
153     uint64_t periodBytesMin;
154     /* max period size */
155     uint64_t periodBytesMax;
156     /* min # of periods */
157     uint64_t periodsMin;
158     /* max # of periods */
159     uint64_t periodsMax;
160 };
161 
162 struct AudioPortInfo {
163     struct AudioPcmStream render;
164     struct AudioPcmStream capture;
165 };
166 
167 struct PcmInfo {
168     enum AudioStreamType streamType;
169     /* The number of channels in a frame */
170     uint32_t channels;
171     /* The number of frames per second */
172     uint32_t rate;
173     uint32_t bitWidth;
174     uint32_t frameSize;
175     bool isBigEndian;
176     bool isSignedData;
177     uint32_t startThreshold;
178     uint32_t stopThreshold;
179     uint32_t silenceThreshold;
180     uint32_t totalStreamSize;
181     uint32_t interleaved;
182 };
183 
184 struct AudioPcmHwParams {
185     /* The stream type in a frame */
186     enum AudioStreamType streamType;
187     /* The number of channels in a frame */
188     uint32_t channels;
189     /* The number of frames per second */
190     uint32_t rate;
191     /* The number of frames in a period */
192     uint32_t periodSize;
193     /* The number of periods in a PCM */
194     uint32_t periodCount;
195     /* The sample format of a PCM */
196     enum AudioFormat format; /* < Audio data format. For details, see {@link AudioFormat}. */
197     const char *cardServiceName;
198     uint32_t period;
199     uint32_t frameSize;
200     bool isBigEndian;
201     bool isSignedData;
202     uint32_t startThreshold;
203     uint32_t stopThreshold;
204     uint32_t silenceThreshold;
205 };
206 
207 struct AudioHost {
208     struct IDeviceIoService service;
209     struct HdfDeviceObject *device;
210     void *priv;
211 };
212 
213 struct AudioTxData {
214     int32_t status;
215     char *buf; /* buf address */
216     unsigned long frames; /* frames number */
217 };
218 
219 struct AudioRxData {
220     int32_t status;
221     char *buf;
222     unsigned long bufSize;
223     unsigned long frames; /* frames number */
224 };
225 
226 struct AudioMmapData {
227     void *memoryAddress;                 /**< Pointer to the mmap buffer */
228     int32_t memoryFd;                    /**< File descriptor of the mmap buffer */
229     int32_t totalBufferFrames;           /**< Total size of the mmap buffer (unit: frame )*/
230     int32_t transferFrameSize;           /**< Transfer size (unit: frame) */
231     int32_t isShareable;                 /**< Whether the mmap buffer can be shared among processes */
232     uint32_t offset;
233 };
234 
235 struct AudioRuntimeDeivces       {
236     /* runtime devices */
237     struct CodecDevice *codec;
238     struct PlatformDevice *platform;
239     struct DaiDevice *codecDai;
240     struct DaiDevice *cpuDai;
241     struct DspDevice *dsp;
242     struct DaiDevice *dspDai;
243     uint8_t complete;
244     uint32_t frameBits;
245     uint8_t *dmaArea;
246     unsigned long bufferSize;
247 };
248 
249 struct AudioHost *AudioHostCreateAndBind(struct HdfDeviceObject *device);
250 
251 /* Get a sound card instance */
252 struct AudioCard *GetCardInstance(const char *serviceName);
253 
254 /* Get all sound card instance */
255 const struct DListHead *GetAllCardInstance(void);
256 
257 #ifdef __cplusplus
258 #if __cplusplus
259 }
260 #endif
261 #endif /* __cplusplus */
262 
263 #endif /* AUDIO_HOST_H */
264