Restructure files
[platform/adaptation/spreadtrum/audio-hal-sc7727.git] / tizen-audio-modem.c
1 /*
2  * audio-hal
3  *
4  * Copyright (c) 2014 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <expat.h>
30 #include <stdbool.h>
31 #include <vconf.h>
32
33 #include "tizen-audio-internal.h"
34 #include "tizen-audio-impl.h"
35
36 #define vbc_thread_new pthread_create
37 #define MIXER_VBC_SWITCH                            "VBC Switch"
38 /* pin_switch */
39 #define PIN_SWITCH_IIS0_SYS_SEL              "IIS0 pin select"
40 #define PIN_SWITCH_IIS0_AP_ID                0
41 #define PIN_SWITCH_IIS0_CP0_ID               1
42 #define PIN_SWITCH_IIS0_CP1_ID               2
43 #define PIN_SWITCH_IIS0_CP2_ID               3
44 #define PIN_SWITCH_IIS0_VBC_ID               4
45
46 #define PIN_SWITCH_BT_IIS_SYS_SEL            "BT IIS pin select"
47 #define PIN_SWITCH_BT_IIS_CP0_IIS0_ID        0
48 #define PIN_SWITCH_BT_IIS_CP1_IIS0_ID        4
49 #define PIN_SWITCH_BT_IIS_AP_IIS0_ID         8
50
51 #define PIN_SWITCH_BT_IIS_CON_SWITCH         "BT IIS con switch"
52
53 #define VBC_TD_CHANNELID                            0  /*  cp [3g] */
54 #define VBC_ARM_CHANNELID                           2  /*  ap */
55
56 #define VBPIPE_DEVICE           "/dev/spipe_w6"
57 #define VBPIPE_VOIP_DEVICE      "/dev/spipe_w4"
58 #define VBC_CMD_TAG             "VBC"
59
60 /* Voice */
61 typedef enum {
62     VBC_CMD_NONE = 0,
63     /* current mode and volume gain parameters.*/
64     VBC_CMD_SET_MODE = 1,
65     VBC_CMD_RESP_MODE = 2,
66
67     VBC_CMD_SET_GAIN = 3,
68     VBC_CMD_RESP_GAIN = 4,
69
70     /* whether switch vb control to dsp parameters.*/
71     VBC_CMD_SWITCH_CTRL = 5,
72     VBC_CMD_RESP_SWITCH = 6,
73
74     /* whether mute or not.*/
75     VBC_CMD_SET_MUTE = 7,
76     VBC_CMD_RESP_MUTE = 8,
77
78     /* open/close device parameters.*/
79     VBC_CMD_DEVICE_CTRL = 9,
80     VBC_CMD_RESP_DEVICE = 10,
81
82     VBC_CMD_PCM_OPEN = 11,
83     VBC_CMD_RESP_OPEN  =12,
84
85     VBC_CMD_PCM_CLOSE = 13,
86     VBC_CMD_RESP_CLOSE = 14,
87
88     VBC_CMD_SET_SAMPLERATE = 15,
89     VBC_CMD_RESP_SAMPLERATE = 16,
90
91     VBC_CMD_MAX
92 } vbc_command;
93
94 const static char* vbc_cmd_str_arry[VBC_CMD_MAX] = {"NONE", "SET_MODE", "RESP_MODE", "SET_GAIN", "RESP_GAIN", "SWITCH_CTL", "RESP_SWITCH",
95 "SET_MUTE", "RESP_MUTE", "DEVICE_CTL", "RESP_DEVICE", "PCM_OPEN", "RESP_OPEN", "PCM_CLOSE", "RESP_CLOSE", "SET_SAMPL", "RESP_SAMPL"};
96
97 typedef struct {
98     unsigned int  sim_card;   /*sim card number*/
99 } open_pcm_t;
100
101 typedef struct _vbc_parameters_head {
102     char            tag[4];
103     unsigned int    cmd_type;
104     unsigned int    paras_size;
105 } vbc_parameters_head;
106
107 typedef struct vbc_control_params {
108     int vbchannel_id;
109     audio_hal_t *ah;
110 } vbc_control_params_t;
111
112 typedef struct {
113     unsigned short      is_open; /* if is_open is true, open device; else close device.*/
114     unsigned short      is_headphone;
115     unsigned int        is_downlink_mute;
116     unsigned int        is_uplink_mute;
117 } device_ctrl_t;
118
119 typedef struct samplerate_ctrl {
120     unsigned int samplerate; /* change samplerate.*/
121 } set_samplerate_t;
122
123 typedef struct {
124     unsigned int  is_switch; /* switch vbc contrl to dsp.*/
125 } switch_ctrl_t;
126
127 static int __read_nonblock(int fd, void *buf, int bytes)
128 {
129     int ret = 0;
130     int bytes_to_read = bytes;
131
132     if ((fd > 0) && (buf != NULL)) {
133         do {
134             ret = read(fd, buf, bytes);
135             if ( ret > 0) {
136                 if (ret <= bytes) {
137                     bytes -= ret;
138                 }
139             } else if ((!((errno == EAGAIN) || (errno == EINTR))) || (0 == ret)) {
140                 break;
141             }
142         } while(bytes);
143     }
144
145     if (bytes == bytes_to_read)
146         return ret ;
147     else
148         return (bytes_to_read - bytes);
149 }
150
151 static int __write_nonblock(int fd, void *buf, int bytes)
152 {
153     int ret = -1;
154     int bytes_to_write = bytes;
155
156     if ((fd > 0) && (buf != NULL)) {
157         do {
158             ret = write(fd, buf, bytes);
159             if ( ret > 0) {
160                 if (ret <= bytes) {
161                     bytes -= ret;
162                 }
163             } else if ((!((errno == EAGAIN) || (errno == EINTR))) || (0 == ret)) {
164                 break;
165             }
166         } while(bytes);
167     }
168
169     if (bytes == bytes_to_write)
170         return ret ;
171     else
172         return (bytes_to_write - bytes);
173
174 }
175
176 int _audio_modem_is_call_connected(audio_hal_t *ah)
177 {
178     int val = -1; /* Mixer values 0 - cp [3g] ,1 - cp [2g] ,2 - ap */
179
180     _mixer_control_get_value(ah, MIXER_VBC_SWITCH, &val);
181     AUDIO_LOG_INFO("modem is connected for call = %d", (val == VBC_TD_CHANNELID));
182
183     return (val == VBC_TD_CHANNELID) ? 1 : 0;
184 }
185
186 static int __vbc_write_response(int fd, unsigned int cmd, uint32_t paras_size)
187 {
188     int ret = 0;
189     vbc_parameters_head write_head;
190
191     memset(&write_head, 0, sizeof(vbc_parameters_head));
192     memcpy(&write_head.tag[0], VBC_CMD_TAG, 3);
193     write_head.cmd_type = cmd + 1;
194     write_head.paras_size = paras_size;
195
196     ret = __write_nonblock(fd, (void*)&write_head, sizeof(vbc_parameters_head));
197     if (ret < 0)
198         AUDIO_LOG_ERROR("write failed");
199     else
200         AUDIO_LOG_DEBUG("write success for VBC_CMD_[%s]", vbc_cmd_str_arry[cmd]);
201
202     return 0;
203 }
204
205 #define FM_IIS                                      0x10
206 static void __i2s_pin_mux_sel(audio_hal_t *ah, int type)
207 {
208     audio_return_t ret = AUDIO_RET_OK;
209     audio_modem_t *modem;
210
211     if (!ah) {
212         AUDIO_LOG_ERROR("ah is null");
213         return;
214     }
215
216     AUDIO_LOG_INFO("type is %d",type);
217     modem = ah->modem.cp;
218
219     if (type == FM_IIS) {
220         _mixer_control_set_value(ah,
221                     PIN_SWITCH_IIS0_SYS_SEL, PIN_SWITCH_IIS0_VBC_ID);
222         return;
223     }
224     if (type == 0) {
225        if(ah->device.active_out & AUDIO_DEVICE_OUT_BT_SCO) {
226             if(modem->i2s_bt.is_ext) {
227                 if(modem->i2s_bt.is_switch) {
228                     ret = _mixer_control_set_value(ah,
229                             PIN_SWITCH_IIS0_SYS_SEL, PIN_SWITCH_IIS0_CP0_ID);
230                 }
231             } else {
232                 if(modem->i2s_bt.is_switch) {
233                     int value = 0;
234                     _mixer_control_get_value (ah, PIN_SWITCH_IIS0_SYS_SEL, &value);
235                     if(value == PIN_SWITCH_IIS0_CP0_ID) {
236                         ret = _mixer_control_set_value(ah,
237                                 PIN_SWITCH_IIS0_SYS_SEL, PIN_SWITCH_IIS0_AP_ID);
238                     }
239                 }
240                 if(ah->device.active_out & AUDIO_DEVICE_OUT_BT_SCO) {
241                     if(modem->i2s_bt.is_switch) {
242                         ret = _mixer_control_set_value(ah,
243                                 PIN_SWITCH_BT_IIS_SYS_SEL, PIN_SWITCH_BT_IIS_CP0_IIS0_ID);
244                     }
245                 }
246             }
247         }
248     } else if (type == 1) {
249         if(ah->device.active_out & AUDIO_DEVICE_OUT_BT_SCO) {
250             if(modem->i2s_bt.is_ext) {
251                 if(modem->i2s_bt.is_switch) {
252                     ret = _mixer_control_set_value(ah,
253                             PIN_SWITCH_IIS0_SYS_SEL, PIN_SWITCH_IIS0_CP1_ID);
254                 }
255             } else {
256                 if(modem->i2s_bt.is_switch) {
257                     int value = 0;
258                     _mixer_control_get_value (ah, PIN_SWITCH_IIS0_SYS_SEL, &value);
259                     if(value == PIN_SWITCH_IIS0_CP1_ID) {
260                         ret = _mixer_control_set_value(ah,
261                                 PIN_SWITCH_IIS0_SYS_SEL, PIN_SWITCH_IIS0_CP2_ID);
262                     }
263                 }
264                 if(ah->device.active_out & AUDIO_DEVICE_OUT_BT_SCO) {
265                     if(modem->i2s_bt.is_switch) {
266                         ret = _mixer_control_set_value(ah,
267                                 PIN_SWITCH_BT_IIS_SYS_SEL, PIN_SWITCH_BT_IIS_CP1_IIS0_ID);
268                     }
269                 }
270             }
271         }
272     } else {
273         AUDIO_LOG_ERROR("invalid type");
274         return;
275     }
276     if (ret)
277         AUDIO_LOG_ERROR("error(0x%x)", ret);
278
279     return;
280 }
281
282 static void *__vbc_control_voice_thread_run(void *args)
283 {
284     audio_return_t audio_ret = AUDIO_RET_OK;
285     vbc_parameters_head read_head;
286     vbc_parameters_head write_head;
287     int exit_thread = 0; /* make exit variable global if required to gracefully exit */
288     int vbpipe_fd;
289     vbc_control_params_t *params = (vbc_control_params_t*)args;
290     if (params == NULL) {
291         return (void*)AUDIO_ERR_PARAMETER;
292     }
293     audio_hal_t *ah = params->ah;
294     fd_set fds_read;
295     struct timeval timeout = {5,0};
296
297     memset(&read_head, 0, sizeof(vbc_parameters_head));
298     memset(&write_head, 0, sizeof(vbc_parameters_head));
299
300     memcpy(&write_head.tag[0], VBC_CMD_TAG, 3);
301     write_head.cmd_type = VBC_CMD_NONE;
302     write_head.paras_size = 0;
303
304     AUDIO_LOG_INFO("[voice] vbc control VOICE thread run");
305
306 again:
307     /* open vbpipe device for vb parameter interface between ap and cp */
308     vbpipe_fd = open(VBPIPE_DEVICE, O_RDWR);
309     if (vbpipe_fd < 0) {
310         if (errno == EINTR)
311             goto again;
312         AUDIO_LOG_ERROR("[voice] vbpipe open failed: %s", strerror(errno));
313         return (void*)AUDIO_ERR_IOCTL;
314     }
315     ah->modem.vbc.vbpipe_fd = vbpipe_fd;
316
317     if (fcntl(vbpipe_fd, F_SETFL, O_NONBLOCK) < 0) {
318         AUDIO_LOG_DEBUG("[voice] vbpipe_fd(%d) fcntl error.", vbpipe_fd);
319     }
320
321     AUDIO_LOG_INFO("[voice] %s opened. vbc start loop", VBPIPE_DEVICE);
322
323     /* start loop */
324     while (!exit_thread) {
325         int ret;
326         timeout.tv_sec = 10;
327         timeout.tv_usec = 0;
328
329         /* read command received from cp */
330         FD_ZERO(&fds_read);
331         FD_SET(vbpipe_fd, &fds_read);
332
333         ret = select(vbpipe_fd+1, &fds_read, NULL, NULL, &timeout);
334         if (ret < 0) {
335             ALOGE("voice:select error %d", errno);
336
337             continue;
338         }
339
340         ret = __read_nonblock(vbpipe_fd, &read_head, sizeof(vbc_parameters_head));
341         if (ret < 0) {
342
343             continue;
344         }
345
346         AUDIO_LOG_DEBUG("[voice] Received %d bytes. data: %s, cmd_type: %d", ret, read_head.tag, read_head.cmd_type);
347
348         if (!memcmp(&read_head.tag[0], VBC_CMD_TAG, 3)) {
349             switch (read_head.cmd_type) {
350                 case VBC_CMD_PCM_OPEN: {
351                     open_pcm_t open_pcm_params;
352                     uint32_t paras_size = ((ah->modem.cp->i2s_bt.is_switch << 8) | (ah->modem.cp->i2s_bt.index << 0)
353                                         | (ah->modem.cp->i2s_extspk.is_switch << 9) | (ah->modem.cp->i2s_extspk.index << 4));
354
355                     AUDIO_LOG_INFO("[voice] Received VBC_CMD_PCM_OPEN");
356
357                     ah->modem.is_connected = 1;
358                     if ((audio_ret = _audio_update_route_voicecall(ah, ah->device.init_call_devices, ah->device.num_of_call_devices))) {
359                         AUDIO_LOG_WARN("set voicecall route return 0x%x", audio_ret);
360                         if (audio_ret == AUDIO_ERR_INVALID_STATE) {
361                             /* send signal and wait for the ucm setting,
362                              * it might an incoming call scenario */
363                             _audio_comm_send_message(ah, SIGNAL_ROUTING_FOR_VOICE_CALL, 1);
364                             COND_TIMEDWAIT(ah->device.device_cond, ah->device.device_lock, "device_cond", TIMEOUT_SEC);
365                             MUTEX_UNLOCK(ah->device.device_lock, "device_lock");
366                         }
367                     }
368
369                     memset(&open_pcm_params, 0, sizeof(open_pcm_t));
370                     ret = __read_nonblock(vbpipe_fd, &open_pcm_params, sizeof(open_pcm_t));
371                     if (ret < 0)
372                         AUDIO_LOG_ERROR("read failed");
373                     else
374                         ah->modem.sim_id = open_pcm_params.sim_card;
375
376                     if (ah->device.active_out & (AUDIO_DEVICE_OUT_SPEAKER | AUDIO_DEVICE_OUT_BT_SCO)) {
377                         if (ah->modem.cp_type == CP_TG)
378                             __i2s_pin_mux_sel(ah, 1);
379                         else if(ah->modem.cp_type == CP_W)
380                             __i2s_pin_mux_sel(ah, 0);
381                     }
382
383                     AUDIO_LOG_DEBUG("[voice] Send response for VBC_CMD_PCM_OPEN");
384                     __vbc_write_response(vbpipe_fd, VBC_CMD_PCM_OPEN, paras_size);
385                     break;
386                 }
387
388                 case VBC_CMD_PCM_CLOSE: {
389                     AUDIO_LOG_INFO("[voice] Received VBC_CMD_PCM_CLOSE");
390
391                     ah->modem.samplerate = 0;
392                     ah->modem.is_connected = 0;
393
394                     /* send signal and wait for the reset ucm */
395                     _audio_comm_send_message(ah, SIGNAL_ROUTING_FOR_VOICE_CALL, 0);
396                     COND_TIMEDWAIT(ah->device.device_cond, ah->device.device_lock, "device_cond", TIMEOUT_SEC);
397                     MUTEX_UNLOCK(ah->device.device_lock, "device_lock");
398
399                     _mixer_control_set_value(ah, MIXER_VBC_SWITCH, VBC_ARM_CHANNELID);
400
401                     AUDIO_LOG_DEBUG("[voice] Send response for VBC_CMD_PCM_CLOSE");
402                     __vbc_write_response(vbpipe_fd, VBC_CMD_PCM_CLOSE, 0);
403                     break;
404                 }
405
406                 case VBC_CMD_RESP_CLOSE: {
407                     AUDIO_LOG_INFO("[voice] Received VBC_CMD_RESP_CLOSE & send response");
408                     ret = __vbc_write_response(vbpipe_fd, VBC_CMD_PCM_CLOSE, 0);
409                     break;
410                 }
411
412                 case VBC_CMD_SET_MODE: {
413                     char dummy[52];
414                     memset(dummy, 0, sizeof(dummy));
415                     AUDIO_LOG_INFO("[voice] Received VBC_CMD_SET_MODE");
416
417                     if (ah->device.active_out & (AUDIO_DEVICE_OUT_SPEAKER | AUDIO_DEVICE_OUT_BT_SCO)) {
418                         if (ah->modem.cp_type == CP_TG)
419                             __i2s_pin_mux_sel(ah, 1);
420                         else if(ah->modem.cp_type == CP_W)
421                             __i2s_pin_mux_sel(ah, 0);
422                     }
423                     /* To do: set mode params : __vbc_set_mode_params(ah, vbpipe_fd); */
424
425                     __read_nonblock(vbpipe_fd, dummy, sizeof(dummy));
426                     AUDIO_LOG_DEBUG("[voice] Send response for VBC_CMD_SET_MODE");
427                     __vbc_write_response(vbpipe_fd, VBC_CMD_SET_MODE, 0);
428                     break;
429                 }
430                 case VBC_CMD_SET_GAIN: {
431                     AUDIO_LOG_INFO("[voice] Received VBC_CMD_SET_GAIN");
432
433                     /* To do: set gain params : __vbc_set_gain_params(ah, vbpipe_fd); */
434
435                     AUDIO_LOG_DEBUG("[voice] Send response for VBC_CMD_SET_GAIN");
436                     __vbc_write_response(vbpipe_fd, VBC_CMD_SET_GAIN, 0);
437                     break;
438                 }
439                 case VBC_CMD_SWITCH_CTRL: {
440                     switch_ctrl_t switch_ctrl_params;
441
442                     AUDIO_LOG_INFO("[voice] Received VBC_CMD_SWITCH_CTRL");
443
444                     memset(&switch_ctrl_params,0,sizeof(switch_ctrl_t));
445                     ret = __read_nonblock(vbpipe_fd, &switch_ctrl_params, sizeof(switch_ctrl_t));
446                     if (ret < 0)
447                         AUDIO_LOG_ERROR("read failed");
448                     else
449                         AUDIO_LOG_INFO("is_switch:%d", switch_ctrl_params.is_switch);
450
451                     _mixer_control_set_value(ah, MIXER_VBC_SWITCH, VBC_TD_CHANNELID);
452
453                     AUDIO_LOG_DEBUG("[voice] Send response for VBC_CMD_SET_GAIN");
454                     __vbc_write_response(vbpipe_fd, VBC_CMD_SWITCH_CTRL, 0);
455                    break;
456                 }
457                 case VBC_CMD_SET_MUTE: {
458                     AUDIO_LOG_INFO("[voice] Received VBC_CMD_SET_MUTE");
459                     break;
460                 }
461                 case VBC_CMD_DEVICE_CTRL: {
462                     char dummy[64];
463                     memset(dummy, 0, sizeof(dummy));
464                     AUDIO_LOG_INFO("[voice] Received VBC_CMD_DEVICE_CTRL");
465
466                     /* To do: set device ctrl params :__vbc_set_device_ctrl_params(ah, vbpipe_fd); */
467                     __read_nonblock(vbpipe_fd, dummy, sizeof(dummy));
468
469                     AUDIO_LOG_DEBUG("[voice] Send response for VBC_CMD_DEVICE_CTRL");
470                     __vbc_write_response(vbpipe_fd, VBC_CMD_DEVICE_CTRL, 0);
471
472                     break;
473                 }
474                 case VBC_CMD_SET_SAMPLERATE: {
475                     AUDIO_LOG_INFO("[voice] Received VBC_CMD_SET_SAMPLERATE");
476
477                     AUDIO_LOG_DEBUG("[voice] Send response for VBC_CMD_SET_SAMPLERATE");
478                     __vbc_write_response(vbpipe_fd, VBC_CMD_SET_SAMPLERATE, 0);
479                     break;
480                 }
481                 default:
482                     AUDIO_LOG_WARN("[voice] Unknown command received : %d", read_head.cmd_type);
483                     break;
484             }
485         }
486     }
487     if (params)
488         free(params);
489
490     close(vbpipe_fd);
491
492     AUDIO_LOG_INFO("Exit vbc VOICE thread");
493
494     return (void*)0;
495 }
496
497 static void *__vbc_control_voip_thread_run(void *args)
498 {
499     open_pcm_t open_pcm_params;
500     vbc_parameters_head read_head;
501     vbc_parameters_head write_head;
502     int exit_thread = 0; /* make exit variable global if required to gracefully exit */
503     int vbpipe_fd;
504     vbc_control_params_t *params = (vbc_control_params_t*)args;
505     if (params == NULL) {
506         return (void*)AUDIO_ERR_PARAMETER;
507     }
508     audio_hal_t *ah = params->ah;
509     fd_set fds_read;
510
511     struct timeval timeout = {5,0};
512
513     memset(&read_head, 0, sizeof(vbc_parameters_head));
514     memset(&write_head, 0, sizeof(vbc_parameters_head));
515
516     memcpy(&write_head.tag[0], VBC_CMD_TAG, 3);
517     write_head.cmd_type = VBC_CMD_NONE;
518     write_head.paras_size = 0;
519
520     AUDIO_LOG_INFO("[voip] vbc control VOIP thread run");
521
522 again:
523     /* open vbpipe device for vb parameter interface between ap and cp */
524     vbpipe_fd = open(VBPIPE_VOIP_DEVICE, O_RDWR);
525     if (vbpipe_fd < 0) {
526         if (errno == EINTR)
527             goto again;
528         AUDIO_LOG_ERROR("[voip] vbpipe open failed: %s", strerror(errno));
529         return (void*)0;
530     }
531     ah->modem.vbc.vbpipe_voip_fd = vbpipe_fd;
532
533     if (fcntl(vbpipe_fd, F_SETFL, O_NONBLOCK) < 0) {
534         AUDIO_LOG_DEBUG("[voip] vbpipe_fd(%d) fcntl error.", vbpipe_fd);
535     }
536
537     AUDIO_LOG_INFO("[voip] %s opened. vbc start loop", VBPIPE_VOIP_DEVICE);
538
539     /* start loop */
540     while (!exit_thread) {
541         int ret;
542         timeout.tv_sec = 5;;
543         timeout.tv_usec = 0;
544
545         /* read command received from cp */
546
547         FD_ZERO(&fds_read);
548         FD_SET(vbpipe_fd, &fds_read);
549
550         ret = select(vbpipe_fd+1, &fds_read, NULL, NULL, &timeout);
551         if (ret < 0) {
552             ALOGE("[voip] select error %d", errno);
553             continue;
554         }
555
556         ret = __read_nonblock(vbpipe_fd, &read_head, sizeof(vbc_parameters_head));
557         if (ret < 0) {
558             continue;
559         }
560
561         AUDIO_LOG_DEBUG("[voip] Received %d bytes. data: %s, cmd_type: %d", ret, read_head.tag, read_head.cmd_type);
562
563         if (!memcmp(&read_head.tag[0], VBC_CMD_TAG, 3)) {
564             switch (read_head.cmd_type) {
565                 case VBC_CMD_PCM_OPEN: {
566                     uint32_t paras_size = ((ah->modem.cp->i2s_bt.is_switch << 8) | (ah->modem.cp->i2s_bt.index << 0)
567                                         | (ah->modem.cp->i2s_extspk.is_switch << 9) | (ah->modem.cp->i2s_extspk.index << 4));
568
569                     AUDIO_LOG_INFO("[voip] Received VBC_CMD_PCM_OPEN");
570
571                     memset(&open_pcm_params, 0, sizeof(open_pcm_t));
572                     ret = __read_nonblock(vbpipe_fd, &open_pcm_params, sizeof(open_pcm_t));
573                     if (ret < 0)
574                         AUDIO_LOG_ERROR("read failed");
575
576                     AUDIO_LOG_DEBUG("[voip] Send response for VBC_CMD_PCM_OPEN");
577                     __vbc_write_response(vbpipe_fd, VBC_CMD_PCM_OPEN, paras_size);
578                     break;
579                 }
580                 case VBC_CMD_PCM_CLOSE: {
581                     AUDIO_LOG_INFO("[voip] Received VBC_CMD_PCM_CLOSE & send response");
582
583                     __vbc_write_response(vbpipe_fd, VBC_CMD_PCM_CLOSE, 0);
584
585                     break;
586                 }
587                 case VBC_CMD_RESP_CLOSE: {
588                     AUDIO_LOG_INFO("[voip] Received VBC_CMD_RESP_CLOSE & send response");
589
590                     ret = __vbc_write_response(vbpipe_fd, VBC_CMD_PCM_CLOSE, 0);
591                     break;
592                 }
593                 case VBC_CMD_SET_MODE: {
594                     AUDIO_LOG_INFO("[voip] Received VBC_CMD_SET_MODE");
595
596                     if (ah->device.active_out & (AUDIO_DEVICE_OUT_SPEAKER | AUDIO_DEVICE_OUT_BT_SCO)) {
597                         if (ah->modem.cp_type == CP_TG)
598                             __i2s_pin_mux_sel(ah, 1);
599                         else if(ah->modem.cp_type == CP_W)
600                             __i2s_pin_mux_sel(ah, 0);
601                     }
602                     /* To do: set mode params : __vbc_set_mode_params(ah, vbpipe_fd); */
603                     AUDIO_LOG_DEBUG("[voip] Send response for VBC_CMD_SET_MODE");
604                     __vbc_write_response(vbpipe_fd, VBC_CMD_SET_MODE, 0);
605                     break;
606                 }
607                 case VBC_CMD_SET_GAIN: {
608                     AUDIO_LOG_INFO("[voip] Received VBC_CMD_SET_GAIN");
609
610                     /* To do: set gain params : __vbc_set_gain_params(ah, vbpipe_fd); */
611                     AUDIO_LOG_DEBUG("[voip] Send response for VBC_CMD_SET_GAIN");
612                     __vbc_write_response(vbpipe_fd, VBC_CMD_SET_GAIN, 0);
613                     break;
614                 }
615                 case VBC_CMD_SWITCH_CTRL: {
616                     switch_ctrl_t switch_ctrl_params;
617
618                     AUDIO_LOG_INFO("[voip] Received VBC_CMD_SWITCH_CTRL");
619
620                     memset(&switch_ctrl_params, 0, sizeof(switch_ctrl_t));
621                     ret = __read_nonblock(vbpipe_fd, &switch_ctrl_params, sizeof(switch_ctrl_t));
622                     if (ret < 0)
623                         AUDIO_LOG_ERROR("read failed");
624                     else
625                         AUDIO_LOG_INFO("is_switch:%d", switch_ctrl_params.is_switch);
626
627                     _mixer_control_set_value(ah, MIXER_VBC_SWITCH, VBC_TD_CHANNELID);
628
629                     AUDIO_LOG_DEBUG("[voip] Send response for VBC_CMD_SWITCH_CTRL");
630                     __vbc_write_response(vbpipe_fd, VBC_CMD_SWITCH_CTRL, 0);
631                     break;
632                 }
633                 case VBC_CMD_SET_MUTE: {
634                     AUDIO_LOG_INFO("[voip] Received VBC_CMD_SET_MUTE & send response");
635                     __vbc_write_response(vbpipe_fd, VBC_CMD_SET_MUTE, 0);
636                     break;
637                 }
638                 case VBC_CMD_DEVICE_CTRL: {
639                     AUDIO_LOG_INFO("[voip] Received VBC_CMD_DEVICE_CTRL");
640                     __vbc_write_response(vbpipe_fd, VBC_CMD_DEVICE_CTRL, 0);
641                     break;
642                 }
643                 case VBC_CMD_SET_SAMPLERATE: {
644                     AUDIO_LOG_INFO("[voip] Received VBC_CMD_SET_SAMPLERATE");
645
646                     AUDIO_LOG_DEBUG("[voip] Send response for VBC_CMD_SET_SAMPLERATE");
647                     __vbc_write_response(vbpipe_fd, VBC_CMD_SET_SAMPLERATE, 0);
648                     break;
649                 }
650                 default:
651                     AUDIO_LOG_WARN("Unknown command received : %d", read_head.cmd_type);
652                     break;
653             }
654         }
655     }
656     close(vbpipe_fd);
657     if (params)
658         free(params);
659
660     AUDIO_LOG_INFO("Exit vbc VOIP thread");
661
662     return (void*)0;
663 }
664
665 static audio_return_t __vbc_control_open(audio_hal_t *ah)
666 {
667     vbc_control_params_t *params = (vbc_control_params_t*)malloc(sizeof(vbc_control_params_t));
668     audio_return_t ret = AUDIO_RET_OK;
669     audio_return_t ret2 = AUDIO_RET_OK;
670
671     if (params == NULL) {
672          AUDIO_LOG_ERROR("vbc control param allocation failed");
673          return AUDIO_ERR_RESOURCE;
674     }
675
676     params->ah = ah;
677     AUDIO_LOG_INFO("vbc control thread create");
678     ret = vbc_thread_new(&ah->modem.vbc.voice_thread_handle, NULL, __vbc_control_voice_thread_run, (void*)params);
679     if (ret < 0) {
680         AUDIO_LOG_ERROR("vbc control thread create failed");
681         ret = AUDIO_ERR_RESOURCE;
682         return ret;
683     }
684
685     ret2 = vbc_thread_new(&ah->modem.vbc.voip_thread_handle, NULL, __vbc_control_voip_thread_run, (void*)params);
686     if (ret2 < 0) {
687         AUDIO_LOG_ERROR("vbc control VOIP thread create failed");
688         ret2 = AUDIO_ERR_RESOURCE;
689         return ret2;
690     }
691
692     return AUDIO_RET_OK;
693 }
694
695 void __vbc_control_close(audio_hal_t *ah)
696 {
697     /* TODO. Make sure we always receive CLOSE command from modem and then close pcm device */
698     ah->modem.vbc.exit_vbc_thread = 1;
699     close(ah->modem.vbc.vbpipe_fd);
700
701     pthread_cancel(ah->modem.vbc.voice_thread_handle);
702     pthread_cancel(ah->modem.vbc.voip_thread_handle);
703
704     return;
705 }
706
707 static void __audio_modem_create(audio_modem_t *modem, const char *num)
708 {
709     if (!atoi((char *)num)) {
710         AUDIO_LOG_ERROR("Unnormal modem num!");
711         return;
712     }
713
714     modem->num = atoi((char *)num);
715     /* check if we need to allocate  space for modem profile */
716     if (!modem->vbc_ctrl_pipe_info) {
717         modem->vbc_ctrl_pipe_info = malloc(modem->num * sizeof(vbc_ctrl_pipe_para_t));
718         if (modem->vbc_ctrl_pipe_info == NULL) {
719             AUDIO_LOG_ERROR("Unable to allocate modem profiles");
720             return;
721         }
722
723         /* initialise the new profile */
724         memset((void*)modem->vbc_ctrl_pipe_info, 0x00, modem->num * sizeof(vbc_ctrl_pipe_para_t));
725     }
726
727     AUDIO_LOG_DEBUG("peter: modem num is %d", modem->num);
728 }
729
730
731 static void __audio_modem_start_tag(void *data, const XML_Char *tag_name,
732         const XML_Char **attr)
733 {
734     audio_modem_t *modem = (audio_modem_t *)data;
735
736     /* Look at tags */
737     if (strcmp(tag_name, "audio") == 0) {
738         if (strcmp(attr[0], "device") == 0) {
739             AUDIO_LOG_INFO("The device name is %s", attr[1]);
740         } else {
741             AUDIO_LOG_ERROR("Unnamed audio!");
742         }
743     } else if (strcmp(tag_name, "modem") == 0) {
744         /* Obtain the modem num */
745         if (strcmp(attr[0], "num") == 0) {
746             AUDIO_LOG_DEBUG("The modem num is '%s'", attr[1]);
747             __audio_modem_create(modem, attr[1]);
748         } else {
749             AUDIO_LOG_ERROR("no modem num!");
750         }
751     } else if (strcmp(tag_name, "cp") == 0) {
752         if (modem->vbc_ctrl_pipe_info) {
753             /* Obtain the modem name  \pipe\vbc   filed */
754             if (strcmp(attr[0], "name") != 0) {
755                 AUDIO_LOG_ERROR("Unnamed modem!");
756                 goto attr_err;
757             }
758             if (strcmp(attr[2], "pipe") != 0) {
759                 AUDIO_LOG_ERROR("'%s' No pipe filed!", attr[0]);
760                 goto attr_err;
761             }
762             if (strcmp(attr[4], "vbchannel") != 0) {
763                 AUDIO_LOG_ERROR("'%s' No vbc filed!", attr[0]);
764                 goto attr_err;
765             }
766             AUDIO_LOG_DEBUG("cp name is '%s', pipe is '%s',vbc is '%s'", attr[1], attr[3],attr[5]);
767             if(strcmp(attr[1], "w") == 0)
768             {
769                 modem->vbc_ctrl_pipe_info->cp_type = CP_W;
770             }
771             else if(strcmp(attr[1], "t") == 0)
772             {
773                 modem->vbc_ctrl_pipe_info->cp_type = CP_TG;
774             }
775             memcpy((void*)modem->vbc_ctrl_pipe_info->s_vbc_ctrl_pipe_name,(void*)attr[3],strlen((char *)attr[3]));
776             modem->vbc_ctrl_pipe_info->channel_id = atoi((char *)attr[5]);
777             modem->vbc_ctrl_pipe_info++;
778
779         } else {
780             AUDIO_LOG_ERROR("error profile!");
781         }
782     } else if (strcmp(tag_name, "i2s_for_btcall") == 0) {
783         if (strcmp(attr[0], "index") == 0) {
784             AUDIO_LOG_DEBUG("The iis_for_btcall index is '%s'", attr[1]);
785             modem->i2s_bt.index = atoi((char *)attr[1]);
786         } else {
787             AUDIO_LOG_ERROR("no iis_ctl index for bt call!");
788         }
789
790         if (strcmp(attr[2], "switch") == 0) {
791             AUDIO_LOG_DEBUG("The iis_for_btcall switch is '%s'", attr[3]);
792             if(strcmp(attr[3],"1") == 0)
793                 modem->i2s_bt.is_switch = true;
794             else if(strcmp(attr[3],"0") == 0)
795                 modem->i2s_bt.is_switch = false;
796         } else {
797             AUDIO_LOG_ERROR("no iis_ctl switch for bt call!");
798         }
799         if (strcmp(attr[4], "dst") == 0) {
800             AUDIO_LOG_DEBUG("The iis_for_btcall dst  is '%s'", attr[5]);
801             if (strcmp(attr[5], "internal") == 0)
802                 modem->i2s_bt.is_ext = 0;
803             else if (strcmp(attr[5], "external") == 0)
804                 modem->i2s_bt.is_ext = 1;
805         } else {
806             AUDIO_LOG_ERROR("no dst path for bt call!");
807         }
808     } else if (strcmp(tag_name, "i2s_for_extspeaker") == 0) {
809         if (strcmp(attr[0], "index") == 0) {
810             AUDIO_LOG_DEBUG("The i2s_for_extspeaker index is '%s'", attr[1]);
811             modem->i2s_extspk.index = atoi((char *)attr[1]);
812         } else {
813             AUDIO_LOG_ERROR("no iis_ctl index for extspk call!");
814         }
815         if (strcmp(attr[2], "switch") == 0) {
816             AUDIO_LOG_DEBUG("The iis_for_btcall switch is '%s'", attr[3]);
817             if(strcmp(attr[3],"1") == 0)
818                 modem->i2s_extspk.is_switch = true;
819             else if(strcmp(attr[3],"0") == 0)
820                 modem->i2s_extspk.is_switch = false;
821         } else {
822             AUDIO_LOG_ERROR("no iis_ctl switch for extspk call!");
823         }
824         if (strcmp(attr[4], "dst") == 0) {
825             if (strcmp(attr[5], "external") == 0)
826                 modem->i2s_extspk.is_ext = 1;
827             else if(strcmp(attr[5], "internal") == 0)
828                 modem->i2s_extspk.is_ext = 0;
829
830             AUDIO_LOG_DEBUG("The i2s_for_extspeaker dst  is '%d'", modem->i2s_extspk.is_ext);
831
832         } else {
833             AUDIO_LOG_ERROR("no dst path for bt call!");
834         }
835     } else if (strcmp(tag_name, "debug") == 0) {  //parse debug info
836         if (strcmp(attr[0], "enable") == 0) {
837             if (strcmp(attr[1], "0") == 0) {
838                 modem->debug_info.enable = 0;
839             } else {
840                 modem->debug_info.enable = 1;
841             }
842         } else {
843             AUDIO_LOG_ERROR("no adaptable type for debug!");
844             goto attr_err;
845         }
846     } else if (strcmp(tag_name, "debuginfo") == 0) { //parse debug info
847         if (strcmp(attr[0], "sleepdeltatimegate") == 0) {
848             AUDIO_LOG_DEBUG("The sleepdeltatimegate is  '%s'", attr[1]);
849             modem->debug_info.sleeptime_gate=atoi((char *)attr[1]);
850         } else if (strcmp(attr[0], "pcmwritetimegate") == 0) {
851             AUDIO_LOG_DEBUG("The pcmwritetimegate is  '%s'", attr[1]);
852             modem->debug_info.pcmwritetime_gate=atoi((char *)attr[1]);
853         } else if (strcmp(attr[0], "lastthiswritetimegate") == 0) {
854             AUDIO_LOG_DEBUG("The lastthiswritetimegate is  '%s'", attr[1]);
855             modem->debug_info.lastthis_outwritetime_gate=atoi((char *)attr[1]);
856         } else {
857             AUDIO_LOG_ERROR("no adaptable info for debuginfo!");
858             goto attr_err;
859         }
860    }
861
862 attr_err:
863     return;
864 }
865 static void __audio_modem_end_tag(void *data, const XML_Char *tag_name)
866 {
867     return;
868 }
869
870 static audio_modem_t * __audio_modem_parse (void)
871 {
872     XML_Parser parser;
873     FILE *file;
874     int bytes_read;
875     void *buf;
876     audio_modem_t *modem = NULL;
877
878     modem = calloc(1, sizeof(audio_modem_t));
879
880     if(modem == NULL) {
881         goto err_alloc;
882     }
883     memset(modem, 0, sizeof(audio_modem_t));
884     modem->num = 0;
885     modem->vbc_ctrl_pipe_info = NULL;
886
887     file = fopen(AUDIO_XML_PATH, "r");
888     if (!file) {
889         AUDIO_LOG_ERROR("Failed to open %s", AUDIO_XML_PATH);
890         goto err_fopen;
891     }
892
893     parser = XML_ParserCreate(NULL);
894     if (!parser) {
895         AUDIO_LOG_ERROR("Failed to create XML parser");
896         goto err_parser_create;
897     }
898
899     XML_SetUserData(parser, modem);
900     XML_SetElementHandler(parser, __audio_modem_start_tag, __audio_modem_end_tag);
901
902     for (;;) {
903         buf = XML_GetBuffer(parser, BUF_SIZE);
904         if (buf == NULL)
905             goto err_parse;
906
907         bytes_read = fread(buf, 1, BUF_SIZE, file);
908         if (bytes_read < 0)
909             goto err_parse;
910
911         if (XML_ParseBuffer(parser, bytes_read, bytes_read == 0) == XML_STATUS_ERROR) {
912             AUDIO_LOG_ERROR("Error in codec PGA xml (%s)", AUDIO_XML_PATH);
913             goto err_parse;
914         }
915
916         if (bytes_read == 0)
917             break;
918     }
919     XML_ParserFree(parser);
920     fclose(file);
921     return modem;
922
923 err_parse:
924     XML_ParserFree(parser);
925 err_parser_create:
926     fclose(file);
927 err_fopen:
928     free(modem);
929 err_alloc:
930     modem = NULL;
931     return NULL;
932 }
933
934 audio_return_t _audio_modem_init(audio_hal_t *ah)
935 {
936     audio_return_t audio_ret = AUDIO_RET_OK;
937
938     AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);
939
940     ah->modem.vbc.vbpipe_count = 0;
941
942     /* Initialize vbc interface */
943     if ((audio_ret = __vbc_control_open(ah))) {
944         AUDIO_LOG_ERROR("__vbc_control_open failed");
945         goto exit;
946     }
947     ah->modem.vbc.voice_pcm_handle_p = NULL;
948     ah->modem.vbc.voice_pcm_handle_c = NULL;
949     ah->modem.samplerate = 0;
950     ah->modem.cp = __audio_modem_parse();
951     if (ah->modem.cp == NULL) {
952         AUDIO_LOG_ERROR("modem parse failed");
953         goto exit;
954     }
955     ah->modem.cp_type = ah->modem.cp->vbc_ctrl_pipe_info->cp_type;
956
957     /* This ctrl need to be set "0" always - SPRD */
958     _mixer_control_set_value(ah, PIN_SWITCH_BT_IIS_CON_SWITCH, 0);
959
960 exit:
961     return audio_ret;
962 }
963
964 audio_return_t _audio_modem_deinit(audio_hal_t *ah)
965 {
966     AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);
967
968     /* Close vbc interface */
969     __vbc_control_close(ah);
970
971     return AUDIO_RET_OK;
972 }
973