1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
|
/* Copyright (c) 2014-2019, The Linux Foundation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/miscdevice.h>
#include <linux/uaccess.h>
#include <linux/mutex.h>
#include <dsp/audio_cal_utils.h>
static int unmap_memory(struct cal_type_data *cal_type,
struct cal_block_data *cal_block);
size_t get_cal_info_size(int32_t cal_type)
{
size_t size = 0;
size_t size1 = 0, size2 = 0;
switch (cal_type) {
case CVP_VOC_RX_TOPOLOGY_CAL_TYPE:
size = sizeof(struct audio_cal_info_voc_top);
break;
case CVP_VOC_TX_TOPOLOGY_CAL_TYPE:
size = sizeof(struct audio_cal_info_voc_top);
break;
case CVP_VOCPROC_STATIC_CAL_TYPE:
size = sizeof(struct audio_cal_info_vocproc);
break;
case CVP_VOCPROC_DYNAMIC_CAL_TYPE:
size = sizeof(struct audio_cal_info_vocvol);
break;
case CVS_VOCSTRM_STATIC_CAL_TYPE:
size = 0;
break;
case CVP_VOCDEV_CFG_CAL_TYPE:
size = sizeof(struct audio_cal_info_vocdev_cfg);
break;
case CVP_VOCPROC_STATIC_COL_CAL_TYPE:
size = sizeof(struct audio_cal_info_voc_col);
break;
case CVP_VOCPROC_DYNAMIC_COL_CAL_TYPE:
size = sizeof(struct audio_cal_info_voc_col);
break;
case CVS_VOCSTRM_STATIC_COL_CAL_TYPE:
size = sizeof(struct audio_cal_info_voc_col);
break;
case ADM_TOPOLOGY_CAL_TYPE:
case ADM_LSM_TOPOLOGY_CAL_TYPE:
size = sizeof(struct audio_cal_info_adm_top);
break;
case ADM_CUST_TOPOLOGY_CAL_TYPE:
case CORE_CUSTOM_TOPOLOGIES_CAL_TYPE:
size = 0;
break;
case ADM_AUDPROC_CAL_TYPE:
case ADM_LSM_AUDPROC_CAL_TYPE:
case ADM_LSM_AUDPROC_PERSISTENT_CAL_TYPE:
size = sizeof(struct audio_cal_info_audproc);
break;
case ADM_AUDVOL_CAL_TYPE:
case ADM_RTAC_AUDVOL_CAL_TYPE:
size = sizeof(struct audio_cal_info_audvol);
break;
case ASM_TOPOLOGY_CAL_TYPE:
size = sizeof(struct audio_cal_info_asm_top);
break;
case ASM_CUST_TOPOLOGY_CAL_TYPE:
size = 0;
break;
case ASM_AUDSTRM_CAL_TYPE:
size = sizeof(struct audio_cal_info_audstrm);
break;
case AFE_TOPOLOGY_CAL_TYPE:
case AFE_LSM_TOPOLOGY_CAL_TYPE:
size = sizeof(struct audio_cal_info_afe_top);
break;
case AFE_CUST_TOPOLOGY_CAL_TYPE:
size = 0;
break;
case AFE_COMMON_RX_CAL_TYPE:
size = sizeof(struct audio_cal_info_afe);
break;
case AFE_COMMON_TX_CAL_TYPE:
case AFE_LSM_TX_CAL_TYPE:
size = sizeof(struct audio_cal_info_afe);
break;
case AFE_FB_SPKR_PROT_CAL_TYPE:
size = sizeof(struct audio_cal_info_spk_prot_cfg);
break;
case AFE_FB_SPKR_PROT_TH_VI_CAL_TYPE:
/*
* Since get and set parameter structures are different in size
* use the maximum size of get and set parameter structure
*/
size1 = max(sizeof(struct audio_cal_info_sp_th_vi_ftm_cfg),
sizeof(struct audio_cal_info_sp_th_vi_param));
size2 = max(sizeof(struct audio_cal_info_sp_th_vi_v_vali_cfg),
sizeof(struct audio_cal_info_sp_th_vi_v_vali_param));
size = max(size1, size2);
break;
case AFE_FB_SPKR_PROT_EX_VI_CAL_TYPE:
/*
* Since get and set parameter structures are different in size
* use the maximum size of get and set parameter structure
*/
size = max(sizeof(struct audio_cal_info_sp_ex_vi_ftm_cfg),
sizeof(struct audio_cal_info_sp_ex_vi_param));
break;
case AFE_ANC_CAL_TYPE:
size = 0;
break;
case AFE_AANC_CAL_TYPE:
size = sizeof(struct audio_cal_info_aanc);
break;
case AFE_HW_DELAY_CAL_TYPE:
size = sizeof(struct audio_cal_info_hw_delay);
break;
case AFE_SIDETONE_CAL_TYPE:
size = sizeof(struct audio_cal_info_sidetone);
break;
case AFE_SIDETONE_IIR_CAL_TYPE:
size = sizeof(struct audio_cal_info_sidetone_iir);
break;
case LSM_CUST_TOPOLOGY_CAL_TYPE:
size = 0;
break;
case LSM_TOPOLOGY_CAL_TYPE:
size = sizeof(struct audio_cal_info_lsm_top);
break;
case ULP_LSM_TOPOLOGY_ID_CAL_TYPE:
size = sizeof(struct audio_cal_info_lsm_top);
break;
case LSM_CAL_TYPE:
size = sizeof(struct audio_cal_info_lsm);
break;
case ADM_RTAC_INFO_CAL_TYPE:
size = 0;
break;
case VOICE_RTAC_INFO_CAL_TYPE:
size = 0;
break;
case ADM_RTAC_APR_CAL_TYPE:
size = 0;
break;
case ASM_RTAC_APR_CAL_TYPE:
size = 0;
break;
case VOICE_RTAC_APR_CAL_TYPE:
size = 0;
break;
case MAD_CAL_TYPE:
size = 0;
break;
case ULP_AFE_CAL_TYPE:
size = sizeof(struct audio_cal_info_afe);
break;
case ULP_LSM_CAL_TYPE:
size = sizeof(struct audio_cal_info_lsm);
break;
case AUDIO_CORE_METAINFO_CAL_TYPE:
size = sizeof(struct audio_cal_info_metainfo);
break;
case SRS_TRUMEDIA_CAL_TYPE:
size = 0;
break;
default:
pr_err("%s:Invalid cal type %d!",
__func__, cal_type);
}
return size;
}
size_t get_user_cal_type_size(int32_t cal_type)
{
size_t size = 0;
switch (cal_type) {
case CVP_VOC_RX_TOPOLOGY_CAL_TYPE:
size = sizeof(struct audio_cal_type_voc_top);
break;
case CVP_VOC_TX_TOPOLOGY_CAL_TYPE:
size = sizeof(struct audio_cal_type_voc_top);
break;
case CVP_VOCPROC_STATIC_CAL_TYPE:
size = sizeof(struct audio_cal_type_vocproc);
break;
case CVP_VOCPROC_DYNAMIC_CAL_TYPE:
size = sizeof(struct audio_cal_type_vocvol);
break;
case CVS_VOCSTRM_STATIC_CAL_TYPE:
size = sizeof(struct audio_cal_type_basic);
break;
case CVP_VOCDEV_CFG_CAL_TYPE:
size = sizeof(struct audio_cal_type_vocdev_cfg);
break;
case CVP_VOCPROC_STATIC_COL_CAL_TYPE:
case CVP_VOCPROC_DYNAMIC_COL_CAL_TYPE:
case CVS_VOCSTRM_STATIC_COL_CAL_TYPE:
size = sizeof(struct audio_cal_type_voc_col);
break;
case ADM_TOPOLOGY_CAL_TYPE:
case ADM_LSM_TOPOLOGY_CAL_TYPE:
size = sizeof(struct audio_cal_type_adm_top);
break;
case ADM_CUST_TOPOLOGY_CAL_TYPE:
case CORE_CUSTOM_TOPOLOGIES_CAL_TYPE:
size = sizeof(struct audio_cal_type_basic);
break;
case ADM_AUDPROC_CAL_TYPE:
case ADM_LSM_AUDPROC_CAL_TYPE:
case ADM_LSM_AUDPROC_PERSISTENT_CAL_TYPE:
size = sizeof(struct audio_cal_type_audproc);
break;
case ADM_AUDVOL_CAL_TYPE:
case ADM_RTAC_AUDVOL_CAL_TYPE:
size = sizeof(struct audio_cal_type_audvol);
break;
case ASM_TOPOLOGY_CAL_TYPE:
size = sizeof(struct audio_cal_type_asm_top);
break;
case ASM_CUST_TOPOLOGY_CAL_TYPE:
size = sizeof(struct audio_cal_type_basic);
break;
case ASM_AUDSTRM_CAL_TYPE:
size = sizeof(struct audio_cal_type_audstrm);
break;
case AFE_TOPOLOGY_CAL_TYPE:
case AFE_LSM_TOPOLOGY_CAL_TYPE:
size = sizeof(struct audio_cal_type_afe_top);
break;
case AFE_CUST_TOPOLOGY_CAL_TYPE:
size = sizeof(struct audio_cal_type_basic);
break;
case AFE_COMMON_RX_CAL_TYPE:
size = sizeof(struct audio_cal_type_afe);
break;
case AFE_COMMON_TX_CAL_TYPE:
case AFE_LSM_TX_CAL_TYPE:
size = sizeof(struct audio_cal_type_afe);
break;
case AFE_FB_SPKR_PROT_CAL_TYPE:
size = sizeof(struct audio_cal_type_fb_spk_prot_cfg);
break;
case AFE_FB_SPKR_PROT_TH_VI_CAL_TYPE:
/*
* Since get and set parameter structures are different in size
* use the maximum size of get and set parameter structure
*/
size = max(sizeof(struct audio_cal_type_sp_th_vi_ftm_cfg),
sizeof(struct audio_cal_type_sp_th_vi_param));
break;
case AFE_FB_SPKR_PROT_EX_VI_CAL_TYPE:
/*
* Since get and set parameter structures are different in size
* use the maximum size of get and set parameter structure
*/
size = max(sizeof(struct audio_cal_type_sp_ex_vi_ftm_cfg),
sizeof(struct audio_cal_type_sp_ex_vi_param));
break;
case AFE_ANC_CAL_TYPE:
size = 0;
break;
case AFE_AANC_CAL_TYPE:
size = sizeof(struct audio_cal_type_aanc);
break;
case AFE_HW_DELAY_CAL_TYPE:
size = sizeof(struct audio_cal_type_hw_delay);
break;
case AFE_SIDETONE_CAL_TYPE:
size = sizeof(struct audio_cal_type_sidetone);
break;
case AFE_SIDETONE_IIR_CAL_TYPE:
size = sizeof(struct audio_cal_type_sidetone_iir);
break;
case LSM_CUST_TOPOLOGY_CAL_TYPE:
size = sizeof(struct audio_cal_type_basic);
break;
case LSM_TOPOLOGY_CAL_TYPE:
size = sizeof(struct audio_cal_type_lsm_top);
break;
case ULP_LSM_TOPOLOGY_ID_CAL_TYPE:
size = sizeof(struct audio_cal_type_lsm_top);
break;
case LSM_CAL_TYPE:
size = sizeof(struct audio_cal_type_lsm);
break;
case ADM_RTAC_INFO_CAL_TYPE:
size = 0;
break;
case VOICE_RTAC_INFO_CAL_TYPE:
size = 0;
break;
case ADM_RTAC_APR_CAL_TYPE:
size = 0;
break;
case ASM_RTAC_APR_CAL_TYPE:
size = 0;
break;
case VOICE_RTAC_APR_CAL_TYPE:
size = 0;
break;
case MAD_CAL_TYPE:
size = 0;
break;
case ULP_AFE_CAL_TYPE:
size = sizeof(struct audio_cal_type_afe);
break;
case ULP_LSM_CAL_TYPE:
size = sizeof(struct audio_cal_type_lsm);
break;
case AUDIO_CORE_METAINFO_CAL_TYPE:
size = sizeof(struct audio_cal_type_metainfo);
break;
case SRS_TRUMEDIA_CAL_TYPE:
size = 0;
break;
default:
pr_err("%s:Invalid cal type %d!",
__func__, cal_type);
}
return size;
}
int32_t cal_utils_get_cal_type_version(void *cal_type_data)
{
struct audio_cal_type_basic *data = NULL;
data = (struct audio_cal_type_basic *)cal_type_data;
return data->cal_hdr.version;
}
static struct cal_type_data *create_cal_type_data(
struct cal_type_info *info)
{
struct cal_type_data *cal_type = NULL;
if ((info->reg.cal_type < 0) ||
(info->reg.cal_type >= MAX_CAL_TYPES)) {
pr_err("%s: cal type %d is Invalid!\n",
__func__, info->reg.cal_type);
goto done;
}
if (info->cal_util_callbacks.match_block == NULL) {
pr_err("%s: cal type %d no method to match blocks!\n",
__func__, info->reg.cal_type);
goto done;
}
cal_type = kmalloc(sizeof(*cal_type), GFP_KERNEL);
if (cal_type == NULL)
goto done;
INIT_LIST_HEAD(&cal_type->cal_blocks);
mutex_init(&cal_type->lock);
memcpy(&cal_type->info, info,
sizeof(cal_type->info));
done:
return cal_type;
}
/**
* cal_utils_create_cal_types
*
* @num_cal_types: number of types
* @cal_type: pointer to the cal types pointer
* @info: pointer to info
*
* Returns 0 on success, EINVAL otherwise
*/
int cal_utils_create_cal_types(int num_cal_types,
struct cal_type_data **cal_type,
struct cal_type_info *info)
{
int ret = 0;
int i;
pr_debug("%s\n", __func__);
if (cal_type == NULL) {
pr_err("%s: cal_type is NULL!\n", __func__);
ret = -EINVAL;
goto done;
} else if (info == NULL) {
pr_err("%s: info is NULL!\n", __func__);
ret = -EINVAL;
goto done;
} else if ((num_cal_types <= 0) ||
(num_cal_types > MAX_CAL_TYPES)) {
pr_err("%s: num_cal_types of %d is Invalid!\n",
__func__, num_cal_types);
ret = -EINVAL;
goto done;
}
for (i = 0; i < num_cal_types; i++) {
if ((info[i].reg.cal_type < 0) ||
(info[i].reg.cal_type >= MAX_CAL_TYPES)) {
pr_err("%s: cal type %d at index %d is Invalid!\n",
__func__, info[i].reg.cal_type, i);
ret = -EINVAL;
goto done;
}
cal_type[i] = create_cal_type_data(&info[i]);
if (cal_type[i] == NULL) {
pr_err("%s: Could not allocate cal_type of index %d!\n",
__func__, i);
ret = -EINVAL;
goto done;
}
ret = audio_cal_register(1, &info[i].reg);
if (ret < 0) {
pr_err("%s: audio_cal_register failed, ret = %d!\n",
__func__, ret);
ret = -EINVAL;
goto done;
}
pr_debug("%s: cal type %d at index %d!\n",
__func__, info[i].reg.cal_type, i);
}
done:
return ret;
}
EXPORT_SYMBOL(cal_utils_create_cal_types);
static void delete_cal_block(struct cal_block_data *cal_block)
{
pr_debug("%s\n", __func__);
if (cal_block == NULL)
goto done;
list_del(&cal_block->list);
kfree(cal_block->client_info);
cal_block->client_info = NULL;
kfree(cal_block->cal_info);
cal_block->cal_info = NULL;
if (cal_block->map_data.dma_buf != NULL) {
msm_audio_ion_free(cal_block->map_data.dma_buf);
cal_block->map_data.dma_buf = NULL;
}
kfree(cal_block);
done:
return;
}
static void destroy_all_cal_blocks(struct cal_type_data *cal_type)
{
int ret = 0;
struct list_head *ptr, *next;
struct cal_block_data *cal_block;
list_for_each_safe(ptr, next,
&cal_type->cal_blocks) {
cal_block = list_entry(ptr,
struct cal_block_data, list);
ret = unmap_memory(cal_type, cal_block);
if (ret < 0) {
pr_err("%s: unmap_memory failed, cal type %d, ret = %d!\n",
__func__,
cal_type->info.reg.cal_type,
ret);
}
delete_cal_block(cal_block);
cal_block = NULL;
}
}
static void destroy_cal_type_data(struct cal_type_data *cal_type)
{
if (cal_type == NULL)
goto done;
destroy_all_cal_blocks(cal_type);
list_del(&cal_type->cal_blocks);
kfree(cal_type);
done:
return;
}
/**
* cal_utils_destroy_cal_types -
* Destroys cal types and deregister from cal info
*
* @num_cal_types: number of cal types
* @cal_type: cal type pointer with cal info
*
*/
void cal_utils_destroy_cal_types(int num_cal_types,
struct cal_type_data **cal_type)
{
int i;
pr_debug("%s\n", __func__);
if (cal_type == NULL) {
pr_err("%s: cal_type is NULL!\n", __func__);
goto done;
} else if ((num_cal_types <= 0) ||
(num_cal_types > MAX_CAL_TYPES)) {
pr_err("%s: num_cal_types of %d is Invalid!\n",
__func__, num_cal_types);
goto done;
}
for (i = 0; i < num_cal_types; i++) {
audio_cal_deregister(1, &cal_type[i]->info.reg);
destroy_cal_type_data(cal_type[i]);
cal_type[i] = NULL;
}
done:
return;
}
EXPORT_SYMBOL(cal_utils_destroy_cal_types);
/**
* cal_utils_get_only_cal_block
*
* @cal_type: pointer to the cal type
*
* Returns cal_block structure
*/
struct cal_block_data *cal_utils_get_only_cal_block(
struct cal_type_data *cal_type)
{
struct list_head *ptr, *next;
struct cal_block_data *cal_block = NULL;
if (cal_type == NULL)
goto done;
list_for_each_safe(ptr, next,
&cal_type->cal_blocks) {
cal_block = list_entry(ptr,
struct cal_block_data, list);
break;
}
done:
return cal_block;
}
EXPORT_SYMBOL(cal_utils_get_only_cal_block);
/**
* cal_utils_get_only_cal_block
*
* @cal_block: pointer to cal block struct
* @user_data: pointer to user data
*
* Returns true on match
*/
bool cal_utils_match_buf_num(struct cal_block_data *cal_block,
void *user_data)
{
bool ret = false;
struct audio_cal_type_basic *data = user_data;
if (cal_block->buffer_number == data->cal_hdr.buffer_number)
ret = true;
return ret;
}
EXPORT_SYMBOL(cal_utils_match_buf_num);
static struct cal_block_data *get_matching_cal_block(
struct cal_type_data *cal_type,
void *data)
{
struct list_head *ptr, *next;
struct cal_block_data *cal_block = NULL;
list_for_each_safe(ptr, next,
&cal_type->cal_blocks) {
cal_block = list_entry(ptr,
struct cal_block_data, list);
if (cal_type->info.cal_util_callbacks.
match_block(cal_block, data))
return cal_block;
}
return NULL;
}
static int cal_block_ion_alloc(struct cal_block_data *cal_block)
{
int ret = 0;
if (cal_block == NULL) {
pr_err("%s: cal_block is NULL!\n", __func__);
ret = -EINVAL;
goto done;
}
ret = msm_audio_ion_import(&cal_block->map_data.dma_buf,
cal_block->map_data.ion_map_handle,
NULL, 0,
&cal_block->cal_data.paddr,
&cal_block->map_data.map_size,
&cal_block->cal_data.kvaddr);
if (ret) {
pr_err("%s: audio ION import failed, rc = %d\n",
__func__, ret);
ret = -ENOMEM;
goto done;
}
done:
return ret;
}
static struct cal_block_data *create_cal_block(struct cal_type_data *cal_type,
struct audio_cal_type_basic *basic_cal,
size_t client_info_size, void *client_info)
{
struct cal_block_data *cal_block = NULL;
if (cal_type == NULL) {
pr_err("%s: cal_type is NULL!\n", __func__);
goto done;
} else if (basic_cal == NULL) {
pr_err("%s: basic_cal is NULL!\n", __func__);
goto done;
}
cal_block = kzalloc(sizeof(*cal_block),
GFP_KERNEL);
if (cal_block == NULL)
goto done;
INIT_LIST_HEAD(&cal_block->list);
cal_block->map_data.ion_map_handle = basic_cal->cal_data.mem_handle;
if (basic_cal->cal_data.mem_handle > 0) {
if (cal_block_ion_alloc(cal_block)) {
pr_err("%s: cal_block_ion_alloc failed!\n",
__func__);
goto err;
}
}
if (client_info_size > 0) {
cal_block->client_info_size = client_info_size;
cal_block->client_info = kmalloc(client_info_size, GFP_KERNEL);
if (cal_block->client_info == NULL) {
pr_err("%s: could not allocats client_info!\n",
__func__);
goto err;
}
if (client_info != NULL)
memcpy(cal_block->client_info, client_info,
client_info_size);
}
cal_block->cal_info = kzalloc(
get_cal_info_size(cal_type->info.reg.cal_type),
GFP_KERNEL);
if (cal_block->cal_info == NULL) {
pr_err("%s: could not allocats cal_info!\n",
__func__);
goto err;
}
cal_block->buffer_number = basic_cal->cal_hdr.buffer_number;
list_add_tail(&cal_block->list, &cal_type->cal_blocks);
pr_debug("%s: created block for cal type %d, buf num %d, map handle %d, map size %zd paddr 0x%pK!\n",
__func__, cal_type->info.reg.cal_type,
cal_block->buffer_number,
cal_block->map_data.ion_map_handle,
cal_block->map_data.map_size,
&cal_block->cal_data.paddr);
done:
return cal_block;
err:
kfree(cal_block->cal_info);
cal_block->cal_info = NULL;
kfree(cal_block->client_info);
cal_block->client_info = NULL;
kfree(cal_block);
cal_block = NULL;
return cal_block;
}
void cal_utils_clear_cal_block_q6maps(int num_cal_types,
struct cal_type_data **cal_type)
{
int i = 0;
struct list_head *ptr, *next;
struct cal_block_data *cal_block;
pr_debug("%s\n", __func__);
if (cal_type == NULL) {
pr_err("%s: cal_type is NULL!\n", __func__);
goto done;
} else if ((num_cal_types <= 0) ||
(num_cal_types > MAX_CAL_TYPES)) {
pr_err("%s: num_cal_types of %d is Invalid!\n",
__func__, num_cal_types);
goto done;
}
for (; i < num_cal_types; i++) {
if (cal_type[i] == NULL)
continue;
mutex_lock(&cal_type[i]->lock);
list_for_each_safe(ptr, next,
&cal_type[i]->cal_blocks) {
cal_block = list_entry(ptr,
struct cal_block_data, list);
cal_block->map_data.q6map_handle = 0;
}
mutex_unlock(&cal_type[i]->lock);
}
done:
return;
}
static int realloc_memory(struct cal_block_data *cal_block)
{
int ret = 0;
msm_audio_ion_free(cal_block->map_data.dma_buf);
cal_block->map_data.dma_buf = NULL;
cal_block->cal_data.size = 0;
ret = cal_block_ion_alloc(cal_block);
if (ret < 0)
pr_err("%s: realloc_memory failed!\n",
__func__);
return ret;
}
static int map_memory(struct cal_type_data *cal_type,
struct cal_block_data *cal_block)
{
int ret = 0;
if (cal_type->info.cal_util_callbacks.map_cal != NULL) {
if ((cal_block->map_data.ion_map_handle < 0) ||
(cal_block->map_data.map_size <= 0) ||
(cal_block->map_data.q6map_handle != 0)) {
goto done;
}
pr_debug("%s: cal type %d call map\n",
__func__, cal_type->info.reg.cal_type);
ret = cal_type->info.cal_util_callbacks.
map_cal(cal_type->info.reg.cal_type, cal_block);
if (ret < 0) {
pr_err("%s: map_cal failed, cal type %d, ret = %d!\n",
__func__, cal_type->info.reg.cal_type,
ret);
goto done;
}
}
done:
return ret;
}
static int unmap_memory(struct cal_type_data *cal_type,
struct cal_block_data *cal_block)
{
int ret = 0;
if (cal_type->info.cal_util_callbacks.unmap_cal != NULL) {
if ((cal_block->map_data.ion_map_handle < 0) ||
(cal_block->map_data.map_size <= 0) ||
(cal_block->map_data.q6map_handle == 0)) {
goto done;
}
pr_debug("%s: cal type %d call unmap\n",
__func__, cal_type->info.reg.cal_type);
ret = cal_type->info.cal_util_callbacks.
unmap_cal(cal_type->info.reg.cal_type, cal_block);
if (ret < 0) {
pr_err("%s: unmap_cal failed, cal type %d, ret = %d!\n",
__func__, cal_type->info.reg.cal_type,
ret);
goto done;
}
}
done:
return ret;
}
/**
* cal_utils_alloc_cal
*
* @data_size: size of data to allocate
* @data: data pointer
* @cal_type: pointer to the cal type
* @client_info_size: client info size
* @client_info: pointer to client info
*
* Returns 0 on success, appropriate error code otherwise
*/
int cal_utils_alloc_cal(size_t data_size, void *data,
struct cal_type_data *cal_type,
size_t client_info_size, void *client_info)
{
int ret = 0;
struct cal_block_data *cal_block;
struct audio_cal_type_alloc *alloc_data = data;
pr_debug("%s\n", __func__);
if (cal_type == NULL) {
pr_err("%s: cal_type is NULL!\n",
__func__);
ret = -EINVAL;
goto done;
}
if (data_size < sizeof(struct audio_cal_type_alloc)) {
pr_err("%s: data_size of %zd does not equal alloc struct size of %zd!\n",
__func__, data_size,
sizeof(struct audio_cal_type_alloc));
ret = -EINVAL;
goto done;
}
if ((client_info_size > 0) && (client_info == NULL)) {
pr_err("%s: User info pointer is NULL but size is %zd!\n",
__func__, client_info_size);
ret = -EINVAL;
goto done;
}
if (alloc_data->cal_data.mem_handle < 0) {
pr_err("%s: mem_handle %d invalid!\n",
__func__, alloc_data->cal_data.mem_handle);
ret = -EINVAL;
goto done;
}
mutex_lock(&cal_type->lock);
cal_block = get_matching_cal_block(cal_type,
data);
if (cal_block != NULL) {
ret = unmap_memory(cal_type, cal_block);
if (ret < 0)
goto err;
ret = realloc_memory(cal_block);
if (ret < 0)
goto err;
} else {
cal_block = create_cal_block(cal_type,
(struct audio_cal_type_basic *)alloc_data,
client_info_size, client_info);
if (cal_block == NULL) {
pr_err("%s: create_cal_block failed for %d!\n",
__func__, alloc_data->cal_data.mem_handle);
ret = -EINVAL;
goto err;
}
}
ret = map_memory(cal_type, cal_block);
if (ret < 0)
goto err;
err:
mutex_unlock(&cal_type->lock);
done:
return ret;
}
EXPORT_SYMBOL(cal_utils_alloc_cal);
/**
* cal_utils_dealloc_cal
*
* @data_size: size of data to allocate
* @data: data pointer
* @cal_type: pointer to the cal type
*
* Returns 0 on success, appropriate error code otherwise
*/
int cal_utils_dealloc_cal(size_t data_size, void *data,
struct cal_type_data *cal_type)
{
int ret = 0;
struct cal_block_data *cal_block;
struct audio_cal_type_dealloc *dealloc_data = data;
pr_debug("%s\n", __func__);
if (cal_type == NULL) {
pr_err("%s: cal_type is NULL!\n",
__func__);
ret = -EINVAL;
goto done;
}
if (data_size < sizeof(struct audio_cal_type_dealloc)) {
pr_err("%s: data_size of %zd does not equal struct size of %zd!\n",
__func__, data_size,
sizeof(struct audio_cal_type_dealloc));
ret = -EINVAL;
goto done;
}
if ((dealloc_data->cal_data.mem_handle == -1) &&
(dealloc_data->cal_hdr.buffer_number == ALL_CAL_BLOCKS)) {
destroy_all_cal_blocks(cal_type);
goto done;
}
if (dealloc_data->cal_data.mem_handle < 0) {
pr_err("%s: mem_handle %d invalid!\n",
__func__, dealloc_data->cal_data.mem_handle);
ret = -EINVAL;
goto done;
}
mutex_lock(&cal_type->lock);
cal_block = get_matching_cal_block(
cal_type,
data);
if (cal_block == NULL) {
pr_err("%s: allocation does not exist for %d!\n",
__func__, dealloc_data->cal_data.mem_handle);
ret = -EINVAL;
goto err;
}
ret = unmap_memory(cal_type, cal_block);
if (ret < 0)
goto err;
delete_cal_block(cal_block);
err:
mutex_unlock(&cal_type->lock);
done:
return ret;
}
EXPORT_SYMBOL(cal_utils_dealloc_cal);
/**
* cal_utils_set_cal
*
* @data_size: size of data to allocate
* @data: data pointer
* @cal_type: pointer to the cal type
* @client_info_size: client info size
* @client_info: pointer to client info
*
* Returns 0 on success, appropriate error code otherwise
*/
int cal_utils_set_cal(size_t data_size, void *data,
struct cal_type_data *cal_type,
size_t client_info_size, void *client_info)
{
int ret = 0;
struct cal_block_data *cal_block;
struct audio_cal_type_basic *basic_data = data;
pr_debug("%s\n", __func__);
if (cal_type == NULL) {
pr_err("%s: cal_type is NULL!\n",
__func__);
ret = -EINVAL;
goto done;
}
if ((client_info_size > 0) && (client_info == NULL)) {
pr_err("%s: User info pointer is NULL but size is %zd!\n",
__func__, client_info_size);
ret = -EINVAL;
goto done;
}
if ((data_size > get_user_cal_type_size(
cal_type->info.reg.cal_type)) || (data_size < 0)) {
pr_err("%s: cal_type %d, data_size of %zd is invalid, expecting %zd!\n",
__func__, cal_type->info.reg.cal_type, data_size,
get_user_cal_type_size(cal_type->info.reg.cal_type));
ret = -EINVAL;
goto done;
}
mutex_lock(&cal_type->lock);
cal_block = get_matching_cal_block(
cal_type,
data);
if (cal_block == NULL) {
if (basic_data->cal_data.mem_handle > 0) {
pr_err("%s: allocation does not exist for %d!\n",
__func__, basic_data->cal_data.mem_handle);
ret = -EINVAL;
goto err;
} else {
cal_block = create_cal_block(
cal_type,
basic_data,
client_info_size, client_info);
if (cal_block == NULL) {
pr_err("%s: create_cal_block failed for cal type %d!\n",
__func__,
cal_type->info.reg.cal_type);
ret = -EINVAL;
goto err;
}
}
}
ret = map_memory(cal_type, cal_block);
if (ret < 0)
goto err;
cal_block->cal_data.size = basic_data->cal_data.cal_size;
if (client_info_size > 0) {
memcpy(cal_block->client_info,
client_info,
client_info_size);
}
memcpy(cal_block->cal_info,
((uint8_t *)data + sizeof(struct audio_cal_type_basic)),
data_size - sizeof(struct audio_cal_type_basic));
/* reset buffer stale flag */
cal_block->cal_stale = false;
err:
mutex_unlock(&cal_type->lock);
done:
return ret;
}
EXPORT_SYMBOL(cal_utils_set_cal);
/**
* cal_utils_mark_cal_used
*
* @cal_block: pointer to cal block
*/
void cal_utils_mark_cal_used(struct cal_block_data *cal_block)
{
if (cal_block)
cal_block->cal_stale = true;
}
EXPORT_SYMBOL(cal_utils_mark_cal_used);
/**
* cal_utils_is_cal_stale
*
* @cal_block: pointer to cal block
*
* Returns true if cal block is stale, false otherwise
*/
bool cal_utils_is_cal_stale(struct cal_block_data *cal_block)
{
if ((cal_block) && (cal_block->cal_stale))
return true;
return false;
}
EXPORT_SYMBOL(cal_utils_is_cal_stale);
|