blob: 33b3bedfff116cb606a57a0e8b210d8072890c82 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*****************************************************************************
2** °æÈ¨ËùÓÐ (C)2015, ÖÐÐËͨѶ¹É·ÝÓÐÏÞ¹«Ë¾¡£
3**
4** ÎļþÃû³Æ: voice.c
5** Îļþ±êʶ:
6** ÄÚÈÝÕªÒª:
7** ʹÓ÷½·¨:
8**
9** ÐÞ¸ÄÈÕÆÚ °æ±¾ºÅ Ð޸ıê¼Ç ÐÞ¸ÄÈË ÐÞ¸ÄÄÚÈÝ
10** -----------------------------------------------------------------------------
11** 2019/09/25 V1.0 Create xxq ´´½¨
12**
13* ******************************************************************************/
14
15#include <stdio.h>
16#include <unistd.h>
17#include <string.h>
18#include <stdlib.h>
19
20#include <stdint.h>
21#include <linux/volte_drv.h>
22#include <sys/ioctl.h>
23#include <fcntl.h>
24#include <tinyalsa/audio_mixer_ctrl.h>
xf.libdd93d52023-05-12 07:10:14 -070025#include "voice_lib.h"
xf.li742dd022023-06-08 01:43:32 -070026#include "voice_ipc.h"
27
xf.libdd93d52023-05-12 07:10:14 -070028
lh9ed821d2023-04-07 01:36:19 -070029
30#define VOICE_DEV_NAME "/dev/voice_device"
31
32
33
34typedef struct {
35 int fd;
36 unsigned int state; // 0 close;1 open
37 T_Voice_Para vpara;
38} T_voiceInfo;
39
40T_voiceInfo *voiceinfo = NULL;
41
42static int slic_flag = 0;
43static int slic_flag_already = 0;// 0 not get ,1 already get
44
45struct pcm *volte_pcm_voice_out = NULL;
46struct pcm *volte_pcm_voice_in = NULL;
47
48#if 0
49
50
51T_Voice_Para gsmhwpara = {
52 8000, //8000;16000
53 0, //// 0 gsm;1 td;2 wcdma;3 lte
54 0//0 hardware dsp;1 soft amr lib
55};
56
57T_Voice_Para tdnbhwpara = {
58 8000, //8000;16000
59 1, //// 0 gsm;1 td;2 wcdma;3 lte
60 0,//0 amr-nb;1 amr-wb
61 0//0 hardware dsp;1 soft amr lib
62};
63
64T_Voice_Para tdnbsfpara = {
65 8000, //8000;16000
66 1, //// 0 gsm;1 td;2 wcdma;3 lte
67 0,//0 amr-nb;1 amr-wb
68 1//0 hardware dsp;1 soft amr lib
69};
70
71T_Voice_Para tdwbsfpara = {
72 8000, //8000;16000
73 1, //// 0 gsm;1 td;2 wcdma;3 lte
74 1,//0 amr-nb;1 amr-wb
75 1//0 hardware dsp;1 soft amr lib
76};
77
78
79#endif
80//static volatile int voiceinfo.fd = 0;
81
82
83int voice_open(T_Voice_Para *para)
84{
85 int ret = 0;
86 if (!para) {
87
88 printf("voice: open para is NULL.\n");
89
90 return -1;
91 }
92
93 voiceinfo = calloc(1, sizeof(T_voiceInfo));
94 if (!voiceinfo) {
95
96 printf("voice: open calloc fail!\n");
97 return -1;
98 }
99
100 voiceinfo->vpara = *para;
101
102 voiceinfo->fd = open(VOICE_DEV_NAME, O_RDONLY);
103 if (voiceinfo->fd < 0) {
104 printf("voice lib: open voice device error.\n");
105 free(voiceinfo);
106 return -1;
107
108 }
109
110
111 ret = ioctl(voiceinfo->fd, VOICE_IOCTL_START, para);
112 if (ret) {
113 printf("voice lib: voice start fd=%d,ret=%d.\n", voiceinfo->fd, ret);
114 goto err;
115 }
116 printf("voice open end !\n");
117
118 return 0;
119
120err:
121 close(voiceinfo->fd);
122 free(voiceinfo);
123 printf("voice open err end !\n");
124 return -1;
125
126
127}
128
129
130int voice_close(T_Voice_Para *para)
131{
132 int ret = 0;
133
134 if (!voiceinfo) {
135
136 printf("voice: close voiceinfo is NUll!\n");
137 return 0;
138 }
139
140 ret = ioctl(voiceinfo->fd, VOICE_IOCTL_STOP, para);
141
142 if (ret) {
143 printf("voice lib: voice stop fd=%d,ret=%d.\n", voiceinfo->fd, ret);
144 }
145
146 if (voiceinfo->fd >= 0) {
147 close(voiceinfo->fd);
148 }
149 voiceinfo->fd = -1;
150
151 free(voiceinfo);
152 voiceinfo = NULL;
153
154 printf("voice close end !\n");
155
156 return 0;
157}
158int voice_Vploop(int *path)
159{
160 int ret = 0;
161 int fd = -1;
162 printf("voice_Vploop: start path=%d!\n",*path);
163 fd = open(VOICE_DEV_NAME, O_RDONLY);
164 if (fd < 0) {
165 printf("voice_Vploop: open voice device error.\n");
166 return -1;
167 }
168 ret = ioctl(fd, VOICE_IOCTL_VPLOOP, path);
169 if (ret) {
170 printf("voice_Vploop: ret=%d,path=%p.\n", ret, path);
171 close(fd);
172 return -1;
173 }
174 close(fd);
175 return 0;
176
177}
178
179int voice_GetSlicFlag(void)
180{
181 int ret = 0;
182 int fd = -1;
183 int flag = 0;
184 printf("voice_GetSlicFlag: start!\n");
185 fd = open(VOICE_DEV_NAME, O_RDONLY);
186 if (fd < 0) {
187 printf("voice_GetSlicFlag: open voice device error.\n");
188 return -1;
189 }
190 ret = ioctl(fd, VOICE_IOCTL_GET_SLIC_USE_FLAG, &flag);
191 if (ret) {
192 printf("voice_GetSlicFlag: ret=%d,flag=%d.\n", ret, flag);
193 close(fd);
194 return -1;
195 }
196 close(fd);
197 slic_flag_already = 1;
198 return flag;
199
200}
201
xf.li742dd022023-06-08 01:43:32 -0700202static T_ZDrvVoice_Cfg s_cfgParam = {0};
203static int cur_vmode = MAX_AVOICE_MODE;
204
lh9ed821d2023-04-07 01:36:19 -0700205int zDrvVolte_PreOpen(T_ZDrvVolte_Cfg *cfgParam)
206{
207
lh9ed821d2023-04-07 01:36:19 -0700208 struct mixer *voice_mixer = NULL;
209 struct pcm_config config_voice = {0};
xf.li742dd022023-06-08 01:43:32 -0700210 int ret = 0;
lh9ed821d2023-04-07 01:36:19 -0700211 printf(" voice lib zDrvVolte_PreOpen!\n");
212
213 if (slic_flag_already == 1) {
214 printf(" voice slic flag already get, slic_flag=%d!\n", slic_flag);
215 if (slic_flag == 1) {
216 return 0;
217 }
218
219 } else {
220 slic_flag = voice_GetSlicFlag();
221 printf(" voice slic flag get, slic_flag=%d!\n", slic_flag);
222 if (slic_flag == 1) {
223 return 0;
224 }
225 }
xf.li742dd022023-06-08 01:43:32 -0700226#if defined(_ALSA_CODEC_IN_CAP) && defined(_USE_ALSA_AT_INTF)
xf.libdd93d52023-05-12 07:10:14 -0700227
xf.li742dd022023-06-08 01:43:32 -0700228
229 printf("%s: i2s and codec not need config,return!\n",__func__);
230 return 0;
231
232#elif defined(_ALSA_CODEC_IN_CAP)
233
234
235 if(cfgParam->clock_rate == 8000){
236
237 ret = cap_alsa_voice_open(AVOICE_4G_NB);
238 printf("%s:cap_alsa_voice_open clock_rate(%d) ret=%d!\n",__func__,cfgParam->clock_rate,ret);
239 cur_vmode = AVOICE_4G_NB;
240
241 }
242 else if(cfgParam->clock_rate == 16000){
243
244 ret = cap_alsa_voice_open(AVOICE_4G_WB);
245 printf("%s:cap_alsa_voice_open clock_rate(%d) ret=%d!\n",__func__,cfgParam->clock_rate,ret);
246 cur_vmode = AVOICE_4G_WB;
247 }
248 else{
249
250 printf("%s:cap_alsa_voice_open clock_rate(%d) not support!\n",__func__,cfgParam->clock_rate);
251 return -2;
252
253 }
254 s_cfgParam = *cfgParam;
255 return ret;
256
xf.libdd93d52023-05-12 07:10:14 -0700257#endif
lh9ed821d2023-04-07 01:36:19 -0700258
259
260 //open mixer dev for codec control
261 voice_mixer = mixer_open(0);
262 if (!voice_mixer) {
xf.li742dd022023-06-08 01:43:32 -0700263 printf("voice_mixer open failed!\n");
lh9ed821d2023-04-07 01:36:19 -0700264 return -1;
265 }
266
267 //config mixer dev
268 mix_set_voice_path(voice_mixer, T_OUTPUT_SPEAKER);
269 mix_set_voice_vol(voice_mixer, T_VOICE_VOL_3_LEVEL);
270
271 //close mixer
272 mixer_close(voice_mixer);
273 voice_mixer = NULL;
274
275 //open pcm dev for data tranf
276 config_voice.channels = cfgParam->channel_count;
277 config_voice.rate = cfgParam->clock_rate;
278 //buffer num
279 config_voice.period_count = 3;
280 //buffer size
281 config_voice.period_size = cfgParam->samples_per_frame * cfgParam->bits_per_sample / 8;
282 //16-bit signed
283 config_voice.format = PCM_FORMAT_S16_LE;
284
285 //card 0 dev 1
286 //23G card 0 dev 2
287 volte_pcm_voice_out = pcm_open(0, 1, PCM_OUT, &config_voice);
288 if (!pcm_is_ready(volte_pcm_voice_out)) {
xf.li742dd022023-06-08 01:43:32 -0700289 printf("volte_pcm_voice_out open failed!\n");
lh9ed821d2023-04-07 01:36:19 -0700290 goto err_ret;
291 }
292
293 volte_pcm_voice_in = pcm_open(0, 1, PCM_IN, &config_voice);
294 if (!pcm_is_ready(volte_pcm_voice_in)) {
xf.li742dd022023-06-08 01:43:32 -0700295 printf("volte_pcm_voice_in open failed!\n");
lh9ed821d2023-04-07 01:36:19 -0700296 goto err_ret;
297 }
298
299 if (0 != pcm_prepare(volte_pcm_voice_out)) {
xf.li742dd022023-06-08 01:43:32 -0700300 printf("volte_pcm_voice_out pcm_prepare failed!\n");
lh9ed821d2023-04-07 01:36:19 -0700301 goto err_ret;
302 }
303
304 if (0 != pcm_prepare(volte_pcm_voice_in)) {
xf.li742dd022023-06-08 01:43:32 -0700305 printf("volte_pcm_voice_in pcm_prepare failed!\n");
lh9ed821d2023-04-07 01:36:19 -0700306 goto err_ret;
307 }
308 return 0;
309err_ret:
310
311 pcm_close(volte_pcm_voice_out);
312 volte_pcm_voice_out = NULL;
313
314 if (volte_pcm_voice_in) {
315 pcm_close(volte_pcm_voice_in);
316 volte_pcm_voice_in = NULL;
317 }
318 return -1;
319
320}
321
322void zDrvVolte_PreClose(void)
323{
xf.li742dd022023-06-08 01:43:32 -0700324 int ret = 0;
lh9ed821d2023-04-07 01:36:19 -0700325
326 printf(" voice lib zDrvVolte_PreClose!\n");
327
328 if (slic_flag_already == 1) {
329 printf(" voice slic flag already get, slic_flag=%d!\n", slic_flag);
330 if (slic_flag == 1) {
xf.li742dd022023-06-08 01:43:32 -0700331 return;
lh9ed821d2023-04-07 01:36:19 -0700332 }
333
334 } else {
335 slic_flag = voice_GetSlicFlag();
336 printf(" voice slic flag get, slic_flag=%d!\n", slic_flag);
337 if (slic_flag == 1) {
xf.li742dd022023-06-08 01:43:32 -0700338 return;
lh9ed821d2023-04-07 01:36:19 -0700339 }
340 }
xf.li742dd022023-06-08 01:43:32 -0700341#if defined(_ALSA_CODEC_IN_CAP) && defined(_USE_ALSA_AT_INTF)
342
xf.libdd93d52023-05-12 07:10:14 -0700343 printf("%s: i2s and codec not need config,return!\n",__func__);
344 return ;
xf.li742dd022023-06-08 01:43:32 -0700345#elif defined(_ALSA_CODEC_IN_CAP)
346 if(s_cfgParam.clock_rate == 8000){
347
348 ret = cap_alsa_voice_close(AVOICE_4G_NB);
349 printf("%s:cap_alsa_voice_close clock_rate(%d) ret=%d!\n",__func__,s_cfgParam.clock_rate,ret);
350
351 }
352 else if(s_cfgParam.clock_rate == 16000){
353
354 ret = cap_alsa_voice_close(AVOICE_4G_WB);
355 printf("%s:cap_alsa_voice_close clock_rate(%d) ret=%d!\n",__func__,s_cfgParam.clock_rate,ret);
356
357 }
358 else{
359
360 printf("%s:cap_alsa_voice_close clock_rate(%d) not support!\n",__func__,s_cfgParam.clock_rate);
361
362 }
363 return;
364
365
366
xf.libdd93d52023-05-12 07:10:14 -0700367#endif
lh9ed821d2023-04-07 01:36:19 -0700368
369
370 if (volte_pcm_voice_out) {
371 pcm_close(volte_pcm_voice_out);
372 volte_pcm_voice_out = NULL;
373 }
374 if (volte_pcm_voice_in) {
375 pcm_close(volte_pcm_voice_in);
376 volte_pcm_voice_in = NULL;
377 }
378}
379
380
381
382
383