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
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
|
/*! \file
\brief Declaration of library functions
Any definitions in this file will be shared among GLUE Layer and internal Driver Stack.
*/
/*******************************************************************************
* C O M P I L E R F L A G S
********************************************************************************
*/
/*******************************************************************************
* M A C R O S
********************************************************************************
*/
/*******************************************************************************
* E X T E R N A L R E F E R E N C E S
********************************************************************************
*/
#include "osal_typedef.h"
#include "osal.h"
/*******************************************************************************
* C O N S T A N T S
********************************************************************************
*/
#define GPIO_ASSERT 70
/*******************************************************************************
* D A T A T Y P E S
********************************************************************************
*/
/*******************************************************************************
* P U B L I C D A T A
********************************************************************************
*/
/*******************************************************************************
* P R I V A T E D A T A
********************************************************************************
*/
/* CRC table for the CRC-16. The poly is 0x8005 (x^16 + x^15 + x^2 + 1) */
static UINT16 const crc16_table[256] = {
0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,
0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40,
0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841,
0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40,
0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41,
0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641,
0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040,
0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240,
0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441,
0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41,
0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840,
0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41,
0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40,
0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640,
0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041,
0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240,
0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441,
0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41,
0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840,
0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41,
0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40,
0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640,
0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041,
0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241,
0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440,
0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40,
0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841,
0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40,
0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41,
0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641,
0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040
};
/*******************************************************************************
* F U N C T I O N D E C L A R A T I O N S
********************************************************************************
*/
/*******************************************************************************
* F U N C T I O N S
********************************************************************************
*/
/*string operations*/
_osal_inline_ UINT32 osal_strlen(const PINT8 str)
{
return strlen(str);
}
_osal_inline_ INT32 osal_strcmp(const PINT8 dst, const PINT8 src)
{
return strcmp(dst, src);
}
_osal_inline_ INT32 osal_strncmp(const PINT8 dst, const PINT8 src, UINT32 len)
{
return strncmp(dst, src, len);
}
_osal_inline_ PINT8 osal_strcpy(PINT8 dst, const PINT8 src)
{
return strcpy(dst, src);
}
_osal_inline_ PINT8 osal_strncpy(PINT8 dst, const PINT8 src, UINT32 len)
{
return strncpy(dst, src, len);
}
_osal_inline_ PINT8 osal_strcat(PINT8 dst, const PINT8 src)
{
return strcat(dst, src);
}
_osal_inline_ PINT8 osal_strncat(PINT8 dst, const PINT8 src, UINT32 len)
{
return strncat(dst, src, len);
}
_osal_inline_ PINT8 osal_strchr(const PINT8 str, UINT8 c)
{
return strchr(str, c);
}
_osal_inline_ PINT8 osal_strsep(PPINT8 str, const PINT8 c)
{
return strsep(str, c);
}
_osal_inline_ VOID osal_bug_on(UINT32 val)
{
BUG_ON(val);
}
_osal_inline_ INT32 osal_strtol(const PINT8 str, UINT32 adecimal, long *res)
{
return kstrtol(str, adecimal, res);
}
_osal_inline_ PINT8 osal_strstr(PINT8 str1, const PINT8 str2)
{
return strstr(str1, str2);
}
_osal_inline_ PINT8 osal_strnstr(PINT8 str1, const PINT8 str2, INT32 n)
{
return strnstr(str1, str2, n);
}
INT32 osal_snprintf(PINT8 buf, UINT32 len, const PINT8 fmt, ...)
{
INT32 iRet = 0;
va_list args;
/*va_start(args, fmt); */
va_start(args, fmt);
/*iRet = snprintf(buf, len, fmt, args); TODO: [FixMe][GeorgeKuo] BUG? */
iRet = vsnprintf(buf, len, fmt, args);
va_end(args);
return iRet;
}
INT32 osal_print(const PINT8 str, ...)
{
va_list args;
INT8 tempString[DBG_LOG_STR_SIZE];
va_start(args, str);
vsnprintf(tempString, DBG_LOG_STR_SIZE, str, args);
va_end(args);
pr_warn("%s", tempString);
return 0;
}
INT32 osal_dbg_print(const PINT8 str, ...)
{
va_list args;
INT8 tempString[DBG_LOG_STR_SIZE];
va_start(args, str);
vsnprintf(tempString, DBG_LOG_STR_SIZE, str, args);
va_end(args);
pr_debug("%s", tempString);
return 0;
}
INT32 osal_dbg_assert(INT32 expr, const PINT8 file, INT32 line)
{
if (!expr) {
pr_warn("%s (%d)\n", file, line);
/*BUG_ON(!expr); */
#ifdef CFG_COMMON_GPIO_DBG_PIN
/* package this part */
gpio_direction_output(GPIO_ASSERT, 0);
pr_warn("toggle GPIO_ASSERT = %d\n", GPIO_ASSERT);
udelay(10);
gpio_set_value(GPIO_ASSERT, 1);
#endif
return 1;
}
return 0;
}
INT32 osal_dbg_assert_aee(const PINT8 module, const PINT8 detail_description)
{
osal_err_print("[WMT-ASSERT]" "[E][Module]:%s, [INFO]%s\n", module, detail_description);
#ifdef CONFIG_MTK_AEE_FEATURE
aee_kernel_warning(module, detail_description);
#endif
return 0;
}
INT32 osal_sprintf(PINT8 str, const PINT8 format, ...)
{
INT32 iRet = 0;
va_list args;
va_start(args, format);
iRet = vsnprintf(str, DBG_LOG_STR_SIZE, format, args);
va_end(args);
return iRet;
}
_osal_inline_ PVOID osal_malloc(UINT32 size)
{
return vmalloc(size);
}
_osal_inline_ VOID osal_free(const PVOID dst)
{
vfree(dst);
}
_osal_inline_ PVOID osal_memset(PVOID buf, INT32 i, UINT32 len)
{
return memset(buf, i, len);
}
_osal_inline_ PVOID osal_memcpy(PVOID dst, const PVOID src, UINT32 len)
{
return memcpy(dst, src, len);
}
_osal_inline_ INT32 osal_memcmp(const PVOID buf1, const PVOID buf2, UINT32 len)
{
return memcmp(buf1, buf2, len);
}
_osal_inline_ UINT16 osal_crc16(const PUINT8 buffer, const UINT32 length)
{
UINT16 crc = 0;
UINT32 i = 0;
PUINT8 temp = buffer;
/* FIXME: Add STP checksum feature */
crc = 0;
for (i = 0; i < length; i++, temp++)
crc = (crc >> 8) ^ crc16_table[(crc ^ (*temp)) & 0xff];
return crc;
}
/*
*OSAL layer Thread Opeartion related APIs
*
*
*/
_osal_inline_ INT32 osal_thread_create(P_OSAL_THREAD pThread)
{
pThread->pThread = kthread_create(pThread->pThreadFunc,
pThread->pThreadData, pThread->threadName);
if (NULL == pThread->pThread)
return -1;
return 0;
}
_osal_inline_ INT32 osal_thread_run(P_OSAL_THREAD pThread)
{
if (pThread->pThread) {
wake_up_process(pThread->pThread);
return 0;
} else {
return -1;
}
}
_osal_inline_ INT32 osal_thread_stop(P_OSAL_THREAD pThread)
{
INT32 iRet;
if ((pThread) && (pThread->pThread)) {
iRet = kthread_stop(pThread->pThread);
/* pThread->pThread = NULL; */
return iRet;
}
return -1;
}
_osal_inline_ INT32 osal_thread_should_stop(P_OSAL_THREAD pThread)
{
if ((pThread) && (pThread->pThread))
return kthread_should_stop();
else
return 1;
}
_osal_inline_ INT32
osal_thread_wait_for_event(P_OSAL_THREAD pThread,
P_OSAL_EVENT pEvent, P_OSAL_EVENT_CHECKER pChecker)
{
/* P_DEV_WMT pDevWmt;*/
if ((pThread) && (pThread->pThread) && (pEvent) && (pChecker)) {
/* pDevWmt = (P_DEV_WMT)(pThread->pThreadData); */
return wait_event_interruptible(pEvent->waitQueue, (/*!RB_EMPTY(&pDevWmt->rActiveOpQ) || */
osal_thread_should_stop
(pThread)
||
(*pChecker) (pThread)));
}
return -1;
}
_osal_inline_ INT32 osal_thread_destroy(P_OSAL_THREAD pThread)
{
if (pThread && (pThread->pThread)) {
kthread_stop(pThread->pThread);
pThread->pThread = NULL;
}
return 0;
}
/*
*OSAL layer Signal Opeartion related APIs
*initialization
*wait for signal
*wait for signal timerout
*raise signal
*destroy a signal
*
*/
_osal_inline_ INT32 osal_signal_init(P_OSAL_SIGNAL pSignal)
{
if (pSignal) {
init_completion(&pSignal->comp);
return 0;
} else {
return -1;
}
}
_osal_inline_ INT32 osal_wait_for_signal(P_OSAL_SIGNAL pSignal)
{
if (pSignal) {
wait_for_completion_interruptible(&pSignal->comp);
return 0;
} else {
return -1;
}
}
_osal_inline_ INT32 osal_wait_for_signal_timeout(P_OSAL_SIGNAL pSignal)
{
/* return wait_for_completion_interruptible_timeout(&pSignal->comp, msecs_to_jiffies(pSignal->timeoutValue)); */
/* [ChangeFeature][George] gps driver may be closed by -ERESTARTSYS.
* Avoid using *interruptible" version in order to complete our jobs, such
* as function off gracefully.
*/
return wait_for_completion_timeout(&pSignal->comp, msecs_to_jiffies(pSignal->timeoutValue));
}
_osal_inline_ INT32 osal_raise_signal(P_OSAL_SIGNAL pSignal)
{
if (pSignal) {
complete(&pSignal->comp);
return 0;
} else
return -1;
}
_osal_inline_ INT32 osal_signal_active_state(P_OSAL_SIGNAL pSignal)
{
if (pSignal)
return pSignal->timeoutValue;
else
return -1;
}
_osal_inline_ INT32 osal_signal_deinit(P_OSAL_SIGNAL pSignal)
{
if (pSignal) {
pSignal->timeoutValue = 0;
return 0;
} else
return -1;
}
/*
*OSAL layer Event Opeartion related APIs
*initialization
*wait for signal
*wait for signal timerout
*raise signal
*destroy a signal
*
*/
INT32 osal_event_init(P_OSAL_EVENT pEvent)
{
if (pEvent) {
init_waitqueue_head(&pEvent->waitQueue);
return 0;
} else
return -1;
}
INT32 osal_wait_for_event(P_OSAL_EVENT pEvent, INT32(*condition) (PVOID), PVOID cond_pa)
{
if (pEvent)
return wait_event_interruptible(pEvent->waitQueue, condition(cond_pa));
else
return -1;
}
INT32 osal_wait_for_event_timeout(P_OSAL_EVENT pEvent, INT32(*condition) (PVOID), PVOID cond_pa)
{
if (pEvent)
return wait_event_interruptible_timeout(pEvent->waitQueue,
condition(cond_pa),
msecs_to_jiffies(pEvent->timeoutValue));
return -1;
}
INT32 osal_trigger_event(P_OSAL_EVENT pEvent)
{
INT32 ret = 0;
if (pEvent) {
wake_up_interruptible(&pEvent->waitQueue);
return ret;
}
return -1;
}
INT32 osal_event_deinit(P_OSAL_EVENT pEvent)
{
return 0;
}
_osal_inline_ INT32 osal_wait_for_event_bit_set(P_OSAL_EVENT pEvent, PUINT32 pState,
UINT32 bitOffset)
{
UINT32 ms = 0;
if (pEvent) {
ms = pEvent->timeoutValue;
if (ms != 0)
return wait_event_interruptible_timeout(pEvent->waitQueue,
test_bit(bitOffset,
((unsigned long *)
pState)),
msecs_to_jiffies(ms));
else
return wait_event_interruptible(pEvent->waitQueue,
test_bit(bitOffset,
((unsigned long *)pState)));
} else
return -1;
}
_osal_inline_ INT32 osal_wait_for_event_bit_clr(P_OSAL_EVENT pEvent, PUINT32 pState,
UINT32 bitOffset)
{
UINT32 ms = 0;
if (pEvent) {
ms = pEvent->timeoutValue;
if (ms != 0)
return wait_event_interruptible_timeout(pEvent->waitQueue,
!test_bit(bitOffset,
((unsigned long *)
pState)),
msecs_to_jiffies(ms));
else
return wait_event_interruptible(pEvent->waitQueue,
!test_bit(bitOffset,
((unsigned long *)pState)));
} else
return -1;
}
/*
*bit test and set/clear operations APIs
*
*
*/
#if OS_BIT_OPS_SUPPORT
#define osal_bit_op_lock(x)
#define osal_bit_op_unlock(x)
#else
_osal_inline_ INT32 osal_bit_op_lock(P_OSAL_UNSLEEPABLE_LOCK pLock)
{
return 0;
}
_osal_inline_ INT32 osal_bit_op_unlock(P_OSAL_UNSLEEPABLE_LOCK pLock)
{
return 0;
}
#endif
_osal_inline_ INT32 osal_clear_bit(UINT32 bitOffset, P_OSAL_BIT_OP_VAR pData)
{
osal_bit_op_lock(&(pData->opLock));
clear_bit(bitOffset, ((unsigned long *)&pData->data));
osal_bit_op_unlock(&(pData->opLock));
return 0;
}
_osal_inline_ INT32 osal_set_bit(UINT32 bitOffset, P_OSAL_BIT_OP_VAR pData)
{
osal_bit_op_lock(&(pData->opLock));
set_bit(bitOffset, ((unsigned long *)&pData->data));
osal_bit_op_unlock(&(pData->opLock));
return 0;
}
_osal_inline_ INT32 osal_test_bit(UINT32 bitOffset, P_OSAL_BIT_OP_VAR pData)
{
UINT32 iRet = 0;
osal_bit_op_lock(&(pData->opLock));
iRet = test_bit(bitOffset, ((unsigned long *)&pData->data));
osal_bit_op_unlock(&(pData->opLock));
return iRet;
}
_osal_inline_ INT32 osal_test_and_clear_bit(UINT32 bitOffset, P_OSAL_BIT_OP_VAR pData)
{
UINT32 iRet = 0;
osal_bit_op_lock(&(pData->opLock));
iRet = test_and_clear_bit(bitOffset, ((unsigned long *)&pData->data));
osal_bit_op_unlock(&(pData->opLock));
return iRet;
}
_osal_inline_ INT32 osal_test_and_set_bit(UINT32 bitOffset, P_OSAL_BIT_OP_VAR pData)
{
UINT32 iRet = 0;
osal_bit_op_lock(&(pData->opLock));
iRet = test_and_set_bit(bitOffset, ((unsigned long *)&pData->data));
osal_bit_op_unlock(&(pData->opLock));
return iRet;
}
/*
*tiemr operations APIs
*create
*stop
* modify
*create
*delete
*
*/
INT32 osal_timer_create(P_OSAL_TIMER pTimer)
{
struct timer_list *timer = &pTimer->timer;
init_timer(timer);
timer->function = pTimer->timeoutHandler;
timer->data = (unsigned long)pTimer->timeroutHandlerData;
return 0;
}
INT32 osal_timer_start(P_OSAL_TIMER pTimer, UINT32 ms)
{
struct timer_list *timer = &pTimer->timer;
timer->expires = jiffies + (ms / (1000 / HZ));
add_timer(timer);
return 0;
}
INT32 osal_timer_stop(P_OSAL_TIMER pTimer)
{
struct timer_list *timer = &pTimer->timer;
del_timer(timer);
return 0;
}
INT32 osal_timer_stop_sync(P_OSAL_TIMER pTimer)
{
struct timer_list *timer = &pTimer->timer;
del_timer_sync(timer);
return 0;
}
INT32 osal_timer_modify(P_OSAL_TIMER pTimer, UINT32 ms)
{
mod_timer(&pTimer->timer, jiffies + (ms) / (1000 / HZ));
return 0;
}
INT32 _osal_fifo_init(OSAL_FIFO *pFifo, PUINT8 buf, UINT32 size)
{
struct kfifo *fifo = NULL;
INT32 ret = -1;
if (!pFifo || pFifo->pFifoBody) {
pr_err("pFifo must be !NULL, pFifo->pFifoBody must be NULL\n");
if (!pFifo)
pr_err("pFifo is NULL\n");
else if (!pFifo->pFifoBody)
pr_err("pFifo->pFifoBody is NULL\n");
return -1;
}
fifo = kzalloc(sizeof(struct kfifo), GFP_ATOMIC);
if (!buf) {
/*fifo's buffer is not ready, we allocate automatically */
ret = kfifo_alloc(fifo, size, /*GFP_KERNEL */ GFP_ATOMIC);
} else {
if (is_power_of_2(size)) {
kfifo_init(fifo, buf, size);
ret = 0;
} else {
kfifo_free(fifo);
fifo = NULL;
ret = -1;
}
}
pFifo->pFifoBody = fifo;
return (ret < 0) ? (-1) : (0);
}
INT32 _osal_fifo_deinit(OSAL_FIFO *pFifo)
{
struct kfifo *fifo = NULL;
if (!pFifo || !pFifo->pFifoBody) {
pr_warn("%s:pFifo = NULL or pFifo->pFifoBody = NULL, error\n", __func__);
return -1;
}
fifo = (struct kfifo *)pFifo->pFifoBody;
if (fifo)
kfifo_free(fifo);
return 0;
}
INT32 _osal_fifo_size(OSAL_FIFO *pFifo)
{
struct kfifo *fifo = NULL;
INT32 ret = 0;
if (!pFifo || !pFifo->pFifoBody) {
pr_warn("%s:pFifo = NULL or pFifo->pFifoBody = NULL, error\n", __func__);
return -1;
}
fifo = (struct kfifo *)pFifo->pFifoBody;
if (fifo)
ret = kfifo_size(fifo);
return ret;
}
/*returns unused bytes in fifo*/
INT32 _osal_fifo_avail_size(OSAL_FIFO *pFifo)
{
struct kfifo *fifo = NULL;
INT32 ret = 0;
if (!pFifo || !pFifo->pFifoBody) {
pr_warn("%s:pFifo = NULL or pFifo->pFifoBody = NULL, error\n", __func__);
return -1;
}
fifo = (struct kfifo *)pFifo->pFifoBody;
if (fifo)
ret = kfifo_avail(fifo);
return ret;
}
/*returns used bytes in fifo*/
INT32 _osal_fifo_len(OSAL_FIFO *pFifo)
{
struct kfifo *fifo = NULL;
INT32 ret = 0;
if (!pFifo || !pFifo->pFifoBody) {
pr_warn("%s:pFifo = NULL or pFifo->pFifoBody = NULL, error\n", __func__);
return -1;
}
fifo = (struct kfifo *)pFifo->pFifoBody;
if (fifo)
ret = kfifo_len(fifo);
return ret;
}
INT32 _osal_fifo_is_empty(OSAL_FIFO *pFifo)
{
struct kfifo *fifo = NULL;
INT32 ret = 0;
if (!pFifo || !pFifo->pFifoBody) {
pr_warn("%s:pFifo = NULL or pFifo->pFifoBody = NULL, error\n", __func__);
return -1;
}
fifo = (struct kfifo *)pFifo->pFifoBody;
if (fifo)
ret = kfifo_is_empty(fifo);
return ret;
}
INT32 _osal_fifo_is_full(OSAL_FIFO *pFifo)
{
struct kfifo *fifo = NULL;
INT32 ret = 0;
if (!pFifo || !pFifo->pFifoBody) {
pr_warn("%s:pFifo = NULL or pFifo->pFifoBody = NULL, error\n", __func__);
return -1;
}
fifo = (struct kfifo *)pFifo->pFifoBody;
if (fifo)
ret = kfifo_is_full(fifo);
return ret;
}
INT32 _osal_fifo_data_in(OSAL_FIFO *pFifo, const PVOID buf, UINT32 len)
{
struct kfifo *fifo = NULL;
INT32 ret = 0;
if (!pFifo || !pFifo->pFifoBody) {
pr_warn("%s:pFifo = NULL or pFifo->pFifoBody = NULL, error\n", __func__);
return -1;
}
fifo = (struct kfifo *)pFifo->pFifoBody;
if (fifo && buf && (len <= _osal_fifo_avail_size(pFifo)))
ret = kfifo_in(fifo, buf, len);
else {
pr_warn("%s: kfifo_in, error, len = %d, _osal_fifo_avail_size = %d, buf=%p\n",
__func__, len, _osal_fifo_avail_size(pFifo), buf);
ret = 0;
}
return ret;
}
INT32 _osal_fifo_data_out(OSAL_FIFO *pFifo, PVOID buf, UINT32 len)
{
struct kfifo *fifo = NULL;
INT32 ret = 0;
if (!pFifo || !pFifo->pFifoBody) {
pr_warn("%s:pFifo = NULL or pFifo->pFifoBody = NULL, error\n", __func__);
return -1;
}
fifo = (struct kfifo *)pFifo->pFifoBody;
if (fifo && buf && (len <= _osal_fifo_len(pFifo)))
ret = kfifo_out(fifo, buf, len);
else {
pr_warn("%s: kfifo_out, error, len = %d, osal_fifo_len = %d, buf=%p\n",
__func__, len, _osal_fifo_len(pFifo), buf);
ret = 0;
}
return ret;
}
INT32 _osal_fifo_reset(OSAL_FIFO *pFifo)
{
struct kfifo *fifo = NULL;
if (!pFifo || !pFifo->pFifoBody) {
pr_warn("%s:pFifo = NULL or pFifo->pFifoBody = NULL, error\n", __func__);
return -1;
}
fifo = (struct kfifo *)pFifo->pFifoBody;
if (fifo)
kfifo_reset(fifo);
return 0;
}
INT32 osal_fifo_init(P_OSAL_FIFO pFifo, PUINT8 buffer, UINT32 size)
{
if (!pFifo) {
pr_warn("%s:pFifo = NULL, error\n", __func__);
return -1;
}
pFifo->FifoInit = _osal_fifo_init;
pFifo->FifoDeInit = _osal_fifo_deinit;
pFifo->FifoSz = _osal_fifo_size;
pFifo->FifoAvailSz = _osal_fifo_avail_size;
pFifo->FifoLen = _osal_fifo_len;
pFifo->FifoIsEmpty = _osal_fifo_is_empty;
pFifo->FifoIsFull = _osal_fifo_is_full;
pFifo->FifoDataIn = _osal_fifo_data_in;
pFifo->FifoDataOut = _osal_fifo_data_out;
pFifo->FifoReset = _osal_fifo_reset;
if (NULL != pFifo->pFifoBody) {
pr_warn
("%s:Because pFifo room is avialable, we clear the room and allocate them again.\n",
__func__);
pFifo->FifoDeInit(pFifo->pFifoBody);
pFifo->pFifoBody = NULL;
}
pFifo->FifoInit(pFifo, buffer, size);
return 0;
}
VOID osal_fifo_deinit(P_OSAL_FIFO pFifo)
{
if (pFifo) {
pFifo->FifoDeInit(pFifo);
kfree(pFifo->pFifoBody);
} else
pr_warn("%s:pFifo = NULL, error\n", __func__);
}
INT32 osal_fifo_reset(P_OSAL_FIFO pFifo)
{
INT32 ret = -1;
if (pFifo)
ret = pFifo->FifoReset(pFifo);
else
pr_warn("%s:pFifo = NULL, error\n", __func__);
return ret;
}
UINT32 osal_fifo_in(P_OSAL_FIFO pFifo, PUINT8 buffer, UINT32 size)
{
UINT32 ret = 0;
if (pFifo)
ret = pFifo->FifoDataIn(pFifo, buffer, size);
else
pr_warn("%s:pFifo = NULL, error\n", __func__);
return ret;
}
UINT32 osal_fifo_out(P_OSAL_FIFO pFifo, PUINT8 buffer, UINT32 size)
{
UINT32 ret = 0;
if (pFifo)
ret = pFifo->FifoDataOut(pFifo, buffer, size);
else
pr_warn("%s:pFifo = NULL, error\n", __func__);
return ret;
}
UINT32 osal_fifo_len(P_OSAL_FIFO pFifo)
{
UINT32 ret = 0;
if (pFifo)
ret = pFifo->FifoLen(pFifo);
else
pr_warn("%s:pFifo = NULL, error\n", __func__);
return ret;
}
UINT32 osal_fifo_sz(P_OSAL_FIFO pFifo)
{
UINT32 ret = 0;
if (pFifo)
ret = pFifo->FifoSz(pFifo);
else
pr_warn("%s:pFifo = NULL, error\n", __func__);
return ret;
}
UINT32 osal_fifo_avail(P_OSAL_FIFO pFifo)
{
UINT32 ret = 0;
if (pFifo)
ret = pFifo->FifoAvailSz(pFifo);
else
pr_warn("%s:pFifo = NULL, error\n", __func__);
return ret;
}
UINT32 osal_fifo_is_empty(P_OSAL_FIFO pFifo)
{
UINT32 ret = 0;
if (pFifo)
ret = pFifo->FifoIsEmpty(pFifo);
else
pr_warn("%s:pFifo = NULL, error\n", __func__);
return ret;
}
UINT32 osal_fifo_is_full(P_OSAL_FIFO pFifo)
{
UINT32 ret = 0;
if (pFifo)
ret = pFifo->FifoIsFull(pFifo);
else
pr_warn("%s:pFifo = NULL, error\n", __func__);
return ret;
}
INT32 osal_wake_lock_init(P_OSAL_WAKE_LOCK pLock)
{
INT32 ret = -1;
if (pLock) {
wake_lock_init(&pLock->wake_lock, WAKE_LOCK_SUSPEND, pLock->name);
wake_unlock(&pLock->wake_lock);
ret = 0;
}
return ret;
}
INT32 osal_wake_lock(P_OSAL_WAKE_LOCK pLock)
{
INT32 ret = -1;
if (pLock) {
wake_lock(&pLock->wake_lock);
ret = 0;
}
return ret;
}
INT32 osal_wake_unlock(P_OSAL_WAKE_LOCK pLock)
{
INT32 ret = -1;
if (pLock) {
wake_unlock(&pLock->wake_lock);
ret = 0;
}
return ret;
}
INT32 osal_wake_lock_count(P_OSAL_WAKE_LOCK pLock)
{
INT32 count = -1;
if (pLock)
count = wake_lock_active(&pLock->wake_lock);
return count;
}
/*
*sleepable lock operations APIs
*init
*lock
*unlock
*destroy
*
*/
#if !defined(CONFIG_PROVE_LOCKING)
INT32 osal_unsleepable_lock_init(P_OSAL_UNSLEEPABLE_LOCK pUSL)
{
spin_lock_init(&(pUSL->lock));
return 0;
}
#endif
INT32 osal_lock_unsleepable_lock(P_OSAL_UNSLEEPABLE_LOCK pUSL)
{
spin_lock_irqsave(&(pUSL->lock), pUSL->flag);
return 0;
}
INT32 osal_unlock_unsleepable_lock(P_OSAL_UNSLEEPABLE_LOCK pUSL)
{
spin_unlock_irqrestore(&(pUSL->lock), pUSL->flag);
return 0;
}
INT32 osal_unsleepable_lock_deinit(P_OSAL_UNSLEEPABLE_LOCK pUSL)
{
return 0;
}
/*
*unsleepable operations APIs
*init
*lock
*unlock
*destroy
*
*/
#if !defined(CONFIG_PROVE_LOCKING)
INT32 osal_sleepable_lock_init(P_OSAL_SLEEPABLE_LOCK pSL)
{
mutex_init(&pSL->lock);
return 0;
}
#endif
INT32 osal_lock_sleepable_lock(P_OSAL_SLEEPABLE_LOCK pSL)
{
return mutex_lock_killable(&pSL->lock);
}
INT32 osal_unlock_sleepable_lock(P_OSAL_SLEEPABLE_LOCK pSL)
{
mutex_unlock(&pSL->lock);
return 0;
}
INT32 osal_sleepable_lock_deinit(P_OSAL_SLEEPABLE_LOCK pSL)
{
mutex_destroy(&pSL->lock);
return 0;
}
INT32 osal_sleep_ms(UINT32 ms)
{
msleep(ms);
return 0;
}
INT32 osal_gettimeofday(PINT32 sec, PINT32 usec)
{
INT32 ret = 0;
struct timeval now;
do_gettimeofday(&now);
if (sec != NULL)
*sec = now.tv_sec;
else
ret = -1;
if (usec != NULL)
*usec = now.tv_usec;
else
ret = -1;
return ret;
}
INT32 osal_printtimeofday(const PUINT8 prefix)
{
INT32 ret;
INT32 sec;
INT32 usec;
ret = osal_gettimeofday(&sec, &usec);
ret += osal_dbg_print("%s>sec=%d, usec=%d\n", prefix, sec, usec);
return ret;
}
VOID osal_buffer_dump(const PUINT8 buf, const PUINT8 title, const UINT32 len, const UINT32 limit)
{
INT32 k;
UINT32 dump_len;
pr_warn("start of dump>[%s] len=%d, limit=%d,", title, len, limit);
dump_len = ((0 != limit) && (len > limit)) ? limit : len;
for (k = 0; k < dump_len; k++) {
if ((k != 0) && (k % 16 == 0))
pr_warn("\n");
pr_warn("0x%02x ", buf[k]);
}
pr_warn("<end of dump\n");
}
UINT32 osal_op_get_id(P_OSAL_OP pOp)
{
return (pOp) ? pOp->op.opId : 0xFFFFFFFF;
}
MTK_WCN_BOOL osal_op_is_wait_for_signal(P_OSAL_OP pOp)
{
return (pOp && pOp->signal.timeoutValue) ? MTK_WCN_BOOL_TRUE : MTK_WCN_BOOL_FALSE;
}
VOID osal_op_raise_signal(P_OSAL_OP pOp, INT32 result)
{
if (pOp) {
pOp->result = result;
osal_raise_signal(&pOp->signal);
}
}
|