OSDN Git Service

Merge branch msm-4.4-android-10-clang into 10
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / sound / soc / msm / qdsp6v2 / q6voice.c
1 /*  Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 and
5  * only version 2 as published by the Free Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  */
12 #include <linux/slab.h>
13 #include <linux/kthread.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/uaccess.h>
17 #include <linux/wait.h>
18 #include <linux/mutex.h>
19 #include <linux/msm_audio_ion.h>
20
21 #include <soc/qcom/socinfo.h>
22 #include <linux/qdsp6v2/apr_tal.h>
23
24 #include "sound/q6audio-v2.h"
25 #include "sound/apr_audio-v2.h"
26 #include "sound/q6afe-v2.h"
27 #include <sound/q6common.h>
28 #include <sound/audio_cal_utils.h>
29 #include "q6voice.h"
30 #include <sound/adsp_err.h>
31
32 #define TIMEOUT_MS 300
33
34
35 #define CMD_STATUS_SUCCESS 0
36 #define CMD_STATUS_FAIL 1
37
38 enum {
39         VOC_TOKEN_NONE,
40         VOIP_MEM_MAP_TOKEN,
41         VOC_CAL_MEM_MAP_TOKEN,
42         VOC_VOICE_HOST_PCM_MAP_TOKEN,
43         VOC_RTAC_MEM_MAP_TOKEN,
44         VOC_SOURCE_TRACKING_MEM_MAP_TOKEN
45 };
46
47 struct cvd_version_table cvd_version_table_mapping[CVD_INT_VERSION_MAX] = {
48                 {CVD_VERSION_DEFAULT, CVD_INT_VERSION_DEFAULT},
49                 {CVD_VERSION_0_0, CVD_INT_VERSION_0_0},
50                 {CVD_VERSION_2_1, CVD_INT_VERSION_2_1},
51                 {CVD_VERSION_2_2, CVD_INT_VERSION_2_2},
52                 {CVD_VERSION_2_3, CVD_INT_VERSION_2_3},
53 };
54
55 static struct common_data common;
56 static bool module_initialized;
57
58 static int voice_send_enable_vocproc_cmd(struct voice_data *v);
59 static int voice_send_netid_timing_cmd(struct voice_data *v);
60 static int voice_send_attach_vocproc_cmd(struct voice_data *v);
61 static int voice_send_set_device_cmd(struct voice_data *v);
62 static int voice_send_vol_step_cmd(struct voice_data *v);
63 static int voice_send_mvm_unmap_memory_physical_cmd(struct voice_data *v,
64                                                     uint32_t mem_handle);
65 static int voice_send_mvm_cal_network_cmd(struct voice_data *v);
66 static int voice_send_mvm_media_type_cmd(struct voice_data *v);
67 static int voice_send_mvm_cvd_version_cmd(struct voice_data *v);
68 static int voice_send_cvs_data_exchange_mode_cmd(struct voice_data *v);
69 static int voice_send_cvs_packet_exchange_config_cmd(struct voice_data *v);
70 static int voice_set_packet_exchange_mode_and_config(uint32_t session_id,
71                                                      uint32_t mode);
72
73 static int voice_send_cvs_register_cal_cmd(struct voice_data *v);
74 static int voice_send_cvs_deregister_cal_cmd(struct voice_data *v);
75 static int voice_send_cvp_create_cmd(struct voice_data *v);
76 static int voice_send_cvp_register_dev_cfg_cmd(struct voice_data *v);
77 static int voice_send_cvp_deregister_dev_cfg_cmd(struct voice_data *v);
78 static int voice_send_cvp_register_cal_cmd(struct voice_data *v);
79 static int voice_send_cvp_deregister_cal_cmd(struct voice_data *v);
80 static int voice_send_cvp_register_vol_cal_cmd(struct voice_data *v);
81 static int voice_send_cvp_deregister_vol_cal_cmd(struct voice_data *v);
82 static int voice_send_cvp_media_fmt_info_cmd(struct voice_data *v);
83 static int voice_send_cvp_device_channels_cmd(struct voice_data *v);
84 static int voice_send_cvp_media_format_cmd(struct voice_data *v,
85                                            uint32_t param_type);
86 static int voice_send_cvp_topology_commit_cmd(struct voice_data *v);
87
88 static int voice_cvs_stop_playback(struct voice_data *v);
89 static int voice_cvs_start_playback(struct voice_data *v);
90 static int voice_cvs_start_record(struct voice_data *v, uint32_t rec_mode);
91 static int voice_cvs_stop_record(struct voice_data *v);
92
93 static int32_t qdsp_mvm_callback(struct apr_client_data *data, void *priv);
94 static int32_t qdsp_cvs_callback(struct apr_client_data *data, void *priv);
95 static int32_t qdsp_cvp_callback(struct apr_client_data *data, void *priv);
96
97 static int voice_send_set_pp_enable_cmd(
98         struct voice_data *v, struct module_instance_info mod_inst_info,
99         int enable);
100 static int is_cal_memory_allocated(void);
101 static bool is_cvd_version_queried(void);
102 static int is_voip_memory_allocated(void);
103 static int voice_get_cvd_int_version(char *cvd_ver_string);
104 static int voice_alloc_cal_mem_map_table(void);
105 static int voice_alloc_rtac_mem_map_table(void);
106 static int voice_alloc_oob_shared_mem(void);
107 static int voice_free_oob_shared_mem(void);
108 static int voice_alloc_oob_mem_table(void);
109 static int voice_alloc_and_map_oob_mem(struct voice_data *v);
110 static void voice_vote_powerstate_to_bms(struct voice_data *v, bool state);
111
112 static struct voice_data *voice_get_session_by_idx(int idx);
113
114 static int remap_cal_data(struct cal_block_data *cal_block,
115                           uint32_t session_id);
116 static int voice_unmap_cal_memory(int32_t cal_type,
117                                   struct cal_block_data *cal_block);
118
119 static int is_source_tracking_shared_memomry_allocated(void);
120 static int voice_alloc_source_tracking_shared_memory(void);
121 static int voice_alloc_and_map_source_tracking_shared_memory(
122                                                 struct voice_data *v);
123 static int voice_unmap_and_free_source_tracking_shared_memory(
124                                                 struct voice_data *v);
125 static int voice_send_set_sound_focus_cmd(struct voice_data *v,
126                                 struct sound_focus_param soundFocusData);
127 static int voice_send_get_sound_focus_cmd(struct voice_data *v,
128                                 struct sound_focus_param *soundFocusData);
129 static int voice_send_get_source_tracking_cmd(struct voice_data *v,
130                         struct source_tracking_param *sourceTrackingData);
131 static int voice_pack_and_set_cvp_param(struct voice_data *v,
132                                         struct param_hdr_v3 param_hdr,
133                                         u8 *param_data);
134 static int voice_pack_and_set_cvs_ui_property(struct voice_data *v,
135                                               struct param_hdr_v3 param_hdr,
136                                               u8 *param_data);
137
138 static void voice_itr_init(struct voice_session_itr *itr,
139                            u32 session_id)
140 {
141         if (itr == NULL)
142                 return;
143         itr->session_idx = voice_get_idx_for_session(session_id);
144         if (session_id == ALL_SESSION_VSID)
145                 itr->cur_idx = 0;
146         else
147                 itr->cur_idx = itr->session_idx;
148
149 }
150
151 static bool voice_itr_get_next_session(struct voice_session_itr *itr,
152                                         struct voice_data **voice)
153 {
154         bool ret = false;
155
156         if (itr == NULL)
157                 return false;
158         pr_debug("%s : cur idx = %d session idx = %d\n",
159                          __func__, itr->cur_idx, itr->session_idx);
160
161         if (itr->cur_idx <= itr->session_idx) {
162                 ret = true;
163                 *voice = voice_get_session_by_idx(itr->cur_idx);
164                 itr->cur_idx++;
165         } else {
166                 *voice = NULL;
167         }
168
169         return ret;
170 }
171
172 static bool voice_is_valid_session_id(uint32_t session_id)
173 {
174         bool ret = false;
175
176         switch (session_id) {
177         case VOICE_SESSION_VSID:
178         case VOICE2_SESSION_VSID:
179         case VOLTE_SESSION_VSID:
180         case VOIP_SESSION_VSID:
181         case QCHAT_SESSION_VSID:
182         case VOWLAN_SESSION_VSID:
183         case VOICEMMODE1_VSID:
184         case VOICEMMODE2_VSID:
185         case ALL_SESSION_VSID:
186                 ret = true;
187                 break;
188         default:
189                 pr_err("%s: Invalid session_id : %x\n", __func__, session_id);
190
191                 break;
192         }
193
194         return ret;
195 }
196
197 static u16 voice_get_mvm_handle(struct voice_data *v)
198 {
199         if (v == NULL) {
200                 pr_err("%s: v is NULL\n", __func__);
201                 return 0;
202         }
203
204         pr_debug("%s: mvm_handle %d\n", __func__, v->mvm_handle);
205
206         return v->mvm_handle;
207 }
208
209 static void voice_set_mvm_handle(struct voice_data *v, u16 mvm_handle)
210 {
211         pr_debug("%s: mvm_handle %d\n", __func__, mvm_handle);
212         if (v == NULL) {
213                 pr_err("%s: v is NULL\n", __func__);
214                 return;
215         }
216
217         v->mvm_handle = mvm_handle;
218 }
219
220 static u16 voice_get_cvs_handle(struct voice_data *v)
221 {
222         if (v == NULL) {
223                 pr_err("%s: v is NULL\n", __func__);
224                 return 0;
225         }
226
227         pr_debug("%s: cvs_handle %d\n", __func__, v->cvs_handle);
228
229         return v->cvs_handle;
230 }
231
232 static void voice_set_cvs_handle(struct voice_data *v, u16 cvs_handle)
233 {
234         pr_debug("%s: cvs_handle %d\n", __func__, cvs_handle);
235         if (v == NULL) {
236                 pr_err("%s: v is NULL\n", __func__);
237                 return;
238         }
239
240         v->cvs_handle = cvs_handle;
241 }
242
243 static u16 voice_get_cvp_handle(struct voice_data *v)
244 {
245         if (v == NULL) {
246                 pr_err("%s: v is NULL\n", __func__);
247                 return 0;
248         }
249
250         pr_debug("%s: cvp_handle %d\n", __func__, v->cvp_handle);
251
252         return v->cvp_handle;
253 }
254
255 static void voice_set_cvp_handle(struct voice_data *v, u16 cvp_handle)
256 {
257         pr_debug("%s: cvp_handle %d\n", __func__, cvp_handle);
258         if (v == NULL) {
259                 pr_err("%s: v is NULL\n", __func__);
260                 return;
261         }
262
263         v->cvp_handle = cvp_handle;
264 }
265
266 char *voc_get_session_name(u32 session_id)
267 {
268         char *session_name = NULL;
269
270         if (session_id == common.voice[VOC_PATH_PASSIVE].session_id) {
271                 session_name = VOICE_SESSION_NAME;
272         } else if (session_id ==
273                         common.voice[VOC_PATH_VOLTE_PASSIVE].session_id) {
274                 session_name = VOLTE_SESSION_NAME;
275         } else if (session_id ==
276                         common.voice[VOC_PATH_QCHAT_PASSIVE].session_id) {
277                 session_name = QCHAT_SESSION_NAME;
278         } else if (session_id ==
279                         common.voice[VOC_PATH_VOWLAN_PASSIVE].session_id) {
280                 session_name = VOWLAN_SESSION_NAME;
281         } else if (session_id ==
282                 common.voice[VOC_PATH_VOICEMMODE1_PASSIVE].session_id) {
283                 session_name = VOICEMMODE1_NAME;
284         } else if (session_id ==
285                 common.voice[VOC_PATH_VOICEMMODE2_PASSIVE].session_id) {
286                 session_name = VOICEMMODE2_NAME;
287         } else if (session_id == common.voice[VOC_PATH_FULL].session_id) {
288                 session_name = VOIP_SESSION_NAME;
289         }
290         return session_name;
291 }
292
293 uint32_t voc_get_session_id(char *name)
294 {
295         u32 session_id = 0;
296
297         if (name != NULL) {
298                 if (!strncmp(name, "Voice session", 13))
299                         session_id = common.voice[VOC_PATH_PASSIVE].session_id;
300                 else if (!strncmp(name, "Voice2 session", 14))
301                         session_id =
302                         common.voice[VOC_PATH_VOICE2_PASSIVE].session_id;
303                 else if (!strncmp(name, "VoLTE session", 13))
304                         session_id =
305                         common.voice[VOC_PATH_VOLTE_PASSIVE].session_id;
306                 else if (!strncmp(name, "QCHAT session", 13))
307                         session_id =
308                         common.voice[VOC_PATH_QCHAT_PASSIVE].session_id;
309                 else if (!strncmp(name, "VoWLAN session", 14))
310                         session_id =
311                         common.voice[VOC_PATH_VOWLAN_PASSIVE].session_id;
312                 else if (!strcmp(name, "VoiceMMode1"))
313                         session_id =
314                         common.voice[VOC_PATH_VOICEMMODE1_PASSIVE].session_id;
315                 else if (!strcmp(name, "VoiceMMode2"))
316                         session_id =
317                         common.voice[VOC_PATH_VOICEMMODE2_PASSIVE].session_id;
318                 else
319                         session_id = common.voice[VOC_PATH_FULL].session_id;
320
321                 pr_debug("%s: %s has session id 0x%x\n", __func__, name,
322                                 session_id);
323         }
324
325         return session_id;
326 }
327
328 static struct voice_data *voice_get_session(u32 session_id)
329 {
330         struct voice_data *v = NULL;
331
332         switch (session_id) {
333         case VOICE_SESSION_VSID:
334                 v = &common.voice[VOC_PATH_PASSIVE];
335                 break;
336
337         case VOICE2_SESSION_VSID:
338                 v = &common.voice[VOC_PATH_VOICE2_PASSIVE];
339                 break;
340
341         case VOLTE_SESSION_VSID:
342                 v = &common.voice[VOC_PATH_VOLTE_PASSIVE];
343                 break;
344
345         case VOIP_SESSION_VSID:
346                 v = &common.voice[VOC_PATH_FULL];
347                 break;
348
349         case QCHAT_SESSION_VSID:
350                 v = &common.voice[VOC_PATH_QCHAT_PASSIVE];
351                 break;
352
353         case VOWLAN_SESSION_VSID:
354                 v = &common.voice[VOC_PATH_VOWLAN_PASSIVE];
355                 break;
356
357         case VOICEMMODE1_VSID:
358                 v = &common.voice[VOC_PATH_VOICEMMODE1_PASSIVE];
359                 break;
360
361         case VOICEMMODE2_VSID:
362                 v = &common.voice[VOC_PATH_VOICEMMODE2_PASSIVE];
363                 break;
364
365         case ALL_SESSION_VSID:
366                 break;
367
368         default:
369                 pr_err("%s: Invalid session_id : %x\n", __func__, session_id);
370
371                 break;
372         }
373
374         pr_debug("%s:session_id 0x%x session handle %pK\n",
375                 __func__, session_id, v);
376
377         return v;
378 }
379
380 int voice_get_idx_for_session(u32 session_id)
381 {
382         int idx = 0;
383
384         switch (session_id) {
385         case VOICE_SESSION_VSID:
386                 idx = VOC_PATH_PASSIVE;
387                 break;
388
389         case VOICE2_SESSION_VSID:
390                 idx = VOC_PATH_VOICE2_PASSIVE;
391                 break;
392
393         case VOLTE_SESSION_VSID:
394                 idx = VOC_PATH_VOLTE_PASSIVE;
395                 break;
396
397         case VOIP_SESSION_VSID:
398                 idx = VOC_PATH_FULL;
399                 break;
400
401         case QCHAT_SESSION_VSID:
402                 idx = VOC_PATH_QCHAT_PASSIVE;
403                 break;
404
405         case VOWLAN_SESSION_VSID:
406                 idx = VOC_PATH_VOWLAN_PASSIVE;
407                 break;
408
409         case VOICEMMODE1_VSID:
410                 idx = VOC_PATH_VOICEMMODE1_PASSIVE;
411                 break;
412
413         case VOICEMMODE2_VSID:
414                 idx = VOC_PATH_VOICEMMODE2_PASSIVE;
415                 break;
416
417         case ALL_SESSION_VSID:
418                 idx = MAX_VOC_SESSIONS - 1;
419                 break;
420
421         default:
422                 pr_err("%s: Invalid session_id : %x\n", __func__, session_id);
423
424                 break;
425         }
426
427         return idx;
428 }
429
430 static struct voice_data *voice_get_session_by_idx(int idx)
431 {
432         return ((idx < 0 || idx >= MAX_VOC_SESSIONS) ?
433                                 NULL : &common.voice[idx]);
434 }
435
436 static bool is_voip_session(u32 session_id)
437 {
438         return (session_id == common.voice[VOC_PATH_FULL].session_id);
439 }
440
441 static bool is_volte_session(u32 session_id)
442 {
443         return (session_id == common.voice[VOC_PATH_VOLTE_PASSIVE].session_id);
444 }
445
446 static bool is_voice2_session(u32 session_id)
447 {
448         return (session_id == common.voice[VOC_PATH_VOICE2_PASSIVE].session_id);
449 }
450
451 static bool is_qchat_session(u32 session_id)
452 {
453         return (session_id == common.voice[VOC_PATH_QCHAT_PASSIVE].session_id);
454 }
455
456 static bool is_vowlan_session(u32 session_id)
457 {
458         return (session_id == common.voice[VOC_PATH_VOWLAN_PASSIVE].session_id);
459 }
460
461 static bool is_voicemmode1(u32 session_id)
462 {
463         return session_id ==
464                         common.voice[VOC_PATH_VOICEMMODE1_PASSIVE].session_id;
465 }
466
467 static bool is_voicemmode2(u32 session_id)
468 {
469         return session_id ==
470                         common.voice[VOC_PATH_VOICEMMODE2_PASSIVE].session_id;
471 }
472
473 static bool is_voc_state_active(int voc_state)
474 {
475         if ((voc_state == VOC_RUN) ||
476                 (voc_state == VOC_CHANGE) ||
477                 (voc_state == VOC_STANDBY))
478                 return true;
479
480         return false;
481 }
482
483 static void voc_set_error_state(uint16_t reset_proc)
484 {
485         struct voice_data *v = NULL;
486         int i;
487
488         for (i = 0; i < MAX_VOC_SESSIONS; i++) {
489                 v = &common.voice[i];
490                 if (v != NULL) {
491                         v->voc_state = VOC_ERROR;
492                         v->rec_info.recording = 0;
493                 }
494         }
495 }
496
497 static bool is_other_session_active(u32 session_id)
498 {
499         int i;
500         bool ret = false;
501
502         /* Check if there is other active session except the input one */
503         for (i = 0; i < MAX_VOC_SESSIONS; i++) {
504                 if (common.voice[i].session_id == session_id)
505                         continue;
506
507                 if (is_voc_state_active(common.voice[i].voc_state)) {
508                         ret = true;
509                         break;
510                 }
511         }
512         pr_debug("%s: ret %d\n", __func__, ret);
513
514         return ret;
515 }
516
517 static bool is_sub1_vsid(u32 session_id)
518 {
519         bool ret;
520
521         switch (session_id) {
522         case VOICE_SESSION_VSID:
523         case VOLTE_SESSION_VSID:
524         case VOWLAN_SESSION_VSID:
525         case VOICEMMODE1_VSID:
526                 ret = true;
527                 break;
528         default:
529                 ret = false;
530         }
531
532         return ret;
533 }
534
535 static bool is_sub2_vsid(u32 session_id)
536 {
537         bool ret;
538
539         switch (session_id) {
540         case VOICE2_SESSION_VSID:
541         case VOICEMMODE2_VSID:
542                 ret = true;
543                 break;
544         default:
545                 ret = false;
546         }
547
548         return ret;
549 }
550
551 static bool is_voice_app_id(u32 session_id)
552 {
553         return is_sub1_vsid(session_id) || is_sub2_vsid(session_id);
554 }
555
556 static void init_session_id(void)
557 {
558         common.voice[VOC_PATH_PASSIVE].session_id = VOICE_SESSION_VSID;
559         common.voice[VOC_PATH_VOLTE_PASSIVE].session_id = VOLTE_SESSION_VSID;
560         common.voice[VOC_PATH_VOICE2_PASSIVE].session_id = VOICE2_SESSION_VSID;
561         common.voice[VOC_PATH_FULL].session_id = VOIP_SESSION_VSID;
562         common.voice[VOC_PATH_QCHAT_PASSIVE].session_id = QCHAT_SESSION_VSID;
563         common.voice[VOC_PATH_VOWLAN_PASSIVE].session_id = VOWLAN_SESSION_VSID;
564         common.voice[VOC_PATH_VOICEMMODE1_PASSIVE].session_id =
565                                                         VOICEMMODE1_VSID;
566         common.voice[VOC_PATH_VOICEMMODE2_PASSIVE].session_id =
567                                                         VOICEMMODE2_VSID;
568 }
569
570 static bool is_cvd_version_queried(void)
571 {
572         bool ret = 0;
573
574         if (!strcmp(common.cvd_version, CVD_VERSION_DEFAULT))
575                 ret = false;
576         else
577                 ret = true;
578
579         return ret;
580 }
581
582 static int voice_get_cvd_int_version(char *cvd_ver_string)
583 {
584         unsigned int idx;
585         int cvd_int_ver = CVD_INT_VERSION_DEFAULT;
586
587         for (idx = 0; idx < CVD_INT_VERSION_MAX; idx++) {
588                 if (strcmp((char *)cvd_ver_string,
589                           cvd_version_table_mapping[idx].cvd_ver) == 0) {
590                         cvd_int_ver =
591                         cvd_version_table_mapping[idx].cvd_ver_int;
592                         break;
593                 }
594         }
595         return cvd_int_ver;
596 }
597
598 static int voice_apr_register(uint32_t session_id)
599 {
600
601         pr_debug("%s\n", __func__);
602
603         mutex_lock(&common.common_lock);
604
605         /* register callback to APR */
606         if (common.apr_q6_mvm == NULL) {
607                 pr_debug("%s: Start to register MVM callback\n", __func__);
608
609                 common.apr_q6_mvm = apr_register("ADSP", "MVM",
610                                                  qdsp_mvm_callback,
611                                                  0xFFFFFFFF, &common);
612
613                 if (common.apr_q6_mvm == NULL) {
614                         pr_err("%s: Unable to register MVM\n", __func__);
615                         goto err;
616                 }
617         }
618
619         if (common.apr_q6_cvs == NULL) {
620                 pr_debug("%s: Start to register CVS callback\n", __func__);
621
622                 common.apr_q6_cvs = apr_register("ADSP", "CVS",
623                                                  qdsp_cvs_callback,
624                                                  0xFFFFFFFF, &common);
625
626                 if (common.apr_q6_cvs == NULL) {
627                         pr_err("%s: Unable to register CVS\n", __func__);
628                         goto err;
629                 }
630                 rtac_set_voice_handle(RTAC_CVS, common.apr_q6_cvs);
631         }
632
633         if (common.apr_q6_cvp == NULL) {
634                 pr_debug("%s: Start to register CVP callback\n", __func__);
635
636                 common.apr_q6_cvp = apr_register("ADSP", "CVP",
637                                                  qdsp_cvp_callback,
638                                                  0xFFFFFFFF, &common);
639
640                 if (common.apr_q6_cvp == NULL) {
641                         pr_err("%s: Unable to register CVP\n", __func__);
642                         goto err;
643                 }
644                 rtac_set_voice_handle(RTAC_CVP, common.apr_q6_cvp);
645         }
646
647         mutex_unlock(&common.common_lock);
648
649         return 0;
650
651 err:
652         if (common.apr_q6_cvs != NULL) {
653                 apr_deregister(common.apr_q6_cvs);
654                 common.apr_q6_cvs = NULL;
655                 rtac_set_voice_handle(RTAC_CVS, NULL);
656         }
657         if (common.apr_q6_mvm != NULL) {
658                 apr_deregister(common.apr_q6_mvm);
659                 common.apr_q6_mvm = NULL;
660         }
661
662         mutex_unlock(&common.common_lock);
663
664         return -ENODEV;
665 }
666
667 static int voice_send_mvm_cvd_version_cmd(struct voice_data *v)
668 {
669         int ret;
670         struct apr_hdr cvd_version_get_cmd;
671         void *apr_mvm;
672
673         if (v == NULL) {
674                 pr_err("%s: v is NULL\n", __func__);
675
676                 ret = -EINVAL;
677                 goto done;
678         }
679
680         apr_mvm = common.apr_q6_mvm;
681         if (!apr_mvm) {
682                 pr_err("%s: apr_mvm is NULL.\n", __func__);
683
684                 ret = -EINVAL;
685                 goto done;
686         }
687
688         /* Send command to CVD to retrieve Version */
689         cvd_version_get_cmd.hdr_field = APR_HDR_FIELD(
690                                 APR_MSG_TYPE_SEQ_CMD,
691                                 APR_HDR_LEN(APR_HDR_SIZE),
692                                 APR_PKT_VER);
693         cvd_version_get_cmd.pkt_size = APR_PKT_SIZE(
694                                 APR_HDR_SIZE,
695                                 sizeof(cvd_version_get_cmd) -
696                                 APR_HDR_SIZE);
697         cvd_version_get_cmd.src_port =
698                 voice_get_idx_for_session(v->session_id);
699         cvd_version_get_cmd.dest_port = 0;
700         cvd_version_get_cmd.token = 0;
701         cvd_version_get_cmd.opcode = VSS_IVERSION_CMD_GET;
702
703         pr_debug("%s: send CVD version get cmd, pkt size = %d\n",
704                  __func__, cvd_version_get_cmd.pkt_size);
705
706         v->mvm_state = CMD_STATUS_FAIL;
707         v->async_err = 0;
708         ret = apr_send_pkt(apr_mvm,
709                            (uint32_t *) &cvd_version_get_cmd);
710         if (ret < 0) {
711                 pr_err("%s: Error sending command\n", __func__);
712
713                 ret = -EINVAL;
714                 goto done;
715         }
716
717         ret = wait_event_timeout(v->mvm_wait,
718                         (v->mvm_state == CMD_STATUS_SUCCESS),
719                         msecs_to_jiffies(TIMEOUT_MS));
720         if (!ret) {
721                 pr_err("%s: wait_event timeout, fall back to default\n",
722                        __func__);
723
724                 ret = -EINVAL;
725                 goto done;
726         }
727         if (v->async_err > 0) {
728                 pr_err("%s: DSP returned error[%s]\n",
729                         __func__, adsp_err_get_err_str(
730                         v->async_err));
731                 ret = adsp_err_get_lnx_err_code(
732                         v->async_err);
733                 goto done;
734         }
735         ret = 0;
736
737 done:
738         if (ret) {
739                 strlcpy(common.cvd_version, CVD_VERSION_0_0,
740                                 sizeof(common.cvd_version));
741         }
742         pr_debug("%s: CVD Version retrieved=%s\n",
743                  __func__, common.cvd_version);
744
745         return ret;
746 }
747
748 static int voice_send_dual_control_cmd(struct voice_data *v)
749 {
750         int ret = 0;
751         struct mvm_modem_dual_control_session_cmd mvm_voice_ctl_cmd;
752         void *apr_mvm;
753         u16 mvm_handle;
754
755         if (v == NULL) {
756                 pr_err("%s: v is NULL\n", __func__);
757                 return -EINVAL;
758         }
759         apr_mvm = common.apr_q6_mvm;
760         if (!apr_mvm) {
761                 pr_err("%s: apr_mvm is NULL.\n", __func__);
762                 return -EINVAL;
763         }
764         pr_debug("%s: Send Dual Control command to MVM\n", __func__);
765         if (!is_voip_session(v->session_id)) {
766                 mvm_handle = voice_get_mvm_handle(v);
767                 mvm_voice_ctl_cmd.hdr.hdr_field = APR_HDR_FIELD(
768                                                 APR_MSG_TYPE_SEQ_CMD,
769                                                 APR_HDR_LEN(APR_HDR_SIZE),
770                                                 APR_PKT_VER);
771                 mvm_voice_ctl_cmd.hdr.pkt_size = APR_PKT_SIZE(
772                                                 APR_HDR_SIZE,
773                                                 sizeof(mvm_voice_ctl_cmd) -
774                                                 APR_HDR_SIZE);
775                 pr_debug("%s: send mvm Voice Ctl pkt size = %d\n",
776                         __func__, mvm_voice_ctl_cmd.hdr.pkt_size);
777                 mvm_voice_ctl_cmd.hdr.src_port =
778                                 voice_get_idx_for_session(v->session_id);
779                 mvm_voice_ctl_cmd.hdr.dest_port = mvm_handle;
780                 mvm_voice_ctl_cmd.hdr.token = 0;
781                 mvm_voice_ctl_cmd.hdr.opcode =
782                                         VSS_IMVM_CMD_SET_POLICY_DUAL_CONTROL;
783                 mvm_voice_ctl_cmd.voice_ctl.enable_flag = true;
784                 v->mvm_state = CMD_STATUS_FAIL;
785                 v->async_err = 0;
786
787                 ret = apr_send_pkt(apr_mvm, (uint32_t *) &mvm_voice_ctl_cmd);
788                 if (ret < 0) {
789                         pr_err("%s: Error sending MVM Voice CTL CMD\n",
790                                                         __func__);
791                         ret = -EINVAL;
792                         goto fail;
793                 }
794                 ret = wait_event_timeout(v->mvm_wait,
795                                 (v->mvm_state == CMD_STATUS_SUCCESS),
796                                 msecs_to_jiffies(TIMEOUT_MS));
797                 if (!ret) {
798                         pr_err("%s: wait_event timeout\n", __func__);
799                         ret = -EINVAL;
800                         goto fail;
801                 }
802                 if (v->async_err > 0) {
803                         pr_err("%s: DSP returned error[%s]\n",
804                                 __func__, adsp_err_get_err_str(
805                                 v->async_err));
806                         ret = adsp_err_get_lnx_err_code(
807                                         v->async_err);
808                         goto fail;
809                 }
810         }
811         ret = 0;
812 fail:
813         return ret;
814 }
815
816
817 static int voice_create_mvm_cvs_session(struct voice_data *v)
818 {
819         int ret = 0;
820         struct mvm_create_ctl_session_cmd mvm_session_cmd;
821         struct cvs_create_passive_ctl_session_cmd cvs_session_cmd;
822         struct cvs_create_full_ctl_session_cmd cvs_full_ctl_cmd;
823         struct mvm_attach_stream_cmd attach_stream_cmd;
824         void *apr_mvm, *apr_cvs, *apr_cvp;
825         u16 mvm_handle, cvs_handle, cvp_handle;
826
827         if (v == NULL) {
828                 pr_err("%s: v is NULL\n", __func__);
829                 return -EINVAL;
830         }
831         apr_mvm = common.apr_q6_mvm;
832         apr_cvs = common.apr_q6_cvs;
833         apr_cvp = common.apr_q6_cvp;
834
835         if (!apr_mvm || !apr_cvs || !apr_cvp) {
836                 pr_err("%s: apr_mvm or apr_cvs or apr_cvp is NULL\n", __func__);
837                 return -EINVAL;
838         }
839         mvm_handle = voice_get_mvm_handle(v);
840         cvs_handle = voice_get_cvs_handle(v);
841         cvp_handle = voice_get_cvp_handle(v);
842
843         pr_debug("%s: mvm_hdl=%d, cvs_hdl=%d\n", __func__,
844                 mvm_handle, cvs_handle);
845         /* send cmd to create mvm session and wait for response */
846
847         if (!mvm_handle) {
848                 memset(mvm_session_cmd.mvm_session.name, 0,
849                         sizeof(mvm_session_cmd.mvm_session.name));
850                 if (!is_voip_session(v->session_id)) {
851                         mvm_session_cmd.hdr.hdr_field = APR_HDR_FIELD(
852                                                 APR_MSG_TYPE_SEQ_CMD,
853                                                 APR_HDR_LEN(APR_HDR_SIZE),
854                                                 APR_PKT_VER);
855                         mvm_session_cmd.hdr.pkt_size = APR_PKT_SIZE(
856                                                 APR_HDR_SIZE,
857                                                 sizeof(mvm_session_cmd) -
858                                                 APR_HDR_SIZE);
859                         pr_debug("%s: send mvm create session pkt size = %d\n",
860                                  __func__, mvm_session_cmd.hdr.pkt_size);
861                         mvm_session_cmd.hdr.src_port =
862                                 voice_get_idx_for_session(v->session_id);
863                         mvm_session_cmd.hdr.dest_port = 0;
864                         mvm_session_cmd.hdr.token = 0;
865                         mvm_session_cmd.hdr.opcode =
866                                 VSS_IMVM_CMD_CREATE_PASSIVE_CONTROL_SESSION;
867                         if (is_volte_session(v->session_id)) {
868                                 strlcpy(mvm_session_cmd.mvm_session.name,
869                                 "default volte voice",
870                                 strlen("default volte voice")+1);
871                         } else if (is_voice2_session(v->session_id)) {
872                                 strlcpy(mvm_session_cmd.mvm_session.name,
873                                 VOICE2_SESSION_VSID_STR,
874                                 strlen(VOICE2_SESSION_VSID_STR)+1);
875                         } else if (is_qchat_session(v->session_id)) {
876                                 strlcpy(mvm_session_cmd.mvm_session.name,
877                                 QCHAT_SESSION_VSID_STR,
878                                 strlen(QCHAT_SESSION_VSID_STR)+1);
879                         } else if (is_vowlan_session(v->session_id)) {
880                                 strlcpy(mvm_session_cmd.mvm_session.name,
881                                 VOWLAN_SESSION_VSID_STR,
882                                 strlen(VOWLAN_SESSION_VSID_STR)+1);
883                         } else if (is_voicemmode1(v->session_id)) {
884                                 strlcpy(mvm_session_cmd.mvm_session.name,
885                                 VOICEMMODE1_VSID_STR,
886                                 strlen(VOICEMMODE1_VSID_STR) + 1);
887                         } else if (is_voicemmode2(v->session_id)) {
888                                 strlcpy(mvm_session_cmd.mvm_session.name,
889                                 VOICEMMODE2_VSID_STR,
890                                 strlen(VOICEMMODE2_VSID_STR) + 1);
891                         } else {
892                                 strlcpy(mvm_session_cmd.mvm_session.name,
893                                 "default modem voice",
894                                 strlen("default modem voice")+1);
895                         }
896
897                         v->mvm_state = CMD_STATUS_FAIL;
898                         v->async_err = 0;
899
900                         ret = apr_send_pkt(apr_mvm,
901                                         (uint32_t *) &mvm_session_cmd);
902                         if (ret < 0) {
903                                 pr_err("%s: Error sending MVM_CONTROL_SESSION\n",
904                                        __func__);
905                                 goto fail;
906                         }
907                         ret = wait_event_timeout(v->mvm_wait,
908                                         (v->mvm_state == CMD_STATUS_SUCCESS),
909                                         msecs_to_jiffies(TIMEOUT_MS));
910                         if (!ret) {
911                                 pr_err("%s: wait_event timeout\n", __func__);
912                                 goto fail;
913                         }
914                         if (v->async_err > 0) {
915                                 pr_err("%s: DSP returned error[%s]\n",
916                                         __func__, adsp_err_get_err_str(
917                                         v->async_err));
918                                 ret = adsp_err_get_lnx_err_code(
919                                                 v->async_err);
920                                 goto fail;
921                         }
922                 } else {
923                         pr_debug("%s: creating MVM full ctrl\n", __func__);
924                         mvm_session_cmd.hdr.hdr_field =
925                                         APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
926                                         APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
927                         mvm_session_cmd.hdr.pkt_size =
928                                         APR_PKT_SIZE(APR_HDR_SIZE,
929                                         sizeof(mvm_session_cmd) -
930                                         APR_HDR_SIZE);
931                         mvm_session_cmd.hdr.src_port =
932                                 voice_get_idx_for_session(v->session_id);
933                         mvm_session_cmd.hdr.dest_port = 0;
934                         mvm_session_cmd.hdr.token = 0;
935                         mvm_session_cmd.hdr.opcode =
936                                 VSS_IMVM_CMD_CREATE_FULL_CONTROL_SESSION;
937                         strlcpy(mvm_session_cmd.mvm_session.name,
938                                 "default voip",
939                                 strlen("default voip")+1);
940
941                         v->mvm_state = CMD_STATUS_FAIL;
942                         v->async_err = 0;
943
944                         ret = apr_send_pkt(apr_mvm,
945                                         (uint32_t *) &mvm_session_cmd);
946                         if (ret < 0) {
947                                 pr_err("Fail in sending MVM_CONTROL_SESSION\n");
948                                 goto fail;
949                         }
950                         ret = wait_event_timeout(v->mvm_wait,
951                                          (v->mvm_state == CMD_STATUS_SUCCESS),
952                                          msecs_to_jiffies(TIMEOUT_MS));
953                         if (!ret) {
954                                 pr_err("%s: wait_event timeout\n", __func__);
955                                 goto fail;
956                         }
957                         if (v->async_err > 0) {
958                                 pr_err("%s: DSP returned error[%s]\n",
959                                         __func__, adsp_err_get_err_str(
960                                         v->async_err));
961                                 ret = adsp_err_get_lnx_err_code(
962                                                 v->async_err);
963                                 goto fail;
964                         }
965                 }
966                 /* Get the created MVM handle. */
967                 mvm_handle = voice_get_mvm_handle(v);
968         }
969         /* send cmd to create cvs session */
970         if (!cvs_handle) {
971                 memset(cvs_session_cmd.cvs_session.name, 0,
972                         sizeof(cvs_session_cmd.cvs_session.name));
973                 if (!is_voip_session(v->session_id)) {
974                         pr_debug("%s: creating CVS passive session\n",
975                                  __func__);
976
977                         cvs_session_cmd.hdr.hdr_field = APR_HDR_FIELD(
978                                                 APR_MSG_TYPE_SEQ_CMD,
979                                                 APR_HDR_LEN(APR_HDR_SIZE),
980                                                 APR_PKT_VER);
981                         cvs_session_cmd.hdr.pkt_size =
982                                                 APR_PKT_SIZE(APR_HDR_SIZE,
983                                                 sizeof(cvs_session_cmd) -
984                                                 APR_HDR_SIZE);
985                         cvs_session_cmd.hdr.src_port =
986                                 voice_get_idx_for_session(v->session_id);
987                         cvs_session_cmd.hdr.dest_port = 0;
988                         cvs_session_cmd.hdr.token = 0;
989                         cvs_session_cmd.hdr.opcode =
990                                 VSS_ISTREAM_CMD_CREATE_PASSIVE_CONTROL_SESSION;
991                         if (is_volte_session(v->session_id)) {
992                                 strlcpy(cvs_session_cmd.cvs_session.name,
993                                 "default volte voice",
994                                 strlen("default volte voice")+1);
995                         } else if (is_voice2_session(v->session_id)) {
996                                 strlcpy(cvs_session_cmd.cvs_session.name,
997                                 VOICE2_SESSION_VSID_STR,
998                                 strlen(VOICE2_SESSION_VSID_STR)+1);
999                         } else if (is_qchat_session(v->session_id)) {
1000                                 strlcpy(cvs_session_cmd.cvs_session.name,
1001                                 QCHAT_SESSION_VSID_STR,
1002                                 strlen(QCHAT_SESSION_VSID_STR)+1);
1003                         } else if (is_vowlan_session(v->session_id)) {
1004                                 strlcpy(cvs_session_cmd.cvs_session.name,
1005                                 VOWLAN_SESSION_VSID_STR,
1006                                 strlen(VOWLAN_SESSION_VSID_STR)+1);
1007                         } else if (is_voicemmode1(v->session_id)) {
1008                                 strlcpy(cvs_session_cmd.cvs_session.name,
1009                                 VOICEMMODE1_VSID_STR,
1010                                 strlen(VOICEMMODE1_VSID_STR) + 1);
1011                         } else if (is_voicemmode2(v->session_id)) {
1012                                 strlcpy(cvs_session_cmd.cvs_session.name,
1013                                 VOICEMMODE2_VSID_STR,
1014                                 strlen(VOICEMMODE2_VSID_STR) + 1);
1015                         } else {
1016                         strlcpy(cvs_session_cmd.cvs_session.name,
1017                                 "default modem voice",
1018                                 strlen("default modem voice")+1);
1019                         }
1020                         v->cvs_state = CMD_STATUS_FAIL;
1021                         v->async_err = 0;
1022
1023                         ret = apr_send_pkt(apr_cvs,
1024                                         (uint32_t *) &cvs_session_cmd);
1025                         if (ret < 0) {
1026                                 pr_err("Fail in sending STREAM_CONTROL_SESSION\n");
1027                                 goto fail;
1028                         }
1029                         ret = wait_event_timeout(v->cvs_wait,
1030                                          (v->cvs_state == CMD_STATUS_SUCCESS),
1031                                          msecs_to_jiffies(TIMEOUT_MS));
1032                         if (!ret) {
1033                                 pr_err("%s: wait_event timeout\n", __func__);
1034                                 goto fail;
1035                         }
1036                         if (v->async_err > 0) {
1037                                 pr_err("%s: DSP returned error[%s]\n",
1038                                         __func__, adsp_err_get_err_str(
1039                                         v->async_err));
1040                                 ret = adsp_err_get_lnx_err_code(
1041                                                 v->async_err);
1042                                 goto fail;
1043                         }
1044                         /* Get the created CVS handle. */
1045                         cvs_handle = voice_get_cvs_handle(v);
1046
1047                 } else {
1048                         pr_debug("%s: creating CVS full session\n", __func__);
1049
1050                         cvs_full_ctl_cmd.hdr.hdr_field =
1051                                         APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
1052                                         APR_HDR_LEN(APR_HDR_SIZE),
1053                                         APR_PKT_VER);
1054
1055                         cvs_full_ctl_cmd.hdr.pkt_size =
1056                                         APR_PKT_SIZE(APR_HDR_SIZE,
1057                                         sizeof(cvs_full_ctl_cmd) -
1058                                         APR_HDR_SIZE);
1059
1060                         cvs_full_ctl_cmd.hdr.src_port =
1061                                 voice_get_idx_for_session(v->session_id);
1062                         cvs_full_ctl_cmd.hdr.dest_port = 0;
1063                         cvs_full_ctl_cmd.hdr.token = 0;
1064                         cvs_full_ctl_cmd.hdr.opcode =
1065                                 VSS_ISTREAM_CMD_CREATE_FULL_CONTROL_SESSION;
1066                         cvs_full_ctl_cmd.cvs_session.direction = 2;
1067                         cvs_full_ctl_cmd.cvs_session.enc_media_type =
1068                                                 common.mvs_info.media_type;
1069                         cvs_full_ctl_cmd.cvs_session.dec_media_type =
1070                                                 common.mvs_info.media_type;
1071                         cvs_full_ctl_cmd.cvs_session.network_id =
1072                                                common.mvs_info.network_type;
1073                         strlcpy(cvs_full_ctl_cmd.cvs_session.name,
1074                                 "default q6 voice",
1075                                 strlen("default q6 voice")+1);
1076
1077                         v->cvs_state = CMD_STATUS_FAIL;
1078                         v->async_err = 0;
1079
1080                         ret = apr_send_pkt(apr_cvs,
1081                                            (uint32_t *) &cvs_full_ctl_cmd);
1082
1083                         if (ret < 0) {
1084                                 pr_err("%s: Err %d sending CREATE_FULL_CTRL\n",
1085                                         __func__, ret);
1086                                 goto fail;
1087                         }
1088                         ret = wait_event_timeout(v->cvs_wait,
1089                                         (v->cvs_state == CMD_STATUS_SUCCESS),
1090                                         msecs_to_jiffies(TIMEOUT_MS));
1091                         if (!ret) {
1092                                 pr_err("%s: wait_event timeout\n", __func__);
1093                                 goto fail;
1094                         }
1095                         if (v->async_err > 0) {
1096                                 pr_err("%s: DSP returned error[%s]\n",
1097                                         __func__, adsp_err_get_err_str(
1098                                         v->async_err));
1099                                 ret = adsp_err_get_lnx_err_code(
1100                                                 v->async_err);
1101                                 goto fail;
1102                         }
1103                         /* Get the created CVS handle. */
1104                         cvs_handle = voice_get_cvs_handle(v);
1105
1106                         /* Attach MVM to CVS. */
1107                         pr_debug("%s: Attach MVM to stream\n", __func__);
1108
1109                         attach_stream_cmd.hdr.hdr_field =
1110                                         APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
1111                                         APR_HDR_LEN(APR_HDR_SIZE),
1112                                         APR_PKT_VER);
1113                         attach_stream_cmd.hdr.pkt_size =
1114                                         APR_PKT_SIZE(APR_HDR_SIZE,
1115                                         sizeof(attach_stream_cmd) -
1116                                         APR_HDR_SIZE);
1117                         attach_stream_cmd.hdr.src_port =
1118                                 voice_get_idx_for_session(v->session_id);
1119                         attach_stream_cmd.hdr.dest_port = mvm_handle;
1120                         attach_stream_cmd.hdr.token = 0;
1121                         attach_stream_cmd.hdr.opcode =
1122                                                 VSS_IMVM_CMD_ATTACH_STREAM;
1123                         attach_stream_cmd.attach_stream.handle = cvs_handle;
1124
1125                         v->mvm_state = CMD_STATUS_FAIL;
1126                         v->async_err = 0;
1127                         ret = apr_send_pkt(apr_mvm,
1128                                            (uint32_t *) &attach_stream_cmd);
1129                         if (ret < 0) {
1130                                 pr_err("%s: Error %d sending ATTACH_STREAM\n",
1131                                        __func__, ret);
1132                                 goto fail;
1133                         }
1134                         ret = wait_event_timeout(v->mvm_wait,
1135                                          (v->mvm_state == CMD_STATUS_SUCCESS),
1136                                          msecs_to_jiffies(TIMEOUT_MS));
1137                         if (!ret) {
1138                                 pr_err("%s: wait_event timeout\n", __func__);
1139                                 goto fail;
1140                         }
1141                         if (v->async_err > 0) {
1142                                 pr_err("%s: DSP returned error[%s]\n",
1143                                         __func__, adsp_err_get_err_str(
1144                                         v->async_err));
1145                                 ret = adsp_err_get_lnx_err_code(
1146                                                 v->async_err);
1147                                 goto fail;
1148                         }
1149                 }
1150         }
1151         return 0;
1152
1153 fail:
1154         return ret;
1155 }
1156
1157 static int voice_unmap_cal_block(struct voice_data *v, int cal_index)
1158 {
1159         int result = 0;
1160         struct cal_block_data *cal_block;
1161
1162         if (common.cal_data[cal_index] == NULL) {
1163                 pr_err("%s: Cal type is NULL, index %d!\n",
1164                         __func__, cal_index);
1165
1166                 goto done;
1167         }
1168
1169         mutex_lock(&common.cal_data[cal_index]->lock);
1170         cal_block = cal_utils_get_only_cal_block(
1171                 common.cal_data[cal_index]);
1172         if (cal_block == NULL) {
1173                 pr_err("%s: Cal block is NULL, index %d!\n",
1174                         __func__, cal_index);
1175
1176                 result = -EINVAL;
1177                 goto unlock;
1178         }
1179
1180         if (cal_block->map_data.q6map_handle == 0) {
1181                 pr_debug("%s: Q6 handle is not set!\n", __func__);
1182
1183                 result = -EINVAL;
1184                 goto unlock;
1185         }
1186
1187         mutex_lock(&common.common_lock);
1188         result = voice_send_mvm_unmap_memory_physical_cmd(
1189                 v, cal_block->map_data.q6map_handle);
1190         if (result)
1191                 pr_err("%s: Voice_send_mvm_unmap_memory_physical_cmd failed for session 0x%x, err %d!\n",
1192                         __func__, v->session_id, result);
1193
1194         cal_block->map_data.q6map_handle = 0;
1195         mutex_unlock(&common.common_lock);
1196 unlock:
1197         mutex_unlock(&common.cal_data[cal_index]->lock);
1198 done:
1199         return result;
1200 }
1201
1202 static int voice_destroy_mvm_cvs_session(struct voice_data *v)
1203 {
1204         int ret = 0;
1205         struct mvm_detach_stream_cmd detach_stream;
1206         struct apr_hdr mvm_destroy;
1207         struct apr_hdr cvs_destroy;
1208         void *apr_mvm, *apr_cvs;
1209         u16 mvm_handle, cvs_handle;
1210
1211         if (v == NULL) {
1212                 pr_err("%s: v is NULL\n", __func__);
1213                 return -EINVAL;
1214         }
1215         apr_mvm = common.apr_q6_mvm;
1216         apr_cvs = common.apr_q6_cvs;
1217
1218         if (!apr_mvm || !apr_cvs) {
1219                 pr_err("%s: apr_mvm or apr_cvs is NULL\n", __func__);
1220                 return -EINVAL;
1221         }
1222         mvm_handle = voice_get_mvm_handle(v);
1223         cvs_handle = voice_get_cvs_handle(v);
1224
1225         /* MVM, CVS sessions are destroyed only for Full control sessions. */
1226         if (is_voip_session(v->session_id)) {
1227                 pr_debug("%s: MVM detach stream, VOC_STATE: %d\n", __func__,
1228                                 v->voc_state);
1229
1230                 /* Detach voice stream. */
1231                 detach_stream.hdr.hdr_field =
1232                                         APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
1233                                         APR_HDR_LEN(APR_HDR_SIZE),
1234                                         APR_PKT_VER);
1235                 detach_stream.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
1236                                         sizeof(detach_stream) - APR_HDR_SIZE);
1237                 detach_stream.hdr.src_port =
1238                                 voice_get_idx_for_session(v->session_id);
1239                 detach_stream.hdr.dest_port = mvm_handle;
1240                 detach_stream.hdr.token = 0;
1241                 detach_stream.hdr.opcode = VSS_IMVM_CMD_DETACH_STREAM;
1242                 detach_stream.detach_stream.handle = cvs_handle;
1243
1244                 v->mvm_state = CMD_STATUS_FAIL;
1245                 v->async_err = 0;
1246                 ret = apr_send_pkt(apr_mvm, (uint32_t *) &detach_stream);
1247                 if (ret < 0) {
1248                         pr_err("%s: Error %d sending DETACH_STREAM\n",
1249                                __func__, ret);
1250
1251                         goto fail;
1252                 }
1253                 ret = wait_event_timeout(v->mvm_wait,
1254                                          (v->mvm_state == CMD_STATUS_SUCCESS),
1255                                          msecs_to_jiffies(TIMEOUT_MS));
1256                 if (!ret) {
1257                         pr_err("%s: wait event timeout\n", __func__);
1258
1259                         goto fail;
1260                 }
1261                 if (v->async_err > 0) {
1262                         pr_err("%s: DSP returned error[%s]\n",
1263                                 __func__, adsp_err_get_err_str(
1264                                 v->async_err));
1265                         ret = adsp_err_get_lnx_err_code(
1266                                         v->async_err);
1267                         goto fail;
1268                 }
1269
1270                 /* Unmap memory */
1271                 if (v->shmem_info.mem_handle != 0) {
1272                         ret = voice_send_mvm_unmap_memory_physical_cmd(v,
1273                                                 v->shmem_info.mem_handle);
1274                         if (ret < 0) {
1275                                 pr_err("%s Memory_unmap for voip failed %d\n",
1276                                        __func__, ret);
1277
1278                                 goto fail;
1279                         }
1280                         v->shmem_info.mem_handle = 0;
1281                 }
1282         }
1283
1284         /* Unmap Source Tracking shared memory if mapped earlier */
1285         voice_unmap_and_free_source_tracking_shared_memory(v);
1286
1287         if (is_voip_session(v->session_id) ||
1288             is_qchat_session(v->session_id) ||
1289             is_volte_session(v->session_id) ||
1290             is_vowlan_session(v->session_id) ||
1291             v->voc_state == VOC_ERROR || common.is_destroy_cvd) {
1292                 /* Destroy CVS. */
1293                 pr_debug("%s: CVS destroy session\n", __func__);
1294
1295                 cvs_destroy.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
1296                                                       APR_HDR_LEN(APR_HDR_SIZE),
1297                                                       APR_PKT_VER);
1298                 cvs_destroy.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
1299                                         sizeof(cvs_destroy) - APR_HDR_SIZE);
1300                 cvs_destroy.src_port =
1301                                 voice_get_idx_for_session(v->session_id);
1302                 cvs_destroy.dest_port = cvs_handle;
1303                 cvs_destroy.token = 0;
1304                 cvs_destroy.opcode = APRV2_IBASIC_CMD_DESTROY_SESSION;
1305
1306                 v->cvs_state = CMD_STATUS_FAIL;
1307                 v->async_err = 0;
1308                 ret = apr_send_pkt(apr_cvs, (uint32_t *) &cvs_destroy);
1309                 if (ret < 0) {
1310                         pr_err("%s: Error %d sending CVS DESTROY\n",
1311                                __func__, ret);
1312
1313                         goto fail;
1314                 }
1315                 ret = wait_event_timeout(v->cvs_wait,
1316                                          (v->cvs_state == CMD_STATUS_SUCCESS),
1317                                          msecs_to_jiffies(TIMEOUT_MS));
1318                 if (!ret) {
1319                         pr_err("%s: wait event timeout\n", __func__);
1320
1321                         goto fail;
1322                 }
1323                 if (v->async_err > 0) {
1324                         pr_err("%s: DSP returned error[%s]\n",
1325                                 __func__, adsp_err_get_err_str(
1326                                 v->async_err));
1327                         ret = adsp_err_get_lnx_err_code(
1328                                         v->async_err);
1329                         goto fail;
1330                 }
1331                 cvs_handle = 0;
1332                 voice_set_cvs_handle(v, cvs_handle);
1333
1334                 /* Unmap physical memory for all calibration buffers */
1335                 if (!is_other_session_active(v->session_id)) {
1336                         if (voice_unmap_cal_block(v, CVP_VOCPROC_CAL))
1337                                 pr_err("%s: Unmap VOCPROC cal failed\n",
1338                                         __func__);
1339                         if (voice_unmap_cal_block(v, CVP_VOCVOL_CAL))
1340                                 pr_err("%s: Unmap VOCVOL cal failed\n",
1341                                         __func__);
1342                         if (voice_unmap_cal_block(v, CVP_VOCDEV_CFG_CAL))
1343                                 pr_err("%s: Unmap VOCDEV_CFG cal failed\n",
1344                                         __func__);
1345                         if (voice_unmap_cal_block(v, CVS_VOCSTRM_CAL))
1346                                 pr_err("%s: Unmap VOCSTRM cal failed\n",
1347                                         __func__);
1348                 }
1349
1350                 /* Destroy MVM. */
1351                 pr_debug("%s: MVM destroy session\n", __func__);
1352
1353                 mvm_destroy.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
1354                                                       APR_HDR_LEN(APR_HDR_SIZE),
1355                                                       APR_PKT_VER);
1356                 mvm_destroy.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
1357                                             sizeof(mvm_destroy) - APR_HDR_SIZE);
1358                 mvm_destroy.src_port =
1359                                 voice_get_idx_for_session(v->session_id);
1360                 mvm_destroy.dest_port = mvm_handle;
1361                 mvm_destroy.token = 0;
1362                 mvm_destroy.opcode = APRV2_IBASIC_CMD_DESTROY_SESSION;
1363
1364                 v->mvm_state = CMD_STATUS_FAIL;
1365                 v->async_err = 0;
1366
1367                 ret = apr_send_pkt(apr_mvm, (uint32_t *) &mvm_destroy);
1368                 if (ret < 0) {
1369                         pr_err("%s: Error %d sending MVM DESTROY\n",
1370                                __func__, ret);
1371
1372                         goto fail;
1373                 }
1374                 ret = wait_event_timeout(v->mvm_wait,
1375                                          (v->mvm_state == CMD_STATUS_SUCCESS),
1376                                          msecs_to_jiffies(TIMEOUT_MS));
1377                 if (!ret) {
1378                         pr_err("%s: wait event timeout\n", __func__);
1379                         goto fail;
1380                 }
1381                 if (v->async_err > 0) {
1382                         pr_err("%s: DSP returned error[%s]\n",
1383                                 __func__, adsp_err_get_err_str(
1384                                 v->async_err));
1385                         ret = adsp_err_get_lnx_err_code(
1386                                         v->async_err);
1387                         goto fail;
1388                 }
1389                 mvm_handle = 0;
1390                 voice_set_mvm_handle(v, mvm_handle);
1391         }
1392         return 0;
1393 fail:
1394         return ret;
1395 }
1396
1397 static int voice_send_tty_mode_cmd(struct voice_data *v)
1398 {
1399         int ret = 0;
1400         struct mvm_set_tty_mode_cmd mvm_tty_mode_cmd;
1401         void *apr_mvm;
1402         u16 mvm_handle;
1403
1404         if (v == NULL) {
1405                 pr_err("%s: v is NULL\n", __func__);
1406                 return -EINVAL;
1407         }
1408         apr_mvm = common.apr_q6_mvm;
1409
1410         if (!apr_mvm) {
1411                 pr_err("%s: apr_mvm is NULL.\n", __func__);
1412                 return -EINVAL;
1413         }
1414         mvm_handle = voice_get_mvm_handle(v);
1415
1416         /* send tty mode cmd to mvm */
1417         mvm_tty_mode_cmd.hdr.hdr_field = APR_HDR_FIELD(
1418                                         APR_MSG_TYPE_SEQ_CMD,
1419                                         APR_HDR_LEN(APR_HDR_SIZE),
1420                                         APR_PKT_VER);
1421         mvm_tty_mode_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
1422                                         sizeof(mvm_tty_mode_cmd) -
1423                                         APR_HDR_SIZE);
1424         pr_debug("%s: pkt size = %d\n",
1425                  __func__, mvm_tty_mode_cmd.hdr.pkt_size);
1426         mvm_tty_mode_cmd.hdr.src_port =
1427                         voice_get_idx_for_session(v->session_id);
1428         mvm_tty_mode_cmd.hdr.dest_port = mvm_handle;
1429         mvm_tty_mode_cmd.hdr.token = 0;
1430         mvm_tty_mode_cmd.hdr.opcode = VSS_ISTREAM_CMD_SET_TTY_MODE;
1431         mvm_tty_mode_cmd.tty_mode.mode = v->tty_mode;
1432         pr_debug("tty mode =%d\n", mvm_tty_mode_cmd.tty_mode.mode);
1433
1434         v->mvm_state = CMD_STATUS_FAIL;
1435         v->async_err = 0;
1436         ret = apr_send_pkt(apr_mvm, (uint32_t *) &mvm_tty_mode_cmd);
1437         if (ret < 0) {
1438                 pr_err("%s: Error %d sending SET_TTY_MODE\n",
1439                        __func__, ret);
1440                 goto fail;
1441         }
1442         ret = wait_event_timeout(v->mvm_wait,
1443                                  (v->mvm_state == CMD_STATUS_SUCCESS),
1444                                  msecs_to_jiffies(TIMEOUT_MS));
1445         if (!ret) {
1446                 pr_err("%s: wait_event timeout\n", __func__);
1447                 goto fail;
1448         }
1449         if (v->async_err > 0) {
1450                 pr_err("%s: DSP returned error[%s]\n",
1451                                 __func__, adsp_err_get_err_str(
1452                                 v->async_err));
1453                 ret = adsp_err_get_lnx_err_code(
1454                                 v->async_err);
1455                 goto fail;
1456         }
1457         return 0;
1458 fail:
1459         return ret;
1460 }
1461
1462 static int voice_send_set_pp_enable_cmd(
1463         struct voice_data *v, struct module_instance_info mod_inst_info,
1464         int enable)
1465 {
1466         struct enable_param enable_param = {0};
1467         struct param_hdr_v3 param_hdr = {0};
1468         int ret = 0;
1469
1470         param_hdr.module_id = mod_inst_info.module_id;
1471         param_hdr.instance_id = mod_inst_info.instance_id;
1472         param_hdr.param_id = VOICE_PARAM_MOD_ENABLE;
1473         param_hdr.param_size = sizeof(enable_param);
1474         enable_param.enable = enable ? 1 : 0;
1475
1476         pr_debug("%s: voice_send_set_pp_enable_cmd, module_id=%d, instance_id=%d, enable=%d\n",
1477                 __func__, mod_inst_info.module_id, mod_inst_info.instance_id,
1478                 enable);
1479
1480         ret = voice_pack_and_set_cvs_ui_property(v, param_hdr,
1481                                                  (uint8_t *) &enable_param);
1482         if (ret < 0)
1483                 pr_err("Fail: sending cvs set pp enable\n");
1484
1485         return ret;
1486 }
1487
1488 static int voice_send_hd_cmd(struct voice_data *v, int enable)
1489 {
1490         struct mvm_set_hd_enable_cmd mvm_set_hd_cmd;
1491         int ret = 0;
1492         void *apr_mvm;
1493         u16 mvm_handle;
1494
1495         if (v == NULL) {
1496                 pr_err("%s: v is NULL\n", __func__);
1497
1498                 ret = -EINVAL;
1499                 goto done;
1500         }
1501
1502         apr_mvm = common.apr_q6_mvm;
1503         if (!apr_mvm) {
1504                 pr_err("%s: apr_mvm is NULL.\n", __func__);
1505
1506                 ret = -EINVAL;
1507                 goto done;
1508         }
1509
1510         mvm_handle = voice_get_mvm_handle(v);
1511         if (!mvm_handle) {
1512                 pr_err("%s: mvm_handle is NULL\n", __func__);
1513
1514                 ret = -EINVAL;
1515                 goto done;
1516         }
1517
1518         mvm_set_hd_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
1519                                                      APR_HDR_LEN(APR_HDR_SIZE),
1520                                                      APR_PKT_VER);
1521         mvm_set_hd_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
1522                                                    sizeof(mvm_set_hd_cmd) -
1523                                                    APR_HDR_SIZE);
1524         mvm_set_hd_cmd.hdr.src_port = voice_get_idx_for_session(v->session_id);
1525         mvm_set_hd_cmd.hdr.dest_port = mvm_handle;
1526         mvm_set_hd_cmd.hdr.token = 0;
1527
1528         if (enable)
1529                 mvm_set_hd_cmd.hdr.opcode = VSS_IHDVOICE_CMD_ENABLE;
1530         else
1531                 mvm_set_hd_cmd.hdr.opcode = VSS_IHDVOICE_CMD_DISABLE;
1532
1533         pr_debug("%s: enable=%d\n", __func__, enable);
1534
1535         v->mvm_state = CMD_STATUS_FAIL;
1536         v->async_err = 0;
1537         ret = apr_send_pkt(apr_mvm, (uint32_t *) &mvm_set_hd_cmd);
1538         if (ret < 0) {
1539                 pr_err("%s: Failed to sending mvm set HD Voice enable %d\n",
1540                        __func__, ret);
1541
1542                 ret = -EINVAL;
1543                 goto done;
1544         }
1545
1546         ret = wait_event_timeout(v->mvm_wait,
1547                                  (v->mvm_state == CMD_STATUS_SUCCESS),
1548                                  msecs_to_jiffies(TIMEOUT_MS));
1549         if (!ret) {
1550                 pr_err("%s: wait_event timeout\n", __func__);
1551
1552                 ret = -EINVAL;
1553                 goto done;
1554         }
1555         if (v->async_err > 0) {
1556                 pr_err("%s: DSP returned error[%s]\n",
1557                                 __func__, adsp_err_get_err_str(
1558                                 v->async_err));
1559                 ret = adsp_err_get_lnx_err_code(
1560                                 v->async_err);
1561                 goto done;
1562         }
1563
1564 done:
1565         return ret;
1566 }
1567
1568 static int voice_set_dtx(struct voice_data *v)
1569 {
1570         int ret = 0;
1571         void *apr_cvs;
1572         u16 cvs_handle;
1573         struct cvs_set_enc_dtx_mode_cmd cvs_set_dtx;
1574
1575         if (v == NULL) {
1576                 pr_err("%s: v is NULL\n", __func__);
1577                 return -EINVAL;
1578         }
1579         apr_cvs = common.apr_q6_cvs;
1580
1581         if (!apr_cvs) {
1582                 pr_err("%s: apr_cvs is NULL.\n", __func__);
1583                 return -EINVAL;
1584         }
1585
1586         cvs_handle = voice_get_cvs_handle(v);
1587
1588         /* Set DTX */
1589         cvs_set_dtx.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
1590                                               APR_HDR_LEN(APR_HDR_SIZE),
1591                                               APR_PKT_VER);
1592         cvs_set_dtx.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
1593                                         sizeof(cvs_set_dtx) - APR_HDR_SIZE);
1594         cvs_set_dtx.hdr.src_port =
1595                                 voice_get_idx_for_session(v->session_id);
1596         cvs_set_dtx.hdr.dest_port = cvs_handle;
1597         cvs_set_dtx.hdr.token = 0;
1598         cvs_set_dtx.hdr.opcode = VSS_ISTREAM_CMD_SET_ENC_DTX_MODE;
1599         cvs_set_dtx.dtx_mode.enable = common.mvs_info.dtx_mode;
1600
1601         pr_debug("%s: Setting DTX %d\n", __func__, common.mvs_info.dtx_mode);
1602
1603         v->cvs_state = CMD_STATUS_FAIL;
1604         v->async_err = 0;
1605
1606         ret = apr_send_pkt(apr_cvs, (uint32_t *) &cvs_set_dtx);
1607         if (ret < 0) {
1608                 pr_err("%s: Error %d sending SET_DTX\n", __func__, ret);
1609                 return -EINVAL;
1610         }
1611
1612         ret = wait_event_timeout(v->cvs_wait,
1613                                  (v->cvs_state == CMD_STATUS_SUCCESS),
1614                                  msecs_to_jiffies(TIMEOUT_MS));
1615         if (!ret) {
1616                 pr_err("%s: wait_event timeout\n", __func__);
1617                 return -EINVAL;
1618         }
1619         if (v->async_err > 0) {
1620                 pr_err("%s: DSP returned error[%s]\n",
1621                                 __func__, adsp_err_get_err_str(
1622                                 v->async_err));
1623                 ret = adsp_err_get_lnx_err_code(
1624                                 v->async_err);
1625                 return ret;
1626         }
1627
1628         return 0;
1629 }
1630
1631 static int voice_send_mvm_media_type_cmd(struct voice_data *v)
1632 {
1633         struct vss_imvm_cmd_set_cal_media_type_t mvm_set_cal_media_type;
1634         int ret = 0;
1635         void *apr_mvm;
1636         u16 mvm_handle;
1637
1638         if (v == NULL) {
1639                 pr_err("%s: v is NULL\n", __func__);
1640                 return -EINVAL;
1641         }
1642         apr_mvm = common.apr_q6_mvm;
1643
1644         if (!apr_mvm) {
1645                 pr_err("%s: apr_mvm is NULL.\n", __func__);
1646                 return -EINVAL;
1647         }
1648         mvm_handle = voice_get_mvm_handle(v);
1649
1650         mvm_set_cal_media_type.hdr.hdr_field =
1651                                         APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
1652                                         APR_HDR_LEN(APR_HDR_SIZE),
1653                                         APR_PKT_VER);
1654         mvm_set_cal_media_type.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
1655                                         sizeof(mvm_set_cal_media_type) -
1656                                         APR_HDR_SIZE);
1657         mvm_set_cal_media_type.hdr.src_port =
1658                                 voice_get_idx_for_session(v->session_id);
1659         mvm_set_cal_media_type.hdr.dest_port = mvm_handle;
1660         mvm_set_cal_media_type.hdr.token = 0;
1661         mvm_set_cal_media_type.hdr.opcode = VSS_IMVM_CMD_SET_CAL_MEDIA_TYPE;
1662         mvm_set_cal_media_type.media_id = common.mvs_info.media_type;
1663         pr_debug("%s: setting media_id as %x\n",
1664                  __func__ , mvm_set_cal_media_type.media_id);
1665
1666         v->mvm_state = CMD_STATUS_FAIL;
1667         v->async_err = 0;
1668         ret = apr_send_pkt(apr_mvm, (uint32_t *) &mvm_set_cal_media_type);
1669         if (ret < 0) {
1670                 pr_err("%s: Error %d sending media type\n", __func__, ret);
1671                 goto fail;
1672         }
1673
1674         ret = wait_event_timeout(v->mvm_wait,
1675                                 (v->mvm_state == CMD_STATUS_SUCCESS),
1676                                  msecs_to_jiffies(TIMEOUT_MS));
1677         if (!ret) {
1678                 pr_err("%s: wait_event timeout %d\n", __func__, ret);
1679                 goto fail;
1680         }
1681         if (v->async_err > 0) {
1682                 pr_err("%s: DSP returned error[%s]\n",
1683                                 __func__, adsp_err_get_err_str(
1684                                 v->async_err));
1685                 ret = adsp_err_get_lnx_err_code(
1686                                 v->async_err);
1687                 goto fail;
1688         }
1689         return 0;
1690 fail:
1691         return ret;
1692 }
1693
1694 static int voice_send_dtmf_rx_detection_cmd(struct voice_data *v,
1695                                             uint32_t enable)
1696 {
1697         int ret = 0;
1698         void *apr_cvs;
1699         u16 cvs_handle;
1700         struct cvs_set_rx_dtmf_detection_cmd cvs_dtmf_rx_detection;
1701
1702         if (v == NULL) {
1703                 pr_err("%s: v is NULL\n", __func__);
1704                 return -EINVAL;
1705         }
1706         apr_cvs = common.apr_q6_cvs;
1707
1708         if (!apr_cvs) {
1709                 pr_err("%s: apr_cvs is NULL.\n", __func__);
1710                 return -EINVAL;
1711         }
1712
1713         cvs_handle = voice_get_cvs_handle(v);
1714
1715         /* Set SET_DTMF_RX_DETECTION */
1716         cvs_dtmf_rx_detection.hdr.hdr_field =
1717                                 APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
1718                                               APR_HDR_LEN(APR_HDR_SIZE),
1719                                               APR_PKT_VER);
1720         cvs_dtmf_rx_detection.hdr.pkt_size =
1721                                 APR_PKT_SIZE(APR_HDR_SIZE,
1722                                 sizeof(cvs_dtmf_rx_detection) - APR_HDR_SIZE);
1723         cvs_dtmf_rx_detection.hdr.src_port =
1724                                 voice_get_idx_for_session(v->session_id);
1725         cvs_dtmf_rx_detection.hdr.dest_port = cvs_handle;
1726         cvs_dtmf_rx_detection.hdr.token = 0;
1727         cvs_dtmf_rx_detection.hdr.opcode =
1728                                         VSS_ISTREAM_CMD_SET_RX_DTMF_DETECTION;
1729         cvs_dtmf_rx_detection.cvs_dtmf_det.enable = enable;
1730
1731         v->cvs_state = CMD_STATUS_FAIL;
1732         v->async_err = 0;
1733
1734         ret = apr_send_pkt(apr_cvs, (uint32_t *) &cvs_dtmf_rx_detection);
1735         if (ret < 0) {
1736                 pr_err("%s: Error %d sending SET_DTMF_RX_DETECTION\n",
1737                        __func__,
1738                        ret);
1739                 return -EINVAL;
1740         }
1741
1742         ret = wait_event_timeout(v->cvs_wait,
1743                                  (v->cvs_state == CMD_STATUS_SUCCESS),
1744                                  msecs_to_jiffies(TIMEOUT_MS));
1745
1746         if (!ret) {
1747                 pr_err("%s: wait_event timeout\n", __func__);
1748                 return -EINVAL;
1749         }
1750         if (v->async_err > 0) {
1751                 pr_err("%s: DSP returned error[%s]\n",
1752                                 __func__, adsp_err_get_err_str(
1753                                 v->async_err));
1754                 ret = adsp_err_get_lnx_err_code(
1755                                 v->async_err);
1756                 return ret;
1757         }
1758
1759         return ret;
1760 }
1761
1762 static void voice_vote_powerstate_to_bms(struct voice_data *v, bool state)
1763 {
1764         union power_supply_propval pval = {0, };
1765
1766         if (!v->psy)
1767                 v->psy = power_supply_get_by_name("bms");
1768         if (v->psy && !(is_voip_session(v->session_id) ||
1769                         is_vowlan_session(v->session_id))) {
1770                 pval.intval = VMBMS_VOICE_CALL_BIT;
1771                 if (state) {
1772                         power_supply_set_property(v->psy,
1773                                 POWER_SUPPLY_PROP_HI_POWER,
1774                                 &pval);
1775                         pr_debug("%s : Vote High power to BMS\n",
1776                                 __func__);
1777                 } else {
1778                         power_supply_set_property(v->psy,
1779                                 POWER_SUPPLY_PROP_LOW_POWER,
1780                                 &pval);
1781                         pr_debug("%s: Vote low power to BMS\n",
1782                                 __func__);
1783                 }
1784         } else {
1785                 pr_debug("%s: No OP", __func__);
1786         }
1787
1788 }
1789
1790 void voc_disable_dtmf_det_on_active_sessions(void)
1791 {
1792         struct voice_data *v = NULL;
1793         int i;
1794         for (i = 0; i < MAX_VOC_SESSIONS; i++) {
1795                 v = &common.voice[i];
1796                 if ((v->dtmf_rx_detect_en) &&
1797                         is_voc_state_active(v->voc_state)) {
1798
1799                         pr_debug("disable dtmf det on ses_id=%d\n",
1800                                  v->session_id);
1801                         voice_send_dtmf_rx_detection_cmd(v, 0);
1802                 }
1803         }
1804 }
1805
1806 int voc_enable_dtmf_rx_detection(uint32_t session_id, uint32_t enable)
1807 {
1808         struct voice_data *v = voice_get_session(session_id);
1809         int ret = 0;
1810
1811         if (v == NULL) {
1812                 pr_err("%s: invalid session_id 0x%x\n", __func__, session_id);
1813                 return -EINVAL;
1814         }
1815
1816         mutex_lock(&v->lock);
1817         v->dtmf_rx_detect_en = enable;
1818
1819         if (is_voc_state_active(v->voc_state))
1820                 ret = voice_send_dtmf_rx_detection_cmd(v,
1821                                                        v->dtmf_rx_detect_en);
1822
1823         mutex_unlock(&v->lock);
1824
1825         return ret;
1826 }
1827
1828 void voc_set_destroy_cvd_flag(bool is_destroy_cvd)
1829 {
1830         pr_debug("%s: %d\n", __func__, is_destroy_cvd);
1831         common.is_destroy_cvd = is_destroy_cvd;
1832 }
1833
1834 void voc_set_vote_bms_flag(bool is_vote_bms)
1835 {
1836         pr_debug("%s: flag value: %d\n", __func__, is_vote_bms);
1837         common.is_vote_bms = is_vote_bms;
1838 }
1839
1840 int voc_alloc_cal_shared_memory(void)
1841 {
1842         int rc = 0;
1843
1844         mutex_lock(&common.common_lock);
1845         if (is_cal_memory_allocated()) {
1846                 pr_debug("%s: Calibration shared buffer already allocated",
1847                          __func__);
1848         } else {
1849                 /* Allocate memory for calibration memory map table. */
1850                 rc = voice_alloc_cal_mem_map_table();
1851                 if ((rc < 0) && (rc != -EPROBE_DEFER)) {
1852                         pr_err("%s: Failed to allocate cal memory, err=%d",
1853                                __func__, rc);
1854                 }
1855         }
1856         mutex_unlock(&common.common_lock);
1857
1858         return rc;
1859 }
1860
1861 int voc_alloc_voip_shared_memory(void)
1862 {
1863         int rc = 0;
1864
1865         /* Allocate shared memory for OOB Voip */
1866         rc = voice_alloc_oob_shared_mem();
1867         if (rc < 0) {
1868                 pr_err("%s: Failed to alloc shared memory for OOB rc:%d\n",
1869                            __func__, rc);
1870         } else {
1871                 /* Allocate mem map table for OOB */
1872                 rc = voice_alloc_oob_mem_table();
1873                 if (rc < 0) {
1874                         pr_err("%s: Failed to alloc mem map talbe rc:%d\n",
1875                                __func__, rc);
1876
1877                         voice_free_oob_shared_mem();
1878                 }
1879         }
1880
1881         return rc;
1882 }
1883
1884 static int is_cal_memory_allocated(void)
1885 {
1886         bool ret;
1887
1888         if (common.cal_mem_map_table.client != NULL &&
1889             common.cal_mem_map_table.handle != NULL)
1890                 ret = true;
1891         else
1892                 ret = false;
1893
1894         return ret;
1895 }
1896
1897
1898 static int free_cal_map_table(void)
1899 {
1900         int ret = 0;
1901
1902         if ((common.cal_mem_map_table.client == NULL) ||
1903                 (common.cal_mem_map_table.handle == NULL))
1904                 goto done;
1905
1906         ret = msm_audio_ion_free(common.cal_mem_map_table.client,
1907                 common.cal_mem_map_table.handle);
1908         if (ret < 0)
1909                 pr_err("%s: msm_audio_ion_free failed:\n", __func__);
1910
1911 done:
1912         common.cal_mem_map_table.client = NULL;
1913         common.cal_mem_map_table.handle = NULL;
1914         return ret;
1915 }
1916
1917 static int is_rtac_memory_allocated(void)
1918 {
1919         bool ret;
1920
1921         if (common.rtac_mem_map_table.client != NULL &&
1922             common.rtac_mem_map_table.handle != NULL)
1923                 ret = true;
1924         else
1925                 ret = false;
1926
1927         return ret;
1928 }
1929
1930 static int free_rtac_map_table(void)
1931 {
1932         int ret = 0;
1933
1934         if ((common.rtac_mem_map_table.client == NULL) ||
1935                 (common.rtac_mem_map_table.handle == NULL))
1936                 goto done;
1937
1938         ret = msm_audio_ion_free(common.rtac_mem_map_table.client,
1939                 common.rtac_mem_map_table.handle);
1940         if (ret < 0)
1941                 pr_err("%s: msm_audio_ion_free failed:\n", __func__);
1942
1943 done:
1944         common.rtac_mem_map_table.client = NULL;
1945         common.rtac_mem_map_table.handle = NULL;
1946         return ret;
1947 }
1948
1949
1950 static int is_voip_memory_allocated(void)
1951 {
1952         bool ret;
1953         struct voice_data *v = voice_get_session(
1954                                 common.voice[VOC_PATH_FULL].session_id);
1955
1956         if (v == NULL) {
1957                 pr_err("%s: v is NULL, session_id:%d\n", __func__,
1958                 common.voice[VOC_PATH_FULL].session_id);
1959
1960                 ret = false;
1961                 goto done;
1962         }
1963
1964         mutex_lock(&common.common_lock);
1965         if (v->shmem_info.sh_buf.client != NULL &&
1966             v->shmem_info.sh_buf.handle != NULL)
1967                 ret = true;
1968         else
1969                 ret = false;
1970         mutex_unlock(&common.common_lock);
1971
1972 done:
1973         return ret;
1974 }
1975
1976 static int voice_config_cvs_vocoder_amr_rate(struct voice_data *v)
1977 {
1978         int ret = 0;
1979         void *apr_cvs;
1980         u16 cvs_handle;
1981         struct cvs_set_amr_enc_rate_cmd cvs_set_amr_rate;
1982
1983         if (v == NULL) {
1984                 pr_err("%s: v is NULL\n", __func__);
1985
1986                 ret = -EINVAL;
1987                 goto done;
1988         }
1989         apr_cvs = common.apr_q6_cvs;
1990
1991         if (!apr_cvs) {
1992                 pr_err("%s: apr_cvs is NULL.\n", __func__);
1993
1994                 ret = -EINVAL;
1995                 goto done;
1996         }
1997
1998         cvs_handle = voice_get_cvs_handle(v);
1999
2000         pr_debug("%s: Setting AMR rate. Media Type: %d\n", __func__,
2001                  common.mvs_info.media_type);
2002
2003         cvs_set_amr_rate.hdr.hdr_field =
2004                         APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
2005                         APR_HDR_LEN(APR_HDR_SIZE),
2006                         APR_PKT_VER);
2007         cvs_set_amr_rate.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
2008                                sizeof(cvs_set_amr_rate) - APR_HDR_SIZE);
2009         cvs_set_amr_rate.hdr.src_port =
2010                         voice_get_idx_for_session(v->session_id);
2011         cvs_set_amr_rate.hdr.dest_port = cvs_handle;
2012         cvs_set_amr_rate.hdr.token = 0;
2013
2014         if (common.mvs_info.media_type == VSS_MEDIA_ID_AMR_NB_MODEM)
2015                 cvs_set_amr_rate.hdr.opcode =
2016                                 VSS_ISTREAM_CMD_VOC_AMR_SET_ENC_RATE;
2017         else if (common.mvs_info.media_type == VSS_MEDIA_ID_AMR_WB_MODEM)
2018                 cvs_set_amr_rate.hdr.opcode =
2019                                 VSS_ISTREAM_CMD_VOC_AMRWB_SET_ENC_RATE;
2020
2021         cvs_set_amr_rate.amr_rate.mode = common.mvs_info.rate;
2022
2023         v->cvs_state = CMD_STATUS_FAIL;
2024         v->async_err = 0;
2025
2026         ret = apr_send_pkt(apr_cvs, (uint32_t *) &cvs_set_amr_rate);
2027         if (ret < 0) {
2028                 pr_err("%s: Error %d sending SET_AMR_RATE\n",
2029                        __func__, ret);
2030
2031                 goto done;
2032         }
2033         ret = wait_event_timeout(v->cvs_wait,
2034                                  (v->cvs_state == CMD_STATUS_SUCCESS),
2035                                  msecs_to_jiffies(TIMEOUT_MS));
2036         if (!ret) {
2037                 pr_err("%s: wait_event timeout\n", __func__);
2038
2039                 ret = -EINVAL;
2040                 goto done;
2041         }
2042         if (v->async_err > 0) {
2043                 pr_err("%s: DSP returned error[%s]\n",
2044                                 __func__, adsp_err_get_err_str(
2045                                 v->async_err));
2046                 ret = adsp_err_get_lnx_err_code(
2047                                 v->async_err);
2048                 goto done;
2049         }
2050
2051         return 0;
2052 done:
2053         return ret;
2054 }
2055
2056 static int voice_config_cvs_vocoder(struct voice_data *v)
2057 {
2058         int ret = 0;
2059         void *apr_cvs;
2060         u16 cvs_handle;
2061         /* Set media type. */
2062         struct cvs_set_media_type_cmd cvs_set_media_cmd;
2063
2064         if (v == NULL) {
2065                 pr_err("%s: v is NULL\n", __func__);
2066                 return -EINVAL;
2067         }
2068         apr_cvs = common.apr_q6_cvs;
2069
2070         if (!apr_cvs) {
2071                 pr_err("%s: apr_cvs is NULL.\n", __func__);
2072                 return -EINVAL;
2073         }
2074
2075         cvs_handle = voice_get_cvs_handle(v);
2076
2077         cvs_set_media_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
2078                                                 APR_HDR_LEN(APR_HDR_SIZE),
2079                                                 APR_PKT_VER);
2080         cvs_set_media_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
2081                                                 sizeof(cvs_set_media_cmd) -
2082                                                 APR_HDR_SIZE);
2083         cvs_set_media_cmd.hdr.src_port =
2084                                 voice_get_idx_for_session(v->session_id);
2085         cvs_set_media_cmd.hdr.dest_port = cvs_handle;
2086         cvs_set_media_cmd.hdr.token = 0;
2087         cvs_set_media_cmd.hdr.opcode = VSS_ISTREAM_CMD_SET_MEDIA_TYPE;
2088         cvs_set_media_cmd.media_type.tx_media_id = common.mvs_info.media_type;
2089         cvs_set_media_cmd.media_type.rx_media_id = common.mvs_info.media_type;
2090
2091         v->cvs_state = CMD_STATUS_FAIL;
2092         v->async_err = 0;
2093
2094         ret = apr_send_pkt(apr_cvs, (uint32_t *) &cvs_set_media_cmd);
2095         if (ret < 0) {
2096                 pr_err("%s: Error %d sending SET_MEDIA_TYPE\n",
2097                         __func__, ret);
2098
2099                 goto fail;
2100         }
2101         ret = wait_event_timeout(v->cvs_wait,
2102                                  (v->cvs_state == CMD_STATUS_SUCCESS),
2103                                  msecs_to_jiffies(TIMEOUT_MS));
2104         if (!ret) {
2105                 pr_err("%s: wait_event timeout\n", __func__);
2106
2107                 goto fail;
2108         }
2109         if (v->async_err > 0) {
2110                 pr_err("%s: DSP returned error[%s]\n",
2111                                 __func__, adsp_err_get_err_str(
2112                                 v->async_err));
2113                 ret = adsp_err_get_lnx_err_code(
2114                                 v->async_err);
2115                 goto fail;
2116         }
2117         /* Set encoder properties. */
2118         switch (common.mvs_info.media_type) {
2119         case VSS_MEDIA_ID_EVRC_MODEM:
2120         case VSS_MEDIA_ID_4GV_NB_MODEM:
2121         case VSS_MEDIA_ID_4GV_WB_MODEM:
2122         case VSS_MEDIA_ID_4GV_NW_MODEM: {
2123                 struct cvs_set_cdma_enc_minmax_rate_cmd cvs_set_cdma_rate;
2124
2125                 pr_debug("Setting EVRC min-max rate\n");
2126
2127                 cvs_set_cdma_rate.hdr.hdr_field =
2128                                         APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
2129                                         APR_HDR_LEN(APR_HDR_SIZE),
2130                                         APR_PKT_VER);
2131                 cvs_set_cdma_rate.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
2132                                       sizeof(cvs_set_cdma_rate) - APR_HDR_SIZE);
2133                 cvs_set_cdma_rate.hdr.src_port =
2134                                 voice_get_idx_for_session(v->session_id);
2135                 cvs_set_cdma_rate.hdr.dest_port = cvs_handle;
2136                 cvs_set_cdma_rate.hdr.token = 0;
2137                 cvs_set_cdma_rate.hdr.opcode =
2138                                 VSS_ISTREAM_CMD_CDMA_SET_ENC_MINMAX_RATE;
2139                 cvs_set_cdma_rate.cdma_rate.min_rate =
2140                                 common.mvs_info.evrc_min_rate;
2141                 cvs_set_cdma_rate.cdma_rate.max_rate =
2142                                 common.mvs_info.evrc_max_rate;
2143
2144                 v->cvs_state = CMD_STATUS_FAIL;
2145                 v->async_err = 0;
2146
2147                 ret = apr_send_pkt(apr_cvs, (uint32_t *) &cvs_set_cdma_rate);
2148                 if (ret < 0) {
2149                         pr_err("%s: Error %d sending SET_EVRC_MINMAX_RATE\n",
2150                                __func__, ret);
2151                         goto fail;
2152                 }
2153                 ret = wait_event_timeout(v->cvs_wait,
2154                                          (v->cvs_state == CMD_STATUS_SUCCESS),
2155                                          msecs_to_jiffies(TIMEOUT_MS));
2156                 if (!ret) {
2157                         pr_err("%s: wait_event timeout\n", __func__);
2158
2159                         goto fail;
2160                 }
2161                 if (v->async_err > 0) {
2162                         pr_err("%s: DSP returned error[%s]\n",
2163                                 __func__, adsp_err_get_err_str(
2164                                 v->async_err));
2165                         ret = adsp_err_get_lnx_err_code(
2166                                         v->async_err);
2167                         goto fail;
2168                 }
2169
2170                 if (common.mvs_info.media_type != VSS_MEDIA_ID_EVRC_MODEM) {
2171                         ret = voice_set_dtx(v);
2172                         if (ret < 0)
2173                                 goto fail;
2174                 }
2175
2176                 break;
2177         }
2178         case VSS_MEDIA_ID_AMR_NB_MODEM:
2179         case VSS_MEDIA_ID_AMR_WB_MODEM: {
2180                 ret = voice_config_cvs_vocoder_amr_rate(v);
2181                 if (ret) {
2182                         pr_err("%s: Failed to update vocoder rate. %d\n",
2183                                __func__, ret);
2184
2185                         goto fail;
2186                 }
2187
2188                 ret = voice_set_dtx(v);
2189                 if (ret < 0)
2190                         goto fail;
2191
2192                 break;
2193         }
2194         case VSS_MEDIA_ID_G729:
2195         case VSS_MEDIA_ID_G711_ALAW:
2196         case VSS_MEDIA_ID_G711_MULAW: {
2197                 ret = voice_set_dtx(v);
2198
2199                 break;
2200         }
2201         default:
2202                 /* Do nothing. */
2203                 break;
2204         }
2205         return 0;
2206
2207 fail:
2208         return ret;
2209 }
2210
2211 int voc_update_amr_vocoder_rate(uint32_t session_id)
2212 {
2213         int ret = 0;
2214         struct voice_data *v;
2215
2216         pr_debug("%s: session_id:%d", __func__, session_id);
2217
2218         v = voice_get_session(session_id);
2219
2220         if (v == NULL) {
2221                 pr_err("%s: v is NULL, session_id:%d\n", __func__,
2222                        session_id);
2223
2224                 ret = -EINVAL;
2225                 goto done;
2226         }
2227
2228         mutex_lock(&v->lock);
2229         ret = voice_config_cvs_vocoder_amr_rate(v);
2230         mutex_unlock(&v->lock);
2231
2232 done:
2233         return ret;
2234 }
2235
2236 static int voice_send_start_voice_cmd(struct voice_data *v)
2237 {
2238         struct apr_hdr mvm_start_voice_cmd;
2239         int ret = 0;
2240         void *apr_mvm;
2241         u16 mvm_handle;
2242
2243         if (v == NULL) {
2244                 pr_err("%s: v is NULL\n", __func__);
2245                 return -EINVAL;
2246         }
2247         apr_mvm = common.apr_q6_mvm;
2248
2249         if (!apr_mvm) {
2250                 pr_err("%s: apr_mvm is NULL.\n", __func__);
2251                 return -EINVAL;
2252         }
2253         mvm_handle = voice_get_mvm_handle(v);
2254
2255         mvm_start_voice_cmd.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
2256                                                 APR_HDR_LEN(APR_HDR_SIZE),
2257                                                 APR_PKT_VER);
2258         mvm_start_voice_cmd.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
2259                                 sizeof(mvm_start_voice_cmd) - APR_HDR_SIZE);
2260         pr_debug("send mvm_start_voice_cmd pkt size = %d\n",
2261                                 mvm_start_voice_cmd.pkt_size);
2262         mvm_start_voice_cmd.src_port =
2263                                 voice_get_idx_for_session(v->session_id);
2264         mvm_start_voice_cmd.dest_port = mvm_handle;
2265         mvm_start_voice_cmd.token = 0;
2266         mvm_start_voice_cmd.opcode = VSS_IMVM_CMD_START_VOICE;
2267
2268         v->mvm_state = CMD_STATUS_FAIL;
2269         v->async_err = 0;
2270         ret = apr_send_pkt(apr_mvm, (uint32_t *) &mvm_start_voice_cmd);
2271         if (ret < 0) {
2272                 pr_err("Fail in sending VSS_IMVM_CMD_START_VOICE\n");
2273                 goto fail;
2274         }
2275         ret = wait_event_timeout(v->mvm_wait,
2276                                  (v->mvm_state == CMD_STATUS_SUCCESS),
2277                                  msecs_to_jiffies(TIMEOUT_MS));
2278         if (!ret) {
2279                 pr_err("%s: wait_event timeout\n", __func__);
2280                 goto fail;
2281         } else {
2282                 if (common.is_vote_bms) {
2283                         /* vote high power to BMS during call start */
2284                         voice_vote_powerstate_to_bms(v, true);
2285                 }
2286         }
2287         if (v->async_err > 0) {
2288                 pr_err("%s: DSP returned error[%s]\n",
2289                                 __func__, adsp_err_get_err_str(
2290                                 v->async_err));
2291                 ret = adsp_err_get_lnx_err_code(
2292                                 v->async_err);
2293                 goto fail;
2294         }
2295         return 0;
2296 fail:
2297         return ret;
2298 }
2299
2300 static void voc_get_tx_rx_topology(struct voice_data *v,
2301                                    uint32_t *tx_topology_id,
2302                                    uint32_t *rx_topology_id)
2303 {
2304         uint32_t tx_id = 0;
2305         uint32_t rx_id = 0;
2306
2307         if (v->lch_mode == VOICE_LCH_START || v->disable_topology) {
2308                 pr_debug("%s: Setting TX and RX topology to NONE for LCH\n",
2309                          __func__);
2310
2311                 tx_id = VSS_IVOCPROC_TOPOLOGY_ID_NONE;
2312                 rx_id = VSS_IVOCPROC_TOPOLOGY_ID_NONE;
2313         } else {
2314                 tx_id = voice_get_topology(CVP_VOC_TX_TOPOLOGY_CAL);
2315                 rx_id = voice_get_topology(CVP_VOC_RX_TOPOLOGY_CAL);
2316         }
2317
2318         *tx_topology_id = tx_id;
2319         *rx_topology_id = rx_id;
2320 }
2321
2322 static int voice_send_set_device_cmd(struct voice_data *v)
2323 {
2324         struct cvp_set_device_cmd  cvp_setdev_cmd;
2325         int ret = 0;
2326         void *apr_cvp;
2327         u16 cvp_handle;
2328
2329         if (v == NULL) {
2330                 pr_err("%s: v is NULL\n", __func__);
2331                 return -EINVAL;
2332         }
2333         apr_cvp = common.apr_q6_cvp;
2334
2335         if (!apr_cvp) {
2336                 pr_err("%s: apr_cvp is NULL.\n", __func__);
2337                 return -EINVAL;
2338         }
2339         cvp_handle = voice_get_cvp_handle(v);
2340
2341         /* set device and wait for response */
2342         cvp_setdev_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
2343                                                 APR_HDR_LEN(APR_HDR_SIZE),
2344                                                 APR_PKT_VER);
2345         cvp_setdev_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
2346                                 sizeof(cvp_setdev_cmd) - APR_HDR_SIZE);
2347         pr_debug(" send create cvp setdev, pkt size = %d\n",
2348                         cvp_setdev_cmd.hdr.pkt_size);
2349         cvp_setdev_cmd.hdr.src_port =
2350                                 voice_get_idx_for_session(v->session_id);
2351         cvp_setdev_cmd.hdr.dest_port = cvp_handle;
2352         cvp_setdev_cmd.hdr.token = 0;
2353
2354         if (voice_get_cvd_int_version(common.cvd_version) >=
2355             CVD_INT_VERSION_2_2)
2356                 cvp_setdev_cmd.hdr.opcode =
2357                                 VSS_IVOCPROC_CMD_SET_DEVICE_V3;
2358         else
2359                 cvp_setdev_cmd.hdr.opcode =
2360                                 VSS_IVOCPROC_CMD_SET_DEVICE_V2;
2361
2362         voc_get_tx_rx_topology(v,
2363                         &cvp_setdev_cmd.cvp_set_device_v2.tx_topology_id,
2364                         &cvp_setdev_cmd.cvp_set_device_v2.rx_topology_id);
2365
2366         cvp_setdev_cmd.cvp_set_device_v2.tx_port_id = v->dev_tx.port_id;
2367         cvp_setdev_cmd.cvp_set_device_v2.rx_port_id = v->dev_rx.port_id;
2368
2369         if (common.ec_ref_ext) {
2370                 cvp_setdev_cmd.cvp_set_device_v2.vocproc_mode =
2371                                 VSS_IVOCPROC_VOCPROC_MODE_EC_EXT_MIXING;
2372                 cvp_setdev_cmd.cvp_set_device_v2.ec_ref_port_id =
2373                                 common.ec_media_fmt_info.port_id;
2374         } else {
2375                 cvp_setdev_cmd.cvp_set_device_v2.vocproc_mode =
2376                                     VSS_IVOCPROC_VOCPROC_MODE_EC_INT_MIXING;
2377                 cvp_setdev_cmd.cvp_set_device_v2.ec_ref_port_id =
2378                                     VSS_IVOCPROC_PORT_ID_NONE;
2379         }
2380         pr_debug("topology=%d , tx_port_id=%d, rx_port_id=%d\n",
2381                 cvp_setdev_cmd.cvp_set_device_v2.tx_topology_id,
2382                 cvp_setdev_cmd.cvp_set_device_v2.tx_port_id,
2383                 cvp_setdev_cmd.cvp_set_device_v2.rx_port_id);
2384
2385         v->cvp_state = CMD_STATUS_FAIL;
2386         v->async_err = 0;
2387         ret = apr_send_pkt(apr_cvp, (uint32_t *) &cvp_setdev_cmd);
2388         if (ret < 0) {
2389                 pr_err("Fail in sending VSS_IVOCPROC_CMD_SET_DEVICE\n");
2390                 goto fail;
2391         }
2392
2393         ret = wait_event_timeout(v->cvp_wait,
2394                         (v->cvp_state == CMD_STATUS_SUCCESS),
2395                         msecs_to_jiffies(TIMEOUT_MS));
2396         if (!ret) {
2397                 pr_err("%s: wait_event timeout\n", __func__);
2398                 goto fail;
2399         }
2400         if (v->async_err > 0) {
2401                 pr_err("%s: DSP returned error[%s]\n",
2402                                 __func__, adsp_err_get_err_str(
2403                                 v->async_err));
2404                 ret = adsp_err_get_lnx_err_code(
2405                                 v->async_err);
2406                 goto fail;
2407         }
2408
2409         return 0;
2410 fail:
2411         return ret;
2412 }
2413
2414 static int voice_send_stop_voice_cmd(struct voice_data *v)
2415 {
2416         struct apr_hdr mvm_stop_voice_cmd;
2417         int ret = 0;
2418         void *apr_mvm;
2419         u16 mvm_handle;
2420
2421         if (v == NULL) {
2422                 pr_err("%s: v is NULL\n", __func__);
2423                 return -EINVAL;
2424         }
2425         apr_mvm = common.apr_q6_mvm;
2426
2427         if (!apr_mvm) {
2428                 pr_err("%s: apr_mvm is NULL.\n", __func__);
2429                 return -EINVAL;
2430         }
2431         mvm_handle = voice_get_mvm_handle(v);
2432
2433         mvm_stop_voice_cmd.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
2434                                                 APR_HDR_LEN(APR_HDR_SIZE),
2435                                                 APR_PKT_VER);
2436         mvm_stop_voice_cmd.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
2437                                 sizeof(mvm_stop_voice_cmd) - APR_HDR_SIZE);
2438         pr_debug("send mvm_stop_voice_cmd pkt size = %d\n",
2439                                 mvm_stop_voice_cmd.pkt_size);
2440         mvm_stop_voice_cmd.src_port =
2441                                 voice_get_idx_for_session(v->session_id);
2442         mvm_stop_voice_cmd.dest_port = mvm_handle;
2443         mvm_stop_voice_cmd.token = 0;
2444         mvm_stop_voice_cmd.opcode = VSS_IMVM_CMD_STOP_VOICE;
2445
2446         v->mvm_state = CMD_STATUS_FAIL;
2447         v->async_err = 0;
2448         ret = apr_send_pkt(apr_mvm, (uint32_t *) &mvm_stop_voice_cmd);
2449         if (ret < 0) {
2450                 pr_err("Fail in sending VSS_IMVM_CMD_STOP_VOICE\n");
2451                 goto fail;
2452         }
2453         ret = wait_event_timeout(v->mvm_wait,
2454                                  (v->mvm_state == CMD_STATUS_SUCCESS),
2455                                  msecs_to_jiffies(TIMEOUT_MS));
2456         if (!ret) {
2457                 pr_err("%s: wait_event timeout\n", __func__);
2458                 goto fail;
2459         }
2460         if (v->async_err > 0) {
2461                 pr_err("%s: DSP returned error[%s]\n",
2462                                 __func__, adsp_err_get_err_str(
2463                                 v->async_err));
2464                 ret = adsp_err_get_lnx_err_code(
2465                                 v->async_err);
2466                 goto fail;
2467         }
2468
2469         return 0;
2470 fail:
2471         return ret;
2472 }
2473 static int voice_get_cal(struct cal_block_data **cal_block,
2474                          int cal_block_idx,
2475                          struct cal_block_data **col_data,
2476                          int col_data_idx, int session_id)
2477 {
2478         int ret = 0;
2479
2480         *cal_block = cal_utils_get_only_cal_block(
2481                 common.cal_data[cal_block_idx]);
2482         if (*cal_block == NULL) {
2483                 pr_err("%s: No cal data for cal %d!\n",
2484                         __func__, cal_block_idx);
2485
2486                 ret = -ENODEV;
2487                 goto done;
2488         }
2489         ret = remap_cal_data(*cal_block, session_id);
2490         if (ret < 0) {
2491                 pr_err("%s: Remap_cal_data failed for cal %d!\n",
2492                         __func__, cal_block_idx);
2493
2494                 ret = -ENODEV;
2495                 goto done;
2496         }
2497
2498         if (col_data == NULL)
2499                 goto done;
2500
2501         *col_data = cal_utils_get_only_cal_block(
2502                 common.cal_data[col_data_idx]);
2503         if (*col_data == NULL) {
2504                 pr_err("%s: No cal data for cal %d!\n",
2505                         __func__, col_data_idx);
2506
2507                 ret = -ENODEV;
2508                 goto done;
2509         }
2510 done:
2511         return ret;
2512 }
2513
2514 static int voice_send_cvs_register_cal_cmd(struct voice_data *v)
2515 {
2516         struct cvs_register_cal_data_cmd cvs_reg_cal_cmd;
2517         struct cal_block_data *cal_block = NULL;
2518         struct cal_block_data *col_data = NULL;
2519         int ret = 0;
2520         memset(&cvs_reg_cal_cmd, 0, sizeof(cvs_reg_cal_cmd));
2521
2522         if (v == NULL) {
2523                 pr_err("%s: v is NULL\n", __func__);
2524
2525                 ret = -EINVAL;
2526                 goto done;
2527         }
2528
2529         if (!common.apr_q6_cvs) {
2530                 pr_err("%s: apr_cvs is NULL\n", __func__);
2531
2532                 ret = -EINVAL;
2533                 goto done;
2534         }
2535
2536         mutex_lock(&common.cal_data[CVS_VOCSTRM_CAL]->lock);
2537         mutex_lock(&common.cal_data[CVS_VOCSTRM_COL_CAL]->lock);
2538
2539         ret = voice_get_cal(&cal_block, CVS_VOCSTRM_CAL, &col_data,
2540                 CVS_VOCSTRM_COL_CAL, v->session_id);
2541         if (ret < 0) {
2542                 pr_err("%s: Voice_get_cal failed for cal %d!\n",
2543                         __func__, CVS_VOCSTRM_CAL);
2544
2545                 goto unlock;
2546         }
2547
2548         memcpy(&cvs_reg_cal_cmd.cvs_cal_data.column_info[0],
2549                (void *) &((struct audio_cal_info_voc_col *)
2550                col_data->cal_info)->data,
2551                col_data->cal_data.size);
2552
2553         cvs_reg_cal_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
2554                                 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
2555         cvs_reg_cal_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
2556                                 sizeof(cvs_reg_cal_cmd) - APR_HDR_SIZE);
2557         cvs_reg_cal_cmd.hdr.src_port =
2558                                 voice_get_idx_for_session(v->session_id);
2559         cvs_reg_cal_cmd.hdr.dest_port = voice_get_cvs_handle(v);
2560         cvs_reg_cal_cmd.hdr.token = 0;
2561         if (common.is_per_vocoder_cal_enabled)
2562                 cvs_reg_cal_cmd.hdr.opcode =
2563                         VSS_ISTREAM_CMD_REGISTER_STATIC_CALIBRATION_DATA;
2564         else
2565                 cvs_reg_cal_cmd.hdr.opcode =
2566                         VSS_ISTREAM_CMD_REGISTER_CALIBRATION_DATA_V2;
2567
2568         cvs_reg_cal_cmd.cvs_cal_data.cal_mem_handle =
2569                 cal_block->map_data.q6map_handle;
2570         cvs_reg_cal_cmd.cvs_cal_data.cal_mem_address_lsw =
2571                 lower_32_bits(cal_block->cal_data.paddr);
2572         cvs_reg_cal_cmd.cvs_cal_data.cal_mem_address_msw =
2573                 msm_audio_populate_upper_32_bits(cal_block->cal_data.paddr);
2574         cvs_reg_cal_cmd.cvs_cal_data.cal_mem_size =
2575                 cal_block->cal_data.size;
2576
2577         v->cvs_state = CMD_STATUS_FAIL;
2578         v->async_err = 0;
2579         ret = apr_send_pkt(common.apr_q6_cvs, (uint32_t *) &cvs_reg_cal_cmd);
2580         if (ret < 0) {
2581                 pr_err("%s: Error %d registering CVS cal\n", __func__, ret);
2582
2583                 ret = -EINVAL;
2584                 goto unlock;
2585         }
2586         ret = wait_event_timeout(v->cvs_wait,
2587                                  (v->cvs_state == CMD_STATUS_SUCCESS),
2588                                  msecs_to_jiffies(TIMEOUT_MS));
2589         if (!ret) {
2590                 pr_err("%s: Command timeout\n", __func__);
2591
2592                 ret = -EINVAL;
2593                 goto unlock;
2594         }
2595         if (v->async_err > 0) {
2596                 pr_err("%s: DSP returned error[%s]\n",
2597                                 __func__, adsp_err_get_err_str(
2598                                 v->async_err));
2599                 ret = adsp_err_get_lnx_err_code(
2600                                 v->async_err);
2601                 goto unlock;
2602         }
2603 unlock:
2604         mutex_unlock(&common.cal_data[CVS_VOCSTRM_COL_CAL]->lock);
2605         mutex_unlock(&common.cal_data[CVS_VOCSTRM_CAL]->lock);
2606 done:
2607         return ret;
2608 }
2609
2610 static int voice_send_cvs_deregister_cal_cmd(struct voice_data *v)
2611 {
2612         struct cvs_deregister_cal_data_cmd cvs_dereg_cal_cmd;
2613         int ret = 0;
2614         memset(&cvs_dereg_cal_cmd, 0, sizeof(cvs_dereg_cal_cmd));
2615
2616         if (v == NULL) {
2617                 pr_err("%s: v is NULL\n", __func__);
2618
2619                 ret = -EINVAL;
2620                 goto done;
2621         }
2622
2623         if (!common.apr_q6_cvs) {
2624                 pr_err("%s: apr_cvs is NULL\n", __func__);
2625
2626                 ret = -EPERM;
2627                 goto done;
2628         }
2629
2630         cvs_dereg_cal_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
2631                                 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
2632         cvs_dereg_cal_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
2633                                 sizeof(cvs_dereg_cal_cmd) - APR_HDR_SIZE);
2634         cvs_dereg_cal_cmd.hdr.src_port =
2635                                 voice_get_idx_for_session(v->session_id);
2636         cvs_dereg_cal_cmd.hdr.dest_port = voice_get_cvs_handle(v);
2637         cvs_dereg_cal_cmd.hdr.token = 0;
2638         if (common.is_per_vocoder_cal_enabled)
2639                 cvs_dereg_cal_cmd.hdr.opcode =
2640                         VSS_ISTREAM_CMD_DEREGISTER_STATIC_CALIBRATION_DATA;
2641         else
2642                 cvs_dereg_cal_cmd.hdr.opcode =
2643                         VSS_ISTREAM_CMD_DEREGISTER_CALIBRATION_DATA;
2644
2645         v->cvs_state = CMD_STATUS_FAIL;
2646         v->async_err = 0;
2647         ret = apr_send_pkt(common.apr_q6_cvs, (uint32_t *) &cvs_dereg_cal_cmd);
2648         if (ret < 0) {
2649                 pr_err("%s: Error %d de-registering CVS cal\n", __func__, ret);
2650                 goto done;
2651         }
2652         ret = wait_event_timeout(v->cvs_wait,
2653                                  (v->cvs_state == CMD_STATUS_SUCCESS),
2654                                  msecs_to_jiffies(TIMEOUT_MS));
2655         if (!ret) {
2656                 pr_err("%s: Command  timeout\n", __func__);
2657                 goto done;
2658         }
2659         if (v->async_err > 0) {
2660                 pr_err("%s: DSP returned error[%s]\n",
2661                                 __func__, adsp_err_get_err_str(
2662                                 v->async_err));
2663                 ret = adsp_err_get_lnx_err_code(
2664                                 v->async_err);
2665                 goto done;
2666         }
2667
2668 done:
2669         return ret;
2670
2671 }
2672
2673 static int voice_send_cvp_create_cmd(struct voice_data *v)
2674 {
2675         struct cvp_create_full_ctl_session_cmd cvp_session_cmd;
2676         void *apr_cvp;
2677         int ret = 0;
2678
2679         if (v == NULL) {
2680                 pr_err("%s: v is NULL\n", __func__);
2681                 ret = -EINVAL;
2682                 goto done;
2683         }
2684
2685         apr_cvp = common.apr_q6_cvp;
2686         if (!apr_cvp) {
2687                 pr_err("%s: apr_cvp is NULL.\n", __func__);
2688
2689                 ret = -EINVAL;
2690                 goto done;
2691         }
2692
2693         /* create cvp session and wait for response */
2694         cvp_session_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
2695                                                 APR_HDR_LEN(APR_HDR_SIZE),
2696                                                 APR_PKT_VER);
2697         cvp_session_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
2698                                 sizeof(cvp_session_cmd) - APR_HDR_SIZE);
2699         pr_debug("%s: send create cvp session, pkt size = %d\n",
2700                  __func__, cvp_session_cmd.hdr.pkt_size);
2701         cvp_session_cmd.hdr.src_port =
2702                                 voice_get_idx_for_session(v->session_id);
2703         cvp_session_cmd.hdr.dest_port = 0;
2704         cvp_session_cmd.hdr.token = 0;
2705
2706         if (voice_get_cvd_int_version(common.cvd_version) >=
2707             CVD_INT_VERSION_2_2)
2708                 cvp_session_cmd.hdr.opcode =
2709                                 VSS_IVOCPROC_CMD_CREATE_FULL_CONTROL_SESSION_V3;
2710         else
2711                 cvp_session_cmd.hdr.opcode =
2712                                 VSS_IVOCPROC_CMD_CREATE_FULL_CONTROL_SESSION_V2;
2713
2714         voc_get_tx_rx_topology(v,
2715                         &cvp_session_cmd.cvp_session.tx_topology_id,
2716                         &cvp_session_cmd.cvp_session.rx_topology_id);
2717
2718         cvp_session_cmd.cvp_session.direction = 2; /*tx and rx*/
2719         cvp_session_cmd.cvp_session.tx_port_id = v->dev_tx.port_id;
2720         cvp_session_cmd.cvp_session.rx_port_id = v->dev_rx.port_id;
2721         cvp_session_cmd.cvp_session.profile_id =
2722                                          VSS_ICOMMON_CAL_NETWORK_ID_NONE;
2723         if (common.ec_ref_ext) {
2724                 cvp_session_cmd.cvp_session.vocproc_mode =
2725                                 VSS_IVOCPROC_VOCPROC_MODE_EC_EXT_MIXING;
2726                 cvp_session_cmd.cvp_session.ec_ref_port_id =
2727                                 common.ec_media_fmt_info.port_id;
2728         } else {
2729                 cvp_session_cmd.cvp_session.vocproc_mode =
2730                                  VSS_IVOCPROC_VOCPROC_MODE_EC_INT_MIXING;
2731                 cvp_session_cmd.cvp_session.ec_ref_port_id =
2732                                                  VSS_IVOCPROC_PORT_ID_NONE;
2733         }
2734
2735         pr_debug("tx_topology: %d tx_port_id=%d, rx_port_id=%d, mode: 0x%x\n",
2736                 cvp_session_cmd.cvp_session.tx_topology_id,
2737                 cvp_session_cmd.cvp_session.tx_port_id,
2738                 cvp_session_cmd.cvp_session.rx_port_id,
2739                 cvp_session_cmd.cvp_session.vocproc_mode);
2740         pr_debug("rx_topology: %d, profile_id: 0x%x, pkt_size: %d\n",
2741                 cvp_session_cmd.cvp_session.rx_topology_id,
2742                 cvp_session_cmd.cvp_session.profile_id,
2743                 cvp_session_cmd.hdr.pkt_size);
2744
2745         v->cvp_state = CMD_STATUS_FAIL;
2746         v->async_err = 0;
2747         ret = apr_send_pkt(apr_cvp, (uint32_t *) &cvp_session_cmd);
2748         if (ret < 0) {
2749                 pr_err("Fail in sending VOCPROC_FULL_CONTROL_SESSION\n");
2750
2751                 ret = -EINVAL;
2752                 goto done;
2753         }
2754
2755         ret = wait_event_timeout(v->cvp_wait,
2756                                  (v->cvp_state == CMD_STATUS_SUCCESS),
2757                                  msecs_to_jiffies(TIMEOUT_MS));
2758         if (!ret) {
2759                 pr_err("%s: wait_event timeout\n", __func__);
2760
2761                 ret = -EINVAL;
2762                 goto done;
2763         }
2764         if (v->async_err > 0) {
2765                 pr_err("%s: DSP returned error[%s]\n",
2766                         __func__, adsp_err_get_err_str(
2767                         v->async_err));
2768                 ret = adsp_err_get_lnx_err_code(
2769                                 v->async_err);
2770                 goto done;
2771         }
2772
2773 done:
2774         return ret;
2775 }
2776
2777 static int voice_send_cvp_register_dev_cfg_cmd(struct voice_data *v)
2778 {
2779         struct cvp_register_dev_cfg_cmd cvp_reg_dev_cfg_cmd;
2780         struct cal_block_data *cal_block = NULL;
2781         int ret = 0;
2782         memset(&cvp_reg_dev_cfg_cmd, 0, sizeof(cvp_reg_dev_cfg_cmd));
2783
2784         if (v == NULL) {
2785                 pr_err("%s: v is NULL\n", __func__);
2786
2787                 ret = -EINVAL;
2788                 goto done;
2789         }
2790
2791         if (!common.apr_q6_cvp) {
2792                 pr_err("%s: apr_cvp is NULL\n", __func__);
2793
2794                 ret = -EPERM;
2795                 goto done;
2796         }
2797
2798         mutex_lock(&common.cal_data[CVP_VOCDEV_CFG_CAL]->lock);
2799
2800         ret = voice_get_cal(&cal_block, CVP_VOCDEV_CFG_CAL, NULL,
2801                 0, v->session_id);
2802         if (ret < 0) {
2803                 pr_err("%s: Voice_get_cal failed for cal %d!\n",
2804                         __func__, CVP_VOCDEV_CFG_CAL);
2805
2806                 goto unlock;
2807         }
2808
2809         cvp_reg_dev_cfg_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
2810                                 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
2811         cvp_reg_dev_cfg_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
2812                                 sizeof(cvp_reg_dev_cfg_cmd) - APR_HDR_SIZE);
2813         cvp_reg_dev_cfg_cmd.hdr.src_port =
2814                                 voice_get_idx_for_session(v->session_id);
2815         cvp_reg_dev_cfg_cmd.hdr.dest_port = voice_get_cvp_handle(v);
2816         cvp_reg_dev_cfg_cmd.hdr.token = 0;
2817         cvp_reg_dev_cfg_cmd.hdr.opcode =
2818                                         VSS_IVOCPROC_CMD_REGISTER_DEVICE_CONFIG;
2819
2820         cvp_reg_dev_cfg_cmd.cvp_dev_cfg_data.mem_handle =
2821                 cal_block->map_data.q6map_handle;
2822         cvp_reg_dev_cfg_cmd.cvp_dev_cfg_data.mem_address_lsw =
2823                 lower_32_bits(cal_block->cal_data.paddr);
2824         cvp_reg_dev_cfg_cmd.cvp_dev_cfg_data.mem_address_msw =
2825                 msm_audio_populate_upper_32_bits(cal_block->cal_data.paddr);
2826         cvp_reg_dev_cfg_cmd.cvp_dev_cfg_data.mem_size =
2827                 cal_block->cal_data.size;
2828
2829         v->cvp_state = CMD_STATUS_FAIL;
2830         v->async_err = 0;
2831         ret = apr_send_pkt(common.apr_q6_cvp,
2832                            (uint32_t *) &cvp_reg_dev_cfg_cmd);
2833         if (ret < 0) {
2834                 pr_err("%s: Error %d registering CVP dev cfg cal\n",
2835                        __func__, ret);
2836
2837                 ret = -EINVAL;
2838                 goto unlock;
2839         }
2840         ret = wait_event_timeout(v->cvp_wait,
2841                                  (v->cvp_state == CMD_STATUS_SUCCESS),
2842                                  msecs_to_jiffies(TIMEOUT_MS));
2843         if (!ret) {
2844                 pr_err("%s: Command timeout\n", __func__);
2845
2846                 ret = -EINVAL;
2847                 goto unlock;
2848         }
2849         if (v->async_err > 0) {
2850                 pr_err("%s: DSP returned error[%s]\n",
2851                                 __func__, adsp_err_get_err_str(
2852                                 v->async_err));
2853                 ret = adsp_err_get_lnx_err_code(
2854                                 v->async_err);
2855                 goto unlock;
2856         }
2857 unlock:
2858         mutex_unlock(&common.cal_data[CVP_VOCDEV_CFG_CAL]->lock);
2859 done:
2860         return ret;
2861 }
2862
2863 static int voice_send_cvp_deregister_dev_cfg_cmd(struct voice_data *v)
2864 {
2865         struct cvp_deregister_dev_cfg_cmd cvp_dereg_dev_cfg_cmd;
2866         int ret = 0;
2867         memset(&cvp_dereg_dev_cfg_cmd, 0, sizeof(cvp_dereg_dev_cfg_cmd));
2868
2869         if (v == NULL) {
2870                 pr_err("%s: v is NULL\n", __func__);
2871
2872                 ret = -EINVAL;
2873                 goto done;
2874         }
2875
2876         if (!common.apr_q6_cvp) {
2877                 pr_err("%s: apr_cvp is NULL\n", __func__);
2878
2879                 ret = -EPERM;
2880                 goto done;
2881         }
2882
2883         cvp_dereg_dev_cfg_cmd.hdr.hdr_field =
2884                                 APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
2885                                         APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
2886         cvp_dereg_dev_cfg_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
2887                                 sizeof(cvp_dereg_dev_cfg_cmd) - APR_HDR_SIZE);
2888         cvp_dereg_dev_cfg_cmd.hdr.src_port =
2889                                 voice_get_idx_for_session(v->session_id);
2890         cvp_dereg_dev_cfg_cmd.hdr.dest_port = voice_get_cvp_handle(v);
2891         cvp_dereg_dev_cfg_cmd.hdr.token = 0;
2892         cvp_dereg_dev_cfg_cmd.hdr.opcode =
2893                                 VSS_IVOCPROC_CMD_DEREGISTER_DEVICE_CONFIG;
2894
2895         v->cvp_state = CMD_STATUS_FAIL;
2896         v->async_err = 0;
2897         ret = apr_send_pkt(common.apr_q6_cvp,
2898                            (uint32_t *) &cvp_dereg_dev_cfg_cmd);
2899         if (ret < 0) {
2900                 pr_err("%s: Error %d de-registering CVP dev cfg cal\n",
2901                        __func__, ret);
2902                 goto done;
2903         }
2904         ret = wait_event_timeout(v->cvp_wait,
2905                                  (v->cvp_state == CMD_STATUS_SUCCESS),
2906                                  msecs_to_jiffies(TIMEOUT_MS));
2907         if (!ret) {
2908                 pr_err("%s: Command timeout\n", __func__);
2909                 goto done;
2910         }
2911         if (v->async_err > 0) {
2912                 pr_err("%s: DSP returned error[%s]\n",
2913                                 __func__, adsp_err_get_err_str(
2914                                 v->async_err));
2915                 ret = adsp_err_get_lnx_err_code(
2916                                 v->async_err);
2917                 goto done;
2918         }
2919
2920 done:
2921         return ret;
2922 }
2923
2924 static int voice_send_cvp_register_cal_cmd(struct voice_data *v)
2925 {
2926         struct cvp_register_cal_data_cmd cvp_reg_cal_cmd;
2927         struct cal_block_data *cal_block = NULL;
2928         struct cal_block_data *col_data = NULL;
2929         int ret = 0;
2930         memset(&cvp_reg_cal_cmd, 0, sizeof(cvp_reg_cal_cmd));
2931
2932         if (v == NULL) {
2933                 pr_err("%s: v is NULL\n", __func__);
2934
2935                 ret = -EINVAL;
2936                 goto done;
2937         }
2938
2939         if (!common.apr_q6_cvp) {
2940                 pr_err("%s: apr_cvp is NULL\n", __func__);
2941
2942                 ret = -EINVAL;
2943                 goto done;
2944         }
2945
2946         mutex_lock(&common.cal_data[CVP_VOCPROC_CAL]->lock);
2947         mutex_lock(&common.cal_data[CVP_VOCPROC_COL_CAL]->lock);
2948
2949         ret = voice_get_cal(&cal_block, CVP_VOCPROC_CAL, &col_data,
2950                 CVP_VOCPROC_COL_CAL, v->session_id);
2951         if (ret < 0) {
2952                 pr_err("%s: Voice_get_cal failed for cal %d!\n",
2953                         __func__, CVP_VOCPROC_CAL);
2954
2955                 goto unlock;
2956         }
2957
2958         v->dev_tx.dev_id = ((struct audio_cal_info_vocproc *)
2959                                 cal_block->cal_info)->tx_acdb_id;
2960         v->dev_rx.dev_id = ((struct audio_cal_info_vocproc *)
2961                                 cal_block->cal_info)->rx_acdb_id;
2962         pr_debug("%s: %s: Tx acdb id = %d and Rx acdb id = %d", __func__,
2963                  voc_get_session_name(v->session_id), v->dev_tx.dev_id,
2964                  v->dev_rx.dev_id);
2965
2966         memcpy(&cvp_reg_cal_cmd.cvp_cal_data.column_info[0],
2967                (void *) &((struct audio_cal_info_voc_col *)
2968                col_data->cal_info)->data,
2969                col_data->cal_data.size);
2970
2971         cvp_reg_cal_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
2972                                 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
2973         cvp_reg_cal_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
2974                                 sizeof(cvp_reg_cal_cmd) - APR_HDR_SIZE);
2975         cvp_reg_cal_cmd.hdr.src_port =
2976                                 voice_get_idx_for_session(v->session_id);
2977         cvp_reg_cal_cmd.hdr.dest_port = voice_get_cvp_handle(v);
2978         cvp_reg_cal_cmd.hdr.token = 0;
2979         if (common.is_per_vocoder_cal_enabled)
2980                 cvp_reg_cal_cmd.hdr.opcode =
2981                         VSS_IVOCPROC_CMD_REGISTER_STATIC_CALIBRATION_DATA;
2982         else
2983                 cvp_reg_cal_cmd.hdr.opcode =
2984                         VSS_IVOCPROC_CMD_REGISTER_CALIBRATION_DATA_V2;
2985
2986         cvp_reg_cal_cmd.cvp_cal_data.cal_mem_handle =
2987                 cal_block->map_data.q6map_handle;
2988         cvp_reg_cal_cmd.cvp_cal_data.cal_mem_address_lsw =
2989                 lower_32_bits(cal_block->cal_data.paddr);
2990         cvp_reg_cal_cmd.cvp_cal_data.cal_mem_address_msw =
2991                 msm_audio_populate_upper_32_bits(cal_block->cal_data.paddr);
2992         cvp_reg_cal_cmd.cvp_cal_data.cal_mem_size =
2993                 cal_block->cal_data.size;
2994
2995         v->cvp_state = CMD_STATUS_FAIL;
2996         v->async_err = 0;
2997         ret = apr_send_pkt(common.apr_q6_cvp, (uint32_t *) &cvp_reg_cal_cmd);
2998         if (ret < 0) {
2999                 pr_err("%s: Error %d registering CVP cal\n", __func__, ret);
3000
3001                 ret = -EINVAL;
3002                 goto unlock;
3003         }
3004         ret = wait_event_timeout(v->cvp_wait,
3005                                  (v->cvp_state == CMD_STATUS_SUCCESS),
3006                                  msecs_to_jiffies(TIMEOUT_MS));
3007         if (!ret) {
3008                 pr_err("%s: Command timeout\n", __func__);
3009
3010                 ret = -EINVAL;
3011                 goto unlock;
3012         }
3013         if (v->async_err > 0) {
3014                 pr_err("%s: DSP returned error[%s]\n",
3015                                 __func__, adsp_err_get_err_str(
3016                                 v->async_err));
3017                 ret = adsp_err_get_lnx_err_code(
3018                                 v->async_err);
3019                 goto unlock;
3020         }
3021 unlock:
3022         mutex_unlock(&common.cal_data[CVP_VOCPROC_COL_CAL]->lock);
3023         mutex_unlock(&common.cal_data[CVP_VOCPROC_CAL]->lock);
3024 done:
3025         return ret;
3026 }
3027
3028 static int voice_send_cvp_deregister_cal_cmd(struct voice_data *v)
3029 {
3030         struct cvp_deregister_cal_data_cmd cvp_dereg_cal_cmd;
3031         int ret = 0;
3032         memset(&cvp_dereg_cal_cmd, 0, sizeof(cvp_dereg_cal_cmd));
3033
3034         if (v == NULL) {
3035                 pr_err("%s: v is NULL\n", __func__);
3036
3037                 ret = -EINVAL;
3038                 goto done;
3039         }
3040
3041         if (!common.apr_q6_cvp) {
3042                 pr_err("%s: apr_cvp is NULL.\n", __func__);
3043
3044                 ret = -EPERM;
3045                 goto done;
3046         }
3047
3048         cvp_dereg_cal_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
3049                                 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
3050         cvp_dereg_cal_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
3051                                 sizeof(cvp_dereg_cal_cmd) - APR_HDR_SIZE);
3052         cvp_dereg_cal_cmd.hdr.src_port =
3053                                 voice_get_idx_for_session(v->session_id);
3054         cvp_dereg_cal_cmd.hdr.dest_port = voice_get_cvp_handle(v);
3055         cvp_dereg_cal_cmd.hdr.token = 0;
3056         if (common.is_per_vocoder_cal_enabled)
3057                 cvp_dereg_cal_cmd.hdr.opcode =
3058                         VSS_IVOCPROC_CMD_DEREGISTER_STATIC_CALIBRATION_DATA;
3059         else
3060                 cvp_dereg_cal_cmd.hdr.opcode =
3061                         VSS_IVOCPROC_CMD_DEREGISTER_CALIBRATION_DATA;
3062
3063         v->cvp_state = CMD_STATUS_FAIL;
3064         v->async_err = 0;
3065         ret = apr_send_pkt(common.apr_q6_cvp, (uint32_t *) &cvp_dereg_cal_cmd);
3066         if (ret < 0) {
3067                 pr_err("%s: Error %d de-registering CVP cal\n", __func__, ret);
3068                 goto done;
3069         }
3070         ret = wait_event_timeout(v->cvp_wait,
3071                                  (v->cvp_state == CMD_STATUS_SUCCESS),
3072                                  msecs_to_jiffies(TIMEOUT_MS));
3073         if (!ret) {
3074                 pr_err("%s: Command timeout\n", __func__);
3075                 goto done;
3076         }
3077         if (v->async_err > 0) {
3078                 pr_err("%s: DSP returned error[%s]\n",
3079                                 __func__, adsp_err_get_err_str(
3080                                 v->async_err));
3081                 ret = adsp_err_get_lnx_err_code(
3082                                 v->async_err);
3083                 goto done;
3084         }
3085
3086 done:
3087         return ret;
3088 }
3089
3090 static int voice_send_cvp_register_vol_cal_cmd(struct voice_data *v)
3091 {
3092         struct cvp_register_vol_cal_data_cmd cvp_reg_vol_cal_cmd;
3093         struct cal_block_data *cal_block = NULL;
3094         struct cal_block_data *col_data = NULL;
3095         int ret = 0;
3096         memset(&cvp_reg_vol_cal_cmd, 0, sizeof(cvp_reg_vol_cal_cmd));
3097
3098         if (v == NULL) {
3099                 pr_err("%s: v is NULL\n", __func__);
3100
3101                 ret = -EINVAL;
3102                 goto done;
3103         }
3104
3105         if (!common.apr_q6_cvp) {
3106                 pr_err("%s: apr_cvp is NULL\n", __func__);
3107
3108                 ret = -EINVAL;
3109                 goto done;
3110         }
3111
3112         mutex_lock(&common.cal_data[CVP_VOCVOL_CAL]->lock);
3113         mutex_lock(&common.cal_data[CVP_VOCVOL_COL_CAL]->lock);
3114
3115         ret = voice_get_cal(&cal_block, CVP_VOCVOL_CAL, &col_data,
3116                 CVP_VOCVOL_COL_CAL, v->session_id);
3117         if (ret < 0) {
3118                 pr_err("%s: Voice_get_cal failed for cal %d!\n",
3119                         __func__, CVP_VOCVOL_CAL);
3120
3121                 goto unlock;
3122         }
3123
3124         memcpy(&cvp_reg_vol_cal_cmd.cvp_vol_cal_data.column_info[0],
3125                (void *) &((struct audio_cal_info_voc_col *)
3126                col_data->cal_info)->data,
3127                col_data->cal_data.size);
3128
3129         cvp_reg_vol_cal_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
3130                                 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
3131         cvp_reg_vol_cal_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
3132                                 sizeof(cvp_reg_vol_cal_cmd) - APR_HDR_SIZE);
3133         cvp_reg_vol_cal_cmd.hdr.src_port =
3134                                 voice_get_idx_for_session(v->session_id);
3135         cvp_reg_vol_cal_cmd.hdr.dest_port = voice_get_cvp_handle(v);
3136         cvp_reg_vol_cal_cmd.hdr.token = 0;
3137         if (common.is_per_vocoder_cal_enabled)
3138                 cvp_reg_vol_cal_cmd.hdr.opcode =
3139                         VSS_IVOCPROC_CMD_REGISTER_DYNAMIC_CALIBRATION_DATA;
3140         else
3141                 cvp_reg_vol_cal_cmd.hdr.opcode =
3142                         VSS_IVOCPROC_CMD_REGISTER_VOL_CALIBRATION_DATA;
3143
3144         cvp_reg_vol_cal_cmd.cvp_vol_cal_data.cal_mem_handle =
3145                 cal_block->map_data.q6map_handle;
3146         cvp_reg_vol_cal_cmd.cvp_vol_cal_data.cal_mem_address_lsw =
3147                 lower_32_bits(cal_block->cal_data.paddr);
3148         cvp_reg_vol_cal_cmd.cvp_vol_cal_data.cal_mem_address_msw =
3149                 msm_audio_populate_upper_32_bits(cal_block->cal_data.paddr);
3150         cvp_reg_vol_cal_cmd.cvp_vol_cal_data.cal_mem_size =
3151                 cal_block->cal_data.size;
3152
3153         v->cvp_state = CMD_STATUS_FAIL;
3154         v->async_err = 0;
3155         ret = apr_send_pkt(common.apr_q6_cvp,
3156                            (uint32_t *) &cvp_reg_vol_cal_cmd);
3157         if (ret < 0) {
3158                 pr_err("%s: Error %d registering CVP vol cal\n", __func__, ret);
3159
3160                 ret = -EINVAL;
3161                 goto unlock;
3162         }
3163         ret = wait_event_timeout(v->cvp_wait,
3164                                  (v->cvp_state == CMD_STATUS_SUCCESS),
3165                                  msecs_to_jiffies(TIMEOUT_MS));
3166         if (!ret) {
3167                 pr_err("%s: Command timeout\n", __func__);
3168
3169                 ret = -EINVAL;
3170                 goto unlock;
3171         }
3172         if (v->async_err > 0) {
3173                 pr_err("%s: DSP returned error[%s]\n",
3174                                 __func__, adsp_err_get_err_str(
3175                                 v->async_err));
3176                 ret = adsp_err_get_lnx_err_code(
3177                                 v->async_err);
3178                 goto unlock;
3179         }
3180 unlock:
3181         mutex_unlock(&common.cal_data[CVP_VOCVOL_COL_CAL]->lock);
3182         mutex_unlock(&common.cal_data[CVP_VOCVOL_CAL]->lock);
3183 done:
3184         return ret;
3185 }
3186
3187 static int voice_send_cvp_deregister_vol_cal_cmd(struct voice_data *v)
3188 {
3189         struct cvp_deregister_vol_cal_data_cmd cvp_dereg_vol_cal_cmd;
3190         int ret = 0;
3191         memset(&cvp_dereg_vol_cal_cmd, 0, sizeof(cvp_dereg_vol_cal_cmd));
3192
3193         if (v == NULL) {
3194                 pr_err("%s: v is NULL\n", __func__);
3195
3196                 ret = -EINVAL;
3197                 goto done;
3198         }
3199
3200         if (!common.apr_q6_cvp) {
3201                 pr_err("%s: apr_cvp is NULL\n", __func__);
3202
3203                 ret = -EPERM;
3204                 goto done;
3205         }
3206
3207         cvp_dereg_vol_cal_cmd.hdr.hdr_field =
3208                         APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
3209                                 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
3210         cvp_dereg_vol_cal_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
3211                                 sizeof(cvp_dereg_vol_cal_cmd) - APR_HDR_SIZE);
3212         cvp_dereg_vol_cal_cmd.hdr.src_port =
3213                                 voice_get_idx_for_session(v->session_id);
3214         cvp_dereg_vol_cal_cmd.hdr.dest_port = voice_get_cvp_handle(v);
3215         cvp_dereg_vol_cal_cmd.hdr.token = 0;
3216         if (common.is_per_vocoder_cal_enabled)
3217                 cvp_dereg_vol_cal_cmd.hdr.opcode =
3218                         VSS_IVOCPROC_CMD_DEREGISTER_DYNAMIC_CALIBRATION_DATA;
3219         else
3220                 cvp_dereg_vol_cal_cmd.hdr.opcode =
3221                         VSS_IVOCPROC_CMD_DEREGISTER_VOL_CALIBRATION_DATA;
3222
3223         v->cvp_state = CMD_STATUS_FAIL;
3224         v->async_err = 0;
3225         ret = apr_send_pkt(common.apr_q6_cvp,
3226                            (uint32_t *) &cvp_dereg_vol_cal_cmd);
3227         if (ret < 0) {
3228                 pr_err("%s: Error %d de-registering CVP vol cal\n",
3229                        __func__, ret);
3230                 goto done;
3231         }
3232         ret = wait_event_timeout(v->cvp_wait,
3233                                  (v->cvp_state == CMD_STATUS_SUCCESS),
3234                                  msecs_to_jiffies(TIMEOUT_MS));
3235         if (!ret) {
3236                 pr_err("%s: Command timeout\n", __func__);
3237                 goto done;
3238         }
3239         if (v->async_err > 0) {
3240                 pr_err("%s: DSP returned error[%s]\n",
3241                                 __func__, adsp_err_get_err_str(
3242                                 v->async_err));
3243                 ret = adsp_err_get_lnx_err_code(
3244                                 v->async_err);
3245                 goto done;
3246         }
3247
3248 done:
3249         return ret;
3250 }
3251
3252 static int voice_map_memory_physical_cmd(struct voice_data *v,
3253                                          struct mem_map_table *table_info,
3254                                          dma_addr_t phys,
3255                                          uint32_t size,
3256                                          uint32_t token)
3257 {
3258         struct vss_imemory_cmd_map_physical_t mvm_map_phys_cmd;
3259         uint32_t *memtable;
3260         int ret = 0;
3261
3262         if (v == NULL) {
3263                 pr_err("%s: v is NULL\n", __func__);
3264                 ret = -EINVAL;
3265                 goto fail;
3266         }
3267
3268         if (!common.apr_q6_mvm) {
3269                 pr_err("%s: apr_mvm is NULL.\n", __func__);
3270                 ret = -EINVAL;
3271                 goto fail;
3272         }
3273
3274         if (!table_info->data) {
3275                 pr_err("%s: memory table is NULL.\n", __func__);
3276                 ret = -EINVAL;
3277                 goto fail;
3278         }
3279
3280         memtable = (uint32_t *) table_info->data;
3281
3282         /*
3283          * Store next table descriptor's address(64 bit) as NULL as there
3284          * is only one memory block
3285          */
3286         memtable[0] = 0;
3287         memtable[1] = 0;
3288
3289         /* Store next table descriptor's size */
3290         memtable[2] = 0;
3291
3292         /* Store shared mem adddress (64 bit) */
3293         memtable[3] = lower_32_bits(phys);
3294         memtable[4] = msm_audio_populate_upper_32_bits(phys);
3295
3296         /* Store shared memory size */
3297         memtable[5] = size;
3298
3299         mvm_map_phys_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
3300                                 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
3301         mvm_map_phys_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
3302                                 sizeof(mvm_map_phys_cmd) - APR_HDR_SIZE);
3303         mvm_map_phys_cmd.hdr.src_port =
3304                                 voice_get_idx_for_session(v->session_id);
3305         mvm_map_phys_cmd.hdr.dest_port = voice_get_mvm_handle(v);
3306         mvm_map_phys_cmd.hdr.token = token;
3307         mvm_map_phys_cmd.hdr.opcode = VSS_IMEMORY_CMD_MAP_PHYSICAL;
3308
3309         mvm_map_phys_cmd.table_descriptor.mem_address_lsw =
3310                         lower_32_bits(table_info->phys);
3311         mvm_map_phys_cmd.table_descriptor.mem_address_msw =
3312                         msm_audio_populate_upper_32_bits(table_info->phys);
3313         mvm_map_phys_cmd.table_descriptor.mem_size =
3314                         sizeof(struct vss_imemory_block_t) +
3315                         sizeof(struct vss_imemory_table_descriptor_t);
3316         mvm_map_phys_cmd.is_cached = true;
3317         mvm_map_phys_cmd.cache_line_size = 128;
3318         mvm_map_phys_cmd.access_mask = 3;
3319         mvm_map_phys_cmd.page_align = 4096;
3320         mvm_map_phys_cmd.min_data_width = 8;
3321         mvm_map_phys_cmd.max_data_width = 64;
3322
3323         pr_debug("%s: next table desc: add: %lld, size: %d\n",
3324                  __func__, *((uint64_t *) memtable),
3325                  *(((uint32_t *) memtable) + 2));
3326         pr_debug("%s: phy add of of mem being mapped LSW:0x%x, MSW:0x%x size: %d\n",
3327                  __func__, *(((uint32_t *) memtable) + 3),
3328                 *(((uint32_t *) memtable) + 4), *(((uint32_t *) memtable) + 5));
3329
3330         v->mvm_state = CMD_STATUS_FAIL;
3331         v->async_err = 0;
3332         ret = apr_send_pkt(common.apr_q6_mvm, (uint32_t *) &mvm_map_phys_cmd);
3333         if (ret < 0) {
3334                 pr_err("%s: Error %d sending mvm map phy cmd\n", __func__, ret);
3335
3336                 goto fail;
3337         }
3338
3339         ret = wait_event_timeout(v->mvm_wait,
3340                                  (v->mvm_state == CMD_STATUS_SUCCESS),
3341                                  msecs_to_jiffies(TIMEOUT_MS));
3342         if (!ret) {
3343                 pr_err("%s: Command timeout\n", __func__);
3344
3345                 goto fail;
3346         }
3347         if (v->async_err > 0) {
3348                 pr_err("%s: DSP returned error[%s]\n",
3349                                 __func__, adsp_err_get_err_str(
3350                                 v->async_err));
3351                 ret = adsp_err_get_lnx_err_code(
3352                                 v->async_err);
3353                 goto fail;
3354         }
3355
3356         return 0;
3357
3358 fail:
3359         return ret;
3360 }
3361
3362 static int voice_pause_voice_call(struct voice_data *v)
3363 {
3364         struct apr_hdr  mvm_pause_voice_cmd;
3365         void            *apr_mvm;
3366         int             ret = 0;
3367
3368         pr_debug("%s\n", __func__);
3369
3370         if (v == NULL) {
3371                 pr_err("%s: Voice data is NULL\n", __func__);
3372
3373                 ret = -EINVAL;
3374                 goto done;
3375         }
3376
3377         apr_mvm = common.apr_q6_mvm;
3378         if (!apr_mvm) {
3379                 pr_err("%s: apr_mvm is NULL.\n", __func__);
3380
3381                 ret = -EINVAL;
3382                 goto done;
3383         }
3384
3385         mvm_pause_voice_cmd.hdr_field =
3386                 APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
3387                 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
3388         mvm_pause_voice_cmd.pkt_size =
3389                 APR_PKT_SIZE(APR_HDR_SIZE,
3390                 sizeof(mvm_pause_voice_cmd) - APR_HDR_SIZE);
3391         mvm_pause_voice_cmd.src_port =
3392                         voice_get_idx_for_session(v->session_id);
3393         mvm_pause_voice_cmd.dest_port = voice_get_mvm_handle(v);
3394         mvm_pause_voice_cmd.token = 0;
3395         mvm_pause_voice_cmd.opcode = VSS_IMVM_CMD_PAUSE_VOICE;
3396         v->mvm_state = CMD_STATUS_FAIL;
3397         v->async_err = 0;
3398
3399         pr_debug("%s: send mvm_pause_voice_cmd pkt size = %d\n",
3400                 __func__, mvm_pause_voice_cmd.pkt_size);
3401
3402         ret = apr_send_pkt(apr_mvm,
3403                 (uint32_t *)&mvm_pause_voice_cmd);
3404         if (ret < 0) {
3405                 pr_err("Fail in sending VSS_IMVM_CMD_PAUSE_VOICE\n");
3406
3407                 ret = -EINVAL;
3408                 goto done;
3409         }
3410
3411         ret = wait_event_timeout(v->mvm_wait,
3412                 (v->mvm_state == CMD_STATUS_SUCCESS),
3413                 msecs_to_jiffies(TIMEOUT_MS));
3414         if (!ret) {
3415                 pr_err("%s: Command timeout\n", __func__);
3416
3417                 ret = -EINVAL;
3418                 goto done;
3419         }
3420         if (v->async_err > 0) {
3421                 pr_err("%s: DSP returned error[%s]\n",
3422                                 __func__, adsp_err_get_err_str(
3423                                 v->async_err));
3424                 ret = adsp_err_get_lnx_err_code(
3425                                 v->async_err);
3426                 goto done;
3427         }
3428
3429 done:
3430         return ret;
3431 }
3432
3433 static int voice_map_cal_memory(struct cal_block_data *cal_block,
3434                                 uint32_t session_id)
3435 {
3436         int result = 0;
3437         int voc_index;
3438         struct voice_data *v = NULL;
3439         pr_debug("%s\n", __func__);
3440
3441         if (cal_block == NULL) {
3442                 pr_err("%s: Cal block is NULL!\n", __func__);
3443
3444                 result = -EINVAL;
3445                 goto done;
3446         }
3447
3448         if (cal_block->cal_data.paddr == 0) {
3449                 pr_debug("%s: No address to map!\n", __func__);
3450
3451                 result = -EINVAL;
3452                 goto done;
3453         }
3454
3455         if (cal_block->map_data.map_size == 0) {
3456                 pr_debug("%s: Map size is 0!\n", __func__);
3457
3458                 result = -EINVAL;
3459                 goto done;
3460         }
3461
3462         voc_index = voice_get_idx_for_session(session_id);
3463         if (voc_index < 0) {
3464                 pr_err("%s:  Invalid session ID %d\n", __func__, session_id);
3465
3466                 goto done;
3467         }
3468
3469         mutex_lock(&common.common_lock);
3470         v = &common.voice[voc_index];
3471
3472         result = voice_map_memory_physical_cmd(v,
3473                 &common.cal_mem_map_table,
3474                 (dma_addr_t)cal_block->cal_data.paddr,
3475                 cal_block->map_data.map_size,
3476                 VOC_CAL_MEM_MAP_TOKEN);
3477         if (result < 0) {
3478                 pr_err("%s: Mmap did not work! addr = 0x%pK, size = %zd\n",
3479                         __func__,
3480                         &cal_block->cal_data.paddr,
3481                         cal_block->map_data.map_size);
3482
3483                 goto done_unlock;
3484         }
3485
3486         cal_block->map_data.q6map_handle = common.cal_mem_handle;
3487 done_unlock:
3488         mutex_unlock(&common.common_lock);
3489 done:
3490         return result;
3491 }
3492
3493 static int remap_cal_data(struct cal_block_data *cal_block,
3494                            uint32_t session_id)
3495 {
3496         int ret = 0;
3497         pr_debug("%s\n", __func__);
3498
3499         if (cal_block->map_data.ion_client == NULL) {
3500                 pr_err("%s: No ION allocation for session_id %d!\n",
3501                         __func__, session_id);
3502                 ret = -EINVAL;
3503                 goto done;
3504         }
3505
3506         if ((cal_block->map_data.map_size > 0) &&
3507                 (cal_block->map_data.q6map_handle == 0)) {
3508
3509                 /* cal type not used */
3510                 ret = voice_map_cal_memory(cal_block, session_id);
3511                 if (ret < 0) {
3512                         pr_err("%s: Mmap did not work! size = %zd\n",
3513                                 __func__, cal_block->map_data.map_size);
3514
3515                         goto done;
3516                 }
3517         } else {
3518                 pr_debug("%s:  Cal block 0x%pK, size %zd already mapped. Q6 map handle = %d\n",
3519                         __func__, &cal_block->cal_data.paddr,
3520                         cal_block->map_data.map_size,
3521                         cal_block->map_data.q6map_handle);
3522         }
3523 done:
3524         return ret;
3525 }
3526
3527 static int voice_unmap_cal_memory(int32_t cal_type,
3528                                   struct cal_block_data *cal_block)
3529 {
3530         int result = 0;
3531         int result2 = 0;
3532         int i;
3533         struct voice_data *v = NULL;
3534         pr_debug("%s\n", __func__);
3535
3536         if (cal_block == NULL) {
3537                 pr_err("%s: Cal block is NULL!\n", __func__);
3538
3539                 result = -EINVAL;
3540                 goto done;
3541         }
3542
3543         if (cal_block->map_data.q6map_handle == 0) {
3544                 pr_debug("%s: Q6 handle is not set!\n", __func__);
3545
3546                 result = -EINVAL;
3547                 goto done;
3548         }
3549
3550         mutex_lock(&common.common_lock);
3551
3552         for (i = 0; i < MAX_VOC_SESSIONS; i++) {
3553                 v = &common.voice[i];
3554
3555                 mutex_lock(&v->lock);
3556                 if (is_voc_state_active(v->voc_state)) {
3557                         result2 = voice_pause_voice_call(v);
3558                         if (result2 < 0) {
3559                                 pr_err("%s: Voice_pause_voice_call failed for session 0x%x, err %d!\n",
3560                                         __func__, v->session_id, result2);
3561
3562                                 result = result2;
3563                         }
3564
3565                         if (cal_type == CVP_VOCPROC_DYNAMIC_CAL_TYPE)
3566                                 voice_send_cvp_deregister_vol_cal_cmd(v);
3567                         else if (cal_type == CVP_VOCPROC_STATIC_CAL_TYPE)
3568                                 voice_send_cvp_deregister_cal_cmd(v);
3569                         else if (cal_type == CVP_VOCDEV_CFG_CAL_TYPE)
3570                                 voice_send_cvp_deregister_dev_cfg_cmd(v);
3571                         else if (cal_type == CVS_VOCSTRM_STATIC_CAL_TYPE)
3572                                 voice_send_cvs_deregister_cal_cmd(v);
3573                         else
3574                                 pr_err("%s: Invalid cal type %d!\n",
3575                                         __func__, cal_type);
3576
3577                         result2 = voice_send_start_voice_cmd(v);
3578                         if (result2) {
3579                                 pr_err("%s: Voice_send_start_voice_cmd failed for session 0x%x, err %d!\n",
3580                                         __func__, v->session_id, result2);
3581
3582                                 result = result2;
3583                         }
3584                 }
3585
3586                 if ((cal_block->map_data.q6map_handle != 0) &&
3587                         (!is_other_session_active(v->session_id))) {
3588
3589                         result2 = voice_send_mvm_unmap_memory_physical_cmd(
3590                                 v, cal_block->map_data.q6map_handle);
3591                         if (result2) {
3592                                 pr_err("%s: Voice_send_mvm_unmap_memory_physical_cmd failed for session 0x%x, err %d!\n",
3593                                         __func__, v->session_id, result2);
3594
3595                                 result = result2;
3596                         }
3597                         cal_block->map_data.q6map_handle = 0;
3598                 }
3599                 mutex_unlock(&v->lock);
3600         }
3601         mutex_unlock(&common.common_lock);
3602 done:
3603         return result;
3604 }
3605
3606 int voc_register_vocproc_vol_table(void)
3607 {
3608         int                     result = 0;
3609         int                     result2 = 0;
3610         int                     i;
3611         struct voice_data       *v = NULL;
3612
3613         pr_debug("%s\n", __func__);
3614
3615         mutex_lock(&common.common_lock);
3616         for (i = 0; i < MAX_VOC_SESSIONS; i++) {
3617                 v = &common.voice[i];
3618
3619                 mutex_lock(&v->lock);
3620                 if (is_voc_state_active(v->voc_state)) {
3621                         result2 = voice_send_cvp_register_vol_cal_cmd(v);
3622                         if (result2 < 0) {
3623                                 pr_err("%s: Failed to register vocvol table for session 0x%x!\n",
3624                                         __func__, v->session_id);
3625
3626                                 result = result2;
3627                                 /* Still try to register other sessions */
3628                         }
3629                 }
3630                 mutex_unlock(&v->lock);
3631         }
3632
3633         mutex_unlock(&common.common_lock);
3634         return result;
3635 }
3636
3637 int voc_deregister_vocproc_vol_table(void)
3638 {
3639         int                     result = 0;
3640         int                     success = 0;
3641         int                     i;
3642         struct voice_data       *v = NULL;
3643
3644         pr_debug("%s\n", __func__);
3645
3646         mutex_lock(&common.common_lock);
3647         for (i = 0; i < MAX_VOC_SESSIONS; i++) {
3648                 v = &common.voice[i];
3649
3650                 mutex_lock(&v->lock);
3651                 if (is_voc_state_active(v->voc_state)) {
3652                         result = voice_send_cvp_deregister_vol_cal_cmd(v);
3653                         if (result < 0) {
3654                                 pr_err("%s: Failed to deregister vocvol table for session 0x%x!\n",
3655                                         __func__, v->session_id);
3656
3657                                 mutex_unlock(&v->lock);
3658                                 mutex_unlock(&common.common_lock);
3659                                 if (success) {
3660                                         pr_err("%s: Try to re-register all deregistered sessions!\n",
3661                                                 __func__);
3662
3663                                         voc_register_vocproc_vol_table();
3664                                 }
3665                                 goto done;
3666                         } else {
3667                                 success = 1;
3668                         }
3669                 }
3670                 mutex_unlock(&v->lock);
3671         }
3672         mutex_unlock(&common.common_lock);
3673 done:
3674         return result;
3675 }
3676
3677 int voc_map_rtac_block(struct rtac_cal_block_data *cal_block)
3678 {
3679         int                     result = 0;
3680         struct voice_data       *v = NULL;
3681
3682         pr_debug("%s\n", __func__);
3683
3684         if (cal_block == NULL) {
3685                 pr_err("%s: cal_block is NULL!\n",
3686                         __func__);
3687
3688                 result = -EINVAL;
3689                 goto done;
3690         }
3691
3692         if (cal_block->cal_data.paddr == 0) {
3693                 pr_debug("%s: No address to map!\n",
3694                         __func__);
3695
3696                 result = -EINVAL;
3697                 goto done;
3698         }
3699
3700         if (cal_block->map_data.map_size == 0) {
3701                 pr_debug("%s: map size is 0!\n",
3702                         __func__);
3703
3704                 result = -EINVAL;
3705                 goto done;
3706         }
3707
3708         mutex_lock(&common.common_lock);
3709         /* use first session */
3710         v = &common.voice[0];
3711         mutex_lock(&v->lock);
3712
3713         if (!is_rtac_memory_allocated()) {
3714                 result = voice_alloc_rtac_mem_map_table();
3715                 if (result < 0) {
3716                         pr_err("%s: RTAC alloc mem map table did not work! addr = 0x%pK, size = %d\n",
3717                                 __func__,
3718                                 &cal_block->cal_data.paddr,
3719                                 cal_block->map_data.map_size);
3720
3721                         goto done_unlock;
3722                 }
3723         }
3724
3725         result = voice_map_memory_physical_cmd(v,
3726                 &common.rtac_mem_map_table,
3727                 (dma_addr_t)cal_block->cal_data.paddr,
3728                 cal_block->map_data.map_size,
3729                 VOC_RTAC_MEM_MAP_TOKEN);
3730         if (result < 0) {
3731                 pr_err("%s: RTAC mmap did not work! addr = 0x%pK, size = %d\n",
3732                         __func__,
3733                         &cal_block->cal_data.paddr,
3734                         cal_block->map_data.map_size);
3735
3736                 free_rtac_map_table();
3737                 goto done_unlock;
3738         }
3739
3740         cal_block->map_data.map_handle = common.rtac_mem_handle;
3741 done_unlock:
3742         mutex_unlock(&v->lock);
3743         mutex_unlock(&common.common_lock);
3744 done:
3745         return result;
3746 }
3747
3748 int voc_unmap_rtac_block(uint32_t *mem_map_handle)
3749 {
3750         int                     result = 0;
3751         struct voice_data       *v = NULL;
3752
3753         pr_debug("%s\n", __func__);
3754
3755         if (mem_map_handle == NULL) {
3756                 pr_debug("%s: Map handle is NULL, nothing to unmap\n",
3757                         __func__);
3758
3759                 goto done;
3760         }
3761
3762         if (*mem_map_handle == 0) {
3763                 pr_debug("%s: Map handle is 0, nothing to unmap\n",
3764                         __func__);
3765
3766                 goto done;
3767         }
3768
3769         mutex_lock(&common.common_lock);
3770         /* Use first session */
3771         /* Only used for apr wait lock */
3772         v = &common.voice[0];
3773         mutex_lock(&v->lock);
3774
3775         result = voice_send_mvm_unmap_memory_physical_cmd(
3776                         v, *mem_map_handle);
3777         if (result) {
3778                 pr_err("%s: voice_send_mvm_unmap_memory_physical_cmd Failed for session 0x%x!\n",
3779                         __func__, v->session_id);
3780         } else {
3781                 *mem_map_handle = 0;
3782                 common.rtac_mem_handle = 0;
3783                 free_rtac_map_table();
3784         }
3785         mutex_unlock(&v->lock);
3786         mutex_unlock(&common.common_lock);
3787 done:
3788         return result;
3789 }
3790
3791 static int voice_setup_vocproc(struct voice_data *v)
3792 {
3793         struct module_instance_info mod_inst_info = {0};
3794         int ret = 0;
3795
3796         ret = voice_send_cvp_create_cmd(v);
3797         if (ret < 0) {
3798                 pr_err("%s: CVP create failed err:%d\n", __func__, ret);
3799                 goto fail;
3800         }
3801
3802         ret = voice_send_cvp_media_fmt_info_cmd(v);
3803         if (ret < 0) {
3804                 pr_err("%s: Set media format info failed err:%d\n", __func__,
3805                        ret);
3806                 goto fail;
3807         }
3808
3809         ret = voice_send_cvp_topology_commit_cmd(v);
3810         if (ret < 0) {
3811                 pr_err("%s: Set topology commit failed err:%d\n",
3812                        __func__, ret);
3813                 goto fail;
3814         }
3815
3816         mod_inst_info.module_id = MODULE_ID_VOICE_MODULE_ST;
3817         mod_inst_info.instance_id = INSTANCE_ID_0;
3818
3819         voice_send_cvs_register_cal_cmd(v);
3820         voice_send_cvp_register_dev_cfg_cmd(v);
3821         voice_send_cvp_register_cal_cmd(v);
3822         voice_send_cvp_register_vol_cal_cmd(v);
3823
3824         /* enable vocproc */
3825         ret = voice_send_enable_vocproc_cmd(v);
3826         if (ret < 0)
3827                 goto fail;
3828
3829         /* attach vocproc */
3830         ret = voice_send_attach_vocproc_cmd(v);
3831         if (ret < 0)
3832                 goto fail;
3833
3834         /* send tty mode if tty device is used */
3835         voice_send_tty_mode_cmd(v);
3836
3837         if (is_voip_session(v->session_id)) {
3838                 ret = voice_send_mvm_cal_network_cmd(v);
3839                 if (ret < 0)
3840                         pr_err("%s: voice_send_mvm_cal_network_cmd: %d\n",
3841                                 __func__, ret);
3842
3843                 ret = voice_send_mvm_media_type_cmd(v);
3844                 if (ret < 0)
3845                         pr_err("%s: voice_send_mvm_media_type_cmd: %d\n",
3846                                 __func__, ret);
3847
3848                 voice_send_netid_timing_cmd(v);
3849         }
3850
3851         if (v->st_enable && !v->tty_mode)
3852                 voice_send_set_pp_enable_cmd(v, mod_inst_info, v->st_enable);
3853         /* Start in-call music delivery if this feature is enabled */
3854         if (v->music_info.play_enable)
3855                 voice_cvs_start_playback(v);
3856
3857         /* Start in-call recording if this feature is enabled */
3858         if (v->rec_info.rec_enable)
3859                 voice_cvs_start_record(v, v->rec_info.rec_mode);
3860
3861         if (v->dtmf_rx_detect_en)
3862                 voice_send_dtmf_rx_detection_cmd(v, v->dtmf_rx_detect_en);
3863
3864         if (v->hd_enable)
3865                 voice_send_hd_cmd(v, v->hd_enable);
3866
3867         rtac_add_voice(voice_get_cvs_handle(v),
3868                 voice_get_cvp_handle(v),
3869                 v->dev_rx.port_id, v->dev_tx.port_id,
3870                 v->dev_rx.dev_id, v->dev_tx.dev_id,
3871                 v->session_id);
3872
3873         return 0;
3874
3875 fail:
3876         return ret;
3877 }
3878
3879 static int voice_send_cvp_device_channels_cmd(struct voice_data *v)
3880 {
3881         int ret = 0;
3882         struct  cvp_set_dev_channels_cmd cvp_set_dev_channels_cmd;
3883         void *apr_cvp;
3884         u16 cvp_handle;
3885
3886         if (!(voice_get_cvd_int_version(common.cvd_version) >=
3887               CVD_INT_VERSION_2_2)) {
3888                 pr_debug("%s CVD ver %s doesnt support send_device_channels cmd\n",
3889                          __func__, common.cvd_version);
3890
3891                 goto done;
3892         }
3893
3894         if (v == NULL) {
3895                 pr_err("%s: v is NULL\n", __func__);
3896
3897                 ret = -EINVAL;
3898                 goto done;
3899         }
3900
3901         apr_cvp = common.apr_q6_cvp;
3902         if (!apr_cvp) {
3903                 pr_err("%s: apr_cvp is NULL.\n", __func__);
3904
3905                 ret = -EINVAL;
3906                 goto done;
3907         }
3908
3909         cvp_handle = voice_get_cvp_handle(v);
3910         cvp_set_dev_channels_cmd.hdr.hdr_field =
3911                         APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
3912                         APR_HDR_LEN(APR_HDR_SIZE),
3913                         APR_PKT_VER);
3914         cvp_set_dev_channels_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
3915                         sizeof(cvp_set_dev_channels_cmd) - APR_HDR_SIZE);
3916         cvp_set_dev_channels_cmd.hdr.src_port =
3917                                 voice_get_idx_for_session(v->session_id);
3918         cvp_set_dev_channels_cmd.hdr.dest_port = cvp_handle;
3919         cvp_set_dev_channels_cmd.hdr.token = 0;
3920         cvp_set_dev_channels_cmd.hdr.opcode =
3921                                 VSS_IVOCPROC_CMD_TOPOLOGY_SET_DEV_CHANNELS;
3922         cvp_set_dev_channels_cmd.cvp_set_channels.rx_num_channels =
3923                                 VSS_NUM_DEV_CHANNELS_1;
3924         cvp_set_dev_channels_cmd.cvp_set_channels.tx_num_channels =
3925                                 v->dev_tx.no_of_channels;
3926
3927         v->cvp_state = CMD_STATUS_FAIL;
3928         v->async_err = 0;
3929         ret = apr_send_pkt(apr_cvp, (uint32_t *) &cvp_set_dev_channels_cmd);
3930         if (ret < 0) {
3931                 pr_err("%s: Fail in sending VSS_IVOCPROC_CMD_TOPOLOGY_SET_DEV_CHANNELS\n",
3932                        __func__);
3933
3934                 ret = -EINVAL;
3935                 goto done;
3936         }
3937
3938         ret = wait_event_timeout(v->cvp_wait,
3939                                 (v->cvp_state == CMD_STATUS_SUCCESS),
3940                                 msecs_to_jiffies(TIMEOUT_MS));
3941         if (!ret) {
3942                 pr_err("%s: wait_event timeout\n", __func__);
3943
3944                 ret = -EINVAL;
3945                 goto done;
3946         }
3947         if (v->async_err > 0) {
3948                 pr_err("%s: DSP returned error[%s]\n",
3949                         __func__, adsp_err_get_err_str(
3950                         v->async_err));
3951                 ret = adsp_err_get_lnx_err_code(
3952                                 v->async_err);
3953                 goto done;
3954         }
3955
3956 done:
3957         return ret;
3958 }
3959
3960 static int voice_send_cvp_media_fmt_info_cmd(struct voice_data *v)
3961 {
3962         int ret;
3963
3964         ret = voice_send_cvp_device_channels_cmd(v);
3965         if (ret < 0)
3966                 goto done;
3967
3968         if (voice_get_cvd_int_version(common.cvd_version) >=
3969             CVD_INT_VERSION_2_3) {
3970                 ret = voice_send_cvp_media_format_cmd(v, RX_PATH);
3971                 if (ret < 0)
3972                         goto done;
3973
3974                 ret = voice_send_cvp_media_format_cmd(v, TX_PATH);
3975                 if (ret < 0)
3976                         goto done;
3977
3978                 if (common.ec_ref_ext)
3979                         ret = voice_send_cvp_media_format_cmd(v, EC_REF_PATH);
3980         }
3981
3982 done:
3983         return ret;
3984 }
3985
3986 static int voice_send_cvp_media_format_cmd(struct voice_data *v,
3987                                            uint32_t param_type)
3988 {
3989         struct vss_param_endpoint_media_format_info media_fmt_info = {0};
3990         struct param_hdr_v3 param_hdr = {0};
3991         int ret = 0;
3992
3993         if (v == NULL) {
3994                 pr_err("%s: v is NULL\n", __func__);
3995                 ret = -EINVAL;
3996                 goto done;
3997         }
3998
3999         param_hdr.module_id = VSS_MODULE_CVD_GENERIC;
4000         param_hdr.instance_id = INSTANCE_ID_0;
4001         param_hdr.param_size = sizeof(media_fmt_info);
4002
4003         switch (param_type) {
4004         case RX_PATH:
4005                 param_hdr.param_id = VSS_PARAM_RX_PORT_ENDPOINT_MEDIA_INFO;
4006                 media_fmt_info.port_id = v->dev_rx.port_id;
4007                 media_fmt_info.num_channels = v->dev_rx.no_of_channels;
4008                 media_fmt_info.bits_per_sample = v->dev_rx.bits_per_sample;
4009                 media_fmt_info.sample_rate = v->dev_rx.sample_rate;
4010                 memcpy(&media_fmt_info.channel_mapping,
4011                        &v->dev_rx.channel_mapping, VSS_CHANNEL_MAPPING_SIZE);
4012                 break;
4013
4014         case TX_PATH:
4015                 param_hdr.param_id = VSS_PARAM_TX_PORT_ENDPOINT_MEDIA_INFO;
4016                 media_fmt_info.port_id = v->dev_tx.port_id;
4017                 media_fmt_info.num_channels = v->dev_tx.no_of_channels;
4018                 media_fmt_info.bits_per_sample = v->dev_tx.bits_per_sample;
4019                 media_fmt_info.sample_rate = v->dev_tx.sample_rate;
4020                 memcpy(&media_fmt_info.channel_mapping,
4021                        &v->dev_tx.channel_mapping, VSS_CHANNEL_MAPPING_SIZE);
4022                 break;
4023
4024         case EC_REF_PATH:
4025                 param_hdr.param_id = VSS_PARAM_EC_REF_PORT_ENDPOINT_MEDIA_INFO;
4026                 media_fmt_info.port_id = common.ec_media_fmt_info.port_id;
4027                 media_fmt_info.num_channels =
4028                         common.ec_media_fmt_info.num_channels;
4029                 media_fmt_info.bits_per_sample =
4030                         common.ec_media_fmt_info.bits_per_sample;
4031                 media_fmt_info.sample_rate =
4032                         common.ec_media_fmt_info.sample_rate;
4033                 memcpy(&media_fmt_info.channel_mapping,
4034                        &common.ec_media_fmt_info.channel_mapping,
4035                        VSS_CHANNEL_MAPPING_SIZE);
4036                 break;
4037
4038         default:
4039                 pr_err("%s: Invalid param type %d\n", __func__, param_type);
4040                 ret = -EINVAL;
4041                 goto done;
4042         }
4043
4044         ret = voice_pack_and_set_cvp_param(v, param_hdr,
4045                                            (u8 *) &media_fmt_info);
4046         if (ret)
4047                 pr_err("%s: Failed to set media format params on CVP, err %d\n",
4048                        __func__, ret);
4049
4050 done:
4051         return ret;
4052 }
4053
4054 static int voice_send_cvp_topology_commit_cmd(struct voice_data *v)
4055 {
4056         int ret = 0;
4057         struct apr_hdr cvp_topology_commit_cmd;
4058         void *apr_cvp;
4059         u16 cvp_handle;
4060
4061         if (!(voice_get_cvd_int_version(common.cvd_version) >=
4062               CVD_INT_VERSION_2_2)) {
4063                 pr_debug("%s CVD version string %s doesnt support this command\n",
4064                          __func__, common.cvd_version);
4065
4066                 goto done;
4067         }
4068
4069         if (v == NULL) {
4070                 pr_err("%s: v is NULL\n", __func__);
4071
4072                 ret = -EINVAL;
4073                 goto done;
4074         }
4075
4076         apr_cvp = common.apr_q6_cvp;
4077         if (!apr_cvp) {
4078                 pr_err("%s: apr_cvp is NULL.\n", __func__);
4079
4080                 ret = -EINVAL;
4081                 goto done;
4082         }
4083
4084         cvp_handle = voice_get_cvp_handle(v);
4085         cvp_topology_commit_cmd.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
4086                                                 APR_HDR_LEN(APR_HDR_SIZE),
4087                                                 APR_PKT_VER);
4088         cvp_topology_commit_cmd.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
4089                                 sizeof(cvp_topology_commit_cmd) - APR_HDR_SIZE);
4090         cvp_topology_commit_cmd.src_port =
4091                                 voice_get_idx_for_session(v->session_id);
4092         cvp_topology_commit_cmd.dest_port = cvp_handle;
4093         cvp_topology_commit_cmd.token = 0;
4094         cvp_topology_commit_cmd.opcode = VSS_IVOCPROC_CMD_TOPOLOGY_COMMIT;
4095
4096         v->cvp_state = CMD_STATUS_FAIL;
4097         v->async_err = 0;
4098         ret = apr_send_pkt(apr_cvp, (uint32_t *) &cvp_topology_commit_cmd);
4099         if (ret < 0) {
4100                 pr_err("%s: Fail in sending VSS_IVOCPROC_CMD_TOPOLOGY_COMMIT\n",
4101                        __func__);
4102
4103                 ret = -EINVAL;
4104                 goto done;
4105         }
4106
4107         ret = wait_event_timeout(v->cvp_wait,
4108                                 (v->cvp_state == CMD_STATUS_SUCCESS),
4109                                 msecs_to_jiffies(TIMEOUT_MS));
4110         if (!ret) {
4111                 pr_err("%s: wait_event timeout\n", __func__);
4112                 ret = -EINVAL;
4113                 goto done;
4114         }
4115         if (v->async_err > 0) {
4116                 pr_err("%s: DSP returned error[%s]\n",
4117                         __func__, adsp_err_get_err_str(
4118                         v->async_err));
4119                 ret = adsp_err_get_lnx_err_code(
4120                                 v->async_err);
4121                 goto done;
4122         }
4123
4124 done:
4125         return ret;
4126 }
4127
4128 static int voice_send_enable_vocproc_cmd(struct voice_data *v)
4129 {
4130         int ret = 0;
4131         struct apr_hdr cvp_enable_cmd;
4132         void *apr_cvp;
4133         u16 cvp_handle;
4134
4135         if (v == NULL) {
4136                 pr_err("%s: v is NULL\n", __func__);
4137                 return -EINVAL;
4138         }
4139         apr_cvp = common.apr_q6_cvp;
4140
4141         if (!apr_cvp) {
4142                 pr_err("%s: apr_cvp is NULL.\n", __func__);
4143                 return -EINVAL;
4144         }
4145         cvp_handle = voice_get_cvp_handle(v);
4146
4147         /* enable vocproc and wait for respose */
4148         cvp_enable_cmd.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
4149                                                 APR_HDR_LEN(APR_HDR_SIZE),
4150                                                 APR_PKT_VER);
4151         cvp_enable_cmd.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
4152                                 sizeof(cvp_enable_cmd) - APR_HDR_SIZE);
4153         pr_debug("cvp_enable_cmd pkt size = %d, cvp_handle=%d\n",
4154                 cvp_enable_cmd.pkt_size, cvp_handle);
4155         cvp_enable_cmd.src_port =
4156                                 voice_get_idx_for_session(v->session_id);
4157         cvp_enable_cmd.dest_port = cvp_handle;
4158         cvp_enable_cmd.token = 0;
4159         cvp_enable_cmd.opcode = VSS_IVOCPROC_CMD_ENABLE;
4160
4161         v->cvp_state = CMD_STATUS_FAIL;
4162         v->async_err = 0;
4163         ret = apr_send_pkt(apr_cvp, (uint32_t *) &cvp_enable_cmd);
4164         if (ret < 0) {
4165                 pr_err("Fail in sending VSS_IVOCPROC_CMD_ENABLE\n");
4166                 goto fail;
4167         }
4168         ret = wait_event_timeout(v->cvp_wait,
4169                                 (v->cvp_state == CMD_STATUS_SUCCESS),
4170                                 msecs_to_jiffies(TIMEOUT_MS));
4171         if (!ret) {
4172                 pr_err("%s: wait_event timeout\n", __func__);
4173                 goto fail;
4174         }
4175         if (v->async_err > 0) {
4176                 pr_err("%s: DSP returned error[%s]\n",
4177                                 __func__, adsp_err_get_err_str(
4178                                 v->async_err));
4179                 ret = adsp_err_get_lnx_err_code(
4180                                 v->async_err);
4181                 goto fail;
4182         }
4183
4184         return 0;
4185 fail:
4186         return ret;
4187 }
4188
4189 static int voice_send_mvm_cal_network_cmd(struct voice_data *v)
4190 {
4191         struct vss_imvm_cmd_set_cal_network_t mvm_set_cal_network;
4192         int ret = 0;
4193         void *apr_mvm;
4194         u16 mvm_handle;
4195
4196         if (v == NULL) {
4197                 pr_err("%s: v is NULL\n", __func__);
4198                 return -EINVAL;
4199         }
4200         apr_mvm = common.apr_q6_mvm;
4201
4202         if (!apr_mvm) {
4203                 pr_err("%s: apr_mvm is NULL.\n", __func__);
4204                 return -EINVAL;
4205         }
4206         mvm_handle = voice_get_mvm_handle(v);
4207
4208         mvm_set_cal_network.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
4209                                                 APR_HDR_LEN(APR_HDR_SIZE),
4210                                                 APR_PKT_VER);
4211         mvm_set_cal_network.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
4212                                 sizeof(mvm_set_cal_network) - APR_HDR_SIZE);
4213         mvm_set_cal_network.hdr.src_port =
4214                                 voice_get_idx_for_session(v->session_id);
4215         mvm_set_cal_network.hdr.dest_port = mvm_handle;
4216         mvm_set_cal_network.hdr.token = 0;
4217         mvm_set_cal_network.hdr.opcode = VSS_IMVM_CMD_SET_CAL_NETWORK;
4218         mvm_set_cal_network.network_id = VSS_ICOMMON_CAL_NETWORK_ID_NONE;
4219
4220         v->mvm_state = CMD_STATUS_FAIL;
4221         v->async_err = 0;
4222         ret = apr_send_pkt(apr_mvm, (uint32_t *) &mvm_set_cal_network);
4223         if (ret < 0) {
4224                 pr_err("%s: Error %d sending SET_NETWORK\n", __func__, ret);
4225                 goto fail;
4226         }
4227
4228         ret = wait_event_timeout(v->mvm_wait,
4229                                 (v->mvm_state == CMD_STATUS_SUCCESS),
4230                                  msecs_to_jiffies(TIMEOUT_MS));
4231         if (!ret) {
4232                 pr_err("%s: wait_event timeout %d\n", __func__, ret);
4233                 goto fail;
4234         }
4235         if (v->async_err > 0) {
4236                 pr_err("%s: DSP returned error[%s]\n",
4237                                 __func__, adsp_err_get_err_str(
4238                                 v->async_err));
4239                 ret = adsp_err_get_lnx_err_code(
4240                                 v->async_err);
4241                 goto fail;
4242         }
4243         return 0;
4244 fail:
4245         return ret;
4246 }
4247
4248 static int voice_send_netid_timing_cmd(struct voice_data *v)
4249 {
4250         int ret = 0;
4251         void *apr_mvm;
4252         u16 mvm_handle;
4253         struct mvm_set_network_cmd mvm_set_network;
4254         struct mvm_set_voice_timing_cmd mvm_set_voice_timing;
4255
4256         if (v == NULL) {
4257                 pr_err("%s: v is NULL\n", __func__);
4258                 return -EINVAL;
4259         }
4260         apr_mvm = common.apr_q6_mvm;
4261
4262         if (!apr_mvm) {
4263                 pr_err("%s: apr_mvm is NULL.\n", __func__);
4264                 return -EINVAL;
4265         }
4266         mvm_handle = voice_get_mvm_handle(v);
4267
4268         ret = voice_config_cvs_vocoder(v);
4269         if (ret < 0) {
4270                 pr_err("%s: Error %d configuring CVS voc",
4271                                         __func__, ret);
4272                 goto fail;
4273         }
4274         /* Set network ID. */
4275         pr_debug("Setting network ID %x\n", common.mvs_info.network_type);
4276
4277         mvm_set_network.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
4278                                                 APR_HDR_LEN(APR_HDR_SIZE),
4279                                                 APR_PKT_VER);
4280         mvm_set_network.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
4281                                         sizeof(mvm_set_network) - APR_HDR_SIZE);
4282         mvm_set_network.hdr.src_port =
4283                                 voice_get_idx_for_session(v->session_id);
4284         mvm_set_network.hdr.dest_port = mvm_handle;
4285         mvm_set_network.hdr.token = 0;
4286         mvm_set_network.hdr.opcode = VSS_IMVM_CMD_SET_CAL_NETWORK;
4287         mvm_set_network.network.network_id = common.mvs_info.network_type;
4288
4289         v->mvm_state = CMD_STATUS_FAIL;
4290         v->async_err = 0;
4291         ret = apr_send_pkt(apr_mvm, (uint32_t *) &mvm_set_network);
4292         if (ret < 0) {
4293                 pr_err("%s: Error %d sending SET_NETWORK\n", __func__, ret);
4294                 goto fail;
4295         }
4296
4297         ret = wait_event_timeout(v->mvm_wait,
4298                                 (v->mvm_state == CMD_STATUS_SUCCESS),
4299                                  msecs_to_jiffies(TIMEOUT_MS));
4300         if (!ret) {
4301                 pr_err("%s: wait_event timeout\n", __func__);
4302                 goto fail;
4303         }
4304         if (v->async_err > 0) {
4305                 pr_err("%s: DSP returned error[%s]\n",
4306                                 __func__, adsp_err_get_err_str(
4307                                 v->async_err));
4308                 ret = adsp_err_get_lnx_err_code(
4309                                 v->async_err);
4310                 goto fail;
4311         }
4312
4313         /* Set voice timing. */
4314          pr_debug("Setting voice timing\n");
4315
4316         mvm_set_voice_timing.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
4317                                                 APR_HDR_LEN(APR_HDR_SIZE),
4318                                                 APR_PKT_VER);
4319         mvm_set_voice_timing.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
4320                                                 sizeof(mvm_set_voice_timing) -
4321                                                 APR_HDR_SIZE);
4322         mvm_set_voice_timing.hdr.src_port =
4323                                 voice_get_idx_for_session(v->session_id);
4324         mvm_set_voice_timing.hdr.dest_port = mvm_handle;
4325         mvm_set_voice_timing.hdr.token = 0;
4326         mvm_set_voice_timing.hdr.opcode = VSS_ICOMMON_CMD_SET_VOICE_TIMING;
4327         mvm_set_voice_timing.timing.mode = 0;
4328         mvm_set_voice_timing.timing.enc_offset = 8000;
4329         mvm_set_voice_timing.timing.dec_req_offset = 3300;
4330         mvm_set_voice_timing.timing.dec_offset = 8300;
4331
4332         v->mvm_state = CMD_STATUS_FAIL;
4333         v->async_err = 0;
4334
4335         ret = apr_send_pkt(apr_mvm, (uint32_t *) &mvm_set_voice_timing);
4336         if (ret < 0) {
4337                 pr_err("%s: Error %d sending SET_TIMING\n", __func__, ret);
4338                 goto fail;
4339         }
4340
4341         ret = wait_event_timeout(v->mvm_wait,
4342                                 (v->mvm_state == CMD_STATUS_SUCCESS),
4343                                 msecs_to_jiffies(TIMEOUT_MS));
4344         if (!ret) {
4345                 pr_err("%s: wait_event timeout\n", __func__);
4346                 goto fail;
4347         }
4348         if (v->async_err > 0) {
4349                 pr_err("%s: DSP returned error[%s]\n",
4350                                 __func__, adsp_err_get_err_str(
4351                                 v->async_err));
4352                 ret = adsp_err_get_lnx_err_code(
4353                                 v->async_err);
4354                 goto fail;
4355         }
4356
4357         return 0;
4358 fail:
4359         return ret;
4360 }
4361
4362 static int voice_send_attach_vocproc_cmd(struct voice_data *v)
4363 {
4364         int ret = 0;
4365         struct mvm_attach_vocproc_cmd mvm_a_vocproc_cmd;
4366         void *apr_mvm;
4367         u16 mvm_handle, cvp_handle;
4368
4369         if (v == NULL) {
4370                 pr_err("%s: v is NULL\n", __func__);
4371                 return -EINVAL;
4372         }
4373         apr_mvm = common.apr_q6_mvm;
4374
4375         if (!apr_mvm) {
4376                 pr_err("%s: apr_mvm is NULL.\n", __func__);
4377                 return -EINVAL;
4378         }
4379         mvm_handle = voice_get_mvm_handle(v);
4380         cvp_handle = voice_get_cvp_handle(v);
4381
4382         /* attach vocproc and wait for response */
4383         mvm_a_vocproc_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
4384                                                 APR_HDR_LEN(APR_HDR_SIZE),
4385                                                 APR_PKT_VER);
4386         mvm_a_vocproc_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
4387                                 sizeof(mvm_a_vocproc_cmd) - APR_HDR_SIZE);
4388         pr_debug("send mvm_a_vocproc_cmd pkt size = %d\n",
4389                 mvm_a_vocproc_cmd.hdr.pkt_size);
4390         mvm_a_vocproc_cmd.hdr.src_port =
4391                                 voice_get_idx_for_session(v->session_id);
4392         mvm_a_vocproc_cmd.hdr.dest_port = mvm_handle;
4393         mvm_a_vocproc_cmd.hdr.token = 0;
4394         mvm_a_vocproc_cmd.hdr.opcode = VSS_IMVM_CMD_ATTACH_VOCPROC;
4395         mvm_a_vocproc_cmd.mvm_attach_cvp_handle.handle = cvp_handle;
4396
4397         v->mvm_state = CMD_STATUS_FAIL;
4398         v->async_err = 0;
4399         ret = apr_send_pkt(apr_mvm, (uint32_t *) &mvm_a_vocproc_cmd);
4400         if (ret < 0) {
4401                 pr_err("Fail in sending VSS_IMVM_CMD_ATTACH_VOCPROC\n");
4402                 goto fail;
4403         }
4404         ret = wait_event_timeout(v->mvm_wait,
4405                                  (v->mvm_state == CMD_STATUS_SUCCESS),
4406                                  msecs_to_jiffies(TIMEOUT_MS));
4407         if (!ret) {
4408                 pr_err("%s: wait_event timeout\n", __func__);
4409                 goto fail;
4410         }
4411         if (v->async_err > 0) {
4412                 pr_err("%s: DSP returned error[%s]\n",
4413                                 __func__, adsp_err_get_err_str(
4414                                 v->async_err));
4415                 ret = adsp_err_get_lnx_err_code(
4416                                 v->async_err);
4417                 goto fail;
4418         }
4419
4420         return 0;
4421 fail:
4422         return ret;
4423 }
4424
4425 static void voc_update_session_params(struct voice_data *v)
4426 {
4427         /* reset LCH mode */
4428         v->lch_mode = 0;
4429
4430         /* clear disable topology setting */
4431         v->disable_topology = false;
4432
4433         /* clear mute setting */
4434         v->dev_rx.dev_mute =  common.default_mute_val;
4435         v->dev_tx.dev_mute =  common.default_mute_val;
4436         v->stream_rx.stream_mute = common.default_mute_val;
4437         v->stream_tx.stream_mute = common.default_mute_val;
4438 }
4439
4440 static int voice_destroy_vocproc(struct voice_data *v)
4441 {
4442         struct mvm_detach_vocproc_cmd mvm_d_vocproc_cmd;
4443         struct apr_hdr cvp_destroy_session_cmd;
4444         struct module_instance_info mod_inst_info = {0};
4445         int ret = 0;
4446         void *apr_mvm, *apr_cvp;
4447         u16 mvm_handle, cvp_handle;
4448
4449         if (v == NULL) {
4450                 pr_err("%s: v is NULL\n", __func__);
4451                 return -EINVAL;
4452         }
4453         apr_mvm = common.apr_q6_mvm;
4454         apr_cvp = common.apr_q6_cvp;
4455
4456         if (!apr_mvm || !apr_cvp) {
4457                 pr_err("%s: apr_mvm or apr_cvp is NULL.\n", __func__);
4458                 return -EINVAL;
4459         }
4460         mvm_handle = voice_get_mvm_handle(v);
4461         cvp_handle = voice_get_cvp_handle(v);
4462
4463         mod_inst_info.module_id = MODULE_ID_VOICE_MODULE_ST;
4464         mod_inst_info.instance_id = INSTANCE_ID_0;
4465
4466         /* disable slowtalk if st_enable is set */
4467         if (v->st_enable)
4468                 voice_send_set_pp_enable_cmd(v, mod_inst_info, 0);
4469
4470         /* Disable HD Voice if hd_enable is set */
4471         if (v->hd_enable)
4472                 voice_send_hd_cmd(v, 0);
4473
4474         /* stop playback or recording */
4475         v->music_info.force = 1;
4476         voice_cvs_stop_playback(v);
4477         voice_cvs_stop_record(v);
4478         /* If voice call is active during VoLTE, SRVCC happens.
4479            Start recording on voice session if recording started during VoLTE.
4480          */
4481         if (is_volte_session(v->session_id) &&
4482             ((common.voice[VOC_PATH_PASSIVE].voc_state == VOC_RUN) ||
4483              (common.voice[VOC_PATH_PASSIVE].voc_state == VOC_CHANGE))) {
4484                 if (v->rec_info.rec_enable) {
4485                         voice_cvs_start_record(
4486                                 &common.voice[VOC_PATH_PASSIVE],
4487                                 v->rec_info.rec_mode);
4488                         common.srvcc_rec_flag = true;
4489
4490                         pr_debug("%s: switch recording, srvcc_rec_flag %d\n",
4491                                  __func__, common.srvcc_rec_flag);
4492                 }
4493         }
4494
4495         /* send stop voice cmd */
4496         voice_send_stop_voice_cmd(v);
4497
4498         /* send stop dtmf detecton cmd */
4499         if (v->dtmf_rx_detect_en)
4500                 voice_send_dtmf_rx_detection_cmd(v, 0);
4501
4502         /* detach VOCPROC and wait for response from mvm */
4503         mvm_d_vocproc_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
4504                                                 APR_HDR_LEN(APR_HDR_SIZE),
4505                                                 APR_PKT_VER);
4506         mvm_d_vocproc_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
4507                                 sizeof(mvm_d_vocproc_cmd) - APR_HDR_SIZE);
4508         pr_debug("mvm_d_vocproc_cmd  pkt size = %d\n",
4509                 mvm_d_vocproc_cmd.hdr.pkt_size);
4510         mvm_d_vocproc_cmd.hdr.src_port =
4511                                 voice_get_idx_for_session(v->session_id);
4512         mvm_d_vocproc_cmd.hdr.dest_port = mvm_handle;
4513         mvm_d_vocproc_cmd.hdr.token = 0;
4514         mvm_d_vocproc_cmd.hdr.opcode = VSS_IMVM_CMD_DETACH_VOCPROC;
4515         mvm_d_vocproc_cmd.mvm_detach_cvp_handle.handle = cvp_handle;
4516
4517         v->mvm_state = CMD_STATUS_FAIL;
4518         v->async_err = 0;
4519         ret = apr_send_pkt(apr_mvm, (uint32_t *) &mvm_d_vocproc_cmd);
4520         if (ret < 0) {
4521                 pr_err("Fail in sending VSS_IMVM_CMD_DETACH_VOCPROC\n");
4522                 goto fail;
4523         }
4524         ret = wait_event_timeout(v->mvm_wait,
4525                                  (v->mvm_state == CMD_STATUS_SUCCESS),
4526                                  msecs_to_jiffies(TIMEOUT_MS));
4527         if (!ret) {
4528                 pr_err("%s: wait_event timeout\n", __func__);
4529                 goto fail;
4530         }
4531         if (v->async_err > 0) {
4532                 pr_err("%s: DSP returned error[%s]\n",
4533                                 __func__, adsp_err_get_err_str(
4534                                 v->async_err));
4535                 ret = adsp_err_get_lnx_err_code(
4536                                 v->async_err);
4537                 goto fail;
4538         }
4539
4540         voice_send_cvp_deregister_vol_cal_cmd(v);
4541         voice_send_cvp_deregister_cal_cmd(v);
4542         voice_send_cvp_deregister_dev_cfg_cmd(v);
4543         voice_send_cvs_deregister_cal_cmd(v);
4544
4545         /* destrop cvp session */
4546         cvp_destroy_session_cmd.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
4547                                                 APR_HDR_LEN(APR_HDR_SIZE),
4548                                                 APR_PKT_VER);
4549         cvp_destroy_session_cmd.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
4550                                 sizeof(cvp_destroy_session_cmd) - APR_HDR_SIZE);
4551         pr_debug("cvp_destroy_session_cmd pkt size = %d\n",
4552                 cvp_destroy_session_cmd.pkt_size);
4553         cvp_destroy_session_cmd.src_port =
4554                                 voice_get_idx_for_session(v->session_id);
4555         cvp_destroy_session_cmd.dest_port = cvp_handle;
4556         cvp_destroy_session_cmd.token = 0;
4557         cvp_destroy_session_cmd.opcode = APRV2_IBASIC_CMD_DESTROY_SESSION;
4558
4559         v->cvp_state = CMD_STATUS_FAIL;
4560         v->async_err = 0;
4561         ret = apr_send_pkt(apr_cvp, (uint32_t *) &cvp_destroy_session_cmd);
4562         if (ret < 0) {
4563                 pr_err("Fail in sending APRV2_IBASIC_CMD_DESTROY_SESSION\n");
4564                 goto fail;
4565         }
4566         ret = wait_event_timeout(v->cvp_wait,
4567                                  (v->cvp_state == CMD_STATUS_SUCCESS),
4568                                  msecs_to_jiffies(TIMEOUT_MS));
4569         if (!ret) {
4570                 pr_err("%s: wait_event timeout\n", __func__);
4571                 goto fail;
4572         }
4573         if (v->async_err > 0) {
4574                 pr_err("%s: DSP returned error[%s]\n",
4575                                 __func__, adsp_err_get_err_str(
4576                                 v->async_err));
4577                 ret = adsp_err_get_lnx_err_code(
4578                                 v->async_err);
4579                 goto fail;
4580         }
4581
4582         rtac_remove_voice(voice_get_cvs_handle(v));
4583         cvp_handle = 0;
4584         voice_set_cvp_handle(v, cvp_handle);
4585         return 0;
4586 fail:
4587         return ret;
4588 }
4589
4590 static int voice_send_mvm_unmap_memory_physical_cmd(struct voice_data *v,
4591                                                     uint32_t mem_handle)
4592 {
4593         struct vss_imemory_cmd_unmap_t mem_unmap;
4594         int ret = 0;
4595         void *apr_mvm;
4596         u16 mvm_handle;
4597
4598         if (v == NULL) {
4599                 pr_err("%s: v is NULL\n", __func__);
4600                 return -EINVAL;
4601         }
4602         apr_mvm = common.apr_q6_mvm;
4603
4604         if (!apr_mvm) {
4605                 pr_err("%s: apr_mvm is NULL.\n", __func__);
4606                 return -EINVAL;
4607         }
4608         mvm_handle = voice_get_mvm_handle(v);
4609
4610         mem_unmap.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
4611                                                 APR_HDR_LEN(APR_HDR_SIZE),
4612                                                 APR_PKT_VER);
4613         mem_unmap.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
4614                                 sizeof(mem_unmap) - APR_HDR_SIZE);
4615         mem_unmap.hdr.src_port =
4616                                 voice_get_idx_for_session(v->session_id);
4617         mem_unmap.hdr.dest_port = mvm_handle;
4618         mem_unmap.hdr.token = 0;
4619         mem_unmap.hdr.opcode = VSS_IMEMORY_CMD_UNMAP;
4620         mem_unmap.mem_handle = mem_handle;
4621
4622         pr_debug("%s: mem_handle: 0x%x\n", __func__, mem_unmap.mem_handle);
4623
4624         v->mvm_state = CMD_STATUS_FAIL;
4625         v->async_err = 0;
4626         ret = apr_send_pkt(apr_mvm, (uint32_t *) &mem_unmap);
4627         if (ret < 0) {
4628                 pr_err("mem_unmap op[0x%x]ret[%d]\n",
4629                         mem_unmap.hdr.opcode, ret);
4630                 goto fail;
4631         }
4632
4633         ret = wait_event_timeout(v->mvm_wait,
4634                                  (v->mvm_state == CMD_STATUS_SUCCESS),
4635                                  msecs_to_jiffies(TIMEOUT_MS));
4636         if (!ret) {
4637                 pr_err("%s: wait_event timeout %d\n", __func__, ret);
4638                 goto fail;
4639         }
4640         if (v->async_err > 0) {
4641                 pr_err("%s: DSP returned error[%s]\n",
4642                                 __func__, adsp_err_get_err_str(
4643                                 v->async_err));
4644                 ret = adsp_err_get_lnx_err_code(
4645                                 v->async_err);
4646                 goto fail;
4647         }
4648         return 0;
4649
4650 fail:
4651         return ret;
4652 }
4653
4654 static int voice_send_cvs_packet_exchange_config_cmd(struct voice_data *v)
4655 {
4656         struct vss_istream_cmd_set_oob_packet_exchange_config_t
4657                                                  packet_exchange_config_pkt;
4658         int ret = 0;
4659         void *apr_cvs;
4660         u16 cvs_handle;
4661
4662         if (v == NULL) {
4663                 pr_err("%s: v is NULL\n", __func__);
4664                 return -EINVAL;
4665         }
4666
4667         apr_cvs = common.apr_q6_cvs;
4668
4669         if (!apr_cvs) {
4670                 pr_err("%s: apr_cvs is NULL.\n", __func__);
4671                 return -EINVAL;
4672         }
4673         cvs_handle = voice_get_cvs_handle(v);
4674
4675         packet_exchange_config_pkt.hdr.hdr_field = APR_HDR_FIELD(
4676                                                 APR_MSG_TYPE_SEQ_CMD,
4677                                                 APR_HDR_LEN(APR_HDR_SIZE),
4678                                                 APR_PKT_VER);
4679         packet_exchange_config_pkt.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
4680                                         sizeof(packet_exchange_config_pkt) -
4681                                          APR_HDR_SIZE);
4682         packet_exchange_config_pkt.hdr.src_port =
4683                                 voice_get_idx_for_session(v->session_id);
4684         packet_exchange_config_pkt.hdr.dest_port = cvs_handle;
4685         packet_exchange_config_pkt.hdr.token = 0;
4686         packet_exchange_config_pkt.hdr.opcode =
4687                          VSS_ISTREAM_CMD_SET_OOB_PACKET_EXCHANGE_CONFIG;
4688         packet_exchange_config_pkt.mem_handle = v->shmem_info.mem_handle;
4689         /* dec buffer address */
4690         packet_exchange_config_pkt.dec_buf_addr_lsw =
4691                 lower_32_bits(v->shmem_info.sh_buf.buf[0].phys);
4692         packet_exchange_config_pkt.dec_buf_addr_msw =
4693                 msm_audio_populate_upper_32_bits(
4694                                         v->shmem_info.sh_buf.buf[0].phys);
4695         packet_exchange_config_pkt.dec_buf_size = 4096;
4696         /* enc buffer address */
4697         packet_exchange_config_pkt.enc_buf_addr_lsw =
4698                 lower_32_bits(v->shmem_info.sh_buf.buf[1].phys);
4699         packet_exchange_config_pkt.enc_buf_addr_msw =
4700                 msm_audio_populate_upper_32_bits(
4701                                         v->shmem_info.sh_buf.buf[1].phys);
4702         packet_exchange_config_pkt.enc_buf_size = 4096;
4703
4704         pr_debug("%s: dec buf add: lsw %0x msw %0x, size %d, enc buf add: lsw %0x msw %0x, size %d\n",
4705                 __func__,
4706                 packet_exchange_config_pkt.dec_buf_addr_lsw,
4707                 packet_exchange_config_pkt.dec_buf_addr_msw,
4708                 packet_exchange_config_pkt.dec_buf_size,
4709                 packet_exchange_config_pkt.enc_buf_addr_lsw,
4710                 packet_exchange_config_pkt.enc_buf_addr_msw,
4711                 packet_exchange_config_pkt.enc_buf_size);
4712
4713         v->cvs_state = CMD_STATUS_FAIL;
4714         v->async_err = 0;
4715         ret = apr_send_pkt(apr_cvs, (uint32_t *) &packet_exchange_config_pkt);
4716         if (ret < 0) {
4717                 pr_err("Failed to send packet exchange config cmd %d\n", ret);
4718                 goto fail;
4719         }
4720
4721         ret = wait_event_timeout(v->cvs_wait,
4722                                  (v->cvs_state == CMD_STATUS_SUCCESS),
4723                                  msecs_to_jiffies(TIMEOUT_MS));
4724         if (!ret)
4725                 pr_err("%s: wait_event timeout %d\n", __func__, ret);
4726
4727         if (v->async_err > 0) {
4728                 pr_err("%s: DSP returned error[%s]\n",
4729                                 __func__, adsp_err_get_err_str(
4730                                 v->async_err));
4731                 ret = adsp_err_get_lnx_err_code(
4732                                 v->async_err);
4733                 goto fail;
4734         }
4735
4736         return 0;
4737 fail:
4738         return ret;
4739 }
4740
4741 static int voice_send_cvs_data_exchange_mode_cmd(struct voice_data *v)
4742 {
4743         struct vss_istream_cmd_set_packet_exchange_mode_t data_exchange_pkt;
4744         int ret = 0;
4745         void *apr_cvs;
4746         u16 cvs_handle;
4747
4748         if (v == NULL) {
4749                 pr_err("%s: v is NULL\n", __func__);
4750                 return -EINVAL;
4751         }
4752         apr_cvs = common.apr_q6_cvs;
4753
4754         if (!apr_cvs) {
4755                 pr_err("%s: apr_cvs is NULL.\n", __func__);
4756                 return -EINVAL;
4757         }
4758         cvs_handle = voice_get_cvs_handle(v);
4759
4760         data_exchange_pkt.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
4761                                                 APR_HDR_LEN(APR_HDR_SIZE),
4762                                                 APR_PKT_VER);
4763         data_exchange_pkt.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
4764                                 sizeof(data_exchange_pkt) - APR_HDR_SIZE);
4765         data_exchange_pkt.hdr.src_port =
4766                                 voice_get_idx_for_session(v->session_id);
4767         data_exchange_pkt.hdr.dest_port = cvs_handle;
4768         data_exchange_pkt.hdr.token = 0;
4769         data_exchange_pkt.hdr.opcode = VSS_ISTREAM_CMD_SET_PACKET_EXCHANGE_MODE;
4770         data_exchange_pkt.mode = VSS_ISTREAM_PACKET_EXCHANGE_MODE_OUT_OF_BAND;
4771
4772         v->cvs_state = CMD_STATUS_FAIL;
4773         v->async_err = 0;
4774         ret = apr_send_pkt(apr_cvs, (uint32_t *) &data_exchange_pkt);
4775         if (ret < 0) {
4776                 pr_err("Failed to send data exchange mode %d\n", ret);
4777                 goto fail;
4778         }
4779
4780         ret = wait_event_timeout(v->cvs_wait,
4781                                  (v->cvs_state == CMD_STATUS_SUCCESS),
4782                                  msecs_to_jiffies(TIMEOUT_MS));
4783         if (!ret)
4784                 pr_err("%s: wait_event timeout %d\n", __func__, ret);
4785
4786         if (v->async_err > 0) {
4787                 pr_err("%s: DSP returned error[%s]\n",
4788                                 __func__, adsp_err_get_err_str(
4789                                 v->async_err));
4790                 ret = adsp_err_get_lnx_err_code(
4791                                 v->async_err);
4792                 goto fail;
4793         }
4794         return 0;
4795 fail:
4796         return ret;
4797 }
4798
4799 static int voice_send_stream_mute_cmd(struct voice_data *v, uint16_t direction,
4800                                      uint16_t mute_flag, uint32_t ramp_duration)
4801 {
4802         struct cvs_set_mute_cmd cvs_mute_cmd;
4803         int ret = 0;
4804
4805         if (v == NULL) {
4806                 pr_err("%s: v is NULL\n", __func__);
4807                 ret = -EINVAL;
4808                 goto fail;
4809         }
4810
4811         if (!common.apr_q6_cvs) {
4812                 pr_err("%s: apr_cvs is NULL.\n", __func__);
4813                 ret = -EINVAL;
4814                 goto fail;
4815         }
4816
4817         /* send mute/unmute to cvs */
4818         cvs_mute_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
4819                                                 APR_HDR_LEN(APR_HDR_SIZE),
4820                                                 APR_PKT_VER);
4821         cvs_mute_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
4822                                         sizeof(cvs_mute_cmd) - APR_HDR_SIZE);
4823         cvs_mute_cmd.hdr.src_port =
4824                                 voice_get_idx_for_session(v->session_id);
4825         cvs_mute_cmd.hdr.dest_port = voice_get_cvs_handle(v);
4826         cvs_mute_cmd.hdr.token = 0;
4827         cvs_mute_cmd.hdr.opcode = VSS_IVOLUME_CMD_MUTE_V2;
4828         cvs_mute_cmd.cvs_set_mute.direction = direction;
4829         cvs_mute_cmd.cvs_set_mute.mute_flag = mute_flag;
4830         cvs_mute_cmd.cvs_set_mute.ramp_duration_ms = ramp_duration;
4831
4832         v->cvs_state = CMD_STATUS_FAIL;
4833         v->async_err = 0;
4834         ret = apr_send_pkt(common.apr_q6_cvs, (uint32_t *) &cvs_mute_cmd);
4835         if (ret < 0) {
4836                 pr_err("%s: Error %d sending stream mute\n", __func__, ret);
4837
4838                 goto fail;
4839         }
4840         ret = wait_event_timeout(v->cvs_wait,
4841                                  (v->cvs_state == CMD_STATUS_SUCCESS),
4842                                  msecs_to_jiffies(TIMEOUT_MS));
4843         if (!ret) {
4844                 pr_err("%s: Command timeout\n", __func__);
4845                 goto fail;
4846         }
4847         if (v->async_err > 0) {
4848                 pr_err("%s: DSP returned error[%s]\n",
4849                                 __func__, adsp_err_get_err_str(
4850                                 v->async_err));
4851                 ret = adsp_err_get_lnx_err_code(
4852                                 v->async_err);
4853                 goto fail;
4854         }
4855
4856         return 0;
4857
4858 fail:
4859         return ret;
4860 }
4861
4862 static int voice_send_device_mute_cmd(struct voice_data *v, uint16_t direction,
4863                                      uint16_t mute_flag, uint32_t ramp_duration)
4864 {
4865         struct cvp_set_mute_cmd cvp_mute_cmd;
4866         int ret = 0;
4867
4868         if (v == NULL) {
4869                 pr_err("%s: v is NULL\n", __func__);
4870                 ret = -EINVAL;
4871                 goto fail;
4872         }
4873
4874         if (!common.apr_q6_cvp) {
4875                 pr_err("%s: apr_cvp is NULL.\n", __func__);
4876                 ret = -EINVAL;
4877                 goto fail;
4878         }
4879
4880         cvp_mute_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
4881                                                 APR_HDR_LEN(APR_HDR_SIZE),
4882                                                 APR_PKT_VER);
4883         cvp_mute_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
4884                                         sizeof(cvp_mute_cmd) - APR_HDR_SIZE);
4885         cvp_mute_cmd.hdr.src_port =
4886                                 voice_get_idx_for_session(v->session_id);
4887         cvp_mute_cmd.hdr.dest_port = voice_get_cvp_handle(v);
4888         cvp_mute_cmd.hdr.token = 0;
4889         cvp_mute_cmd.hdr.opcode = VSS_IVOLUME_CMD_MUTE_V2;
4890         cvp_mute_cmd.cvp_set_mute.direction = direction;
4891         cvp_mute_cmd.cvp_set_mute.mute_flag = mute_flag;
4892         cvp_mute_cmd.cvp_set_mute.ramp_duration_ms = ramp_duration;
4893
4894         v->cvp_state = CMD_STATUS_FAIL;
4895         v->async_err = 0;
4896         ret = apr_send_pkt(common.apr_q6_cvp, (uint32_t *) &cvp_mute_cmd);
4897         if (ret < 0) {
4898                 pr_err("%s: Error %d sending rx device cmd\n", __func__, ret);
4899
4900                 goto fail;
4901         }
4902         ret = wait_event_timeout(v->cvp_wait,
4903                                  (v->cvp_state == CMD_STATUS_SUCCESS),
4904                                  msecs_to_jiffies(TIMEOUT_MS));
4905         if (!ret) {
4906                 pr_err("%s: Command timeout\n", __func__);
4907                 goto fail;
4908         }
4909         if (v->async_err > 0) {
4910                 pr_err("%s: DSP returned error[%s]\n",
4911                                 __func__, adsp_err_get_err_str(
4912                                 v->async_err));
4913                 ret = adsp_err_get_lnx_err_code(
4914                                 v->async_err);
4915                 goto fail;
4916         }
4917
4918         return 0;
4919
4920 fail:
4921         return ret;
4922 }
4923
4924 static int voice_send_vol_step_cmd(struct voice_data *v)
4925 {
4926         struct cvp_set_rx_volume_step_cmd cvp_vol_step_cmd;
4927         int ret = 0;
4928         void *apr_cvp;
4929         u16 cvp_handle;
4930         if (v == NULL) {
4931                 pr_err("%s: v is NULL\n", __func__);
4932                 return -EINVAL;
4933         }
4934         apr_cvp = common.apr_q6_cvp;
4935
4936         if (!apr_cvp) {
4937                 pr_err("%s: apr_cvp is NULL.\n", __func__);
4938                 return -EINVAL;
4939         }
4940         cvp_handle = voice_get_cvp_handle(v);
4941
4942         /* send volume index to cvp */
4943         cvp_vol_step_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
4944                                                 APR_HDR_LEN(APR_HDR_SIZE),
4945                                                 APR_PKT_VER);
4946         cvp_vol_step_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
4947                                 sizeof(cvp_vol_step_cmd) - APR_HDR_SIZE);
4948         cvp_vol_step_cmd.hdr.src_port =
4949                                 voice_get_idx_for_session(v->session_id);
4950         cvp_vol_step_cmd.hdr.dest_port = cvp_handle;
4951         cvp_vol_step_cmd.hdr.token = 0;
4952         cvp_vol_step_cmd.hdr.opcode = VSS_IVOLUME_CMD_SET_STEP;
4953         cvp_vol_step_cmd.cvp_set_vol_step.direction = VSS_IVOLUME_DIRECTION_RX;
4954         cvp_vol_step_cmd.cvp_set_vol_step.value = v->dev_rx.volume_step_value;
4955         cvp_vol_step_cmd.cvp_set_vol_step.ramp_duration_ms =
4956                                         v->dev_rx.volume_ramp_duration_ms;
4957          pr_debug("%s step_value:%d, ramp_duration_ms:%d",
4958                         __func__,
4959                         cvp_vol_step_cmd.cvp_set_vol_step.value,
4960                         cvp_vol_step_cmd.cvp_set_vol_step.ramp_duration_ms);
4961
4962         v->cvp_state = CMD_STATUS_FAIL;
4963         v->async_err = 0;
4964         ret = apr_send_pkt(apr_cvp, (uint32_t *) &cvp_vol_step_cmd);
4965         if (ret < 0) {
4966                 pr_err("Fail in sending RX VOL step\n");
4967                 return -EINVAL;
4968         }
4969         ret = wait_event_timeout(v->cvp_wait,
4970                                  (v->cvp_state == CMD_STATUS_SUCCESS),
4971                                  msecs_to_jiffies(TIMEOUT_MS));
4972         if (!ret) {
4973                 pr_err("%s: wait_event timeout\n", __func__);
4974                 return -EINVAL;
4975         }
4976         if (v->async_err > 0) {
4977                 pr_err("%s: DSP returned error[%s]\n",
4978                                 __func__, adsp_err_get_err_str(
4979                                 v->async_err));
4980                 ret = adsp_err_get_lnx_err_code(
4981                                 v->async_err);
4982                 return ret;
4983         }
4984         return 0;
4985 }
4986
4987 static int voice_cvs_start_record(struct voice_data *v, uint32_t rec_mode)
4988 {
4989         int ret = 0;
4990         void *apr_cvs;
4991         u16 cvs_handle;
4992
4993         struct cvs_start_record_cmd cvs_start_record;
4994
4995         if (v == NULL) {
4996                 pr_err("%s: v is NULL\n", __func__);
4997                 return -EINVAL;
4998         }
4999         apr_cvs = common.apr_q6_cvs;
5000
5001         if (!apr_cvs) {
5002                 pr_err("%s: apr_cvs is NULL.\n", __func__);
5003                 return -EINVAL;
5004         }
5005
5006         cvs_handle = voice_get_cvs_handle(v);
5007
5008         if (!v->rec_info.recording) {
5009                 cvs_start_record.hdr.hdr_field = APR_HDR_FIELD(
5010                                         APR_MSG_TYPE_SEQ_CMD,
5011                                         APR_HDR_LEN(APR_HDR_SIZE),
5012                                         APR_PKT_VER);
5013                 cvs_start_record.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
5014                                   sizeof(cvs_start_record) - APR_HDR_SIZE);
5015                 cvs_start_record.hdr.src_port =
5016                                 voice_get_idx_for_session(v->session_id);
5017                 cvs_start_record.hdr.dest_port = cvs_handle;
5018                 cvs_start_record.hdr.token = 0;
5019                 cvs_start_record.hdr.opcode = VSS_IRECORD_CMD_START;
5020
5021                 cvs_start_record.rec_mode.port_id =
5022                                         VSS_IRECORD_PORT_ID_DEFAULT;
5023                 if (rec_mode == VOC_REC_UPLINK) {
5024                         cvs_start_record.rec_mode.rx_tap_point =
5025                                         VSS_IRECORD_TAP_POINT_NONE;
5026                         cvs_start_record.rec_mode.tx_tap_point =
5027                                         VSS_IRECORD_TAP_POINT_STREAM_END;
5028                 } else if (rec_mode == VOC_REC_DOWNLINK) {
5029                         cvs_start_record.rec_mode.rx_tap_point =
5030                                         VSS_IRECORD_TAP_POINT_STREAM_END;
5031                         cvs_start_record.rec_mode.tx_tap_point =
5032                                         VSS_IRECORD_TAP_POINT_NONE;
5033                 } else if (rec_mode == VOC_REC_BOTH) {
5034                         cvs_start_record.rec_mode.rx_tap_point =
5035                                         VSS_IRECORD_TAP_POINT_STREAM_END;
5036                         cvs_start_record.rec_mode.tx_tap_point =
5037                                         VSS_IRECORD_TAP_POINT_STREAM_END;
5038                 } else {
5039                         pr_err("%s: Invalid in-call rec_mode %d\n", __func__,
5040                                 rec_mode);
5041
5042                         ret = -EINVAL;
5043                         goto fail;
5044                 }
5045
5046                 v->cvs_state = CMD_STATUS_FAIL;
5047                 v->async_err = 0;
5048
5049                 ret = apr_send_pkt(apr_cvs, (uint32_t *) &cvs_start_record);
5050                 if (ret < 0) {
5051                         pr_err("%s: Error %d sending START_RECORD\n", __func__,
5052                                 ret);
5053
5054                         goto fail;
5055                 }
5056
5057                 ret = wait_event_timeout(v->cvs_wait,
5058                                  (v->cvs_state == CMD_STATUS_SUCCESS),
5059                                  msecs_to_jiffies(TIMEOUT_MS));
5060
5061                 if (!ret) {
5062                         pr_err("%s: wait_event timeout\n", __func__);
5063
5064                         goto fail;
5065                 }
5066                 if (v->async_err > 0) {
5067                         pr_err("%s: DSP returned error[%s]\n",
5068                                 __func__, adsp_err_get_err_str(
5069                                 v->async_err));
5070                         ret = adsp_err_get_lnx_err_code(
5071                                         v->async_err);
5072                         goto fail;
5073                 }
5074                 v->rec_info.recording = 1;
5075         } else {
5076                 pr_debug("%s: Start record already sent\n", __func__);
5077         }
5078
5079         return 0;
5080
5081 fail:
5082         return ret;
5083 }
5084
5085 static int voice_cvs_stop_record(struct voice_data *v)
5086 {
5087         int ret = 0;
5088         void *apr_cvs;
5089         u16 cvs_handle;
5090         struct apr_hdr cvs_stop_record;
5091
5092         if (v == NULL) {
5093                 pr_err("%s: v is NULL\n", __func__);
5094                 return -EINVAL;
5095         }
5096         apr_cvs = common.apr_q6_cvs;
5097
5098         if (!apr_cvs) {
5099                 pr_err("%s: apr_cvs is NULL.\n", __func__);
5100                 return -EINVAL;
5101         }
5102
5103         cvs_handle = voice_get_cvs_handle(v);
5104
5105         if (v->rec_info.recording) {
5106                 cvs_stop_record.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
5107                                   APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
5108                 cvs_stop_record.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
5109                                   sizeof(cvs_stop_record) - APR_HDR_SIZE);
5110                 cvs_stop_record.src_port =
5111                                 voice_get_idx_for_session(v->session_id);
5112                 cvs_stop_record.dest_port = cvs_handle;
5113                 cvs_stop_record.token = 0;
5114                 cvs_stop_record.opcode = VSS_IRECORD_CMD_STOP;
5115
5116                 v->cvs_state = CMD_STATUS_FAIL;
5117                 v->async_err = 0;
5118
5119                 ret = apr_send_pkt(apr_cvs, (uint32_t *) &cvs_stop_record);
5120                 if (ret < 0) {
5121                         pr_err("%s: Error %d sending STOP_RECORD\n",
5122                                 __func__, ret);
5123
5124                         goto fail;
5125                 }
5126
5127                 ret = wait_event_timeout(v->cvs_wait,
5128                                  (v->cvs_state == CMD_STATUS_SUCCESS),
5129                                  msecs_to_jiffies(TIMEOUT_MS));
5130                 if (!ret) {
5131                         pr_err("%s: wait_event timeout\n", __func__);
5132
5133                         goto fail;
5134                 }
5135                 if (v->async_err > 0) {
5136                         pr_err("%s: DSP returned error[%s]\n",
5137                                 __func__, adsp_err_get_err_str(
5138                                 v->async_err));
5139                         ret = adsp_err_get_lnx_err_code(
5140                                         v->async_err);
5141                         goto fail;
5142                 }
5143                 v->rec_info.recording = 0;
5144         } else {
5145                 pr_debug("%s: Stop record already sent\n", __func__);
5146         }
5147
5148         return 0;
5149
5150 fail:
5151         return ret;
5152 }
5153
5154 int voc_start_record(uint32_t port_id, uint32_t set, uint32_t session_id)
5155 {
5156         int ret = 0;
5157         int rec_mode = 0;
5158         u16 cvs_handle;
5159         int rec_set = 0;
5160         struct voice_session_itr itr;
5161         struct voice_data *v = NULL;
5162
5163         /* check if session_id is valid */
5164         if (!voice_is_valid_session_id(session_id)) {
5165                 pr_err("%s: Invalid session id:%u\n", __func__,
5166                        session_id);
5167
5168                 return -EINVAL;
5169         }
5170
5171         voice_itr_init(&itr, session_id);
5172         pr_debug("%s: session_id:%u\n", __func__, session_id);
5173
5174         while (voice_itr_get_next_session(&itr, &v)) {
5175                 if (v == NULL) {
5176                         pr_err("%s: v is NULL, sessionid:%u\n", __func__,
5177                                 session_id);
5178
5179                         break;
5180                 }
5181                 pr_debug("%s: port_id: %d, set: %d, v: %pK\n",
5182                          __func__, port_id, set, v);
5183
5184                 mutex_lock(&v->lock);
5185                 rec_mode = v->rec_info.rec_mode;
5186                 rec_set = set;
5187                 if (set) {
5188                         if ((v->rec_route_state.ul_flag != 0) &&
5189                                 (v->rec_route_state.dl_flag != 0)) {
5190                                 pr_debug("%s: rec mode already set.\n",
5191                                         __func__);
5192
5193                                 mutex_unlock(&v->lock);
5194                                 continue;
5195                         }
5196
5197                         if (port_id == VOICE_RECORD_TX) {
5198                                 if ((v->rec_route_state.ul_flag == 0)
5199                                 && (v->rec_route_state.dl_flag == 0)) {
5200                                         rec_mode = VOC_REC_UPLINK;
5201                                         v->rec_route_state.ul_flag = 1;
5202                                 } else if ((v->rec_route_state.ul_flag == 0)
5203                                         && (v->rec_route_state.dl_flag != 0)) {
5204                                         voice_cvs_stop_record(v);
5205                                         rec_mode = VOC_REC_BOTH;
5206                                         v->rec_route_state.ul_flag = 1;
5207                                 }
5208                         } else if (port_id == VOICE_RECORD_RX) {
5209                                 if ((v->rec_route_state.ul_flag == 0)
5210                                         && (v->rec_route_state.dl_flag == 0)) {
5211                                         rec_mode = VOC_REC_DOWNLINK;
5212                                         v->rec_route_state.dl_flag = 1;
5213                                 } else if ((v->rec_route_state.ul_flag != 0)
5214                                         && (v->rec_route_state.dl_flag == 0)) {
5215                                         voice_cvs_stop_record(v);
5216                                         rec_mode = VOC_REC_BOTH;
5217                                         v->rec_route_state.dl_flag = 1;
5218                                 }
5219                         }
5220                         rec_set = 1;
5221                 } else {
5222                         if ((v->rec_route_state.ul_flag == 0) &&
5223                                 (v->rec_route_state.dl_flag == 0)) {
5224                                 pr_debug("%s: rec already stops.\n",
5225                                         __func__);
5226                                 mutex_unlock(&v->lock);
5227                                 continue;
5228                         }
5229
5230                         if (port_id == VOICE_RECORD_TX) {
5231                                 if ((v->rec_route_state.ul_flag != 0)
5232                                         && (v->rec_route_state.dl_flag == 0)) {
5233                                         v->rec_route_state.ul_flag = 0;
5234                                         rec_set = 0;
5235                                 } else if ((v->rec_route_state.ul_flag != 0)
5236                                         && (v->rec_route_state.dl_flag != 0)) {
5237                                         voice_cvs_stop_record(v);
5238                                         v->rec_route_state.ul_flag = 0;
5239                                         rec_mode = VOC_REC_DOWNLINK;
5240                                         rec_set = 1;
5241                                 }
5242                         } else if (port_id == VOICE_RECORD_RX) {
5243                                 if ((v->rec_route_state.ul_flag == 0)
5244                                         && (v->rec_route_state.dl_flag != 0)) {
5245                                         v->rec_route_state.dl_flag = 0;
5246                                         rec_set = 0;
5247                                 } else if ((v->rec_route_state.ul_flag != 0)
5248                                         && (v->rec_route_state.dl_flag != 0)) {
5249                                         voice_cvs_stop_record(v);
5250                                         v->rec_route_state.dl_flag = 0;
5251                                         rec_mode = VOC_REC_UPLINK;
5252                                         rec_set = 1;
5253                                 }
5254                         }
5255                 }
5256                 pr_debug("%s: mode =%d, set =%d\n", __func__,
5257                          rec_mode, rec_set);
5258                 cvs_handle = voice_get_cvs_handle(v);
5259
5260                 if (cvs_handle != 0) {
5261                         if (rec_set)
5262                                 ret = voice_cvs_start_record(v, rec_mode);
5263                         else
5264                                 ret = voice_cvs_stop_record(v);
5265                 }
5266
5267                 /* During SRVCC, recording will switch from VoLTE session to
5268                    voice session.
5269                    Then stop recording, need to stop recording on voice session.
5270                  */
5271                 if ((!rec_set) && common.srvcc_rec_flag) {
5272                         pr_debug("%s, srvcc_rec_flag:%d\n",  __func__,
5273                                  common.srvcc_rec_flag);
5274
5275                         voice_cvs_stop_record(&common.voice[VOC_PATH_PASSIVE]);
5276                         common.srvcc_rec_flag = false;
5277                 }
5278
5279                 /* Cache the value */
5280                 v->rec_info.rec_enable = rec_set;
5281                 v->rec_info.rec_mode = rec_mode;
5282
5283                 mutex_unlock(&v->lock);
5284         }
5285
5286         return ret;
5287 }
5288
5289 static int voice_cvs_start_playback(struct voice_data *v)
5290 {
5291         int ret = 0;
5292         struct cvs_start_playback_cmd cvs_start_playback;
5293         void *apr_cvs;
5294         u16 cvs_handle;
5295
5296         if (v == NULL) {
5297                 pr_err("%s: v is NULL\n", __func__);
5298                 return -EINVAL;
5299         }
5300         apr_cvs = common.apr_q6_cvs;
5301
5302         if (!apr_cvs) {
5303                 pr_err("%s: apr_cvs is NULL.\n", __func__);
5304                 return -EINVAL;
5305         }
5306
5307         cvs_handle = voice_get_cvs_handle(v);
5308
5309         if (!v->music_info.playing && v->music_info.count) {
5310                 cvs_start_playback.hdr.hdr_field = APR_HDR_FIELD(
5311                                         APR_MSG_TYPE_SEQ_CMD,
5312                                         APR_HDR_LEN(APR_HDR_SIZE),
5313                                         APR_PKT_VER);
5314                 cvs_start_playback.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
5315                                 sizeof(cvs_start_playback) - APR_HDR_SIZE);
5316                 cvs_start_playback.hdr.src_port =
5317                                 voice_get_idx_for_session(v->session_id);
5318                 cvs_start_playback.hdr.dest_port = cvs_handle;
5319                 cvs_start_playback.hdr.token = 0;
5320                 cvs_start_playback.hdr.opcode = VSS_IPLAYBACK_CMD_START;
5321                 cvs_start_playback.playback_mode.port_id =
5322                                                 v->music_info.port_id;
5323
5324                 v->cvs_state = CMD_STATUS_FAIL;
5325                 v->async_err = 0;
5326
5327                 ret = apr_send_pkt(apr_cvs, (uint32_t *) &cvs_start_playback);
5328
5329                 if (ret < 0) {
5330                         pr_err("%s: Error %d sending START_PLAYBACK\n",
5331                                 __func__, ret);
5332
5333                         goto fail;
5334                 }
5335
5336                 ret = wait_event_timeout(v->cvs_wait,
5337                                  (v->cvs_state == CMD_STATUS_SUCCESS),
5338                                  msecs_to_jiffies(TIMEOUT_MS));
5339                 if (!ret) {
5340                         pr_err("%s: wait_event timeout\n", __func__);
5341
5342                         goto fail;
5343                 }
5344                 if (v->async_err > 0) {
5345                         pr_err("%s: DSP returned error[%s]\n",
5346                                 __func__, adsp_err_get_err_str(
5347                                 v->async_err));
5348                         ret = adsp_err_get_lnx_err_code(
5349                                         v->async_err);
5350                         goto fail;
5351                 }
5352
5353                 v->music_info.playing = 1;
5354         } else {
5355                 pr_debug("%s: Start playback already sent\n", __func__);
5356         }
5357
5358         return 0;
5359
5360 fail:
5361         return ret;
5362 }
5363
5364 static int voice_cvs_stop_playback(struct voice_data *v)
5365 {
5366          int ret = 0;
5367          struct apr_hdr cvs_stop_playback;
5368          void *apr_cvs;
5369          u16 cvs_handle;
5370
5371         if (v == NULL) {
5372                 pr_err("%s: v is NULL\n", __func__);
5373                 return -EINVAL;
5374         }
5375         apr_cvs = common.apr_q6_cvs;
5376
5377         if (!apr_cvs) {
5378                 pr_err("%s: apr_cvs is NULL.\n", __func__);
5379                 return -EINVAL;
5380         }
5381
5382         cvs_handle = voice_get_cvs_handle(v);
5383
5384         if (v->music_info.playing && ((!v->music_info.count) ||
5385                                                 (v->music_info.force))) {
5386                 cvs_stop_playback.hdr_field =
5387                                 APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
5388                                 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
5389                 cvs_stop_playback.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
5390                                 sizeof(cvs_stop_playback) - APR_HDR_SIZE);
5391                 cvs_stop_playback.src_port =
5392                                 voice_get_idx_for_session(v->session_id);
5393                 cvs_stop_playback.dest_port = cvs_handle;
5394                 cvs_stop_playback.token = 0;
5395
5396                 cvs_stop_playback.opcode = VSS_IPLAYBACK_CMD_STOP;
5397
5398                 v->cvs_state = CMD_STATUS_FAIL;
5399                 v->async_err = 0;
5400
5401                 ret = apr_send_pkt(apr_cvs, (uint32_t *) &cvs_stop_playback);
5402                 if (ret < 0) {
5403                         pr_err("%s: Error %d sending STOP_PLAYBACK\n",
5404                                __func__, ret);
5405
5406
5407                         goto fail;
5408                 }
5409
5410                 ret = wait_event_timeout(v->cvs_wait,
5411                                          (v->cvs_state == CMD_STATUS_SUCCESS),
5412                                          msecs_to_jiffies(TIMEOUT_MS));
5413                 if (!ret) {
5414                         pr_err("%s: wait_event timeout\n", __func__);
5415
5416                         goto fail;
5417                 }
5418                 if (v->async_err > 0) {
5419                         pr_err("%s: DSP returned error[%s]\n",
5420                                 __func__, adsp_err_get_err_str(
5421                                 v->async_err));
5422                         ret = adsp_err_get_lnx_err_code(
5423                                         v->async_err);
5424                         goto fail;
5425                 }
5426
5427                 v->music_info.playing = 0;
5428                 v->music_info.force = 0;
5429         } else {
5430                 pr_debug("%s: Stop playback already sent\n", __func__);
5431         }
5432
5433         return 0;
5434
5435 fail:
5436         return ret;
5437 }
5438
5439 static int voc_lch_ops(struct voice_data *v, enum voice_lch_mode lch_mode)
5440 {
5441         int ret = 0;
5442
5443         if (v == NULL) {
5444                 pr_err("%s: v is NULL\n", __func__);
5445
5446                 ret = -EINVAL;
5447                 goto done;
5448         }
5449
5450         switch (lch_mode) {
5451         case VOICE_LCH_START:
5452
5453                 ret = voc_end_voice_call(v->session_id);
5454                 if (ret < 0)
5455                         pr_err("%s: voice call end failed %d\n",
5456                                 __func__, ret);
5457                 break;
5458         case VOICE_LCH_STOP:
5459
5460                 ret = voc_start_voice_call(v->session_id);
5461                 if (ret < 0) {
5462                         pr_err("%s: voice call start failed %d\n",
5463                                 __func__, ret);
5464                         goto done;
5465                 }
5466                 break;
5467         default:
5468                 pr_err("%s: Invalid LCH mode: %d\n",
5469                         __func__, v->lch_mode);
5470                 break;
5471         }
5472 done:
5473         return ret;
5474 }
5475
5476 int voc_start_playback(uint32_t set, uint16_t port_id)
5477 {
5478         struct voice_data *v = NULL;
5479         int ret = 0;
5480         struct voice_session_itr itr;
5481         u16 cvs_handle;
5482
5483         pr_debug("%s port_id = %#x set = %d", __func__, port_id, set);
5484
5485         voice_itr_init(&itr, ALL_SESSION_VSID);
5486         while (voice_itr_get_next_session(&itr, &v)) {
5487                 if ((v != NULL) &&
5488                     (((port_id == VOICE_PLAYBACK_TX) &&
5489                        is_sub1_vsid(v->session_id)) ||
5490                      ((port_id == VOICE2_PLAYBACK_TX) &&
5491                        is_sub2_vsid(v->session_id)))) {
5492
5493                         mutex_lock(&v->lock);
5494                         v->music_info.port_id = port_id;
5495                         v->music_info.play_enable = set;
5496                         if (set)
5497                                 v->music_info.count++;
5498                         else
5499                                 v->music_info.count--;
5500                         pr_debug("%s: music_info count=%d\n", __func__,
5501                                  v->music_info.count);
5502
5503                         cvs_handle = voice_get_cvs_handle(v);
5504                         if (cvs_handle != 0) {
5505                                 if (set)
5506                                         ret = voice_cvs_start_playback(v);
5507                                 else
5508                                         ret = voice_cvs_stop_playback(v);
5509                         }
5510                         mutex_unlock(&v->lock);
5511                 } else {
5512                         pr_err("%s: Invalid session\n", __func__);
5513                 }
5514         }
5515
5516         return ret;
5517 }
5518
5519 int voc_disable_topology(uint32_t session_id, uint32_t disable)
5520 {
5521         struct voice_data *v = voice_get_session(session_id);
5522         int ret = 0;
5523
5524         if (v == NULL) {
5525                 pr_err("%s: invalid session_id 0x%x\n", __func__, session_id);
5526
5527                 return -EINVAL;
5528         }
5529
5530         mutex_lock(&v->lock);
5531
5532         v->disable_topology = disable;
5533
5534         mutex_unlock(&v->lock);
5535
5536         return ret;
5537 }
5538
5539 static int voice_set_packet_exchange_mode_and_config(uint32_t session_id,
5540                                                  uint32_t mode)
5541 {
5542         struct voice_data *v = voice_get_session(session_id);
5543         int ret = 0;
5544
5545         if (v == NULL) {
5546                 pr_err("%s: invalid session_id 0x%x\n", __func__, session_id);
5547                 return -EINVAL;
5548         }
5549
5550         if (v->voc_state != VOC_RUN)
5551                 ret = voice_send_cvs_data_exchange_mode_cmd(v);
5552
5553         if (ret) {
5554                 pr_err("%s: Error voice_send_data_exchange_mode_cmd %d\n",
5555                         __func__, ret);
5556                 goto fail;
5557         }
5558
5559         ret = voice_send_cvs_packet_exchange_config_cmd(v);
5560         if (ret) {
5561                 pr_err("%s: Error: voice_send_packet_exchange_config_cmd %d\n",
5562                         __func__, ret);
5563                 goto fail;
5564         }
5565
5566         return ret;
5567 fail:
5568         return -EINVAL;
5569 }
5570
5571 int voc_set_tx_mute(uint32_t session_id, uint32_t dir, uint32_t mute,
5572                     uint32_t ramp_duration)
5573 {
5574         struct voice_data *v = NULL;
5575         int ret = 0;
5576         struct voice_session_itr itr;
5577
5578         voice_itr_init(&itr, session_id);
5579         while (voice_itr_get_next_session(&itr, &v)) {
5580                 if (v != NULL) {
5581                         mutex_lock(&v->lock);
5582                         v->stream_tx.stream_mute = mute;
5583                         v->stream_tx.stream_mute_ramp_duration_ms =
5584                                                                 ramp_duration;
5585                         if (is_voc_state_active(v->voc_state) &&
5586                                 (v->lch_mode == 0))
5587                                 ret = voice_send_stream_mute_cmd(v,
5588                                 VSS_IVOLUME_DIRECTION_TX,
5589                                 v->stream_tx.stream_mute,
5590                                 v->stream_tx.stream_mute_ramp_duration_ms);
5591                         mutex_unlock(&v->lock);
5592                 } else {
5593                         pr_err("%s: invalid session_id 0x%x\n", __func__,
5594                                 session_id);
5595
5596                         ret = -EINVAL;
5597                         break;
5598                 }
5599         }
5600
5601         return ret;
5602 }
5603
5604 int voc_set_device_mute(uint32_t session_id, uint32_t dir, uint32_t mute,
5605                         uint32_t ramp_duration)
5606 {
5607         struct voice_data *v = NULL;
5608         int ret = 0;
5609         struct voice_session_itr itr;
5610
5611         voice_itr_init(&itr, session_id);
5612         while (voice_itr_get_next_session(&itr, &v)) {
5613                 if (v != NULL) {
5614                         mutex_lock(&v->lock);
5615                         if (dir == VSS_IVOLUME_DIRECTION_TX) {
5616                                 v->dev_tx.dev_mute = mute;
5617                                 v->dev_tx.dev_mute_ramp_duration_ms =
5618                                                         ramp_duration;
5619                         } else {
5620                                 v->dev_rx.dev_mute = mute;
5621                                 v->dev_rx.dev_mute_ramp_duration_ms =
5622                                                         ramp_duration;
5623                         }
5624
5625                         if (((v->voc_state == VOC_RUN) ||
5626                                 (v->voc_state == VOC_STANDBY)) &&
5627                                 (v->lch_mode == 0))
5628                                 ret = voice_send_device_mute_cmd(v,
5629                                                         dir,
5630                                                         mute,
5631                                                         ramp_duration);
5632                         mutex_unlock(&v->lock);
5633                 } else {
5634                         pr_err("%s: invalid session_id 0x%x\n", __func__,
5635                                 session_id);
5636
5637                         ret = -EINVAL;
5638                         break;
5639                 }
5640         }
5641
5642         return ret;
5643 }
5644
5645 int voc_get_rx_device_mute(uint32_t session_id)
5646 {
5647         struct voice_data *v = voice_get_session(session_id);
5648         int ret = 0;
5649
5650         if (v == NULL) {
5651                 pr_err("%s: invalid session_id 0x%x\n", __func__, session_id);
5652
5653                 return -EINVAL;
5654         }
5655
5656         mutex_lock(&v->lock);
5657
5658         ret = v->dev_rx.dev_mute;
5659
5660         mutex_unlock(&v->lock);
5661
5662         return ret;
5663 }
5664
5665 int voc_set_tty_mode(uint32_t session_id, uint8_t tty_mode)
5666 {
5667         struct voice_data *v = voice_get_session(session_id);
5668         int ret = 0;
5669
5670         if (v == NULL) {
5671                 pr_err("%s: invalid session_id 0x%x\n", __func__, session_id);
5672
5673                 return -EINVAL;
5674         }
5675
5676         mutex_lock(&v->lock);
5677
5678         v->tty_mode = tty_mode;
5679
5680         mutex_unlock(&v->lock);
5681
5682         return ret;
5683 }
5684
5685 uint8_t voc_get_tty_mode(uint32_t session_id)
5686 {
5687         struct voice_data *v = voice_get_session(session_id);
5688         int ret = 0;
5689
5690         if (v == NULL) {
5691                 pr_err("%s: invalid session_id 0x%x\n", __func__, session_id);
5692
5693                 return -EINVAL;
5694         }
5695
5696         mutex_lock(&v->lock);
5697
5698         ret = v->tty_mode;
5699
5700         mutex_unlock(&v->lock);
5701
5702         return ret;
5703 }
5704
5705 int voc_set_pp_enable(uint32_t session_id,
5706                       struct module_instance_info mod_inst_info,
5707                       uint32_t enable)
5708 {
5709         struct voice_data *v = NULL;
5710         int ret = 0;
5711         struct voice_session_itr itr;
5712         int mid = mod_inst_info.module_id;
5713         int iid = mod_inst_info.instance_id;
5714
5715         voice_itr_init(&itr, session_id);
5716         while (voice_itr_get_next_session(&itr, &v)) {
5717                 if (v != NULL) {
5718                         if (!(is_voice_app_id(v->session_id)))
5719                                 continue;
5720
5721                         mutex_lock(&v->lock);
5722                         if (mid == MODULE_ID_VOICE_MODULE_ST &&
5723                             iid == INSTANCE_ID_0)
5724                                 v->st_enable = enable;
5725
5726                         if (v->voc_state == VOC_RUN) {
5727                                 if ((mid == MODULE_ID_VOICE_MODULE_ST) &&
5728                                     iid == INSTANCE_ID_0 && (!v->tty_mode))
5729                                         ret = voice_send_set_pp_enable_cmd(
5730                                                 v, mod_inst_info, enable);
5731                         }
5732                         mutex_unlock(&v->lock);
5733                 } else {
5734                         pr_err("%s: invalid session_id 0x%x\n", __func__,
5735                                                                 session_id);
5736                         ret =  -EINVAL;
5737                         break;
5738                 }
5739         }
5740
5741         return ret;
5742 }
5743
5744 int voc_set_hd_enable(uint32_t session_id, uint32_t enable)
5745 {
5746         struct voice_data *v = NULL;
5747         int ret = 0;
5748         struct voice_session_itr itr;
5749
5750         voice_itr_init(&itr, session_id);
5751         while (voice_itr_get_next_session(&itr, &v)) {
5752                 if (v != NULL) {
5753                         mutex_lock(&v->lock);
5754                         v->hd_enable = enable;
5755
5756                         if (v->voc_state == VOC_RUN)
5757                                 ret = voice_send_hd_cmd(v, enable);
5758
5759                         mutex_unlock(&v->lock);
5760                 } else {
5761                         pr_err("%s: invalid session_id 0x%x\n", __func__,
5762                                session_id);
5763                         ret =  -EINVAL;
5764                         break;
5765                 }
5766         }
5767
5768         return ret;
5769 }
5770
5771 int voc_set_afe_sidetone(uint32_t session_id, bool sidetone_enable)
5772 {
5773         struct voice_data *v = NULL;
5774         int ret = -EINVAL;
5775         struct voice_session_itr itr;
5776         u16 rx_port, tx_port;
5777
5778         common.sidetone_enable = sidetone_enable;
5779         voice_itr_init(&itr, session_id);
5780         while (voice_itr_get_next_session(&itr, &v)) {
5781                 if (v == NULL) {
5782                         pr_err("%s: invalid session_id 0x%x\n", __func__,
5783                                   session_id);
5784                         ret = -EINVAL;
5785                         break;
5786                 }
5787                 mutex_lock(&v->lock);
5788                 if (v->voc_state != VOC_RUN) {
5789                         mutex_unlock(&v->lock);
5790                         continue;
5791                 }
5792                 rx_port = v->dev_rx.port_id;
5793                 tx_port = v->dev_tx.port_id;
5794                 ret = afe_sidetone_enable(tx_port, rx_port,
5795                                           sidetone_enable);
5796                 if (!ret) {
5797                         mutex_unlock(&v->lock);
5798                         break;
5799                 }
5800                 mutex_unlock(&v->lock);
5801         }
5802         return ret;
5803 }
5804
5805 bool voc_get_afe_sidetone(void)
5806 {
5807         bool ret;
5808
5809         ret = common.sidetone_enable;
5810         return ret;
5811 }
5812
5813 int voc_get_pp_enable(uint32_t session_id,
5814                       struct module_instance_info mod_inst_info)
5815 {
5816         struct voice_data *v = voice_get_session(session_id);
5817         int ret = 0;
5818
5819         if (v == NULL) {
5820                 pr_err("%s: invalid session_id 0x%x\n", __func__, session_id);
5821
5822                 return -EINVAL;
5823         }
5824
5825         mutex_lock(&v->lock);
5826         if (mod_inst_info.module_id == MODULE_ID_VOICE_MODULE_ST &&
5827             mod_inst_info.instance_id == INSTANCE_ID_0)
5828                 ret = v->st_enable;
5829         mutex_unlock(&v->lock);
5830
5831         return ret;
5832 }
5833
5834 int voc_set_rx_vol_step(uint32_t session_id, uint32_t dir, uint32_t vol_step,
5835                         uint32_t ramp_duration)
5836 {
5837         struct voice_data *v = NULL;
5838         int ret = 0;
5839         struct voice_session_itr itr;
5840
5841         pr_debug("%s session id = %#x vol = %u", __func__, session_id,
5842                 vol_step);
5843
5844         voice_itr_init(&itr, session_id);
5845         while (voice_itr_get_next_session(&itr, &v)) {
5846                 if (v != NULL) {
5847                         mutex_lock(&v->lock);
5848                         v->dev_rx.volume_step_value = vol_step;
5849                         v->dev_rx.volume_ramp_duration_ms = ramp_duration;
5850                         if (is_voc_state_active(v->voc_state))
5851                                 ret = voice_send_vol_step_cmd(v);
5852                         mutex_unlock(&v->lock);
5853                 } else {
5854                         pr_err("%s: invalid session_id 0x%x\n", __func__,
5855                                 session_id);
5856
5857                         ret = -EINVAL;
5858                         break;
5859                 }
5860         }
5861
5862         return ret;
5863 }
5864
5865 int voc_set_device_config(uint32_t session_id, uint8_t path_dir,
5866                           struct media_format_info *finfo)
5867 {
5868         struct voice_data *v = voice_get_session(session_id);
5869
5870         if (v == NULL) {
5871                 pr_err("%s: Invalid session_id 0x%x\n", __func__, session_id);
5872
5873                 return -EINVAL;
5874         }
5875
5876         pr_debug("%s: path_dir=%d port_id=%x, channels=%d, sample_rate=%d, bits_per_sample=%d\n",
5877                 __func__, path_dir, finfo->port_id, finfo->num_channels,
5878                 finfo->sample_rate, finfo->bits_per_sample);
5879
5880         mutex_lock(&v->lock);
5881         switch (path_dir) {
5882         case RX_PATH:
5883                 v->dev_rx.port_id = q6audio_get_port_id(finfo->port_id);
5884                 v->dev_rx.no_of_channels = finfo->num_channels;
5885                 v->dev_rx.sample_rate = finfo->sample_rate;
5886                 v->dev_rx.bits_per_sample = finfo->bits_per_sample;
5887                 memcpy(&v->dev_rx.channel_mapping, &finfo->channel_mapping,
5888                        VSS_CHANNEL_MAPPING_SIZE);
5889                 break;
5890         case TX_PATH:
5891                 v->dev_tx.port_id = q6audio_get_port_id(finfo->port_id);
5892                 v->dev_tx.no_of_channels = finfo->num_channels;
5893                 v->dev_tx.sample_rate = finfo->sample_rate;
5894                 v->dev_tx.bits_per_sample = finfo->bits_per_sample;
5895                 memcpy(&v->dev_tx.channel_mapping, &finfo->channel_mapping,
5896                        VSS_CHANNEL_MAPPING_SIZE);
5897                 break;
5898         default:
5899                 pr_err("%s: Invalid path_dir %d\n", __func__, path_dir);
5900                 mutex_unlock(&v->lock);
5901                 return -EINVAL;
5902         }
5903
5904         mutex_unlock(&v->lock);
5905
5906         return 0;
5907 }
5908
5909 int voc_set_ext_ec_ref_media_fmt_info(struct media_format_info *finfo)
5910 {
5911         mutex_lock(&common.common_lock);
5912         if (common.ec_ref_ext) {
5913                 common.ec_media_fmt_info.num_channels = finfo->num_channels;
5914                 common.ec_media_fmt_info.bits_per_sample =
5915                         finfo->bits_per_sample;
5916                 common.ec_media_fmt_info.sample_rate = finfo->sample_rate;
5917                 memcpy(&common.ec_media_fmt_info.channel_mapping,
5918                        &finfo->channel_mapping, VSS_CHANNEL_MAPPING_SIZE);
5919         } else {
5920                 pr_debug("%s: Ext Ec Ref not active, returning", __func__);
5921         }
5922         mutex_unlock(&common.common_lock);
5923         return 0;
5924 }
5925
5926 int voc_set_route_flag(uint32_t session_id, uint8_t path_dir, uint8_t set)
5927 {
5928         struct voice_data *v = voice_get_session(session_id);
5929
5930         if (v == NULL) {
5931                 pr_err("%s: invalid session_id 0x%x\n", __func__, session_id);
5932
5933                 return -EINVAL;
5934         }
5935
5936         pr_debug("%s: path_dir=%d, set=%d\n", __func__, path_dir, set);
5937
5938         mutex_lock(&v->lock);
5939
5940         if (path_dir == RX_PATH)
5941                 v->voc_route_state.rx_route_flag = set;
5942         else
5943                 v->voc_route_state.tx_route_flag = set;
5944
5945         mutex_unlock(&v->lock);
5946
5947         return 0;
5948 }
5949
5950 uint8_t voc_get_route_flag(uint32_t session_id, uint8_t path_dir)
5951 {
5952         struct voice_data *v = voice_get_session(session_id);
5953         int ret = 0;
5954
5955         if (v == NULL) {
5956                 pr_err("%s: invalid session_id 0x%x\n", __func__, session_id);
5957
5958                 return 0;
5959         }
5960
5961         mutex_lock(&v->lock);
5962
5963         if (path_dir == RX_PATH)
5964                 ret = v->voc_route_state.rx_route_flag;
5965         else
5966                 ret = v->voc_route_state.tx_route_flag;
5967
5968         mutex_unlock(&v->lock);
5969
5970         return ret;
5971 }
5972
5973 int voc_end_voice_call(uint32_t session_id)
5974 {
5975         struct voice_data *v = voice_get_session(session_id);
5976         int ret = 0;
5977
5978         if (v == NULL) {
5979                 pr_err("%s: invalid session_id 0x%x\n", __func__, session_id);
5980
5981                 return -EINVAL;
5982         }
5983
5984         mutex_lock(&v->lock);
5985
5986         if (v->voc_state == VOC_RUN || v->voc_state == VOC_ERROR ||
5987             v->voc_state == VOC_CHANGE || v->voc_state == VOC_STANDBY) {
5988
5989                 pr_debug("%s: VOC_STATE: %d\n", __func__, v->voc_state);
5990
5991                 ret = voice_destroy_vocproc(v);
5992                 if (ret < 0)
5993                         pr_err("%s:  destroy voice failed\n", __func__);
5994
5995                 voc_update_session_params(v);
5996
5997                 voice_destroy_mvm_cvs_session(v);
5998                 v->voc_state = VOC_RELEASE;
5999                 if (common.is_vote_bms) {
6000                         /* vote low power to BMS during call stop */
6001                         voice_vote_powerstate_to_bms(v, false);
6002                 }
6003         } else {
6004                 pr_err("%s: Error: End voice called in state %d\n",
6005                         __func__, v->voc_state);
6006
6007                 ret = -EINVAL;
6008         }
6009
6010         mutex_unlock(&v->lock);
6011         return ret;
6012 }
6013
6014 int voc_standby_voice_call(uint32_t session_id)
6015 {
6016         struct voice_data *v = voice_get_session(session_id);
6017         struct apr_hdr mvm_standby_voice_cmd;
6018         void *apr_mvm;
6019         u16 mvm_handle;
6020         int ret = 0;
6021
6022         if (v == NULL) {
6023                 pr_err("%s: v is NULL\n", __func__);
6024                 return -EINVAL;
6025         }
6026         pr_debug("%s: voc state=%d", __func__, v->voc_state);
6027
6028         if (v->voc_state == VOC_RUN) {
6029                 apr_mvm = common.apr_q6_mvm;
6030                 if (!apr_mvm) {
6031                         pr_err("%s: apr_mvm is NULL.\n", __func__);
6032                         ret = -EINVAL;
6033                         goto fail;
6034                 }
6035                 mvm_handle = voice_get_mvm_handle(v);
6036                 mvm_standby_voice_cmd.hdr_field =
6037                         APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
6038                         APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
6039                 mvm_standby_voice_cmd.pkt_size =
6040                         APR_PKT_SIZE(APR_HDR_SIZE,
6041                         sizeof(mvm_standby_voice_cmd) - APR_HDR_SIZE);
6042                 pr_debug("send mvm_standby_voice_cmd pkt size = %d\n",
6043                          mvm_standby_voice_cmd.pkt_size);
6044                 mvm_standby_voice_cmd.src_port =
6045                                 voice_get_idx_for_session(v->session_id);
6046                 mvm_standby_voice_cmd.dest_port = mvm_handle;
6047                 mvm_standby_voice_cmd.token = 0;
6048                 mvm_standby_voice_cmd.opcode = VSS_IMVM_CMD_STANDBY_VOICE;
6049                 v->mvm_state = CMD_STATUS_FAIL;
6050                 ret = apr_send_pkt(apr_mvm,
6051                                 (uint32_t *)&mvm_standby_voice_cmd);
6052                 if (ret < 0) {
6053                         pr_err("Fail in sending VSS_IMVM_CMD_STANDBY_VOICE\n");
6054                         ret = -EINVAL;
6055                         goto fail;
6056                 }
6057                 v->voc_state = VOC_STANDBY;
6058         }
6059 fail:
6060         return ret;
6061 }
6062
6063 int voc_disable_device(uint32_t session_id)
6064 {
6065         struct voice_data *v = voice_get_session(session_id);
6066         int ret = 0;
6067
6068         if (v == NULL) {
6069                 pr_err("%s: v is NULL\n", __func__);
6070                 return -EINVAL;
6071         }
6072
6073         pr_debug("%s: voc state=%d\n", __func__, v->voc_state);
6074
6075         mutex_lock(&v->lock);
6076         if (v->voc_state == VOC_RUN) {
6077                 ret = voice_pause_voice_call(v);
6078                 if (ret < 0) {
6079                         pr_err("%s: Pause Voice Call failed for session 0x%x, err %d!\n",
6080                                __func__, v->session_id, ret);
6081                         goto done;
6082                 }
6083                 rtac_remove_voice(voice_get_cvs_handle(v));
6084                 voice_send_cvp_deregister_vol_cal_cmd(v);
6085                 voice_send_cvp_deregister_cal_cmd(v);
6086                 voice_send_cvp_deregister_dev_cfg_cmd(v);
6087
6088                 v->voc_state = VOC_CHANGE;
6089         } else {
6090                 pr_debug("%s: called in voc state=%d, No_OP\n",
6091                          __func__, v->voc_state);
6092         }
6093
6094 done:
6095         mutex_unlock(&v->lock);
6096
6097         return ret;
6098 }
6099
6100 int voc_enable_device(uint32_t session_id)
6101 {
6102         struct voice_data *v = voice_get_session(session_id);
6103         struct module_instance_info mod_inst_info = {0};
6104         int ret = 0;
6105
6106         if (v == NULL) {
6107                 pr_err("%s: v is NULL\n", __func__);
6108                 return -EINVAL;
6109         }
6110
6111         pr_debug("%s: voc state=%d\n", __func__, v->voc_state);
6112         mutex_lock(&v->lock);
6113         if (v->voc_state == VOC_CHANGE) {
6114                 ret = voice_send_tty_mode_cmd(v);
6115                 if (ret < 0) {
6116                         pr_err("%s: Sending TTY mode failed, ret=%d\n",
6117                                __func__, ret);
6118                         /* Not a critical error, allow voice call to continue */
6119                 }
6120
6121                 mod_inst_info.module_id = MODULE_ID_VOICE_MODULE_ST;
6122                 mod_inst_info.instance_id = INSTANCE_ID_0;
6123
6124                 if (v->tty_mode) {
6125                         /* disable slowtalk */
6126                         voice_send_set_pp_enable_cmd(v, mod_inst_info, 0);
6127                 } else {
6128                         /* restore slowtalk */
6129                         voice_send_set_pp_enable_cmd(v, mod_inst_info,
6130                                                      v->st_enable);
6131                 }
6132
6133                 ret = voice_send_set_device_cmd(v);
6134                 if (ret < 0) {
6135                         pr_err("%s: Set device failed, ret=%d\n",
6136                                __func__, ret);
6137                         goto done;
6138                 }
6139
6140                 ret = voice_send_cvp_media_fmt_info_cmd(v);
6141                 if (ret < 0) {
6142                         pr_err("%s: Set format failed err:%d\n", __func__, ret);
6143                         goto done;
6144                 }
6145
6146                 ret = voice_send_cvp_topology_commit_cmd(v);
6147                 if (ret < 0) {
6148                         pr_err("%s:  Set topology commit failed\n", __func__);
6149                         goto done;
6150                 }
6151
6152                 voice_send_cvp_register_dev_cfg_cmd(v);
6153                 voice_send_cvp_register_cal_cmd(v);
6154                 voice_send_cvp_register_vol_cal_cmd(v);
6155
6156                 rtac_add_voice(voice_get_cvs_handle(v),
6157                                voice_get_cvp_handle(v),
6158                                v->dev_rx.port_id, v->dev_tx.port_id,
6159                                v->dev_rx.dev_id, v->dev_tx.dev_id,
6160                                v->session_id);
6161
6162                 ret = voice_send_start_voice_cmd(v);
6163                 if (ret < 0) {
6164                         pr_err("%s: Fail in sending START_VOICE, ret=%d\n",
6165                                __func__, ret);
6166                         goto done;
6167                 }
6168                 v->voc_state = VOC_RUN;
6169         } else {
6170                 pr_debug("%s: called in voc state=%d, No_OP\n",
6171                          __func__, v->voc_state);
6172         }
6173
6174 done:
6175         mutex_unlock(&v->lock);
6176
6177         return ret;
6178 }
6179
6180 int voc_set_lch(uint32_t session_id, enum voice_lch_mode lch_mode)
6181 {
6182         struct voice_data *v = voice_get_session(session_id);
6183         int ret = 0;
6184
6185         if (v == NULL) {
6186                 pr_err("%s: Invalid session_id 0x%x\n", __func__, session_id);
6187
6188                 ret = -EINVAL;
6189                 goto done;
6190         }
6191
6192         mutex_lock(&v->lock);
6193         if (v->lch_mode == lch_mode) {
6194                 pr_debug("%s: Session %d already in LCH mode %d\n",
6195                                  __func__, session_id, lch_mode);
6196
6197                 mutex_unlock(&v->lock);
6198                 goto done;
6199         }
6200
6201         v->lch_mode = lch_mode;
6202         mutex_unlock(&v->lock);
6203
6204         ret = voc_lch_ops(v, v->lch_mode);
6205         if (ret < 0) {
6206                 pr_err("%s: lch ops failed %d\n", __func__, ret);
6207                 goto done;
6208         }
6209
6210 done:
6211         return ret;
6212 }
6213
6214 int voc_resume_voice_call(uint32_t session_id)
6215 {
6216         struct voice_data *v = voice_get_session(session_id);
6217         int ret = 0;
6218
6219         ret = voice_send_start_voice_cmd(v);
6220         if (ret < 0) {
6221                 pr_err("Fail in sending START_VOICE\n");
6222                 goto fail;
6223         }
6224         v->voc_state = VOC_RUN;
6225         return 0;
6226 fail:
6227         return -EINVAL;
6228 }
6229
6230 int voc_start_voice_call(uint32_t session_id)
6231 {
6232         struct voice_data *v = voice_get_session(session_id);
6233         int ret = 0;
6234
6235         if (v == NULL) {
6236                 pr_err("%s: invalid session_id 0x%x\n", __func__, session_id);
6237
6238                 return -EINVAL;
6239         }
6240
6241         mutex_lock(&v->lock);
6242
6243         if (v->voc_state == VOC_ERROR) {
6244                 pr_debug("%s: VOC in ERR state\n", __func__);
6245
6246                 voice_destroy_mvm_cvs_session(v);
6247                 v->voc_state = VOC_INIT;
6248         }
6249
6250         if ((v->voc_state == VOC_INIT) ||
6251                 (v->voc_state == VOC_RELEASE)) {
6252                 ret = voice_apr_register(session_id);
6253                 if (ret < 0) {
6254                         pr_err("%s:  apr register failed\n", __func__);
6255                         goto fail;
6256                 }
6257
6258                 if (is_cvd_version_queried()) {
6259                         pr_debug("%s: Returning the cached value %s\n",
6260                                  __func__, common.cvd_version);
6261                 } else {
6262                         ret = voice_send_mvm_cvd_version_cmd(v);
6263                         if (ret < 0)
6264                                 pr_debug("%s: Error retrieving CVD version %d\n",
6265                                          __func__, ret);
6266                 }
6267
6268                 ret = voice_create_mvm_cvs_session(v);
6269                 if (ret < 0) {
6270                         pr_err("create mvm and cvs failed\n");
6271                         goto fail;
6272                 }
6273
6274                 if (is_voip_session(session_id)) {
6275                         /* Allocate oob mem if not already allocated and
6276                          * memory map the oob memory block.
6277                          */
6278                         ret = voice_alloc_and_map_oob_mem(v);
6279                         if (ret < 0) {
6280                                 pr_err("%s: voice_alloc_and_map_oob_mem() failed, ret:%d\n",
6281                                        __func__, ret);
6282
6283                                 goto fail;
6284                         }
6285
6286                         ret = voice_set_packet_exchange_mode_and_config(
6287                                 session_id,
6288                                 VSS_ISTREAM_PACKET_EXCHANGE_MODE_OUT_OF_BAND);
6289                         if (ret) {
6290                                 pr_err("%s: Err: exchange_mode_and_config  %d\n",
6291                                         __func__, ret);
6292
6293                                 goto fail;
6294                         }
6295                 }
6296                 ret = voice_send_dual_control_cmd(v);
6297                 if (ret < 0) {
6298                         pr_err("Err Dual command failed\n");
6299                         goto fail;
6300                 }
6301                 ret = voice_setup_vocproc(v);
6302                 if (ret < 0) {
6303                         pr_err("setup voice failed\n");
6304                         goto fail;
6305                 }
6306
6307                 ret = voice_send_vol_step_cmd(v);
6308                 if (ret < 0)
6309                         pr_err("voice volume failed\n");
6310
6311                 ret = voice_send_stream_mute_cmd(v,
6312                                 VSS_IVOLUME_DIRECTION_TX,
6313                                 v->stream_tx.stream_mute,
6314                                 v->stream_tx.stream_mute_ramp_duration_ms);
6315                 if (ret < 0)
6316                         pr_err("voice mute failed\n");
6317
6318                 ret = voice_send_start_voice_cmd(v);
6319                 if (ret < 0) {
6320                         pr_err("start voice failed\n");
6321                         goto fail;
6322                 }
6323
6324                 v->voc_state = VOC_RUN;
6325         } else {
6326                 pr_err("%s: Error: Start voice called in state %d\n",
6327                         __func__, v->voc_state);
6328
6329                 ret = -EINVAL;
6330                 goto fail;
6331         }
6332 fail:
6333         mutex_unlock(&v->lock);
6334         return ret;
6335 }
6336
6337 int voc_set_ext_ec_ref_port_id(uint16_t port_id, bool state)
6338 {
6339         int ret = 0;
6340
6341         mutex_lock(&common.common_lock);
6342         if (state == true) {
6343                 if (port_id == AFE_PORT_INVALID) {
6344                         pr_err("%s: Invalid port id", __func__);
6345                         ret = -EINVAL;
6346                         goto exit;
6347                 }
6348                 common.ec_ref_ext = true;
6349         } else {
6350                 common.ec_ref_ext = false;
6351         }
6352         /* Cache EC Fromat Info in common */
6353         common.ec_media_fmt_info.port_id = port_id;
6354 exit:
6355         mutex_unlock(&common.common_lock);
6356         return ret;
6357 }
6358
6359 int voc_get_ext_ec_ref_port_id(void)
6360 {
6361         if (common.ec_ref_ext)
6362                 return common.ec_media_fmt_info.port_id;
6363         else
6364                 return AFE_PORT_INVALID;
6365 }
6366
6367 void voc_register_mvs_cb(ul_cb_fn ul_cb,
6368                            dl_cb_fn dl_cb,
6369                            voip_ssr_cb ssr_cb,
6370                            void *private_data)
6371 {
6372         common.mvs_info.ul_cb = ul_cb;
6373         common.mvs_info.dl_cb = dl_cb;
6374         common.mvs_info.ssr_cb = ssr_cb;
6375         common.mvs_info.private_data = private_data;
6376 }
6377
6378 void voc_register_dtmf_rx_detection_cb(dtmf_rx_det_cb_fn dtmf_rx_ul_cb,
6379                                        void *private_data)
6380 {
6381         common.dtmf_info.dtmf_rx_ul_cb = dtmf_rx_ul_cb;
6382         common.dtmf_info.private_data = private_data;
6383 }
6384
6385 void voc_config_vocoder(uint32_t media_type,
6386                         uint32_t rate,
6387                         uint32_t network_type,
6388                         uint32_t dtx_mode,
6389                         uint32_t evrc_min_rate,
6390                         uint32_t evrc_max_rate)
6391 {
6392         common.mvs_info.media_type = media_type;
6393         common.mvs_info.rate = rate;
6394         common.mvs_info.network_type = network_type;
6395         common.mvs_info.dtx_mode = dtx_mode;
6396         common.mvs_info.evrc_min_rate = evrc_min_rate;
6397         common.mvs_info.evrc_max_rate = evrc_max_rate;
6398 }
6399
6400 static int32_t qdsp_mvm_callback(struct apr_client_data *data, void *priv)
6401 {
6402         uint32_t *ptr = NULL, min_payload_size = 0;
6403         struct common_data *c = NULL;
6404         struct voice_data *v = NULL;
6405         int i = 0;
6406         struct vss_iversion_rsp_get_t *version_rsp = NULL;
6407
6408         if ((data == NULL) || (priv == NULL)) {
6409                 pr_err("%s: data or priv is NULL\n", __func__);
6410                 return -EINVAL;
6411         }
6412
6413         c = priv;
6414
6415         pr_debug("%s: Payload Length = %d, opcode=%x\n", __func__,
6416                 data->payload_size, data->opcode);
6417
6418         if (data->opcode == RESET_EVENTS) {
6419                 pr_debug("%s: Reset event received in Voice service\n",
6420                                 __func__);
6421
6422                 if (common.mvs_info.ssr_cb) {
6423                         pr_debug("%s: Informing reset event to VoIP\n",
6424                                         __func__);
6425                         common.mvs_info.ssr_cb(data->opcode,
6426                                         common.mvs_info.private_data);
6427                 }
6428
6429                 apr_reset(c->apr_q6_mvm);
6430                 c->apr_q6_mvm = NULL;
6431
6432                 /* clean up memory handle */
6433                 c->cal_mem_handle = 0;
6434                 c->rtac_mem_handle = 0;
6435                 cal_utils_clear_cal_block_q6maps(MAX_VOICE_CAL_TYPES,
6436                                 common.cal_data);
6437                 rtac_clear_mapping(VOICE_RTAC_CAL);
6438
6439                 /* Sub-system restart is applicable to all sessions. */
6440                 for (i = 0; i < MAX_VOC_SESSIONS; i++) {
6441                         c->voice[i].mvm_handle = 0;
6442                         c->voice[i].shmem_info.mem_handle = 0;
6443                 }
6444
6445                 /* Free the ION memory and clear handles for Source Tracking */
6446                 if (is_source_tracking_shared_memomry_allocated()) {
6447                         msm_audio_ion_free(
6448                         common.source_tracking_sh_mem.sh_mem_block.client,
6449                         common.source_tracking_sh_mem.sh_mem_block.handle);
6450                         common.source_tracking_sh_mem.mem_handle = 0;
6451                         common.source_tracking_sh_mem.sh_mem_block.client =
6452                                                                         NULL;
6453                         common.source_tracking_sh_mem.sh_mem_block.handle =
6454                                                                         NULL;
6455                 }
6456                 /* clean up srvcc rec flag */
6457                 c->srvcc_rec_flag = false;
6458                 voc_set_error_state(data->reset_proc);
6459                 return 0;
6460         }
6461
6462         pr_debug("%s: session_idx 0x%x\n", __func__, data->dest_port);
6463
6464         v = voice_get_session_by_idx(data->dest_port);
6465         if (v == NULL) {
6466                 pr_err("%s: v is NULL\n", __func__);
6467
6468                 return -EINVAL;
6469         }
6470
6471         if (data->opcode == APR_BASIC_RSP_RESULT) {
6472                 if (data->payload_size >= sizeof(ptr[0]) * 2) {
6473                         ptr = data->payload;
6474
6475                         pr_debug("%x %x\n", ptr[0], ptr[1]);
6476                         /* ping mvm service ACK */
6477                         switch (ptr[0]) {
6478                         case VSS_IMVM_CMD_CREATE_PASSIVE_CONTROL_SESSION:
6479                         case VSS_IMVM_CMD_CREATE_FULL_CONTROL_SESSION:
6480                                 /* Passive session is used for CS call
6481                                  * Full session is used for VoIP call. */
6482                                 pr_debug("%s: cmd = 0x%x\n", __func__, ptr[0]);
6483                                 if (!ptr[1]) {
6484                                         pr_debug("%s: MVM handle is %d\n",
6485                                                  __func__, data->src_port);
6486                                         voice_set_mvm_handle(v, data->src_port);
6487                                 } else
6488                                         pr_err("got NACK for sending MVM create session\n");
6489                                 v->mvm_state = CMD_STATUS_SUCCESS;
6490                                 v->async_err = ptr[1];
6491                                 wake_up(&v->mvm_wait);
6492                                 break;
6493                         case VSS_IMVM_CMD_START_VOICE:
6494                         case VSS_IMVM_CMD_ATTACH_VOCPROC:
6495                         case VSS_IMVM_CMD_STOP_VOICE:
6496                         case VSS_IMVM_CMD_DETACH_VOCPROC:
6497                         case VSS_ISTREAM_CMD_SET_TTY_MODE:
6498                         case APRV2_IBASIC_CMD_DESTROY_SESSION:
6499                         case VSS_IMVM_CMD_ATTACH_STREAM:
6500                         case VSS_IMVM_CMD_DETACH_STREAM:
6501                         case VSS_ICOMMON_CMD_SET_NETWORK:
6502                         case VSS_ICOMMON_CMD_SET_VOICE_TIMING:
6503                         case VSS_IMVM_CMD_SET_POLICY_DUAL_CONTROL:
6504                         case VSS_IMVM_CMD_SET_CAL_NETWORK:
6505                         case VSS_IMVM_CMD_SET_CAL_MEDIA_TYPE:
6506                         case VSS_IMEMORY_CMD_MAP_PHYSICAL:
6507                         case VSS_IMEMORY_CMD_UNMAP:
6508                         case VSS_IMVM_CMD_PAUSE_VOICE:
6509                         case VSS_IMVM_CMD_STANDBY_VOICE:
6510                         case VSS_IHDVOICE_CMD_ENABLE:
6511                         case VSS_IHDVOICE_CMD_DISABLE:
6512                                 pr_debug("%s: cmd = 0x%x\n", __func__, ptr[0]);
6513                                 v->mvm_state = CMD_STATUS_SUCCESS;
6514                                 v->async_err = ptr[1];
6515                                 wake_up(&v->mvm_wait);
6516                                 break;
6517                         case VSS_IVERSION_CMD_GET:
6518                                 pr_debug("%s: Error retrieving CVD Version, error:%d\n",
6519                                          __func__, ptr[1]);
6520
6521                                 strlcpy(common.cvd_version, CVD_VERSION_0_0,
6522                                         sizeof(common.cvd_version));
6523                                 pr_debug("%s: Fall back to default value, CVD Version = %s\n",
6524                                          __func__, common.cvd_version);
6525
6526                                 v->mvm_state = CMD_STATUS_SUCCESS;
6527                                 v->async_err = ptr[1];
6528                                 wake_up(&v->mvm_wait);
6529                                 break;
6530                         default:
6531                                 pr_debug("%s: not match cmd = 0x%x\n",
6532                                         __func__, ptr[0]);
6533                                 break;
6534                         }
6535                 }
6536         } else if (data->opcode == VSS_IMEMORY_RSP_MAP) {
6537                 pr_debug("%s, Revd VSS_IMEMORY_RSP_MAP response\n", __func__);
6538
6539                 if (data->payload_size < sizeof(ptr[0])) {
6540                         pr_err("%s: payload has invalid size[%d]\n", __func__,
6541                                data->payload_size);
6542                         return -EINVAL;
6543                 }
6544
6545                 if (data->payload_size && data->token == VOIP_MEM_MAP_TOKEN) {
6546                         ptr = data->payload;
6547                         if (ptr[0]) {
6548                                 v->shmem_info.mem_handle = ptr[0];
6549                                 pr_debug("%s: shared mem_handle: 0x[%x]\n",
6550                                          __func__, v->shmem_info.mem_handle);
6551                                 v->mvm_state = CMD_STATUS_SUCCESS;
6552                                 wake_up(&v->mvm_wait);
6553                         }
6554                 } else if (data->payload_size &&
6555                            data->token == VOC_CAL_MEM_MAP_TOKEN) {
6556                         ptr = data->payload;
6557                         if (ptr[0]) {
6558                                 c->cal_mem_handle = ptr[0];
6559
6560                                 pr_debug("%s: cal mem handle 0x%x\n",
6561                                          __func__, c->cal_mem_handle);
6562
6563                                 v->mvm_state = CMD_STATUS_SUCCESS;
6564                                 wake_up(&v->mvm_wait);
6565                         }
6566                 } else if (data->payload_size &&
6567                            data->token == VOC_VOICE_HOST_PCM_MAP_TOKEN) {
6568                         ptr = data->payload;
6569                         if (ptr[0]) {
6570                                 common.voice_host_pcm_mem_handle = ptr[0];
6571
6572                                 pr_debug("%s: vhpcm mem handle 0x%x\n",
6573                                          __func__,
6574                                          common.voice_host_pcm_mem_handle);
6575                                 v->mvm_state = CMD_STATUS_SUCCESS;
6576                                 wake_up(&v->mvm_wait);
6577                         }
6578                 } else if (data->payload_size &&
6579                                 data->token == VOC_RTAC_MEM_MAP_TOKEN) {
6580                         ptr = data->payload;
6581                         if (ptr[0]) {
6582                                 c->rtac_mem_handle = ptr[0];
6583
6584                                 pr_debug("%s: cal mem handle 0x%x\n",
6585                                          __func__, c->rtac_mem_handle);
6586
6587                                 v->mvm_state = CMD_STATUS_SUCCESS;
6588                                 wake_up(&v->mvm_wait);
6589                         }
6590                 } else if (data->payload_size &&
6591                            data->token == VOC_SOURCE_TRACKING_MEM_MAP_TOKEN) {
6592                         ptr = data->payload;
6593                         if (ptr[0]) {
6594                                 common.source_tracking_sh_mem.mem_handle =
6595                                                                         ptr[0];
6596
6597                                 pr_debug("%s: Source Tracking shared mem handle 0x%x\n",
6598                                          __func__,
6599                                    common.source_tracking_sh_mem.mem_handle);
6600
6601                                 v->mvm_state = CMD_STATUS_SUCCESS;
6602                                 wake_up(&v->mvm_wait);
6603                         }
6604                 } else {
6605                         pr_err("%s: Unknown mem map token %d\n",
6606                                __func__, data->token);
6607                 }
6608         } else if (data->opcode == VSS_IVERSION_RSP_GET) {
6609                 pr_debug("%s: Received VSS_IVERSION_RSP_GET\n", __func__);
6610
6611                 if (data->payload_size) {
6612                         min_payload_size = (data->payload_size >
6613                                             CVD_VERSION_STRING_MAX_SIZE)
6614                                             ? CVD_VERSION_STRING_MAX_SIZE :
6615                                             data->payload_size;
6616                         version_rsp =
6617                                 (struct vss_iversion_rsp_get_t *)data->payload;
6618                         memcpy(common.cvd_version, version_rsp->version,
6619                                min_payload_size);
6620                         common.cvd_version[min_payload_size - 1] = '\0';
6621                         pr_debug("%s: CVD Version = %s\n",
6622                                  __func__, common.cvd_version);
6623
6624                         v->mvm_state = CMD_STATUS_SUCCESS;
6625                         wake_up(&v->mvm_wait);
6626                 }
6627         }
6628         return 0;
6629 }
6630
6631 static int32_t qdsp_cvs_callback(struct apr_client_data *data, void *priv)
6632 {
6633         uint32_t *ptr = NULL;
6634         struct common_data *c = NULL;
6635         struct voice_data *v = NULL;
6636         int i = 0;
6637
6638         if ((data == NULL) || (priv == NULL)) {
6639                 pr_err("%s: data or priv is NULL\n", __func__);
6640                 return -EINVAL;
6641         }
6642
6643         c = priv;
6644
6645         pr_debug("%s: session_id 0x%x\n", __func__, data->dest_port);
6646         pr_debug("%s: Payload Length = %d, opcode=%x\n", __func__,
6647                 data->payload_size, data->opcode);
6648
6649         if (data->opcode == RESET_EVENTS) {
6650                 pr_debug("%s: Reset event received in Voice service\n",
6651                                 __func__);
6652
6653                 apr_reset(c->apr_q6_cvs);
6654                 c->apr_q6_cvs = NULL;
6655
6656                 /* Sub-system restart is applicable to all sessions. */
6657                 for (i = 0; i < MAX_VOC_SESSIONS; i++)
6658                         c->voice[i].cvs_handle = 0;
6659
6660                 cal_utils_clear_cal_block_q6maps(MAX_VOICE_CAL_TYPES,
6661                                 common.cal_data);
6662
6663                 /* Free the ION memory and clear handles for Source Tracking */
6664                 if (is_source_tracking_shared_memomry_allocated()) {
6665                         msm_audio_ion_free(
6666                         common.source_tracking_sh_mem.sh_mem_block.client,
6667                         common.source_tracking_sh_mem.sh_mem_block.handle);
6668                         common.source_tracking_sh_mem.mem_handle = 0;
6669                         common.source_tracking_sh_mem.sh_mem_block.client =
6670                                                                         NULL;
6671                         common.source_tracking_sh_mem.sh_mem_block.handle =
6672                                                                         NULL;
6673                 }
6674                 voc_set_error_state(data->reset_proc);
6675                 return 0;
6676         }
6677
6678         v = voice_get_session_by_idx(data->dest_port);
6679         if (v == NULL) {
6680                 pr_err("%s: v is NULL\n", __func__);
6681
6682                 return -EINVAL;
6683         }
6684
6685         if (data->opcode == APR_BASIC_RSP_RESULT) {
6686                 if (data->payload_size) {
6687                         ptr = data->payload;
6688
6689                         pr_debug("%x %x\n", ptr[0], ptr[1]);
6690                         if (ptr[1] != 0) {
6691                                 pr_err("%s: cmd = 0x%x returned error = 0x%x\n",
6692                                         __func__, ptr[0], ptr[1]);
6693                         }
6694                         /*response from  CVS */
6695                         switch (ptr[0]) {
6696                         case VSS_ISTREAM_CMD_CREATE_PASSIVE_CONTROL_SESSION:
6697                         case VSS_ISTREAM_CMD_CREATE_FULL_CONTROL_SESSION:
6698                                 if (!ptr[1]) {
6699                                         pr_debug("%s: CVS handle is %d\n",
6700                                                  __func__, data->src_port);
6701                                         voice_set_cvs_handle(v, data->src_port);
6702                                 } else
6703                                         pr_err("got NACK for sending CVS create session\n");
6704                                 v->cvs_state = CMD_STATUS_SUCCESS;
6705                                 v->async_err = ptr[1];
6706                                 wake_up(&v->cvs_wait);
6707                                 break;
6708                         case VSS_IVOLUME_CMD_MUTE_V2:
6709                         case VSS_ISTREAM_CMD_SET_MEDIA_TYPE:
6710                         case VSS_ISTREAM_CMD_VOC_AMR_SET_ENC_RATE:
6711                         case VSS_ISTREAM_CMD_VOC_AMRWB_SET_ENC_RATE:
6712                         case VSS_ISTREAM_CMD_SET_ENC_DTX_MODE:
6713                         case VSS_ISTREAM_CMD_CDMA_SET_ENC_MINMAX_RATE:
6714                         case APRV2_IBASIC_CMD_DESTROY_SESSION:
6715                         case VSS_ISTREAM_CMD_REGISTER_CALIBRATION_DATA_V2:
6716                         case VSS_ISTREAM_CMD_DEREGISTER_CALIBRATION_DATA:
6717                         case VSS_ISTREAM_CMD_REGISTER_STATIC_CALIBRATION_DATA:
6718                         case VSS_ISTREAM_CMD_DEREGISTER_STATIC_CALIBRATION_DATA:
6719                         case VSS_ICOMMON_CMD_MAP_MEMORY:
6720                         case VSS_ICOMMON_CMD_UNMAP_MEMORY:
6721                         case VSS_ICOMMON_CMD_SET_UI_PROPERTY:
6722                         case VSS_ICOMMON_CMD_SET_UI_PROPERTY_V2:
6723                         case VSS_IPLAYBACK_CMD_START:
6724                         case VSS_IPLAYBACK_CMD_STOP:
6725                         case VSS_IRECORD_CMD_START:
6726                         case VSS_IRECORD_CMD_STOP:
6727                         case VSS_ISTREAM_CMD_SET_PACKET_EXCHANGE_MODE:
6728                         case VSS_ISTREAM_CMD_SET_OOB_PACKET_EXCHANGE_CONFIG:
6729                         case VSS_ISTREAM_CMD_SET_RX_DTMF_DETECTION:
6730                                 pr_debug("%s: cmd = 0x%x\n", __func__, ptr[0]);
6731                                 v->cvs_state = CMD_STATUS_SUCCESS;
6732                                 v->async_err = ptr[1];
6733                                 wake_up(&v->cvs_wait);
6734                                 break;
6735                         case VSS_ICOMMON_CMD_SET_PARAM_V2:
6736                         case VSS_ICOMMON_CMD_SET_PARAM_V3:
6737                                 pr_debug("%s: VSS_ICOMMON_CMD_SET_PARAM\n",
6738                                          __func__);
6739                                 rtac_make_voice_callback(RTAC_CVS, ptr,
6740                                                         data->payload_size);
6741                                 break;
6742                         case VSS_ICOMMON_CMD_GET_PARAM_V2:
6743                         case VSS_ICOMMON_CMD_GET_PARAM_V3:
6744                                 pr_debug("%s: VSS_ICOMMON_CMD_GET_PARAM_V2\n",
6745                                          __func__);
6746                                 /* Should only come here if there is an APR */
6747                                 /* error or malformed APR packet. Otherwise */
6748                                 /* response will be returned as */
6749                                 /* VSS_ICOMMON_RSP_GET_PARAM */
6750                                 if (ptr[1] != 0) {
6751                                         pr_err("%s: CVP get param error = %d, resuming\n",
6752                                                 __func__, ptr[1]);
6753                                         rtac_make_voice_callback(RTAC_CVP,
6754                                                 data->payload,
6755                                                 data->payload_size);
6756                                 }
6757                                 break;
6758                         default:
6759                                 pr_debug("%s: cmd = 0x%x\n", __func__, ptr[0]);
6760                                 break;
6761                         }
6762                 }
6763         } else if (data->opcode ==
6764                          VSS_ISTREAM_EVT_OOB_NOTIFY_ENC_BUFFER_READY) {
6765                 int ret = 0;
6766                 u16 cvs_handle;
6767                 uint32_t *cvs_voc_pkt;
6768                 struct cvs_enc_buffer_consumed_cmd send_enc_buf_consumed_cmd;
6769                 void *apr_cvs;
6770
6771                 pr_debug("Encoder buffer is ready\n");
6772
6773                 apr_cvs = common.apr_q6_cvs;
6774                 if (!apr_cvs) {
6775                         pr_err("%s: apr_cvs is NULL\n", __func__);
6776                         return -EINVAL;
6777                 }
6778                 cvs_handle = voice_get_cvs_handle(v);
6779
6780                 send_enc_buf_consumed_cmd.hdr.hdr_field =
6781                                 APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
6782                                 APR_HDR_LEN(APR_HDR_SIZE),
6783                                 APR_PKT_VER);
6784                 send_enc_buf_consumed_cmd.hdr.pkt_size =
6785                         APR_PKT_SIZE(APR_HDR_SIZE,
6786                         sizeof(send_enc_buf_consumed_cmd) - APR_HDR_SIZE);
6787
6788                 send_enc_buf_consumed_cmd.hdr.src_port =
6789                                 voice_get_idx_for_session(v->session_id);
6790                 send_enc_buf_consumed_cmd.hdr.dest_port = cvs_handle;
6791                 send_enc_buf_consumed_cmd.hdr.token = 0;
6792                 send_enc_buf_consumed_cmd.hdr.opcode =
6793                         VSS_ISTREAM_EVT_OOB_NOTIFY_ENC_BUFFER_CONSUMED;
6794
6795                 cvs_voc_pkt = v->shmem_info.sh_buf.buf[1].data;
6796                 if (cvs_voc_pkt != NULL &&  common.mvs_info.ul_cb != NULL) {
6797                         if (v->shmem_info.sh_buf.buf[1].size <
6798                             ((3 * sizeof(uint32_t)) + cvs_voc_pkt[2])) {
6799                                 pr_err("%s: invalid voc pkt size\n", __func__);
6800                                 return -EINVAL;
6801                         }
6802                         /* cvs_voc_pkt[0] contains tx timestamp */
6803                         common.mvs_info.ul_cb((uint8_t *)&cvs_voc_pkt[3],
6804                                               cvs_voc_pkt[2],
6805                                               cvs_voc_pkt[0],
6806                                               common.mvs_info.private_data);
6807                 } else
6808                         pr_err("%s: cvs_voc_pkt or ul_cb is NULL\n", __func__);
6809
6810                 ret = apr_send_pkt(apr_cvs,
6811                         (uint32_t *) &send_enc_buf_consumed_cmd);
6812                 if (ret < 0) {
6813                         pr_err("%s: Err send ENC_BUF_CONSUMED_NOTIFY %d\n",
6814                                 __func__, ret);
6815                         goto fail;
6816                 }
6817         } else if (data->opcode == VSS_ISTREAM_EVT_SEND_ENC_BUFFER) {
6818                 pr_debug("Recd VSS_ISTREAM_EVT_SEND_ENC_BUFFER\n");
6819         } else if (data->opcode ==
6820                          VSS_ISTREAM_EVT_OOB_NOTIFY_DEC_BUFFER_REQUEST) {
6821                 int ret = 0;
6822                 u16 cvs_handle;
6823                 uint32_t *cvs_voc_pkt;
6824                 struct cvs_dec_buffer_ready_cmd send_dec_buf;
6825                 void *apr_cvs;
6826                 apr_cvs = common.apr_q6_cvs;
6827
6828                 if (!apr_cvs) {
6829                         pr_err("%s: apr_cvs is NULL\n", __func__);
6830                         return -EINVAL;
6831                 }
6832                 cvs_handle = voice_get_cvs_handle(v);
6833
6834                 send_dec_buf.hdr.hdr_field =
6835                                 APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
6836                                 APR_HDR_LEN(APR_HDR_SIZE),
6837                                 APR_PKT_VER);
6838
6839                 send_dec_buf.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
6840                                         sizeof(send_dec_buf) - APR_HDR_SIZE);
6841
6842                 send_dec_buf.hdr.src_port =
6843                                 voice_get_idx_for_session(v->session_id);
6844                 send_dec_buf.hdr.dest_port = cvs_handle;
6845                 send_dec_buf.hdr.token = 0;
6846                 send_dec_buf.hdr.opcode =
6847                                  VSS_ISTREAM_EVT_OOB_NOTIFY_DEC_BUFFER_READY;
6848
6849                 cvs_voc_pkt = (uint32_t *)(v->shmem_info.sh_buf.buf[0].data);
6850                 if (cvs_voc_pkt != NULL && common.mvs_info.dl_cb != NULL) {
6851                         /* Set timestamp to 0 and advance the pointer */
6852                         cvs_voc_pkt[0] = 0;
6853                         /* Set media_type and advance the pointer */
6854                         cvs_voc_pkt[1] = common.mvs_info.media_type;
6855                         common.mvs_info.dl_cb(
6856                                               (uint8_t *)&cvs_voc_pkt[2],
6857                                               common.mvs_info.private_data);
6858                         ret = apr_send_pkt(apr_cvs, (uint32_t *) &send_dec_buf);
6859                         if (ret < 0) {
6860                                 pr_err("%s: Err send DEC_BUF_READY_NOTIFI %d\n",
6861                                         __func__, ret);
6862                                 goto fail;
6863                         }
6864                 } else {
6865                         pr_debug("%s: voc_pkt or dl_cb is NULL\n", __func__);
6866                         goto fail;
6867                 }
6868         } else if (data->opcode == VSS_ISTREAM_EVT_REQUEST_DEC_BUFFER) {
6869                 pr_debug("Recd VSS_ISTREAM_EVT_REQUEST_DEC_BUFFER\n");
6870         } else if (data->opcode == VSS_ISTREAM_EVT_SEND_DEC_BUFFER) {
6871                 pr_debug("Send dec buf resp\n");
6872         } else if (data->opcode == APR_RSP_ACCEPTED) {
6873                 ptr = data->payload;
6874                 if (ptr[0])
6875                         pr_debug("%s: APR_RSP_ACCEPTED for 0x%x:\n",
6876                                  __func__, ptr[0]);
6877         } else if (data->opcode == VSS_ISTREAM_EVT_NOT_READY) {
6878                 pr_debug("Recd VSS_ISTREAM_EVT_NOT_READY\n");
6879         } else if (data->opcode == VSS_ISTREAM_EVT_READY) {
6880                 pr_debug("Recd VSS_ISTREAM_EVT_READY\n");
6881         } else if ((data->opcode == VSS_ICOMMON_RSP_GET_PARAM) ||
6882                 (data->opcode == VSS_ICOMMON_RSP_GET_PARAM_V3)) {
6883                 pr_debug("%s: VSS_ICOMMON_RSP_GET_PARAM\n", __func__);
6884                 ptr = data->payload;
6885                 if (ptr[0] != 0) {
6886                         pr_err("%s: VSS_ICOMMON_RSP_GET_PARAM returned error = 0x%x\n",
6887                                __func__, ptr[0]);
6888                 }
6889                 rtac_make_voice_callback(RTAC_CVS, data->payload,
6890                                         data->payload_size);
6891         } else if (data->opcode == VSS_ISTREAM_EVT_RX_DTMF_DETECTED) {
6892                 struct vss_istream_evt_rx_dtmf_detected *dtmf_rx_detected;
6893                 uint32_t *voc_pkt = data->payload;
6894                 uint32_t pkt_len = data->payload_size;
6895
6896                 if ((voc_pkt != NULL) &&
6897                     (pkt_len ==
6898                         sizeof(struct vss_istream_evt_rx_dtmf_detected))) {
6899
6900                         dtmf_rx_detected =
6901                         (struct vss_istream_evt_rx_dtmf_detected *) voc_pkt;
6902                         pr_debug("RX_DTMF_DETECTED low_freq=%d high_freq=%d\n",
6903                                  dtmf_rx_detected->low_freq,
6904                                  dtmf_rx_detected->high_freq);
6905                         if (c->dtmf_info.dtmf_rx_ul_cb)
6906                                 c->dtmf_info.dtmf_rx_ul_cb((uint8_t *)voc_pkt,
6907                                         voc_get_session_name(v->session_id),
6908                                         c->dtmf_info.private_data);
6909                 } else {
6910                         pr_err("Invalid packet\n");
6911                 }
6912         }  else
6913                 pr_debug("Unknown opcode 0x%x\n", data->opcode);
6914
6915 fail:
6916         return 0;
6917 }
6918
6919 static int32_t qdsp_cvp_callback(struct apr_client_data *data, void *priv)
6920 {
6921         uint32_t *ptr = NULL;
6922         struct common_data *c = NULL;
6923         struct voice_data *v = NULL;
6924         int i = 0;
6925
6926         if ((data == NULL) || (priv == NULL)) {
6927                 pr_err("%s: data or priv is NULL\n", __func__);
6928                 return -EINVAL;
6929         }
6930
6931         c = priv;
6932
6933         if (data->opcode == RESET_EVENTS) {
6934                 pr_debug("%s: Reset event received in Voice service\n",
6935                                 __func__);
6936
6937                 apr_reset(c->apr_q6_cvp);
6938                 c->apr_q6_cvp = NULL;
6939                 cal_utils_clear_cal_block_q6maps(MAX_VOICE_CAL_TYPES,
6940                                 common.cal_data);
6941
6942                 /* Sub-system restart is applicable to all sessions. */
6943                 for (i = 0; i < MAX_VOC_SESSIONS; i++)
6944                         c->voice[i].cvp_handle = 0;
6945
6946                 /*
6947                  * Free the ION memory and clear handles for
6948                  * Source Tracking
6949                  */
6950                 if (is_source_tracking_shared_memomry_allocated()) {
6951                         msm_audio_ion_free(
6952                         common.source_tracking_sh_mem.sh_mem_block.client,
6953                         common.source_tracking_sh_mem.sh_mem_block.handle);
6954                         common.source_tracking_sh_mem.mem_handle = 0;
6955                         common.source_tracking_sh_mem.sh_mem_block.client =
6956                                                                         NULL;
6957                         common.source_tracking_sh_mem.sh_mem_block.handle =
6958                                                                         NULL;
6959                 }
6960                 voc_set_error_state(data->reset_proc);
6961                 return 0;
6962         }
6963
6964         v = voice_get_session_by_idx(data->dest_port);
6965         if (v == NULL) {
6966                 pr_err("%s: v is NULL\n", __func__);
6967
6968                 return -EINVAL;
6969         }
6970
6971         if (data->opcode == APR_BASIC_RSP_RESULT) {
6972                 if (data->payload_size >= (2 * sizeof(uint32_t))) {
6973                         ptr = data->payload;
6974
6975                         pr_debug("%x %x\n", ptr[0], ptr[1]);
6976                         if (ptr[1] != 0) {
6977                                 pr_err("%s: cmd = 0x%x returned error = 0x%x\n",
6978                                         __func__, ptr[0], ptr[1]);
6979                         }
6980                         switch (ptr[0]) {
6981                         case VSS_IVOCPROC_CMD_CREATE_FULL_CONTROL_SESSION_V2:
6982                         case VSS_IVOCPROC_CMD_CREATE_FULL_CONTROL_SESSION_V3:
6983                         /*response from  CVP */
6984                                 pr_debug("%s: cmd = 0x%x\n", __func__, ptr[0]);
6985                                 if (!ptr[1]) {
6986                                         voice_set_cvp_handle(v, data->src_port);
6987                                         pr_debug("status: %d, cvphdl=%d\n",
6988                                                  ptr[1], data->src_port);
6989                                 } else
6990                                         pr_err("got NACK from CVP create session response\n");
6991                                 v->cvp_state = CMD_STATUS_SUCCESS;
6992                                 v->async_err = ptr[1];
6993                                 wake_up(&v->cvp_wait);
6994                                 break;
6995                         case VSS_IVOCPROC_CMD_SET_DEVICE_V2:
6996                         case VSS_IVOCPROC_CMD_SET_DEVICE_V3:
6997                         case VSS_IVOLUME_CMD_SET_STEP:
6998                         case VSS_IVOCPROC_CMD_ENABLE:
6999                         case VSS_IVOCPROC_CMD_DISABLE:
7000                         case APRV2_IBASIC_CMD_DESTROY_SESSION:
7001                         case VSS_IVOCPROC_CMD_REGISTER_VOL_CALIBRATION_DATA:
7002                         case VSS_IVOCPROC_CMD_DEREGISTER_VOL_CALIBRATION_DATA:
7003                         case VSS_IVOCPROC_CMD_REGISTER_CALIBRATION_DATA_V2:
7004                         case VSS_IVOCPROC_CMD_DEREGISTER_CALIBRATION_DATA:
7005                         case VSS_IVOCPROC_CMD_REGISTER_DYNAMIC_CALIBRATION_DATA:
7006                     case VSS_IVOCPROC_CMD_DEREGISTER_DYNAMIC_CALIBRATION_DATA:
7007                         case VSS_IVOCPROC_CMD_REGISTER_STATIC_CALIBRATION_DATA:
7008                     case VSS_IVOCPROC_CMD_DEREGISTER_STATIC_CALIBRATION_DATA:
7009                         case VSS_IVOCPROC_CMD_REGISTER_DEVICE_CONFIG:
7010                         case VSS_IVOCPROC_CMD_DEREGISTER_DEVICE_CONFIG:
7011                         case VSS_ICOMMON_CMD_MAP_MEMORY:
7012                         case VSS_ICOMMON_CMD_UNMAP_MEMORY:
7013                         case VSS_IVOLUME_CMD_MUTE_V2:
7014                         case VSS_IVPCM_CMD_START_V2:
7015                         case VSS_IVPCM_CMD_STOP:
7016                         case VSS_IVOCPROC_CMD_TOPOLOGY_SET_DEV_CHANNELS:
7017                         case VSS_IVOCPROC_CMD_TOPOLOGY_COMMIT:
7018                                 v->cvp_state = CMD_STATUS_SUCCESS;
7019                                 v->async_err = ptr[1];
7020                                 wake_up(&v->cvp_wait);
7021                                 break;
7022                         case VSS_IVPCM_EVT_PUSH_BUFFER_V2:
7023                                 break;
7024                         case VSS_ICOMMON_CMD_SET_PARAM_V2:
7025                         case VSS_ICOMMON_CMD_SET_PARAM_V3:
7026                                 switch (data->token) {
7027                                 case VOC_SET_MEDIA_FORMAT_PARAM_TOKEN:
7028                                         pr_debug("%s: VSS_ICOMMON_CMD_SET_PARAM called by voice_send_cvp_media_format_cmd\n",
7029                                                 __func__);
7030                                         v->cvp_state = CMD_STATUS_SUCCESS;
7031                                         v->async_err = ptr[1];
7032                                         wake_up(&v->cvp_wait);
7033                                         break;
7034                                 case VOC_RTAC_SET_PARAM_TOKEN:
7035                                         pr_debug("%s: VSS_ICOMMON_CMD_SET_PARAM called by rtac\n",
7036                                                 __func__);
7037                                         rtac_make_voice_callback(
7038                                                 RTAC_CVP, ptr,
7039                                                 data->payload_size);
7040                                         break;
7041                                 default:
7042                                         pr_debug("%s: invalid token for command VSS_ICOMMON_CMD_SET_PARAM: %d\n",
7043                                                 __func__, data->token);
7044                                         break;
7045                                 }
7046                                 break;
7047                         case VSS_ICOMMON_CMD_GET_PARAM_V2:
7048                         case VSS_ICOMMON_CMD_GET_PARAM_V3:
7049                                 pr_debug("%s: VSS_ICOMMON_CMD_GET_PARAM_V2\n",
7050                                          __func__);
7051                                 /* Should only come here if there is an APR */
7052                                 /* error or malformed APR packet. Otherwise */
7053                                 /* response will be returned as */
7054                                 /* VSS_ICOMMON_RSP_GET_PARAM */
7055                                 if (ptr[1] != 0) {
7056                                         pr_err("%s: CVP get param error = %d, resuming\n",
7057                                                 __func__, ptr[1]);
7058                                         rtac_make_voice_callback(RTAC_CVP,
7059                                                 data->payload,
7060                                                 data->payload_size);
7061                                 }
7062                                 break;
7063                         case VSS_ISOUNDFOCUS_CMD_SET_SECTORS:
7064                                 if (!ptr[1])
7065                                         common.is_sound_focus_resp_success =
7066                                                                         true;
7067                                 else
7068                                         common.is_sound_focus_resp_success =
7069                                                                         false;
7070                                 v->cvp_state = CMD_STATUS_SUCCESS;
7071                                 v->async_err = ptr[1];
7072                                 wake_up(&v->cvp_wait);
7073                                 break;
7074                         case VSS_ISOUNDFOCUS_CMD_GET_SECTORS:
7075                                 /*
7076                                  * Should only come here if there is an error
7077                                  * response received from ADSP. Otherwise
7078                                  * response will be returned as
7079                                  * VSS_ISOUNDFOCUS_RSP_GET_SECTORS
7080                                  */
7081                                 pr_err("%s: VSS_ISOUNDFOCUS_CMD_GET_SECTORS failed\n",
7082                                         __func__);
7083
7084                                 common.is_sound_focus_resp_success = false;
7085                                 v->cvp_state = CMD_STATUS_SUCCESS;
7086                                 v->async_err = ptr[1];
7087                                 wake_up(&v->cvp_wait);
7088                                 break;
7089                         case VSS_ISOURCETRACK_CMD_GET_ACTIVITY:
7090                                 if (!ptr[1]) {
7091                                         /* Read data from shared memory */
7092                                         memcpy(&common.sourceTrackingResponse,
7093                                                common.source_tracking_sh_mem.
7094                                                         sh_mem_block.data,
7095                                                sizeof(struct
7096                                          vss_isourcetrack_activity_data_t));
7097                                         common.is_source_tracking_resp_success =
7098                                                                         true;
7099                                 } else {
7100                                         common.is_source_tracking_resp_success =
7101                                                                         false;
7102                                         pr_err("%s: Error received for source tracking params\n",
7103                                                 __func__);
7104                                 }
7105                                 v->cvp_state = CMD_STATUS_SUCCESS;
7106                                 v->async_err = ptr[1];
7107                                 wake_up(&v->cvp_wait);
7108                                 break;
7109                         default:
7110                                 pr_debug("%s: not match cmd = 0x%x\n",
7111                                           __func__, ptr[0]);
7112                                 break;
7113                         }
7114                 }
7115         } else if (data->opcode == VSS_ICOMMON_RSP_GET_PARAM ||
7116                    data->opcode == VSS_ICOMMON_RSP_GET_PARAM_V3) {
7117                 pr_debug("%s: VSS_ICOMMON_RSP_GET_PARAM\n", __func__);
7118                 ptr = data->payload;
7119                 if (ptr[0] != 0) {
7120                         pr_err("%s: VSS_ICOMMON_RSP_GET_PARAM returned error = 0x%x\n",
7121                                __func__, ptr[0]);
7122                 }
7123                 rtac_make_voice_callback(RTAC_CVP, data->payload,
7124                         data->payload_size);
7125         } else if (data->opcode == VSS_IVPCM_EVT_NOTIFY_V2) {
7126                 if ((data->payload != NULL) && data->payload_size ==
7127                     sizeof(struct vss_ivpcm_evt_notify_v2_t) &&
7128                     common.hostpcm_info.hostpcm_evt_cb != NULL) {
7129                         common.hostpcm_info.hostpcm_evt_cb(data->payload,
7130                                         voc_get_session_name(v->session_id),
7131                                         common.hostpcm_info.private_data);
7132                 }
7133         } else if (data->opcode == VSS_ISOUNDFOCUS_RSP_GET_SECTORS) {
7134                 if (data->payload && (data->payload_size ==
7135                         sizeof(struct vss_isoundfocus_rsp_get_sectors_t))) {
7136                         common.is_sound_focus_resp_success = true;
7137                         memcpy(&common.soundFocusResponse,
7138                                (struct vss_isoundfocus_rsp_get_sectors_t *)
7139                                data->payload,
7140                                sizeof(struct
7141                                          vss_isoundfocus_rsp_get_sectors_t));
7142                 } else {
7143                         common.is_sound_focus_resp_success = false;
7144                         pr_debug("%s: Invalid payload received from CVD\n",
7145                                  __func__);
7146                 }
7147                 v->cvp_state = CMD_STATUS_SUCCESS;
7148                 wake_up(&v->cvp_wait);
7149         }
7150         return 0;
7151 }
7152
7153 static int voice_free_oob_shared_mem(void)
7154 {
7155         int rc = 0;
7156         int cnt = 0;
7157         int bufcnt = NUM_OF_BUFFERS;
7158         struct voice_data *v = voice_get_session(
7159                                 common.voice[VOC_PATH_FULL].session_id);
7160
7161         mutex_lock(&common.common_lock);
7162         if (v == NULL) {
7163                 pr_err("%s: v is NULL\n", __func__);
7164
7165                 rc = -EINVAL;
7166                 goto done;
7167         }
7168
7169         rc = msm_audio_ion_free(v->shmem_info.sh_buf.client,
7170                                 v->shmem_info.sh_buf.handle);
7171         v->shmem_info.sh_buf.client = NULL;
7172         v->shmem_info.sh_buf.handle = NULL;
7173         if (rc < 0) {
7174                 pr_err("%s: Error:%d freeing memory\n", __func__, rc);
7175
7176                 goto done;
7177         }
7178
7179
7180         while (cnt < bufcnt) {
7181                 v->shmem_info.sh_buf.buf[cnt].data =  NULL;
7182                 v->shmem_info.sh_buf.buf[cnt].phys =  0;
7183                 cnt++;
7184         }
7185
7186         v->shmem_info.sh_buf.client = NULL;
7187         v->shmem_info.sh_buf.handle = NULL;
7188
7189 done:
7190         mutex_unlock(&common.common_lock);
7191         return rc;
7192 }
7193
7194 static int voice_alloc_oob_shared_mem(void)
7195 {
7196         int cnt = 0;
7197         int rc = 0;
7198         size_t len;
7199         void *mem_addr;
7200         dma_addr_t phys;
7201         int bufsz = BUFFER_BLOCK_SIZE;
7202         int bufcnt = NUM_OF_BUFFERS;
7203         struct voice_data *v = voice_get_session(
7204                                 common.voice[VOC_PATH_FULL].session_id);
7205
7206         mutex_lock(&common.common_lock);
7207         if (v == NULL) {
7208                 pr_err("%s: v is NULL\n", __func__);
7209
7210                 rc = -EINVAL;
7211                 goto done;
7212         }
7213
7214         rc = msm_audio_ion_alloc("voip_client", &(v->shmem_info.sh_buf.client),
7215                         &(v->shmem_info.sh_buf.handle),
7216                         bufsz*bufcnt,
7217                         &phys, &len,
7218                         &mem_addr);
7219         if (rc < 0) {
7220                 pr_err("%s: audio ION alloc failed, rc = %d\n",
7221                         __func__, rc);
7222
7223                 goto done;
7224         }
7225
7226         while (cnt < bufcnt) {
7227                 v->shmem_info.sh_buf.buf[cnt].data =  mem_addr  + (cnt * bufsz);
7228                 v->shmem_info.sh_buf.buf[cnt].phys =  phys + (cnt * bufsz);
7229                 v->shmem_info.sh_buf.buf[cnt].size = bufsz;
7230                 cnt++;
7231         }
7232
7233         pr_debug("%s buf[0].data:[%pK], buf[0].phys:[%pK], &buf[0].phys:[%pK],\n",
7234                  __func__,
7235                 (void *)v->shmem_info.sh_buf.buf[0].data,
7236                 &v->shmem_info.sh_buf.buf[0].phys,
7237                 (void *)&v->shmem_info.sh_buf.buf[0].phys);
7238         pr_debug("%s: buf[1].data:[%pK], buf[1].phys[%pK], &buf[1].phys[%pK]\n",
7239                 __func__,
7240                 (void *)v->shmem_info.sh_buf.buf[1].data,
7241                 &v->shmem_info.sh_buf.buf[1].phys,
7242                 (void *)&v->shmem_info.sh_buf.buf[1].phys);
7243
7244         memset((void *)v->shmem_info.sh_buf.buf[0].data, 0, (bufsz * bufcnt));
7245
7246 done:
7247         mutex_unlock(&common.common_lock);
7248         return rc;
7249 }
7250
7251 static int voice_alloc_oob_mem_table(void)
7252 {
7253         int rc = 0;
7254         size_t len;
7255         struct voice_data *v = voice_get_session(
7256                                 common.voice[VOC_PATH_FULL].session_id);
7257
7258         mutex_lock(&common.common_lock);
7259         if (v == NULL) {
7260                 pr_err("%s: v is NULL\n", __func__);
7261
7262                 rc = -EINVAL;
7263                 goto done;
7264         }
7265
7266         rc = msm_audio_ion_alloc("voip_client", &(v->shmem_info.memtbl.client),
7267                                 &(v->shmem_info.memtbl.handle),
7268                                 sizeof(struct vss_imemory_table_t),
7269                                 &v->shmem_info.memtbl.phys,
7270                                 &len,
7271                                 &(v->shmem_info.memtbl.data));
7272         if (rc < 0) {
7273                 pr_err("%s: audio ION alloc failed, rc = %d\n",
7274                         __func__, rc);
7275
7276                 goto done;
7277         }
7278
7279         v->shmem_info.memtbl.size = sizeof(struct vss_imemory_table_t);
7280         pr_debug("%s data[%pK]phys[%pK][%pK]\n", __func__,
7281                  (void *)v->shmem_info.memtbl.data,
7282                  &v->shmem_info.memtbl.phys,
7283                  (void *)&v->shmem_info.memtbl.phys);
7284
7285 done:
7286         mutex_unlock(&common.common_lock);
7287         return rc;
7288 }
7289
7290 int voc_send_cvp_start_vocpcm(uint32_t session_id,
7291                               struct vss_ivpcm_tap_point *vpcm_tp,
7292                               uint32_t no_of_tp)
7293 {
7294         struct cvp_start_cmd cvp_start_cmd;
7295         int ret = 0;
7296         void *apr_cvp;
7297         u16 cvp_handle;
7298         struct voice_data *v = voice_get_session(session_id);
7299         int i = 0;
7300
7301         if (v == NULL) {
7302                 pr_err("%s: v is NULL\n", __func__);
7303                 ret = -EINVAL;
7304                 goto done;
7305         }
7306         apr_cvp = common.apr_q6_cvp;
7307
7308         if (!apr_cvp) {
7309                 pr_err("%s: apr_cvp is NULL.\n", __func__);
7310                 ret = -EINVAL;
7311                 goto done;
7312         }
7313
7314         cvp_handle = voice_get_cvp_handle(v);
7315
7316         /* Fill the header */
7317         cvp_start_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
7318                 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
7319         cvp_start_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
7320                 sizeof(struct vss_ivpcm_tap_point) * no_of_tp) +
7321                 sizeof(cvp_start_cmd.vpcm_start_cmd.num_tap_points) +
7322                 sizeof(cvp_start_cmd.vpcm_start_cmd.mem_handle);
7323         cvp_start_cmd.hdr.src_port = voice_get_idx_for_session(v->session_id);
7324         cvp_start_cmd.hdr.dest_port = cvp_handle;
7325         cvp_start_cmd.hdr.token = 0;
7326         cvp_start_cmd.hdr.opcode = VSS_IVPCM_CMD_START_V2;
7327
7328         for (i = 0; i < no_of_tp; i++) {
7329                 cvp_start_cmd.vpcm_start_cmd.tap_points[i].tap_point =
7330                                                         vpcm_tp[i].tap_point;
7331                 cvp_start_cmd.vpcm_start_cmd.tap_points[i].direction =
7332                                                         vpcm_tp[i].direction;
7333                 cvp_start_cmd.vpcm_start_cmd.tap_points[i].sampling_rate =
7334                                                     vpcm_tp[i].sampling_rate;
7335                 cvp_start_cmd.vpcm_start_cmd.tap_points[i].duration = 0;
7336         }
7337
7338         cvp_start_cmd.vpcm_start_cmd.mem_handle =
7339                                 common.voice_host_pcm_mem_handle;
7340         cvp_start_cmd.vpcm_start_cmd.num_tap_points = no_of_tp;
7341
7342         v->cvp_state = CMD_STATUS_FAIL;
7343         v->async_err = 0;
7344         ret = apr_send_pkt(apr_cvp, (uint32_t *) &cvp_start_cmd);
7345         if (ret < 0) {
7346                 pr_err("%s: Fail: sending vocpcm map memory,\n", __func__);
7347                 goto done;
7348         }
7349         ret = wait_event_timeout(v->cvp_wait,
7350                         (v->cvp_state == CMD_STATUS_SUCCESS),
7351                         msecs_to_jiffies(TIMEOUT_MS));
7352         if (!ret) {
7353                 pr_err("%s: wait_event timeout\n", __func__);
7354                 goto done;
7355         }
7356         if (v->async_err > 0) {
7357                 pr_err("%s: DSP returned error[%s]\n",
7358                                 __func__, adsp_err_get_err_str(
7359                                 v->async_err));
7360                 ret = adsp_err_get_lnx_err_code(
7361                                 v->async_err);
7362                 goto done;
7363         }
7364
7365 done:
7366         return ret;
7367 }
7368
7369 int voc_send_cvp_stop_vocpcm(uint32_t session_id)
7370 {
7371         struct cvp_command vpcm_stop_cmd;
7372         int ret = 0;
7373         void *apr_cvp;
7374         u16 cvp_handle;
7375         struct voice_data *v = voice_get_session(session_id);
7376
7377         if (v == NULL) {
7378                 pr_err("%s: v is NULL\n", __func__);
7379                 ret = -EINVAL;
7380                 goto done;
7381         }
7382         apr_cvp = common.apr_q6_cvp;
7383
7384         if (!apr_cvp) {
7385                 pr_err("%s: apr_cvp is NULL.\n", __func__);
7386                 ret = -EINVAL;
7387                 goto done;
7388         }
7389
7390         cvp_handle = voice_get_cvp_handle(v);
7391
7392         /* fill in the header */
7393         vpcm_stop_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
7394                                  APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
7395         vpcm_stop_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
7396                          sizeof(vpcm_stop_cmd) - APR_HDR_SIZE);
7397         vpcm_stop_cmd.hdr.src_port = voice_get_idx_for_session(v->session_id);
7398         vpcm_stop_cmd.hdr.dest_port = cvp_handle;
7399         vpcm_stop_cmd.hdr.token = 0;
7400         vpcm_stop_cmd.hdr.opcode = VSS_IVPCM_CMD_STOP;
7401
7402         v->cvp_state = CMD_STATUS_FAIL;
7403         v->async_err = 0;
7404         ret = apr_send_pkt(apr_cvp, (uint32_t *) &vpcm_stop_cmd);
7405         if (ret < 0) {
7406                 pr_err("Fail: sending vocpcm stop,\n");
7407                 goto done;
7408         }
7409         ret = wait_event_timeout(v->cvp_wait,
7410                         (v->cvp_state == CMD_STATUS_SUCCESS),
7411                         msecs_to_jiffies(TIMEOUT_MS));
7412         if (!ret) {
7413                 pr_err("%s: wait_event timeout\n", __func__);
7414                 goto done;
7415         }
7416
7417         if (v->async_err > 0) {
7418                 pr_err("%s: DSP returned error[%s]\n",
7419                                 __func__, adsp_err_get_err_str(
7420                                 v->async_err));
7421                 ret = adsp_err_get_lnx_err_code(
7422                                 v->async_err);
7423                 goto done;
7424         }
7425
7426 done:
7427         return ret;
7428 }
7429
7430 int voc_send_cvp_map_vocpcm_memory(uint32_t session_id,
7431                                    struct mem_map_table *tp_mem_table,
7432                                    phys_addr_t paddr, uint32_t bufsize)
7433 {
7434         return  voice_map_memory_physical_cmd(voice_get_session(session_id),
7435                                               tp_mem_table,
7436                                               (dma_addr_t) paddr, bufsize,
7437                                               VOC_VOICE_HOST_PCM_MAP_TOKEN);
7438 }
7439
7440 int voc_send_cvp_unmap_vocpcm_memory(uint32_t session_id)
7441 {
7442         int ret = 0;
7443
7444         ret =  voice_send_mvm_unmap_memory_physical_cmd(
7445                                 voice_get_session(session_id),
7446                                 common.voice_host_pcm_mem_handle);
7447
7448         if (ret == 0)
7449                 common.voice_host_pcm_mem_handle = 0;
7450
7451         return ret;
7452 }
7453
7454 int voc_send_cvp_vocpcm_push_buf_evt(uint32_t session_id,
7455                         struct vss_ivpcm_evt_push_buffer_v2_t *push_buff_evt)
7456 {
7457         struct cvp_push_buf_cmd vpcm_push_buf_cmd;
7458         int ret = 0;
7459         void *apr_cvp;
7460         u16 cvp_handle;
7461         struct voice_data *v = voice_get_session(session_id);
7462
7463         if (v == NULL) {
7464                 pr_err("%s: v is NULL\n", __func__);
7465                 ret = -EINVAL;
7466                 goto done;
7467         }
7468         apr_cvp = common.apr_q6_cvp;
7469
7470         if (!apr_cvp) {
7471                 pr_err("%s: apr_cvp is NULL.\n", __func__);
7472                 ret = -EINVAL;
7473                 goto done;
7474         }
7475
7476         memset(&vpcm_push_buf_cmd, 0, sizeof(vpcm_push_buf_cmd));
7477         cvp_handle = voice_get_cvp_handle(v);
7478
7479         /* fill in the header */
7480         vpcm_push_buf_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
7481                                 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
7482         vpcm_push_buf_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
7483                                 sizeof(vpcm_push_buf_cmd) - APR_HDR_SIZE);
7484         vpcm_push_buf_cmd.hdr.src_port =
7485                                 voice_get_idx_for_session(v->session_id);
7486         vpcm_push_buf_cmd.hdr.dest_port = cvp_handle;
7487         vpcm_push_buf_cmd.hdr.token = 0;
7488         vpcm_push_buf_cmd.hdr.opcode = VSS_IVPCM_EVT_PUSH_BUFFER_V2;
7489
7490         vpcm_push_buf_cmd.vpcm_evt_push_buffer.tap_point =
7491                                         push_buff_evt->tap_point;
7492         vpcm_push_buf_cmd.vpcm_evt_push_buffer.push_buf_mask =
7493                                         push_buff_evt->push_buf_mask;
7494         vpcm_push_buf_cmd.vpcm_evt_push_buffer.out_buf_mem_address =
7495                                         push_buff_evt->out_buf_mem_address;
7496         vpcm_push_buf_cmd.vpcm_evt_push_buffer.in_buf_mem_address =
7497                                         push_buff_evt->in_buf_mem_address;
7498         vpcm_push_buf_cmd.vpcm_evt_push_buffer.out_buf_mem_size =
7499                                         push_buff_evt->out_buf_mem_size;
7500         vpcm_push_buf_cmd.vpcm_evt_push_buffer.in_buf_mem_size =
7501                                         push_buff_evt->in_buf_mem_size;
7502         vpcm_push_buf_cmd.vpcm_evt_push_buffer.sampling_rate =
7503                                         push_buff_evt->sampling_rate;
7504         vpcm_push_buf_cmd.vpcm_evt_push_buffer.num_in_channels =
7505                                         push_buff_evt->num_in_channels;
7506
7507         ret = apr_send_pkt(apr_cvp, (uint32_t *) &vpcm_push_buf_cmd);
7508         if (ret < 0) {
7509                 pr_err("Fail: sending vocpcm map memory,\n");
7510                 goto done;
7511         }
7512
7513 done:
7514         return ret;
7515 }
7516
7517 void voc_register_hpcm_evt_cb(hostpcm_cb_fn hostpcm_cb,
7518                               void *private_data)
7519 {
7520         common.hostpcm_info.hostpcm_evt_cb = hostpcm_cb;
7521         common.hostpcm_info.private_data = private_data;
7522 }
7523
7524 void voc_deregister_hpcm_evt_cb(void)
7525 {
7526         common.hostpcm_info.hostpcm_evt_cb = NULL;
7527         common.hostpcm_info.private_data = NULL;
7528 }
7529
7530 int voc_get_cvd_version(char *cvd_version)
7531 {
7532         int ret = 0;
7533         struct voice_data *v = voice_get_session(VOICE_SESSION_VSID);
7534
7535
7536         if (v == NULL) {
7537                 pr_err("%s: invalid session_id 0x%x\n",
7538                        __func__, VOICE_SESSION_VSID);
7539
7540                 ret = -EINVAL;
7541                 goto done;
7542         }
7543
7544         if (is_cvd_version_queried()) {
7545                 pr_debug("%s: Returning the cached value %s\n",
7546                          __func__, common.cvd_version);
7547
7548                 goto done;
7549         }
7550
7551         /* Register callback to APR */
7552         ret = voice_apr_register(VOICE_SESSION_VSID);
7553         if (ret < 0) {
7554                 pr_err("%s: apr register failed\n", __func__);
7555                 goto done;
7556         }
7557
7558         mutex_lock(&common.common_lock);
7559         mutex_lock(&v->lock);
7560         ret = voice_send_mvm_cvd_version_cmd(v);
7561         if (ret < 0) {
7562                 pr_err("%s: voice_send_mvm_cvd_version_cmd failed\n", __func__);
7563                 goto unlock;
7564         }
7565         ret = 0;
7566
7567 unlock:
7568         mutex_unlock(&v->lock);
7569         mutex_unlock(&common.common_lock);
7570
7571 done:
7572         if (cvd_version)
7573                 memcpy(cvd_version, common.cvd_version,
7574                        CVD_VERSION_STRING_MAX_SIZE);
7575
7576         return ret;
7577 }
7578
7579 static int voice_alloc_cal_mem_map_table(void)
7580 {
7581         int ret = 0;
7582         size_t len;
7583
7584         ret = msm_audio_ion_alloc("voc_cal",
7585                                 &(common.cal_mem_map_table.client),
7586                                 &(common.cal_mem_map_table.handle),
7587                                 sizeof(struct vss_imemory_table_t),
7588                                 &common.cal_mem_map_table.phys,
7589                                 &len,
7590                                 &(common.cal_mem_map_table.data));
7591         if ((ret < 0) && (ret != -EPROBE_DEFER)) {
7592                 pr_err("%s: audio ION alloc failed, rc = %d\n",
7593                         __func__, ret);
7594                 goto done;
7595         }
7596
7597         common.cal_mem_map_table.size = sizeof(struct vss_imemory_table_t);
7598         pr_debug("%s: data %pK phys %pK\n", __func__,
7599                  common.cal_mem_map_table.data,
7600                  &common.cal_mem_map_table.phys);
7601
7602 done:
7603         return ret;
7604 }
7605
7606 static int voice_alloc_rtac_mem_map_table(void)
7607 {
7608         int ret = 0;
7609         size_t len;
7610
7611         ret = msm_audio_ion_alloc("voc_rtac_cal",
7612                         &(common.rtac_mem_map_table.client),
7613                         &(common.rtac_mem_map_table.handle),
7614                         sizeof(struct vss_imemory_table_t),
7615                         &common.rtac_mem_map_table.phys,
7616                         &len,
7617                         &(common.rtac_mem_map_table.data));
7618         if (ret < 0) {
7619                 pr_err("%s: audio ION alloc failed, rc = %d\n",
7620                         __func__, ret);
7621                 goto done;
7622         }
7623
7624         common.rtac_mem_map_table.size = sizeof(struct vss_imemory_table_t);
7625         pr_debug("%s: data %pK phys %pK\n", __func__,
7626                  common.rtac_mem_map_table.data,
7627                  &common.rtac_mem_map_table.phys);
7628
7629 done:
7630         return ret;
7631 }
7632
7633 static int voice_alloc_and_map_oob_mem(struct voice_data *v)
7634 {
7635         int ret = 0;
7636
7637         if (v == NULL) {
7638                 pr_err("%s: v is NULL\n", __func__);
7639
7640                 return -EINVAL;
7641         }
7642
7643         if (!is_voip_memory_allocated()) {
7644                 ret = voc_alloc_voip_shared_memory();
7645                 if (ret < 0) {
7646                         pr_err("%s: Failed to create voip oob memory %d\n",
7647                                    __func__, ret);
7648
7649                         goto done;
7650                 }
7651         }
7652
7653         ret = voice_map_memory_physical_cmd(v,
7654                         &v->shmem_info.memtbl,
7655                         v->shmem_info.sh_buf.buf[0].phys,
7656                         v->shmem_info.sh_buf.buf[0].size * NUM_OF_BUFFERS,
7657                         VOIP_MEM_MAP_TOKEN);
7658         if (ret) {
7659                 pr_err("%s: mvm_map_memory_phy failed %d\n",
7660                            __func__, ret);
7661
7662                 goto done;
7663         }
7664
7665 done:
7666         return ret;
7667 }
7668
7669 uint32_t voice_get_topology(uint32_t topology_idx)
7670 {
7671         uint32_t topology = VSS_IVOCPROC_TOPOLOGY_ID_RX_DEFAULT;
7672         struct cal_block_data *cal_block = NULL;
7673
7674         /* initialize as defualt topology */
7675         if (topology_idx == CVP_VOC_RX_TOPOLOGY_CAL) {
7676                 topology = VSS_IVOCPROC_TOPOLOGY_ID_RX_DEFAULT;
7677         } else if (topology_idx == CVP_VOC_TX_TOPOLOGY_CAL) {
7678                 topology = VSS_IVOCPROC_TOPOLOGY_ID_TX_SM_ECNS;
7679         } else {
7680                 pr_err("%s: cal index %x is invalid!\n",
7681                         __func__, topology_idx);
7682
7683                 goto done;
7684         }
7685
7686         if (common.cal_data[topology_idx] == NULL) {
7687                 pr_err("%s: cal type is NULL for cal index %x\n",
7688                         __func__, topology_idx);
7689
7690                 goto done;
7691         }
7692
7693         mutex_lock(&common.cal_data[topology_idx]->lock);
7694         cal_block = cal_utils_get_only_cal_block(
7695                 common.cal_data[topology_idx]);
7696         if (cal_block == NULL) {
7697                 pr_debug("%s: cal_block not found for cal index %x\n",
7698                         __func__, topology_idx);
7699
7700                 goto unlock;
7701         }
7702
7703         topology = ((struct audio_cal_info_voc_top *)
7704                 cal_block->cal_info)->topology;
7705 unlock:
7706         mutex_unlock(&common.cal_data[topology_idx]->lock);
7707 done:
7708         pr_debug("%s: Using topology %d\n", __func__, topology);
7709
7710         return topology;
7711 }
7712
7713 static int get_cal_type_index(int32_t cal_type)
7714 {
7715         int ret = -EINVAL;
7716
7717         switch (cal_type) {
7718         case CVP_VOC_RX_TOPOLOGY_CAL_TYPE:
7719                 ret = CVP_VOC_RX_TOPOLOGY_CAL;
7720                 break;
7721         case CVP_VOC_TX_TOPOLOGY_CAL_TYPE:
7722                 ret = CVP_VOC_TX_TOPOLOGY_CAL;
7723                 break;
7724         case CVP_VOCPROC_STATIC_CAL_TYPE:
7725                 ret = CVP_VOCPROC_CAL;
7726                 break;
7727         case CVP_VOCPROC_DYNAMIC_CAL_TYPE:
7728                 ret = CVP_VOCVOL_CAL;
7729                 break;
7730         case CVS_VOCSTRM_STATIC_CAL_TYPE:
7731                 ret = CVS_VOCSTRM_CAL;
7732                 break;
7733         case CVP_VOCDEV_CFG_CAL_TYPE:
7734                 ret = CVP_VOCDEV_CFG_CAL;
7735                 break;
7736         case CVP_VOCPROC_STATIC_COL_CAL_TYPE:
7737                 ret = CVP_VOCPROC_COL_CAL;
7738                 break;
7739         case CVP_VOCPROC_DYNAMIC_COL_CAL_TYPE:
7740                 ret = CVP_VOCVOL_COL_CAL;
7741                 break;
7742         case CVS_VOCSTRM_STATIC_COL_CAL_TYPE:
7743                 ret = CVS_VOCSTRM_COL_CAL;
7744                 break;
7745         case VOICE_RTAC_INFO_CAL_TYPE:
7746                 ret = VOICE_RTAC_INFO_CAL;
7747                 break;
7748         case VOICE_RTAC_APR_CAL_TYPE:
7749                 ret = VOICE_RTAC_APR_CAL;
7750                 break;
7751         default:
7752                 pr_err("%s: Invalid cal type %d!\n", __func__, cal_type);
7753         }
7754         return ret;
7755 }
7756
7757 static int voice_prepare_volume_boost(int32_t cal_type,
7758                                         size_t data_size, void *data)
7759 {
7760         return voc_deregister_vocproc_vol_table();
7761 }
7762
7763 static int voice_enable_volume_boost(int32_t cal_type,
7764                                 size_t data_size, void *data)
7765 {
7766         return voc_register_vocproc_vol_table();
7767 }
7768
7769 static int voice_alloc_cal(int32_t cal_type,
7770                            size_t data_size, void *data)
7771 {
7772         int ret = 0;
7773         int cal_index;
7774         int cal_version;
7775
7776         pr_debug("%s\n", __func__);
7777
7778         cal_version = cal_utils_get_cal_type_version(data);
7779         common.is_per_vocoder_cal_enabled =
7780                         !!(cal_version & PER_VOCODER_CAL_BIT_MASK);
7781
7782         cal_index = get_cal_type_index(cal_type);
7783         if (cal_index < 0) {
7784                 pr_err("%s: Could not get cal index %d!\n",
7785                         __func__, cal_index);
7786                 ret = -EINVAL;
7787                 goto done;
7788         }
7789
7790         ret = cal_utils_alloc_cal(data_size, data,
7791                 common.cal_data[cal_index], 0, NULL);
7792         if (ret < 0) {
7793                 pr_err("%s: Cal_utils_alloc_block failed, ret = %d, cal type = %d!\n",
7794                         __func__, ret, cal_type);
7795                 ret = -EINVAL;
7796                 goto done;
7797         }
7798 done:
7799         return ret;
7800 }
7801
7802 static int voice_dealloc_cal(int32_t cal_type,
7803                              size_t data_size, void *data)
7804 {
7805         int ret = 0;
7806         int cal_index;
7807         pr_debug("%s\n", __func__);
7808
7809         cal_index = get_cal_type_index(cal_type);
7810         if (cal_index < 0) {
7811                 pr_err("%s: Could not get cal index %d!\n",
7812                         __func__, cal_index);
7813
7814                 ret = -EINVAL;
7815                 goto done;
7816         }
7817
7818         ret = cal_utils_dealloc_cal(data_size, data,
7819                 common.cal_data[cal_index]);
7820         if (ret < 0) {
7821                 pr_err("%s: Cal_utils_dealloc_block failed, ret = %d, cal type = %d!\n",
7822                         __func__, ret, cal_type);
7823
7824                 ret = -EINVAL;
7825                 goto done;
7826         }
7827 done:
7828         return ret;
7829 }
7830
7831 static int voice_set_cal(int32_t cal_type,
7832                          size_t data_size, void *data)
7833 {
7834         int ret = 0;
7835         int cal_index;
7836         pr_debug("%s\n", __func__);
7837
7838         cal_index = get_cal_type_index(cal_type);
7839         if (cal_index < 0) {
7840                 pr_err("%s: Could not get cal index %d!\n",
7841                         __func__, cal_index);
7842
7843                 ret = -EINVAL;
7844                 goto done;
7845         }
7846
7847         ret = cal_utils_set_cal(data_size, data,
7848                 common.cal_data[cal_index], 0, NULL);
7849         if (ret < 0) {
7850                 pr_err("%s: Cal_utils_set_cal failed, ret = %d, cal type = %d!\n",
7851                         __func__, ret, cal_type);
7852
7853                 ret = -EINVAL;
7854                 goto done;
7855         }
7856 done:
7857         return ret;
7858 }
7859
7860 static void voice_delete_cal_data(void)
7861 {
7862         pr_debug("%s\n", __func__);
7863
7864         cal_utils_destroy_cal_types(MAX_VOICE_CAL_TYPES, common.cal_data);
7865
7866         return;
7867 }
7868
7869 static int voice_init_cal_data(void)
7870 {
7871         int ret = 0;
7872         struct cal_type_info cal_type_info[] = {
7873                 {{CVP_VOC_RX_TOPOLOGY_CAL_TYPE,
7874                 {NULL, NULL, NULL, voice_set_cal, NULL, NULL} },
7875                 {NULL, NULL, cal_utils_match_buf_num} },
7876
7877                 {{CVP_VOC_TX_TOPOLOGY_CAL_TYPE,
7878                 {NULL, NULL, NULL, voice_set_cal, NULL, NULL} },
7879                 {NULL, NULL, cal_utils_match_buf_num} },
7880
7881                 {{CVP_VOCPROC_STATIC_CAL_TYPE,
7882                 {voice_alloc_cal, voice_dealloc_cal, NULL,
7883                 voice_set_cal, NULL, NULL} },
7884                 {NULL, voice_unmap_cal_memory,
7885                 cal_utils_match_buf_num} },
7886
7887                 {{CVP_VOCPROC_DYNAMIC_CAL_TYPE,
7888                 {voice_alloc_cal, voice_dealloc_cal,
7889                 voice_prepare_volume_boost,
7890                 voice_set_cal, NULL,
7891                 voice_enable_volume_boost} },
7892                 {NULL, voice_unmap_cal_memory,
7893                 cal_utils_match_buf_num} },
7894
7895                 {{CVP_VOCDEV_CFG_CAL_TYPE,
7896                 {voice_alloc_cal, voice_dealloc_cal, NULL,
7897                 voice_set_cal, NULL, NULL} },
7898                 {NULL, voice_unmap_cal_memory,
7899                 cal_utils_match_buf_num} },
7900
7901                 {{CVP_VOCPROC_STATIC_COL_CAL_TYPE,
7902                 {NULL, NULL, NULL, voice_set_cal, NULL, NULL} },
7903                 {NULL, NULL, cal_utils_match_buf_num} },
7904
7905                 {{CVP_VOCPROC_DYNAMIC_COL_CAL_TYPE,
7906                 {NULL, NULL, NULL, voice_set_cal, NULL, NULL} },
7907                 {NULL, NULL, cal_utils_match_buf_num} },
7908
7909                 {{CVS_VOCSTRM_STATIC_CAL_TYPE,
7910                 {voice_alloc_cal, voice_dealloc_cal, NULL,
7911                 voice_set_cal, NULL, NULL} },
7912                 {NULL, voice_unmap_cal_memory,
7913                 cal_utils_match_buf_num} },
7914
7915                 {{CVS_VOCSTRM_STATIC_COL_CAL_TYPE,
7916                 {NULL, NULL, NULL, voice_set_cal, NULL, NULL} },
7917                 {NULL, NULL, cal_utils_match_buf_num} },
7918
7919                 {{VOICE_RTAC_INFO_CAL_TYPE,
7920                 {NULL, NULL, NULL, NULL, NULL, NULL} },
7921                 {NULL, NULL, cal_utils_match_buf_num} },
7922
7923                 {{VOICE_RTAC_APR_CAL_TYPE,
7924                 {NULL, NULL, NULL, NULL, NULL, NULL} },
7925                 {NULL, NULL, cal_utils_match_buf_num} },
7926         };
7927
7928         ret = cal_utils_create_cal_types(MAX_VOICE_CAL_TYPES, common.cal_data,
7929                 cal_type_info);
7930         if (ret < 0) {
7931                 pr_err("%s: Could not create cal type!\n",
7932                         __func__);
7933
7934                 ret = -EINVAL;
7935                 goto err;
7936         }
7937
7938         return ret;
7939 err:
7940         voice_delete_cal_data();
7941         memset(&common, 0, sizeof(struct common_data));
7942         return ret;
7943 }
7944
7945 static int voice_send_set_sound_focus_cmd(struct voice_data *v,
7946                                  struct sound_focus_param soundFocusData)
7947 {
7948         struct cvp_set_sound_focus_param_cmd_t cvp_set_sound_focus_param_cmd;
7949         int ret = 0;
7950         void *apr_cvp;
7951         u16 cvp_handle;
7952         int i;
7953
7954         pr_debug("%s: Enter\n", __func__);
7955
7956         if (v == NULL) {
7957                 pr_err("%s: v is NULL\n", __func__);
7958
7959                 ret = -EINVAL;
7960                 goto done;
7961         }
7962         apr_cvp = common.apr_q6_cvp;
7963
7964         if (!apr_cvp) {
7965                 pr_err("%s: apr_cvp is NULL.\n", __func__);
7966
7967                 ret = -EINVAL;
7968                 goto done;
7969         }
7970         cvp_handle = voice_get_cvp_handle(v);
7971
7972         /* send Sound Focus Params to cvp */
7973         cvp_set_sound_focus_param_cmd.hdr.hdr_field =
7974                                 APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
7975                                               APR_HDR_LEN(APR_HDR_SIZE),
7976                                               APR_PKT_VER);
7977         cvp_set_sound_focus_param_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
7978                         sizeof(cvp_set_sound_focus_param_cmd) - APR_HDR_SIZE);
7979         cvp_set_sound_focus_param_cmd.hdr.src_port =
7980                                 voice_get_idx_for_session(v->session_id);
7981         cvp_set_sound_focus_param_cmd.hdr.dest_port = cvp_handle;
7982         cvp_set_sound_focus_param_cmd.hdr.token = 0;
7983         cvp_set_sound_focus_param_cmd.hdr.opcode =
7984                                          VSS_ISOUNDFOCUS_CMD_SET_SECTORS;
7985
7986         memset(&(cvp_set_sound_focus_param_cmd.cvp_set_sound_focus_param), 0xFF,
7987                 sizeof(struct vss_isoundfocus_cmd_set_sectors_t));
7988         for (i = 0; i < MAX_SECTORS; i++) {
7989                 cvp_set_sound_focus_param_cmd.cvp_set_sound_focus_param.
7990                         start_angles[i] = soundFocusData.start_angle[i];
7991                 cvp_set_sound_focus_param_cmd.cvp_set_sound_focus_param.
7992                         enables[i] = soundFocusData.enable[i];
7993                 pr_debug("%s: start_angle[%d] = %d\n",
7994                           __func__, i, soundFocusData.start_angle[i]);
7995                 pr_debug("%s: enable[%d] = %d\n",
7996                           __func__, i, soundFocusData.enable[i]);
7997         }
7998         cvp_set_sound_focus_param_cmd.cvp_set_sound_focus_param.gain_step =
7999                                         soundFocusData.gain_step;
8000         pr_debug("%s: gain_step = %d\n", __func__, soundFocusData.gain_step);
8001
8002         v->cvp_state = CMD_STATUS_FAIL;
8003         v->async_err = 0;
8004
8005         ret = apr_send_pkt(apr_cvp, (uint32_t *)&cvp_set_sound_focus_param_cmd);
8006         if (ret < 0) {
8007                 pr_err("%s: Error in sending APR command\n", __func__);
8008
8009                 ret = -EINVAL;
8010                 goto done;
8011         }
8012         ret = wait_event_timeout(v->cvp_wait,
8013                                  (v->cvp_state == CMD_STATUS_SUCCESS),
8014                                  msecs_to_jiffies(TIMEOUT_MS));
8015         if (!ret) {
8016                 pr_err("%s: wait_event timeout\n", __func__);
8017
8018                 ret = -EINVAL;
8019                 goto done;
8020         }
8021
8022         if (v->async_err > 0) {
8023                 pr_err("%s: DSP returned error[%s]\n",
8024                                 __func__, adsp_err_get_err_str(
8025                                 v->async_err));
8026                 ret = adsp_err_get_lnx_err_code(
8027                                 v->async_err);
8028                 goto done;
8029         }
8030
8031         if (common.is_sound_focus_resp_success) {
8032                 ret = 0;
8033         } else {
8034                 pr_err("%s: Error in setting sound focus params\n", __func__);
8035
8036                 ret = -EINVAL;
8037         }
8038
8039 done:
8040         pr_debug("%s: Exit, ret=%d\n", __func__, ret);
8041
8042         return ret;
8043 }
8044
8045 int voc_set_sound_focus(struct sound_focus_param soundFocusData)
8046 {
8047         struct voice_data *v = NULL;
8048         int ret = -EINVAL;
8049         struct voice_session_itr itr;
8050
8051         pr_debug("%s: Enter\n", __func__);
8052
8053         mutex_lock(&common.common_lock);
8054         voice_itr_init(&itr, ALL_SESSION_VSID);
8055         while (voice_itr_get_next_session(&itr, &v)) {
8056                 if (v != NULL) {
8057                         mutex_lock(&v->lock);
8058                         if (is_voc_state_active(v->voc_state) &&
8059                                 (v->lch_mode != VOICE_LCH_START) &&
8060                                 !v->disable_topology)
8061                                 ret = voice_send_set_sound_focus_cmd(v,
8062                                                         soundFocusData);
8063                         mutex_unlock(&v->lock);
8064                 } else {
8065                         pr_err("%s: invalid session\n", __func__);
8066
8067                         ret = -EINVAL;
8068                         break;
8069                 }
8070         }
8071         mutex_unlock(&common.common_lock);
8072         pr_debug("%s: Exit, ret=%d\n", __func__, ret);
8073
8074         return ret;
8075 }
8076
8077 static int voice_send_get_sound_focus_cmd(struct voice_data *v,
8078                                 struct sound_focus_param *soundFocusData)
8079 {
8080         struct apr_hdr cvp_get_sound_focus_param_cmd;
8081         int ret = 0;
8082         void *apr_cvp;
8083         u16 cvp_handle;
8084         int i;
8085
8086         pr_debug("%s: Enter\n", __func__);
8087
8088         if (!v) {
8089                 pr_err("%s: v is NULL\n", __func__);
8090
8091                 ret = -EINVAL;
8092                 goto done;
8093         }
8094         apr_cvp = common.apr_q6_cvp;
8095
8096         if (!apr_cvp) {
8097                 pr_err("%s: apr_cvp is NULL\n", __func__);
8098
8099                 ret = -EINVAL;
8100                 goto done;
8101         }
8102
8103         cvp_handle = voice_get_cvp_handle(v);
8104
8105         /* send APR command to retrive Sound Focus Params */
8106         cvp_get_sound_focus_param_cmd.hdr_field =
8107                                 APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
8108                                               APR_HDR_LEN(APR_HDR_SIZE),
8109                                               APR_PKT_VER);
8110         cvp_get_sound_focus_param_cmd.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
8111                         sizeof(cvp_get_sound_focus_param_cmd) - APR_HDR_SIZE);
8112         cvp_get_sound_focus_param_cmd.src_port =
8113                                 voice_get_idx_for_session(v->session_id);
8114         cvp_get_sound_focus_param_cmd.dest_port = cvp_handle;
8115         cvp_get_sound_focus_param_cmd.token = 0;
8116         cvp_get_sound_focus_param_cmd.opcode = VSS_ISOUNDFOCUS_CMD_GET_SECTORS;
8117
8118         v->cvp_state = CMD_STATUS_FAIL;
8119         v->async_err = 0;
8120         ret = apr_send_pkt(apr_cvp, (uint32_t *)&cvp_get_sound_focus_param_cmd);
8121         if (ret < 0) {
8122                 pr_err("%s: Error in sending APR command\n", __func__);
8123
8124                 ret = -EINVAL;
8125                 goto done;
8126         }
8127         ret = wait_event_timeout(v->cvp_wait,
8128                                  (v->cvp_state == CMD_STATUS_SUCCESS),
8129                                  msecs_to_jiffies(TIMEOUT_MS));
8130         if (!ret) {
8131                 pr_err("%s: wait_event timeout\n", __func__);
8132
8133                 ret = -EINVAL;
8134                 goto done;
8135         }
8136
8137         if (v->async_err > 0) {
8138                 pr_err("%s: DSP returned error[%s]\n",
8139                                 __func__, adsp_err_get_err_str(
8140                                 v->async_err));
8141                 ret = adsp_err_get_lnx_err_code(
8142                                 v->async_err);
8143                 goto done;
8144         }
8145
8146         if (common.is_sound_focus_resp_success) {
8147                 for (i = 0; i < MAX_SECTORS; i++) {
8148                         soundFocusData->start_angle[i] =
8149                                 common.soundFocusResponse.start_angles[i];
8150                         soundFocusData->enable[i] =
8151                                 common.soundFocusResponse.enables[i];
8152                         pr_debug("%s: start_angle[%d] = %d\n",
8153                                   __func__, i, soundFocusData->start_angle[i]);
8154                         pr_debug("%s: enable[%d] = %d\n",
8155                                   __func__, i, soundFocusData->enable[i]);
8156                 }
8157                 soundFocusData->gain_step = common.soundFocusResponse.gain_step;
8158                 pr_debug("%s: gain_step = %d\n", __func__,
8159                           soundFocusData->gain_step);
8160
8161                 common.is_sound_focus_resp_success = false;
8162                 ret = 0;
8163         } else {
8164                 pr_err("%s: Invalid payload received from CVD\n", __func__);
8165
8166                 ret = -EINVAL;
8167         }
8168 done:
8169         pr_debug("%s: Exit, ret=%d\n", __func__, ret);
8170
8171         return ret;
8172 }
8173
8174 int voc_get_sound_focus(struct sound_focus_param *soundFocusData)
8175 {
8176         struct voice_data *v = NULL;
8177         int ret = -EINVAL;
8178         struct voice_session_itr itr;
8179
8180         pr_debug("%s: Enter\n", __func__);
8181
8182         mutex_lock(&common.common_lock);
8183         voice_itr_init(&itr, ALL_SESSION_VSID);
8184         while (voice_itr_get_next_session(&itr, &v)) {
8185                 if (v) {
8186                         mutex_lock(&v->lock);
8187                         if (is_voc_state_active(v->voc_state) &&
8188                                 (v->lch_mode != VOICE_LCH_START) &&
8189                                 !v->disable_topology)
8190                                 ret = voice_send_get_sound_focus_cmd(v,
8191                                                         soundFocusData);
8192                         mutex_unlock(&v->lock);
8193                 } else {
8194                         pr_err("%s: invalid session\n", __func__);
8195
8196                         ret =  -EINVAL;
8197                         break;
8198                 }
8199         }
8200         mutex_unlock(&common.common_lock);
8201         pr_debug("%s: Exit, ret=%d\n", __func__, ret);
8202
8203         return ret;
8204 }
8205
8206 static int is_source_tracking_shared_memomry_allocated(void)
8207 {
8208         bool ret;
8209
8210         pr_debug("%s: Enter\n", __func__);
8211
8212         if (common.source_tracking_sh_mem.sh_mem_block.client != NULL &&
8213             common.source_tracking_sh_mem.sh_mem_block.handle != NULL)
8214                 ret = true;
8215         else
8216                 ret = false;
8217
8218         pr_debug("%s: Exit\n", __func__);
8219
8220         return ret;
8221 }
8222
8223 static int voice_alloc_source_tracking_shared_memory(void)
8224 {
8225         int ret = 0;
8226
8227         pr_debug("%s: Enter\n", __func__);
8228
8229         ret = msm_audio_ion_alloc("source_tracking_sh_mem_block",
8230                 &(common.source_tracking_sh_mem.sh_mem_block.client),
8231                 &(common.source_tracking_sh_mem.sh_mem_block.handle),
8232                 BUFFER_BLOCK_SIZE,
8233                 &(common.source_tracking_sh_mem.sh_mem_block.phys),
8234                 (size_t *)&(common.source_tracking_sh_mem.sh_mem_block.size),
8235                 &(common.source_tracking_sh_mem.sh_mem_block.data));
8236         if (ret < 0) {
8237                 pr_err("%s: audio ION alloc failed for sh_mem block, ret = %d\n",
8238                         __func__, ret);
8239
8240                 ret = -EINVAL;
8241                 goto done;
8242         }
8243         memset((void *)(common.source_tracking_sh_mem.sh_mem_block.data), 0,
8244                    common.source_tracking_sh_mem.sh_mem_block.size);
8245
8246         pr_debug("%s: sh_mem_block: phys:[%pK], data:[0x%pK], size:[%zd]\n",
8247                  __func__,
8248                 &(common.source_tracking_sh_mem.sh_mem_block.phys),
8249                 (void *)(common.source_tracking_sh_mem.sh_mem_block.data),
8250                 (size_t)(common.source_tracking_sh_mem.sh_mem_block.size));
8251
8252         ret = msm_audio_ion_alloc("source_tracking_sh_mem_table",
8253                 &(common.source_tracking_sh_mem.sh_mem_table.client),
8254                 &(common.source_tracking_sh_mem.sh_mem_table.handle),
8255                 sizeof(struct vss_imemory_table_t),
8256                 &(common.source_tracking_sh_mem.sh_mem_table.phys),
8257                 (size_t *)&(common.source_tracking_sh_mem.sh_mem_table.size),
8258                 &(common.source_tracking_sh_mem.sh_mem_table.data));
8259         if (ret < 0) {
8260                 pr_err("%s: audio ION alloc failed for sh_mem table, ret = %d\n",
8261                         __func__, ret);
8262
8263                 ret = msm_audio_ion_free(
8264                         common.source_tracking_sh_mem.sh_mem_block.client,
8265                         common.source_tracking_sh_mem.sh_mem_block.handle);
8266                 common.source_tracking_sh_mem.sh_mem_block.client = NULL;
8267                 common.source_tracking_sh_mem.sh_mem_block.handle = NULL;
8268                 if (ret < 0)
8269                         pr_err("%s: Error:%d freeing memory\n", __func__, ret);
8270
8271                 ret = -EINVAL;
8272                 goto done;
8273         }
8274         memset((void *)(common.source_tracking_sh_mem.sh_mem_table.data), 0,
8275                 common.source_tracking_sh_mem.sh_mem_table.size);
8276
8277         pr_debug("%s sh_mem_table: phys:[%pK], data:[0x%pK], size:[%zd],\n",
8278                  __func__,
8279                 &(common.source_tracking_sh_mem.sh_mem_table.phys),
8280                 (void *)(common.source_tracking_sh_mem.sh_mem_table.data),
8281                 (size_t)(common.source_tracking_sh_mem.sh_mem_table.size));
8282
8283 done:
8284         pr_debug("%s: Exit, ret=%d\n", __func__, ret);
8285
8286         return ret;
8287 }
8288
8289 static int voice_alloc_and_map_source_tracking_shared_memory(
8290                                                 struct voice_data *v)
8291 {
8292         int ret = 0;
8293
8294         pr_debug("%s: Enter\n", __func__);
8295
8296         ret = voice_alloc_source_tracking_shared_memory();
8297         if (ret) {
8298                 pr_err("%s: Failed to allocate shared memory %d\n",
8299                         __func__, ret);
8300
8301                 ret = -EINVAL;
8302                 goto done;
8303         }
8304
8305         ret = voice_map_memory_physical_cmd(v,
8306                         &(common.source_tracking_sh_mem.sh_mem_table),
8307                         common.source_tracking_sh_mem.sh_mem_block.phys,
8308                         common.source_tracking_sh_mem.sh_mem_block.size,
8309                         VOC_SOURCE_TRACKING_MEM_MAP_TOKEN);
8310         if (ret) {
8311                 pr_err("%s: memory mapping failed %d\n",
8312                         __func__, ret);
8313
8314                 ret = -EINVAL;
8315                 goto done;
8316         }
8317
8318 done:
8319         pr_debug("%s: Exit, ret=%d\n", __func__, ret);
8320
8321         return ret;
8322 }
8323
8324 static int voice_unmap_and_free_source_tracking_shared_memory(
8325                                                         struct voice_data *v)
8326 {
8327         int ret = 0;
8328
8329         pr_debug("%s: Enter\n", __func__);
8330
8331         if (common.source_tracking_sh_mem.mem_handle != 0) {
8332                 ret = voice_send_mvm_unmap_memory_physical_cmd(v,
8333                                 common.source_tracking_sh_mem.mem_handle);
8334                 if (ret < 0) {
8335                         pr_err("%s: Memory_unmap failed err %d\n",
8336                                  __func__, ret);
8337
8338                         ret = -EINVAL;
8339                         goto done;
8340                 }
8341         }
8342
8343         if ((common.source_tracking_sh_mem.sh_mem_block.client == NULL) ||
8344             (common.source_tracking_sh_mem.sh_mem_block.handle == NULL))
8345                 goto done;
8346
8347         ret = msm_audio_ion_free(
8348                         common.source_tracking_sh_mem.sh_mem_block.client,
8349                         common.source_tracking_sh_mem.sh_mem_block.handle);
8350         if (ret < 0) {
8351                 pr_err("%s: Error:%d freeing memory\n", __func__, ret);
8352
8353                 ret = -EINVAL;
8354                 goto done;
8355         }
8356
8357 done:
8358         common.source_tracking_sh_mem.mem_handle = 0;
8359         common.source_tracking_sh_mem.sh_mem_block.client = NULL;
8360         common.source_tracking_sh_mem.sh_mem_block.handle = NULL;
8361         pr_debug("%s: Exit, ret=%d\n", __func__, ret);
8362
8363         return ret;
8364 }
8365
8366 static int voice_send_get_source_tracking_cmd(struct voice_data *v,
8367                         struct source_tracking_param *sourceTrackingData)
8368 {
8369         struct cvp_get_source_tracking_param_cmd_t st_cmd;
8370         int ret = 0;
8371         void *apr_cvp;
8372         u16 cvp_handle;
8373         int i;
8374
8375         pr_debug("%s: Enter\n", __func__);
8376
8377         if (!v) {
8378                 pr_err("%s: v is NULL\n", __func__);
8379                 return -EINVAL;
8380         }
8381         apr_cvp = common.apr_q6_cvp;
8382
8383         if (!apr_cvp) {
8384                 pr_err("%s: apr_cvp is NULL.\n", __func__);
8385                 return -EINVAL;
8386         }
8387
8388         cvp_handle = voice_get_cvp_handle(v);
8389
8390         if (!is_source_tracking_shared_memomry_allocated()) {
8391                 ret = voice_alloc_and_map_source_tracking_shared_memory(v);
8392                 if (ret) {
8393                         pr_err("%s: Fail in allocating/mapping shared memory\n",
8394                                 __func__);
8395
8396                         ret = -EINVAL;
8397                         goto done;
8398                 }
8399         }
8400         st_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
8401                                              APR_HDR_LEN(APR_HDR_SIZE),
8402                                              APR_PKT_VER);
8403         st_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
8404                                            sizeof(st_cmd) - APR_HDR_SIZE);
8405         st_cmd.hdr.src_port = voice_get_idx_for_session(v->session_id);
8406         st_cmd.hdr.dest_port = cvp_handle;
8407         st_cmd.hdr.token = 0;
8408         st_cmd.hdr.opcode = VSS_ISOURCETRACK_CMD_GET_ACTIVITY;
8409
8410         st_cmd.cvp_get_source_tracking_param.mem_handle =
8411                                  common.source_tracking_sh_mem.mem_handle;
8412         st_cmd.cvp_get_source_tracking_param.mem_address_lsw =
8413                 lower_32_bits(common.source_tracking_sh_mem.sh_mem_block.phys);
8414         st_cmd.cvp_get_source_tracking_param.mem_address_msw =
8415                 msm_audio_populate_upper_32_bits(common.source_tracking_sh_mem.
8416                                         sh_mem_block.phys);
8417         st_cmd.cvp_get_source_tracking_param.mem_size =
8418                 (uint32_t)common.source_tracking_sh_mem.sh_mem_block.size;
8419         pr_debug("%s: mem_handle=0x%x, mem_address_lsw=0x%x, msw=0x%x, mem_size=%d\n",
8420                  __func__,
8421                  st_cmd.cvp_get_source_tracking_param.mem_handle,
8422                  st_cmd.cvp_get_source_tracking_param.mem_address_lsw,
8423                  st_cmd.cvp_get_source_tracking_param.mem_address_msw,
8424                  (uint32_t)st_cmd.cvp_get_source_tracking_param.mem_size);
8425
8426         v->cvp_state = CMD_STATUS_FAIL;
8427         v->async_err = 0;
8428         ret = apr_send_pkt(apr_cvp,
8429                            (uint32_t *) &st_cmd);
8430         if (ret < 0) {
8431                 pr_err("%s: Error in sending APR command\n", __func__);
8432
8433                 ret = -EINVAL;
8434                 goto done;
8435         }
8436         ret = wait_event_timeout(v->cvp_wait,
8437                                  (v->cvp_state == CMD_STATUS_SUCCESS),
8438                                  msecs_to_jiffies(TIMEOUT_MS));
8439         if (!ret) {
8440                 pr_err("%s: wait_event timeout\n", __func__);
8441
8442                 ret = -EINVAL;
8443                 goto done;
8444         }
8445
8446         if (v->async_err > 0) {
8447                 pr_err("%s: DSP returned error[%s]\n",
8448                                 __func__, adsp_err_get_err_str(
8449                                 v->async_err));
8450                 ret = adsp_err_get_lnx_err_code(
8451                                 v->async_err);
8452                 goto done;
8453         }
8454
8455         if (common.is_source_tracking_resp_success) {
8456                 for (i = 0; i < MAX_SECTORS; i++) {
8457                         sourceTrackingData->vad[i] =
8458                                 common.sourceTrackingResponse.voice_active[i];
8459                         pr_debug("%s: vad[%d] = %d\n",
8460                                   __func__, i, sourceTrackingData->vad[i]);
8461                 }
8462                 sourceTrackingData->doa_speech =
8463                                 common.sourceTrackingResponse.talker_doa;
8464                 pr_debug("%s: doa_speech = %d\n",
8465                           __func__, sourceTrackingData->doa_speech);
8466
8467                 for (i = 0; i < MAX_NOISE_SOURCE_INDICATORS; i++) {
8468                         sourceTrackingData->doa_noise[i] =
8469                          common.sourceTrackingResponse.interferer_doa[i];
8470                         pr_debug("%s: doa_noise[%d] = %d\n",
8471                          __func__, i, sourceTrackingData->doa_noise[i]);
8472                 }
8473                 for (i = 0; i < MAX_POLAR_ACTIVITY_INDICATORS; i++) {
8474                         sourceTrackingData->polar_activity[i] =
8475                          common.sourceTrackingResponse.sound_strength[i];
8476                         pr_debug("%s: polar_activity[%d] = %d\n",
8477                          __func__, i, sourceTrackingData->polar_activity[i]);
8478                 }
8479                 common.is_source_tracking_resp_success = false;
8480                 ret = 0;
8481         } else {
8482                 pr_err("%s: Error response received from CVD\n", __func__);
8483
8484                 ret = -EINVAL;
8485         }
8486 done:
8487         pr_debug("%s: Exit, ret=%d\n", __func__, ret);
8488
8489         return ret;
8490 }
8491
8492 int voc_get_source_tracking(struct source_tracking_param *sourceTrackingData)
8493 {
8494         struct voice_data *v = NULL;
8495         int ret = -EINVAL;
8496         struct voice_session_itr itr;
8497
8498         pr_debug("%s: Enter\n", __func__);
8499
8500         mutex_lock(&common.common_lock);
8501
8502         voice_itr_init(&itr, ALL_SESSION_VSID);
8503         while (voice_itr_get_next_session(&itr, &v)) {
8504                 if (v != NULL) {
8505                         mutex_lock(&v->lock);
8506                         if (is_voc_state_active(v->voc_state) &&
8507                                 (v->lch_mode != VOICE_LCH_START) &&
8508                                 !v->disable_topology)
8509                                 ret = voice_send_get_source_tracking_cmd(v,
8510                                                         sourceTrackingData);
8511                         mutex_unlock(&v->lock);
8512                 } else {
8513                         pr_err("%s: invalid session\n", __func__);
8514
8515                         break;
8516                 }
8517         }
8518
8519         mutex_unlock(&common.common_lock);
8520         pr_debug("%s: Exit, ret=%d\n", __func__, ret);
8521
8522         return ret;
8523 }
8524
8525 static int voice_set_cvp_param(struct voice_data *v,
8526                                struct vss_icommon_mem_mapping_hdr *mem_hdr,
8527                                u32 *param_data, u32 param_size)
8528 {
8529         struct vss_icommon_cmd_set_param *set_param = NULL;
8530         uint32_t pkt_size = sizeof(struct vss_icommon_cmd_set_param);
8531         void *apr_cvp;
8532         int ret = 0;
8533
8534         apr_cvp = common.apr_q6_cvp;
8535         if (!apr_cvp) {
8536                 pr_err("%s: apr_cvp is NULL\n", __func__);
8537                 return -EINVAL;
8538         }
8539
8540         if (param_data != NULL)
8541                 pkt_size += param_size;
8542         set_param = kzalloc(pkt_size, GFP_KERNEL);
8543         if (!set_param)
8544                 return -ENOMEM;
8545
8546         set_param->apr_hdr.hdr_field =
8547                 APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, APR_HDR_LEN(APR_HDR_SIZE),
8548                               APR_PKT_VER);
8549         set_param->apr_hdr.pkt_size =
8550                 APR_PKT_SIZE(APR_HDR_SIZE, pkt_size - APR_HDR_SIZE);
8551         set_param->apr_hdr.src_svc = 0;
8552         set_param->apr_hdr.src_domain = APR_DOMAIN_APPS;
8553         set_param->apr_hdr.src_port = voice_get_idx_for_session(v->session_id);
8554         set_param->apr_hdr.dest_svc = 0;
8555         set_param->apr_hdr.dest_domain = APR_DOMAIN_ADSP;
8556         set_param->apr_hdr.dest_port = voice_get_cvp_handle(v);
8557         set_param->apr_hdr.token = VOC_SET_MEDIA_FORMAT_PARAM_TOKEN;
8558         set_param->apr_hdr.opcode = q6common_is_instance_id_supported() ?
8559                                             VSS_ICOMMON_CMD_SET_PARAM_V3 :
8560                                             VSS_ICOMMON_CMD_SET_PARAM_V2;
8561
8562         set_param->payload_size = param_size;
8563
8564         if (mem_hdr != NULL) {
8565                 set_param->mem_hdr = *mem_hdr;
8566         } else if (param_data != NULL) {
8567                 memcpy(set_param->param_data, param_data, param_size);
8568         } else {
8569                 pr_err("%s: Both memory header and param data are NULL\n",
8570                        __func__);
8571                 ret = -EINVAL;
8572                 goto done;
8573         }
8574
8575         v->cvp_state = CMD_STATUS_FAIL;
8576         v->async_err = 0;
8577         ret = apr_send_pkt(apr_cvp, (u32 *) set_param);
8578         if (ret < 0) {
8579                 pr_err("%s: Failed to send apr packet, error %d\n", __func__,
8580                        ret);
8581                 goto done;
8582         }
8583
8584         ret = wait_event_timeout(v->cvp_wait,
8585                                  v->cvp_state == CMD_STATUS_SUCCESS,
8586                                  msecs_to_jiffies(TIMEOUT_MS));
8587         if (!ret) {
8588                 pr_err("%s: wait_event timeout\n", __func__);
8589                 ret = -ETIMEDOUT;
8590                 goto done;
8591         }
8592
8593         if (v->async_err > 0) {
8594                 pr_err("%s: DSP returned error[%s]\n", __func__,
8595                        adsp_err_get_err_str(v->async_err));
8596                 ret = adsp_err_get_lnx_err_code(v->async_err);
8597                 goto done;
8598         }
8599         ret = 0;
8600
8601 done:
8602         kfree(set_param);
8603         return ret;
8604 }
8605
8606 static int voice_pack_and_set_cvp_param(struct voice_data *v,
8607                                         struct param_hdr_v3 param_hdr,
8608                                         u8 *param_data)
8609 {
8610         u8 *packed_data = NULL;
8611         u32 total_size = 0;
8612         int ret = 0;
8613
8614         total_size = sizeof(union param_hdrs) + param_hdr.param_size;
8615         packed_data = kzalloc(total_size, GFP_KERNEL);
8616         if (!packed_data)
8617                 return -ENOMEM;
8618
8619         ret = q6common_pack_pp_params(packed_data, &param_hdr, param_data,
8620                                     &total_size);
8621         if (ret) {
8622                 pr_err("%s: Failed to pack params, error %d", __func__, ret);
8623                 goto done;
8624         }
8625
8626         ret = voice_set_cvp_param(v, NULL, (u32 *) packed_data, total_size);
8627
8628 done:
8629         kfree(packed_data);
8630         return ret;
8631 }
8632
8633 /*
8634  * Out of band is not supported and there are currently no pre-packed cases,
8635  * so pack and set in the same function. When needed, split up.
8636  */
8637 static int voice_pack_and_set_cvs_ui_property(struct voice_data *v,
8638                                               struct param_hdr_v3 param_hdr,
8639                                               u8 *param_data)
8640 {
8641         struct vss_icommon_cmd_set_ui_property *set_ui_property = NULL;
8642         u32 total_size = 0;
8643         u32 pkt_size = 0;
8644         bool iid_supported = q6common_is_instance_id_supported();
8645         void *apr_cvs;
8646         int ret = 0;
8647
8648         apr_cvs = common.apr_q6_cvs;
8649         if (!apr_cvs) {
8650                 pr_err("%s: apr_cvs is NULL\n", __func__);
8651                 return -EINVAL;
8652         }
8653
8654         total_size = sizeof(struct vss_icommon_cmd_set_ui_property) +
8655                      sizeof(union param_hdrs) + param_hdr.param_size;
8656         set_ui_property = kzalloc(total_size, GFP_KERNEL);
8657         if (!set_ui_property)
8658                 return -ENOMEM;
8659
8660         ret = q6common_pack_pp_params(set_ui_property->param_data, &param_hdr,
8661                                     param_data, &pkt_size);
8662         if (ret) {
8663                 pr_err("%s: Failed to pack params, error %d", __func__, ret);
8664                 goto done;
8665         }
8666
8667         /*
8668          * Pack the APR header after packing the data so we have the actual
8669          * total size of the payload
8670          */
8671         set_ui_property->apr_hdr.hdr_field =
8672                 APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, APR_HDR_LEN(APR_HDR_SIZE),
8673                               APR_PKT_VER);
8674         set_ui_property->apr_hdr.pkt_size =
8675                 APR_PKT_SIZE(APR_HDR_SIZE, pkt_size);
8676         set_ui_property->apr_hdr.src_svc = 0;
8677         set_ui_property->apr_hdr.src_domain = APR_DOMAIN_APPS;
8678         set_ui_property->apr_hdr.src_port =
8679                 voice_get_idx_for_session(v->session_id);
8680         set_ui_property->apr_hdr.dest_svc = 0;
8681         set_ui_property->apr_hdr.dest_domain = APR_DOMAIN_ADSP;
8682         set_ui_property->apr_hdr.dest_port = voice_get_cvs_handle(v);
8683         set_ui_property->apr_hdr.token = 0;
8684
8685         set_ui_property->apr_hdr.opcode =
8686                 iid_supported ? VSS_ICOMMON_CMD_SET_UI_PROPERTY_V2 :
8687                                 VSS_ICOMMON_CMD_SET_UI_PROPERTY;
8688
8689         v->cvs_state = CMD_STATUS_FAIL;
8690         v->async_err = 0;
8691         ret = apr_send_pkt(apr_cvs, (u32 *) set_ui_property);
8692         if (ret < 0) {
8693                 pr_err("%s: Failed to send apr packet, error %d\n", __func__,
8694                        ret);
8695                 goto done;
8696         }
8697
8698         ret = wait_event_timeout(v->cvs_wait,
8699                                  v->cvs_state == CMD_STATUS_SUCCESS,
8700                                  msecs_to_jiffies(TIMEOUT_MS));
8701         if (!ret) {
8702                 pr_err("%s: wait_event timeout\n", __func__);
8703                 ret = -ETIMEDOUT;
8704                 goto done;
8705         }
8706
8707         if (v->async_err > 0) {
8708                 pr_err("%s: DSP returned error[%s]\n", __func__,
8709                        adsp_err_get_err_str(v->async_err));
8710                 ret = adsp_err_get_lnx_err_code(v->async_err);
8711                 goto done;
8712         }
8713         ret = 0;
8714 done:
8715         kfree(set_ui_property);
8716         return ret;
8717 }
8718
8719 int is_voc_initialized(void)
8720 {
8721         return module_initialized;
8722 }
8723
8724 static int __init voice_init(void)
8725 {
8726         int rc = 0, i = 0;
8727
8728         memset(&common, 0, sizeof(struct common_data));
8729
8730         /* set default value */
8731         common.default_mute_val = 0;  /* default is un-mute */
8732         common.default_sample_val = 8000;
8733         common.default_vol_step_val = 0;
8734         common.default_vol_ramp_duration_ms = DEFAULT_VOLUME_RAMP_DURATION;
8735         common.default_mute_ramp_duration_ms = DEFAULT_MUTE_RAMP_DURATION;
8736
8737         /* Initialize EC Ref media format info */
8738         common.ec_ref_ext = false;
8739         common.ec_media_fmt_info.port_id = AFE_PORT_INVALID;
8740         common.ec_media_fmt_info.num_channels = 0;
8741         common.ec_media_fmt_info.bits_per_sample = 16;
8742         common.ec_media_fmt_info.sample_rate = 8000;
8743         memset(&common.ec_media_fmt_info.channel_mapping, 0,
8744                VSS_CHANNEL_MAPPING_SIZE);
8745
8746         /* Initialize AFE Sidetone Enable */
8747         common.sidetone_enable = false;
8748
8749         /* Initialize MVS info. */
8750         common.mvs_info.network_type = VSS_NETWORK_ID_DEFAULT;
8751
8752         /* Initialize is low memory flag */
8753         common.is_destroy_cvd = false;
8754
8755         /* Initialize CVD version */
8756         strlcpy(common.cvd_version, CVD_VERSION_DEFAULT,
8757                 sizeof(common.cvd_version));
8758         /* Initialize Per-Vocoder Calibration flag */
8759         common.is_per_vocoder_cal_enabled = false;
8760
8761         mutex_init(&common.common_lock);
8762
8763         /* Initialize session id with vsid */
8764         init_session_id();
8765
8766         for (i = 0; i < MAX_VOC_SESSIONS; i++) {
8767
8768                 /* initialize dev_rx and dev_tx */
8769                 common.voice[i].dev_rx.dev_mute =  common.default_mute_val;
8770                 common.voice[i].dev_tx.dev_mute =  common.default_mute_val;
8771                 common.voice[i].dev_rx.volume_step_value =
8772                                         common.default_vol_step_val;
8773                 common.voice[i].dev_rx.volume_ramp_duration_ms =
8774                                         common.default_vol_ramp_duration_ms;
8775                 common.voice[i].dev_rx.dev_mute_ramp_duration_ms =
8776                                         common.default_mute_ramp_duration_ms;
8777                 common.voice[i].dev_tx.dev_mute_ramp_duration_ms =
8778                                         common.default_mute_ramp_duration_ms;
8779                 common.voice[i].stream_rx.stream_mute = common.default_mute_val;
8780                 common.voice[i].stream_tx.stream_mute = common.default_mute_val;
8781
8782                 common.voice[i].dev_tx.port_id = 0x100B;
8783                 common.voice[i].dev_rx.port_id = 0x100A;
8784                 common.voice[i].dev_tx.dev_id = 0;
8785                 common.voice[i].dev_rx.dev_id = 0;
8786                 common.voice[i].dev_tx.no_of_channels = 0;
8787                 common.voice[i].dev_rx.no_of_channels = 0;
8788                 common.voice[i].dev_tx.sample_rate = 8000;
8789                 common.voice[i].dev_rx.sample_rate = 8000;
8790                 common.voice[i].dev_tx.bits_per_sample = 16;
8791                 common.voice[i].dev_rx.bits_per_sample = 16;
8792                 memset(&common.voice[i].dev_tx.channel_mapping, 0,
8793                        VSS_CHANNEL_MAPPING_SIZE);
8794                 memset(&common.voice[i].dev_rx.channel_mapping, 0,
8795                        VSS_CHANNEL_MAPPING_SIZE);
8796                 common.voice[i].sidetone_gain = 0x512;
8797                 common.voice[i].dtmf_rx_detect_en = 0;
8798                 common.voice[i].lch_mode = 0;
8799                 common.voice[i].disable_topology = false;
8800
8801                 common.voice[i].voc_state = VOC_INIT;
8802
8803                 init_waitqueue_head(&common.voice[i].mvm_wait);
8804                 init_waitqueue_head(&common.voice[i].cvs_wait);
8805                 init_waitqueue_head(&common.voice[i].cvp_wait);
8806
8807                 mutex_init(&common.voice[i].lock);
8808         }
8809
8810         if (voice_init_cal_data())
8811                 pr_err("%s: Could not init cal data!\n", __func__);
8812
8813         if (rc == 0)
8814                 module_initialized = true;
8815
8816         pr_debug("%s: rc=%d\n", __func__, rc);
8817         return rc;
8818 }
8819
8820 device_initcall(voice_init);
8821
8822 static void __exit voice_exit(void)
8823 {
8824         voice_delete_cal_data();
8825         free_cal_map_table();
8826 }
8827
8828 __exitcall(voice_exit);