SAMA5D4 Xplained Ultra Board BSP
guowenxue
2019-08-19 2e7235d10c6dbff81960282e1a1e2e798f9b8db8
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
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
/******************************************************************************
 *
 * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2 of the GNU General Public License 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.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
 *
 *
 ******************************************************************************/
#define _RTW_EFUSE_C_
 
#include <drv_types.h>
#include <hal_data.h>
 
#include "../hal/efuse/efuse_mask.h"
 
/*------------------------Define local variable------------------------------*/
u8    fakeEfuseBank = {0};
u32    fakeEfuseUsedBytes = {0};
u8    fakeEfuseContent[EFUSE_MAX_HW_SIZE] = {0};
u8    fakeEfuseInitMap[EFUSE_MAX_MAP_LEN] = {0};
u8    fakeEfuseModifiedMap[EFUSE_MAX_MAP_LEN] = {0};
 
u32    BTEfuseUsedBytes = {0};
u8    BTEfuseContent[EFUSE_MAX_BT_BANK][EFUSE_MAX_HW_SIZE];
u8    BTEfuseInitMap[EFUSE_BT_MAX_MAP_LEN] = {0};
u8    BTEfuseModifiedMap[EFUSE_BT_MAX_MAP_LEN] = {0};
 
u32    fakeBTEfuseUsedBytes = {0};
u8    fakeBTEfuseContent[EFUSE_MAX_BT_BANK][EFUSE_MAX_HW_SIZE];
u8    fakeBTEfuseInitMap[EFUSE_BT_MAX_MAP_LEN] = {0};
u8    fakeBTEfuseModifiedMap[EFUSE_BT_MAX_MAP_LEN] = {0};
 
u8    maskfileBuffer[64];
/*------------------------Define local variable------------------------------*/
BOOLEAN rtw_file_efuse_IsMasked(PADAPTER pAdapter, u16 Offset)
{
    int r = Offset / 16;
    int c = (Offset % 16) / 2;
    int result = 0;
 
    if (pAdapter->registrypriv.boffefusemask)
        return FALSE;
 
    if (c < 4) /* Upper double word */
        result = (maskfileBuffer[r] & (0x10 << c));
    else
        result = (maskfileBuffer[r] & (0x01 << (c - 4)));
 
    return (result > 0) ? 0 : 1;
}
 
BOOLEAN efuse_IsMasked(PADAPTER pAdapter, u16 Offset)
{
    PHAL_DATA_TYPE pHalData = GET_HAL_DATA(pAdapter);
 
    if (pAdapter->registrypriv.boffefusemask)
        return FALSE;
 
#if DEV_BUS_TYPE == RT_USB_INTERFACE
#if defined(CONFIG_RTL8188E)
    if (IS_HARDWARE_TYPE_8188E(pAdapter))
        return (IS_MASKED(8188E, _MUSB, Offset)) ? TRUE : FALSE;
#endif
#if defined(CONFIG_RTL8812A)
    if (IS_HARDWARE_TYPE_8812(pAdapter))
        return (IS_MASKED(8812A, _MUSB, Offset)) ? TRUE : FALSE;
#endif
#if defined(CONFIG_RTL8821A)
#if 0
    if (IS_HARDWARE_TYPE_8811AU(pAdapter))
        return (IS_MASKED(8811A, _MUSB, Offset)) ? TRUE : FALSE;
#endif
    if (IS_HARDWARE_TYPE_8821(pAdapter))
        return (IS_MASKED(8821A, _MUSB, Offset)) ? TRUE : FALSE;
#endif
#if defined(CONFIG_RTL8192E)
    if (IS_HARDWARE_TYPE_8192E(pAdapter))
        return (IS_MASKED(8192E, _MUSB, Offset)) ? TRUE : FALSE;
#endif
#if defined(CONFIG_RTL8723B)
    if (IS_HARDWARE_TYPE_8723B(pAdapter))
        return (IS_MASKED(8723B, _MUSB, Offset)) ? TRUE : FALSE;
#endif
#if defined(CONFIG_RTL8703B)
    if (IS_HARDWARE_TYPE_8703B(pAdapter))
        return (IS_MASKED(8703B, _MUSB, Offset)) ? TRUE : FALSE;
#endif
#if defined(CONFIG_RTL8814A)
    if (IS_HARDWARE_TYPE_8814A(pAdapter))
        return (IS_MASKED(8814A, _MUSB, Offset)) ? TRUE : FALSE;
#endif
#if defined(CONFIG_RTL8188F)
    if (IS_HARDWARE_TYPE_8188F(pAdapter))
        return (IS_MASKED(8188F, _MUSB, Offset)) ? TRUE : FALSE;
#endif
#if defined(CONFIG_RTL8822B)
    if (IS_HARDWARE_TYPE_8822B(pAdapter))
        return (IS_MASKED(8822B, _MUSB, Offset)) ? TRUE : FALSE;
#endif
#if defined(CONFIG_RTL8723D)
    if (IS_HARDWARE_TYPE_8723D(pAdapter))
        return (IS_MASKED(8723D, _MUSB, Offset)) ? TRUE : FALSE;
#endif
 
    /*#if defined(CONFIG_RTL8821C)
        if (IS_HARDWARE_TYPE_8821C(pAdapter))
            return (IS_MASKED(8821C,_MUSB,Offset)) ? TRUE : FALSE;
    #endif*/
 
#elif DEV_BUS_TYPE == RT_PCI_INTERFACE
#if defined(CONFIG_RTL8188E)
    if (IS_HARDWARE_TYPE_8188E(pAdapter))
        return (IS_MASKED(8188E, _MPCIE, Offset)) ? TRUE : FALSE;
#endif
#if defined(CONFIG_RTL8192E)
    if (IS_HARDWARE_TYPE_8192E(pAdapter))
        return (IS_MASKED(8192E, _MPCIE, Offset)) ? TRUE : FALSE;
#endif
#if defined(CONFIG_RTL8812A)
    if (IS_HARDWARE_TYPE_8812(pAdapter))
        return (IS_MASKED(8812A, _MPCIE, Offset)) ? TRUE : FALSE;
#endif
#if defined(CONFIG_RTL8821A)
    if (IS_HARDWARE_TYPE_8821(pAdapter))
        return (IS_MASKED(8821A, _MPCIE, Offset)) ? TRUE : FALSE;
#endif
#if defined(CONFIG_RTL8723B)
    if (IS_HARDWARE_TYPE_8723B(pAdapter))
        return (IS_MASKED(8723B, _MPCIE, Offset)) ? TRUE : FALSE;
#endif
#if defined(CONFIG_RTL8814A)
    if (IS_HARDWARE_TYPE_8814A(pAdapter))
        return (IS_MASKED(8814A, _MPCIE, Offset)) ? TRUE : FALSE;
#endif
#if defined(CONFIG_RTL8822B)
    if (IS_HARDWARE_TYPE_8822B(pAdapter))
        return (IS_MASKED(8822B, _MPCIE, Offset)) ? TRUE : FALSE;
#endif
 
#elif DEV_BUS_TYPE == RT_SDIO_INTERFACE
#ifdef CONFIG_RTL8188E_SDIO
    if (IS_HARDWARE_TYPE_8188E(pAdapter))
        return (IS_MASKED(8188E, _MSDIO, Offset)) ? TRUE : FALSE;
#endif
#ifdef CONFIG_RTL8188F_SDIO
    if (IS_HARDWARE_TYPE_8188F(pAdapter))
        return (IS_MASKED(8188F, _MSDIO, Offset)) ? TRUE : FALSE;
#endif
#if defined(CONFIG_RTL8821C)
    if (IS_HARDWARE_TYPE_8821C(pAdapter))
        return (IS_MASKED(8821C, _MSDIO, Offset)) ? TRUE : FALSE;
#endif
#if defined(CONFIG_RTL8822B)
    if (IS_HARDWARE_TYPE_8822B(pAdapter))
        return (IS_MASKED(8822B, _MSDIO, Offset)) ? TRUE : FALSE;
#endif
#endif
 
    return FALSE;
}
 
void rtw_efuse_mask_array(PADAPTER pAdapter, u8 *pArray)
{
    PHAL_DATA_TYPE pHalData = GET_HAL_DATA(pAdapter);
 
#if DEV_BUS_TYPE == RT_USB_INTERFACE
#if defined(CONFIG_RTL8188E)
    if (IS_HARDWARE_TYPE_8188E(pAdapter))
        GET_MASK_ARRAY(8188E, _MUSB, pArray);
#endif
#if defined(CONFIG_RTL8812A)
    if (IS_HARDWARE_TYPE_8812(pAdapter))
        GET_MASK_ARRAY(8812A, _MUSB, pArray);
#endif
#if defined(CONFIG_RTL8821A)
    if (IS_HARDWARE_TYPE_8821(pAdapter))
        GET_MASK_ARRAY(8821A, _MUSB, pArray);
#endif
#if defined(CONFIG_RTL8192E)
    if (IS_HARDWARE_TYPE_8192E(pAdapter))
        GET_MASK_ARRAY(8192E, _MUSB, pArray);
#endif
#if defined(CONFIG_RTL8723B)
    if (IS_HARDWARE_TYPE_8723B(pAdapter))
        GET_MASK_ARRAY(8723B, _MUSB, pArray);
#endif
#if defined(CONFIG_RTL8703B)
    if (IS_HARDWARE_TYPE_8703B(pAdapter))
        GET_MASK_ARRAY(8703B, _MUSB, pArray);
#endif
#if defined(CONFIG_RTL8188F)
    if (IS_HARDWARE_TYPE_8188F(pAdapter))
        GET_MASK_ARRAY(8188F, _MUSB, pArray);
#endif
#if defined(CONFIG_RTL8814A)
    if (IS_HARDWARE_TYPE_8814A(pAdapter))
        GET_MASK_ARRAY(8814A, _MUSB, pArray);
#endif
#if defined(CONFIG_RTL8822B)
    if (IS_HARDWARE_TYPE_8822B(pAdapter))
        GET_MASK_ARRAY(8822B, _MUSB, pArray);
#endif
    /*#if defined(CONFIG_RTL8821C)
        if (IS_HARDWARE_TYPE_8821C(pAdapter))
            GET_MASK_ARRAY(8821C,_MUSB,pArray);
    #endif*/
#elif DEV_BUS_TYPE == RT_PCI_INTERFACE
#if defined(CONFIG_RTL8188E)
    if (IS_HARDWARE_TYPE_8188E(pAdapter))
        GET_MASK_ARRAY(8188E, _MPCIE, pArray);
#endif
#if defined(CONFIG_RTL8192E)
    if (IS_HARDWARE_TYPE_8192E(pAdapter))
        GET_MASK_ARRAY(8192E, _MPCIE, pArray);
#endif
#if defined(CONFIG_RTL8812A)
    if (IS_HARDWARE_TYPE_8812(pAdapter))
        GET_MASK_ARRAY(8812A, _MPCIE, pArray);
#endif
#if defined(CONFIG_RTL8821A)
    if (IS_HARDWARE_TYPE_8821(pAdapter))
        GET_MASK_ARRAY(8821A, _MPCIE, pArray);
#endif
#if defined(CONFIG_RTL8723B)
    if (IS_HARDWARE_TYPE_8723B(pAdapter))
        GET_MASK_ARRAY(8723B, _MPCIE, pArray);
#endif
#if defined(CONFIG_RTL8814A)
    if (IS_HARDWARE_TYPE_8814A(pAdapter))
        GET_MASK_ARRAY(8814A, _MPCIE, pArray);
#endif
#if defined(CONFIG_RTL8822B)
    if (IS_HARDWARE_TYPE_8822B(pAdapter))
        GET_MASK_ARRAY(8822B, _MPCIE, pArray);
#endif
#elif DEV_BUS_TYPE == RT_SDIO_INTERFACE
#if defined(CONFIG_RTL8188E)
    if (IS_HARDWARE_TYPE_8188E(pAdapter))
        GET_MASK_ARRAY(8188E, _MSDIO, pArray);
#endif
#if defined(CONFIG_RTL8188F)
    if (IS_HARDWARE_TYPE_8188F(pAdapter))
        GET_MASK_ARRAY(8188F, _MSDIO, pArray);
#endif
#if defined(CONFIG_RTL8821C)
    if (IS_HARDWARE_TYPE_8821C(pAdapter))
        GET_MASK_ARRAY(8821C , _MSDIO, pArray);
#endif
#if defined(CONFIG_RTL8822B)
    if (IS_HARDWARE_TYPE_8822B(pAdapter))
        GET_MASK_ARRAY(8822B , _MSDIO, pArray);
#endif
#endif /*#elif DEV_BUS_TYPE == RT_SDIO_INTERFACE*/
}
 
u16 rtw_get_efuse_mask_arraylen(PADAPTER pAdapter)
{
    HAL_DATA_TYPE    *pHalData = GET_HAL_DATA(pAdapter);
 
#if DEV_BUS_TYPE == RT_USB_INTERFACE
#if defined(CONFIG_RTL8188E)
    if (IS_HARDWARE_TYPE_8188E(pAdapter))
        return GET_MASK_ARRAY_LEN(8188E, _MUSB);
#endif
#if defined(CONFIG_RTL8812A)
    if (IS_HARDWARE_TYPE_8812(pAdapter))
        return GET_MASK_ARRAY_LEN(8812A, _MUSB);
#endif
#if defined(CONFIG_RTL8821A)
    if (IS_HARDWARE_TYPE_8821(pAdapter))
        return GET_MASK_ARRAY_LEN(8821A, _MUSB);
#endif
#if defined(CONFIG_RTL8192E)
    if (IS_HARDWARE_TYPE_8192E(pAdapter))
        return GET_MASK_ARRAY_LEN(8192E, _MUSB);
#endif
#if defined(CONFIG_RTL8723B)
    if (IS_HARDWARE_TYPE_8723B(pAdapter))
        return GET_MASK_ARRAY_LEN(8723B, _MUSB);
#endif
#if defined(CONFIG_RTL8703B)
    if (IS_HARDWARE_TYPE_8703B(pAdapter))
        return GET_MASK_ARRAY_LEN(8703B, _MUSB);
#endif
#if defined(CONFIG_RTL8188F)
    if (IS_HARDWARE_TYPE_8188F(pAdapter))
        return GET_MASK_ARRAY_LEN(8188F, _MUSB);
#endif
#if defined(CONFIG_RTL8814A)
    if (IS_HARDWARE_TYPE_8814A(pAdapter))
        return GET_MASK_ARRAY_LEN(8814A, _MUSB);
#endif
#if defined(CONFIG_RTL8822B)
    if (IS_HARDWARE_TYPE_8822B(pAdapter))
        return GET_MASK_ARRAY_LEN(8822B, _MUSB);
#endif
    /*#if defined(CONFIG_RTL8821C)
        if (IS_HARDWARE_TYPE_8821C(pAdapter))
            return GET_MASK_ARRAY_LEN(8821C,_MUSB);
    #endif*/
#elif DEV_BUS_TYPE == RT_PCI_INTERFACE
#if defined(CONFIG_RTL8188E)
    if (IS_HARDWARE_TYPE_8188E(pAdapter))
        return GET_MASK_ARRAY_LEN(8188E, _MPCIE);
#endif
#if defined(CONFIG_RTL8192E)
    if (IS_HARDWARE_TYPE_8192E(pAdapter))
        return GET_MASK_ARRAY_LEN(8192E, _MPCIE);
#endif
#if defined(CONFIG_RTL8812A)
    if (IS_HARDWARE_TYPE_8812(pAdapter))
        return GET_MASK_ARRAY_LEN(8812A, _MPCIE);
#endif
#if defined(CONFIG_RTL8821A)
    if (IS_HARDWARE_TYPE_8821(pAdapter))
        return GET_MASK_ARRAY_LEN(8821A, _MPCIE);
#endif
#if defined(CONFIG_RTL8723B)
    if (IS_HARDWARE_TYPE_8723B(pAdapter))
        return GET_MASK_ARRAY_LEN(8723B, _MPCIE);
#endif
#if defined(CONFIG_RTL8814A)
    if (IS_HARDWARE_TYPE_8814A(pAdapter))
        return GET_MASK_ARRAY_LEN(8814A, _MPCIE);
#endif
#if defined(CONFIG_RTL8822B)
    if (IS_HARDWARE_TYPE_8822B(pAdapter))
        return GET_MASK_ARRAY_LEN(8822B, _MPCIE);
#endif
#elif DEV_BUS_TYPE == RT_SDIO_INTERFACE
#if defined(CONFIG_RTL8188E)
    if (IS_HARDWARE_TYPE_8188E(pAdapter))
        return GET_MASK_ARRAY_LEN(8188E, _MSDIO);
#endif
#if defined(CONFIG_RTL8188F)
    if (IS_HARDWARE_TYPE_8188F(pAdapter))
        return GET_MASK_ARRAY_LEN(8188F, _MSDIO);
#endif
#if defined(CONFIG_RTL8821C)
    if (IS_HARDWARE_TYPE_8821C(pAdapter))
        return GET_MASK_ARRAY_LEN(8821C, _MSDIO);
#endif
#if defined(CONFIG_RTL8822B)
    if (IS_HARDWARE_TYPE_8822B(pAdapter))
        return GET_MASK_ARRAY_LEN(8822B, _MSDIO);
#endif
#endif
    return 0;
}
 
u8 rtw_efuse_mask_map_read(PADAPTER padapter, u16 addr, u16 cnts, u8 *data)
{
    u8    ret = _SUCCESS;
    u16    mapLen = 0, i = 0;
 
    EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI, TYPE_EFUSE_MAP_LEN, (PVOID)&mapLen, _FALSE);
 
    ret = rtw_efuse_map_read(padapter, addr, cnts , data);
 
    if (padapter->registrypriv.boffefusemask == 0) {
 
        for (i = 0; i < cnts; i++) {
            if (padapter->registrypriv.bFileMaskEfuse == _TRUE) {
                if (rtw_file_efuse_IsMasked(padapter, addr + i)) /*use file efuse mask.*/
                    data[i] = 0xff;
            } else {
                /*RTW_INFO(" %s , data[%d] = %x\n", __func__, i, data[i]);*/
                if (efuse_IsMasked(padapter, addr + i)) {
                    data[i] = 0xff;
                    /*RTW_INFO(" %s ,mask data[%d] = %x\n", __func__, i, data[i]);*/
                }
            }
        }
 
    }
    return ret;
 
}
 
 
#ifdef RTW_HALMAC
#include "../../hal/hal_halmac.h"
 
void Efuse_PowerSwitch(PADAPTER adapter, u8 write, u8 pwrstate)
{
}
 
void BTEfuse_PowerSwitch(PADAPTER adapter, u8 write, u8 pwrstate)
{
}
 
u8 efuse_GetCurrentSize(PADAPTER adapter, u16 *size)
{
    *size = 0;
 
    return _FAIL;
}
 
u16 efuse_GetMaxSize(PADAPTER adapter)
{
    struct dvobj_priv *d;
    u32 size = 0;
    int err;
 
    d = adapter_to_dvobj(adapter);
    err = rtw_halmac_get_physical_efuse_size(d, &size);
    if (err)
        return 0;
 
    return size;
}
 
u16 efuse_GetavailableSize(PADAPTER adapter)
{
    struct dvobj_priv *d;
    u32 size = 0;
    int err;
 
    d = adapter_to_dvobj(adapter);
    err = rtw_halmac_get_available_efuse_size(d, &size);
    if (err)
        return 0;
 
    return size;
}
 
 
u8 efuse_bt_GetCurrentSize(PADAPTER adapter, u16 *usesize)
{
    u8 *efuse_map;
 
    *usesize = 0;
    efuse_map = rtw_malloc(EFUSE_BT_MAP_LEN);
    if (efuse_map == NULL) {
        RTW_DBG("%s: malloc FAIL\n", __FUNCTION__);
        return _FAIL;
    }
 
    /* for get bt phy efuse last use byte */
    hal_ReadEFuse_BT_logic_map(adapter, 0x00, EFUSE_BT_MAP_LEN, efuse_map);
    *usesize = fakeBTEfuseUsedBytes;
 
    if (efuse_map)
        rtw_mfree(efuse_map, EFUSE_BT_MAP_LEN);
 
    return _SUCCESS;
}
 
u16 efuse_bt_GetMaxSize(PADAPTER adapter)
{
    return EFUSE_BT_REAL_CONTENT_LEN;
}
 
void EFUSE_GetEfuseDefinition(PADAPTER adapter, u8 efusetype, u8 type, void *out, BOOLEAN test)
{
    struct dvobj_priv *d;
    u32 v32 = 0;
 
 
    d = adapter_to_dvobj(adapter);
 
    if (adapter->hal_func.EFUSEGetEfuseDefinition) {
        adapter->hal_func.EFUSEGetEfuseDefinition(adapter, efusetype, type, out, test);
        return;
    }
 
    if (EFUSE_WIFI == efusetype) {
        switch (type) {
        case TYPE_EFUSE_MAP_LEN:
            rtw_halmac_get_logical_efuse_size(d, &v32);
            *(u16 *)out = (u16)v32;
            return;
 
        case TYPE_EFUSE_REAL_CONTENT_LEN:    
            rtw_halmac_get_physical_efuse_size(d, &v32);
            *(u16 *)out = (u16)v32;
            return;
        }
    } else if (EFUSE_BT == efusetype) {
        switch (type) {
        case TYPE_EFUSE_MAP_LEN:
            *(u16 *)out = EFUSE_BT_MAP_LEN;
            return;
 
        case TYPE_EFUSE_REAL_CONTENT_LEN:
            *(u16 *)out = EFUSE_BT_REAL_CONTENT_LEN;
            return;
        }
    }
}
 
/*
 * read/write raw efuse data
 */
u8 rtw_efuse_access(PADAPTER adapter, u8 write, u16 addr, u16 cnts, u8 *data)
{
    struct dvobj_priv *d;
    u8 *efuse = NULL;
    u32 size, i;
    int err;
 
 
    d = adapter_to_dvobj(adapter);
    err = rtw_halmac_get_physical_efuse_size(d, &size);
    if (err)
        size = EFUSE_MAX_SIZE;
 
    if ((addr + cnts) > size)
        return _FAIL;
 
    if (_TRUE == write) {
        err = rtw_halmac_write_physical_efuse(d, addr, cnts, data);
        if (err)
            return _FAIL;
    } else {
        if (cnts > 16)
            efuse = rtw_zmalloc(size);
 
        if (efuse) {
            err = rtw_halmac_read_physical_efuse_map(d, efuse, size);
            if (err) {
                rtw_mfree(efuse, size);
                return _FAIL;
            }
 
            _rtw_memcpy(data, efuse + addr, cnts);
            rtw_mfree(efuse, size);
        } else {
            err = rtw_halmac_read_physical_efuse(d, addr, cnts, data);
            if (err)
                return _FAIL;
        }
    }
 
    return _SUCCESS;
}
 
static inline void dump_buf(u8 *buf, u32 len)
{
    u32 i;
 
    RTW_INFO("-----------------Len %d----------------\n", len);
    for (i = 0; i < len; i++)
        printk("%2.2x-", *(buf + i));
    printk("\n");
}
 
/*
 * read/write raw efuse data
 */
u8 rtw_efuse_bt_access(PADAPTER adapter, u8 write, u16 addr, u16 cnts, u8 *data)
{
    struct dvobj_priv *d;
    u8 *efuse = NULL;
    u32 size, i;
    int err = _FAIL;
 
 
    d = adapter_to_dvobj(adapter);
 
    size = EFUSE_BT_REAL_CONTENT_LEN;
 
    if ((addr + cnts) > size)
        return _FAIL;
 
    if (_TRUE == write) {
        err = rtw_halmac_write_bt_physical_efuse(d, addr, cnts, data);
        if (err == -1) {
            RTW_ERR("%s: rtw_halmac_write_bt_physical_efuse fail!\n", __FUNCTION__);
            return _FAIL;
        }
        RTW_INFO("%s: rtw_halmac_write_bt_physical_efuse OK! data 0x%x\n", __FUNCTION__, *data);
    } else {
        efuse = rtw_zmalloc(size);
 
        if (efuse) {
            err = rtw_halmac_read_bt_physical_efuse_map(d, efuse, size);
            
            if (err == -1) {
                RTW_ERR("%s: rtw_halmac_read_bt_physical_efuse_map fail!\n", __FUNCTION__);
                rtw_mfree(efuse, size);
                return _FAIL;
            }
            dump_buf(efuse + addr, cnts);
 
            _rtw_memcpy(data, efuse + addr, cnts);
 
            RTW_INFO("%s: rtw_halmac_read_bt_physical_efuse_map ok! data 0x%x\n", __FUNCTION__, *data);
            rtw_mfree(efuse, size);
        }
    }
 
    return _SUCCESS;
}
 
u8 rtw_efuse_map_read(PADAPTER adapter, u16 addr, u16 cnts, u8 *data)
{
    struct dvobj_priv *d;
    u8 *efuse = NULL;
    u32 size, i;
    int err;
 
 
    d = adapter_to_dvobj(adapter);
    err = rtw_halmac_get_logical_efuse_size(d, &size);
    if (err)
        return _FAIL;
 
    /* size error handle */
    if ((addr + cnts) > size) {
        if (addr < size)
            cnts = size - addr;
        else
            return _FAIL;
    }
 
    if (cnts > 16)
        efuse = rtw_zmalloc(size);
 
    if (efuse) {
        err = rtw_halmac_read_logical_efuse_map(d, efuse, size);
        if (err) {
            rtw_mfree(efuse, size);
            return _FAIL;
        }
 
        _rtw_memcpy(data, efuse + addr, cnts);
        rtw_mfree(efuse, size);
    } else {
        err = rtw_halmac_read_logical_efuse(d, addr, cnts, data);
        if (err)
            return _FAIL;
    }
 
    return _SUCCESS;
}
 
u8 rtw_efuse_map_write(PADAPTER adapter, u16 addr, u16 cnts, u8 *data)
{
    struct dvobj_priv *d;
    u8 *efuse = NULL;
    u32 size, i;
    int err;
    u8 mask_buf[64] = "";
    u16 mask_len = sizeof(u8) * rtw_get_efuse_mask_arraylen(adapter);
 
    d = adapter_to_dvobj(adapter);
    err = rtw_halmac_get_logical_efuse_size(d, &size);
    if (err)
        return _FAIL;
 
    if ((addr + cnts) > size)
        return _FAIL;
 
    efuse = rtw_zmalloc(size);
    if (!efuse)
        return _FAIL;
 
    err = rtw_halmac_read_logical_efuse_map(d, efuse, size);
    if (err) {
        rtw_mfree(efuse, size);
        return _FAIL;
    }
 
    _rtw_memcpy(efuse + addr, data, cnts);
 
    if (adapter->registrypriv.boffefusemask == 0) {
        RTW_INFO("Use mask Array Len: %d\n", mask_len);
 
        if (mask_len != 0) {
            if (adapter->registrypriv.bFileMaskEfuse == _TRUE)
                _rtw_memcpy(mask_buf, maskfileBuffer, mask_len);
            else
                rtw_efuse_mask_array(adapter, mask_buf);
 
            err = rtw_halmac_write_logical_efuse_map(d, efuse, size, mask_buf, mask_len);
        } else
            err = rtw_halmac_write_logical_efuse_map(d, efuse, size, NULL, 0);
    } else {
        _rtw_memset(mask_buf, 0xFF, sizeof(mask_buf));
        RTW_INFO("Efuse mask off\n");
        err = rtw_halmac_write_logical_efuse_map(d, efuse, size, mask_buf, size/16);
    }
 
    if (err) {
        rtw_mfree(efuse, size);
        return _FAIL;
    }
 
    rtw_mfree(efuse, size);
 
    return _SUCCESS;
}
 
int Efuse_PgPacketRead(PADAPTER adapter, u8 offset, u8 *data, BOOLEAN test)
{
    return _FALSE;
}
 
int Efuse_PgPacketWrite(PADAPTER adapter, u8 offset, u8 word_en, u8 *data, BOOLEAN test)
{
    return _FALSE;
}
 
u8 rtw_BT_efuse_map_read(PADAPTER adapter, u16 addr, u16 cnts, u8 *data)
{
    hal_ReadEFuse_BT_logic_map(adapter,addr, cnts, data);
 
    return _SUCCESS;
}
 
u8 rtw_BT_efuse_map_write(PADAPTER adapter, u16 addr, u16 cnts, u8 *data)
{
#define RT_ASSERT_RET(expr)                                    \
    if (!(expr)) {                                        \
        printk("Assertion failed! %s at ......\n", #expr);                \
        printk("      ......%s,%s, line=%d\n",__FILE__, __FUNCTION__, __LINE__);    \
        return _FAIL;    \
    }
 
    u8    offset, word_en;
    u8    *map;
    u8    newdata[PGPKT_DATA_SIZE];
    s32 i = 0, j = 0, idx;
    u8    ret = _SUCCESS;
    u16 mapLen = 1024;
 
    if ((addr + cnts) > mapLen)
        return _FAIL;
 
    RT_ASSERT_RET(PGPKT_DATA_SIZE == 8); /* have to be 8 byte alignment */
    RT_ASSERT_RET((mapLen & 0x7) == 0); /* have to be PGPKT_DATA_SIZE alignment for memcpy */
 
    map = rtw_zmalloc(mapLen);
    if (map == NULL)
        return _FAIL;
 
    ret = rtw_BT_efuse_map_read(adapter, 0, mapLen, map);
    if (ret == _FAIL)
        goto exit;
    RTW_INFO("OFFSET\tVALUE(hex)\n");
    for (i = 0; i < mapLen; i += 16) { /* set 512 because the iwpriv's extra size have limit 0x7FF */
        RTW_INFO("0x%03x\t", i);
        for (j = 0; j < 8; j++)
            RTW_INFO("%02X ", map[i + j]);
        RTW_INFO("\t");
        for (; j < 16; j++)
            RTW_INFO("%02X ", map[i + j]);
        RTW_INFO("\n");
    }
    RTW_INFO("\n");
 
    idx = 0;
    offset = (addr >> 3);
    while (idx < cnts) {
        word_en = 0xF;
        j = (addr + idx) & 0x7;
        _rtw_memcpy(newdata, &map[offset << 3], PGPKT_DATA_SIZE);
        for (i = j; i < PGPKT_DATA_SIZE && idx < cnts; i++, idx++) {
            if (data[idx] != map[addr + idx]) {
                word_en &= ~BIT(i >> 1);
                newdata[i] = data[idx];
            }
        }
 
        if (word_en != 0xF) {
            ret = EfusePgPacketWrite_BT(adapter, offset, word_en, newdata, _FALSE);
            RTW_INFO("offset=%x\n", offset);
            RTW_INFO("word_en=%x\n", word_en);
            RTW_INFO("%s: data=", __FUNCTION__);
            for (i = 0; i < PGPKT_DATA_SIZE; i++)
                RTW_INFO("0x%02X ", newdata[i]);
            RTW_INFO("\n");
            if (ret == _FAIL)
                break;
        }
        offset++;
    }
exit:
    rtw_mfree(map, mapLen);
    return _SUCCESS;
}
 
VOID hal_ReadEFuse_BT_logic_map(
    PADAPTER    padapter,
    u16            _offset,
    u16            _size_byte,
    u8            *pbuf
)
{
 
    PHAL_DATA_TYPE    pHalData = GET_HAL_DATA(padapter);
    PEFUSE_HAL        pEfuseHal = &pHalData->EfuseHal;
 
    u8    *efuseTbl, *phyefuse;
    u8    bank;
    u16    eFuse_Addr = 0;
    u8    efuseHeader, efuseExtHdr, efuseData;
    u8    offset, wden;
    u16    i, total, used;
    u8    efuse_usage;
 
 
    /* */
    /* Do NOT excess total size of EFuse table. Added by Roger, 2008.11.10. */
    /* */
    if ((_offset + _size_byte) > EFUSE_BT_MAP_LEN) {
        RTW_INFO("%s: Invalid offset(%#x) with read bytes(%#x)!!\n", __FUNCTION__, _offset, _size_byte);
        return;
    }
 
    efuseTbl = rtw_malloc(EFUSE_BT_MAP_LEN);
    phyefuse = rtw_malloc(EFUSE_BT_REAL_CONTENT_LEN);
    if (efuseTbl == NULL || phyefuse == NULL) {
        RTW_INFO("%s: efuseTbl or phyefuse malloc fail!\n", __FUNCTION__);
        goto exit;
    }
 
    /* 0xff will be efuse default value instead of 0x00. */
    _rtw_memset(efuseTbl, 0xFF, EFUSE_BT_MAP_LEN);
    _rtw_memset(phyefuse, 0xFF, EFUSE_BT_REAL_CONTENT_LEN);
 
    if (rtw_efuse_bt_access(padapter, _FALSE, 0, EFUSE_BT_REAL_CONTENT_LEN, phyefuse))
        dump_buf(phyefuse, EFUSE_BT_REAL_BANK_CONTENT_LEN);
    
    total = BANK_NUM;
    for (bank = 1; bank <= total; bank++) { /* 8723d Max bake 0~2 */
        eFuse_Addr = 0;
 
        while (AVAILABLE_EFUSE_ADDR(eFuse_Addr)) {
            /* ReadEFuseByte(padapter, eFuse_Addr++, &efuseHeader, bPseudoTest); */
            efuseHeader = phyefuse[eFuse_Addr++];
 
            if (efuseHeader == 0xFF)
                break;
            RTW_INFO("%s: efuse[%#X]=0x%02x (header)\n", __FUNCTION__, (((bank - 1) * EFUSE_BT_REAL_CONTENT_LEN) + eFuse_Addr - 1), efuseHeader);
 
            /* Check PG header for section num. */
            if (EXT_HEADER(efuseHeader)) {    /* extended header */
                offset = GET_HDR_OFFSET_2_0(efuseHeader);
                RTW_INFO("%s: extended header offset_2_0=0x%X\n", __FUNCTION__, offset);
 
                /* ReadEFuseByte(padapter, eFuse_Addr++, &efuseExtHdr, bPseudoTest); */
                efuseExtHdr = phyefuse[eFuse_Addr++];
 
                RTW_INFO("%s: efuse[%#X]=0x%02x (ext header)\n", __FUNCTION__, (((bank - 1) * EFUSE_BT_REAL_CONTENT_LEN) + eFuse_Addr - 1), efuseExtHdr);
                if (ALL_WORDS_DISABLED(efuseExtHdr))
                    continue;
 
                offset |= ((efuseExtHdr & 0xF0) >> 1);
                wden = (efuseExtHdr & 0x0F);
            } else {
                offset = ((efuseHeader >> 4) & 0x0f);
                wden = (efuseHeader & 0x0f);
            }
 
            if (offset < EFUSE_BT_MAX_SECTION) {
                u16 addr;
 
                /* Get word enable value from PG header */
                RTW_INFO("%s: Offset=%d Worden=%#X\n", __FUNCTION__, offset, wden);
 
                addr = offset * PGPKT_DATA_SIZE;
                for (i = 0; i < EFUSE_MAX_WORD_UNIT; i++) {
                    /* Check word enable condition in the section */
                    if (!(wden & (0x01 << i))) {
                        efuseData = 0;
                        /* ReadEFuseByte(padapter, eFuse_Addr++, &efuseData, bPseudoTest); */
                        efuseData = phyefuse[eFuse_Addr++];
 
                        RTW_INFO("%s: efuse[%#X]=0x%02X\n", __FUNCTION__, eFuse_Addr - 1, efuseData);
                        efuseTbl[addr] = efuseData;
 
                        efuseData = 0;
                        /* ReadEFuseByte(padapter, eFuse_Addr++, &efuseData, bPseudoTest); */
                        efuseData = phyefuse[eFuse_Addr++];
 
                        RTW_INFO("%s: efuse[%#X]=0x%02X\n", __FUNCTION__, eFuse_Addr - 1, efuseData);
                        efuseTbl[addr + 1] = efuseData;
                    }
                    addr += 2;
                }
            } else {
                RTW_INFO("%s: offset(%d) is illegal!!\n", __FUNCTION__, offset);
                eFuse_Addr += Efuse_CalculateWordCnts(wden) * 2;
            }
        }
 
        if ((eFuse_Addr - 1) < total) {
            RTW_INFO("%s: bank(%d) data end at %#x\n", __FUNCTION__, bank, eFuse_Addr - 1);
            break;
        }
    }
 
    /* switch bank back to bank 0 for later BT and wifi use. */
    //hal_EfuseSwitchToBank(padapter, 0, bPseudoTest);
 
    /* Copy from Efuse map to output pointer memory!!! */
    for (i = 0; i < _size_byte; i++)
        pbuf[i] = efuseTbl[_offset + i];
    /* Calculate Efuse utilization */
    total = EFUSE_BT_REAL_BANK_CONTENT_LEN;
 
    used = eFuse_Addr - 1;
 
    if (total)
        efuse_usage = (u8)((used * 100) / total);
    else
        efuse_usage = 100;
 
    fakeBTEfuseUsedBytes = used;
    RTW_INFO("%s: BTEfuseUsed last Bytes = %#x\n", __FUNCTION__, fakeBTEfuseUsedBytes);
 
exit:
    if (efuseTbl)
        rtw_mfree(efuseTbl, EFUSE_BT_MAP_LEN);
    if (phyefuse)
        rtw_mfree(phyefuse, EFUSE_BT_REAL_BANK_CONTENT_LEN);
}
 
 
static u8 hal_EfusePartialWriteCheck(
    PADAPTER        padapter,
    u8                efuseType,
    u16                *pAddr,
    PPGPKT_STRUCT    pTargetPkt,
    u8                bPseudoTest)
{
    PHAL_DATA_TYPE    pHalData = GET_HAL_DATA(padapter);
    PEFUSE_HAL        pEfuseHal = &pHalData->EfuseHal;
    u8    bRet = _FALSE;
    u16    startAddr = 0, efuse_max_available_len = EFUSE_BT_REAL_BANK_CONTENT_LEN, efuse_max = EFUSE_BT_REAL_BANK_CONTENT_LEN;
    u8    efuse_data = 0;
 
    startAddr = (u16)fakeBTEfuseUsedBytes;
 
    startAddr %= efuse_max;
    RTW_INFO("%s: startAddr=%#X\n", __FUNCTION__, startAddr);
 
    while (1) {
        if (startAddr >= efuse_max_available_len) {
            bRet = _FALSE;
            RTW_INFO("%s: startAddr(%d) >= efuse_max_available_len(%d)\n",
                __FUNCTION__, startAddr, efuse_max_available_len);
            break;
        }
        if (rtw_efuse_bt_access(padapter, _FALSE, startAddr, 1, &efuse_data)&& (efuse_data != 0xFF)) {
            bRet = _FALSE;
            RTW_INFO("%s: Something Wrong! last bytes(%#X=0x%02X) is not 0xFF\n",
                 __FUNCTION__, startAddr, efuse_data);
            break;
        } else {
            /* not used header, 0xff */
            *pAddr = startAddr;
            /*            RTW_INFO("%s: Started from unused header offset=%d\n", __FUNCTION__, startAddr)); */
            bRet = _TRUE;
            break;
        }
    }
 
    return bRet;
}
 
 
static u8 hal_EfusePgPacketWrite2ByteHeader(
    PADAPTER        padapter,
    u8                efuseType,
    u16                *pAddr,
    PPGPKT_STRUCT    pTargetPkt,
    u8                bPseudoTest)
{
    u16    efuse_addr, efuse_max_available_len = EFUSE_BT_REAL_BANK_CONTENT_LEN;
    u8    pg_header = 0, tmp_header = 0;
    u8    repeatcnt = 0;
 
    /*    RTW_INFO("%s\n", __FUNCTION__); */
 
    efuse_addr = *pAddr;
    if (efuse_addr >= efuse_max_available_len) {
        RTW_INFO("%s: addr(%d) over avaliable(%d)!!\n", __FUNCTION__, efuse_addr, efuse_max_available_len);
        return _FALSE;
    }
 
    pg_header = ((pTargetPkt->offset & 0x07) << 5) | 0x0F;
    /*    RTW_INFO("%s: pg_header=0x%x\n", __FUNCTION__, pg_header); */
 
    do {
        
        rtw_efuse_bt_access(padapter, _TRUE, efuse_addr, 1, &pg_header);
        rtw_efuse_bt_access(padapter, _FALSE, efuse_addr, 1, &tmp_header);
 
        if (tmp_header != 0xFF)
            break;
        if (repeatcnt++ > EFUSE_REPEAT_THRESHOLD_) {
            RTW_INFO("%s: Repeat over limit for pg_header!!\n", __FUNCTION__);
            return _FALSE;
        }
    } while (1);
 
    if (tmp_header != pg_header) {
        RTW_ERR("%s: PG Header Fail!!(pg=0x%02X read=0x%02X)\n", __FUNCTION__, pg_header, tmp_header);
        return _FALSE;
    }
 
    /* to write ext_header */
    efuse_addr++;
    pg_header = ((pTargetPkt->offset & 0x78) << 1) | pTargetPkt->word_en;
 
    do {
        rtw_efuse_bt_access(padapter, _TRUE, efuse_addr, 1, &pg_header);
        rtw_efuse_bt_access(padapter, _FALSE, efuse_addr, 1, &tmp_header);
 
        if (tmp_header != 0xFF)
            break;
        if (repeatcnt++ > EFUSE_REPEAT_THRESHOLD_) {
            RTW_INFO("%s: Repeat over limit for ext_header!!\n", __FUNCTION__);
            return _FALSE;
        }
    } while (1);
 
    if (tmp_header != pg_header) {    /* offset PG fail */
        RTW_ERR("%s: PG EXT Header Fail!!(pg=0x%02X read=0x%02X)\n", __FUNCTION__, pg_header, tmp_header);
        return _FALSE;
    }
 
    *pAddr = efuse_addr;
 
    return _TRUE;
}
 
 
static u8 hal_EfusePgPacketWrite1ByteHeader(
    PADAPTER        pAdapter,
    u8                efuseType,
    u16                *pAddr,
    PPGPKT_STRUCT    pTargetPkt,
    u8                bPseudoTest)
{
    u8    bRet = _FALSE;
    u8    pg_header = 0, tmp_header = 0;
    u16    efuse_addr = *pAddr;
    u8    repeatcnt = 0;
 
 
    /*    RTW_INFO("%s\n", __FUNCTION__); */
    pg_header = ((pTargetPkt->offset << 4) & 0xf0) | pTargetPkt->word_en;
 
    do {
        rtw_efuse_bt_access(pAdapter, _TRUE, efuse_addr, 1, &pg_header);
        rtw_efuse_bt_access(pAdapter, _FALSE, efuse_addr, 1, &tmp_header);
 
        if (tmp_header != 0xFF)
            break;
        if (repeatcnt++ > EFUSE_REPEAT_THRESHOLD_) {
            RTW_INFO("%s: Repeat over limit for pg_header!!\n", __FUNCTION__);
            return _FALSE;
        }
    } while (1);
 
    if (tmp_header != pg_header) {
        RTW_ERR("%s: PG Header Fail!!(pg=0x%02X read=0x%02X)\n", __FUNCTION__, pg_header, tmp_header);
        return _FALSE;
    }
 
    *pAddr = efuse_addr;
 
    return _TRUE;
}
 
static u8 hal_EfusePgPacketWriteHeader(
    PADAPTER        padapter,
    u8                efuseType,
    u16                *pAddr,
    PPGPKT_STRUCT    pTargetPkt,
    u8                bPseudoTest)
{
    u8 bRet = _FALSE;
 
    if (pTargetPkt->offset >= EFUSE_MAX_SECTION_BASE)
        bRet = hal_EfusePgPacketWrite2ByteHeader(padapter, efuseType, pAddr, pTargetPkt, bPseudoTest);
    else
        bRet = hal_EfusePgPacketWrite1ByteHeader(padapter, efuseType, pAddr, pTargetPkt, bPseudoTest);
 
    return bRet;
}
 
 
static u8
Hal_EfuseWordEnableDataWrite(
    PADAPTER    padapter,
    u16            efuse_addr,
    u8            word_en,
    u8            *data,
    u8            bPseudoTest)
{
    u16    tmpaddr = 0;
    u16    start_addr = efuse_addr;
    u8    badworden = 0x0F;
    u8    tmpdata[PGPKT_DATA_SIZE];
 
 
    /*    RTW_INFO("%s: efuse_addr=%#x word_en=%#x\n", __FUNCTION__, efuse_addr, word_en); */
    _rtw_memset(tmpdata, 0xFF, PGPKT_DATA_SIZE);
 
    if (!(word_en & BIT(0))) {
        tmpaddr = start_addr;
        rtw_efuse_bt_access(padapter, _TRUE, start_addr++, 1, &data[0]);
        rtw_efuse_bt_access(padapter, _TRUE, start_addr++, 1, &data[1]);
        rtw_efuse_bt_access(padapter, _FALSE, tmpaddr, 1, &tmpdata[0]);
        rtw_efuse_bt_access(padapter, _FALSE, tmpaddr + 1, 1, &tmpdata[1]);
        if ((data[0] != tmpdata[0]) || (data[1] != tmpdata[1]))
            badworden &= (~BIT(0));
    }
    if (!(word_en & BIT(1))) {
        tmpaddr = start_addr;
        rtw_efuse_bt_access(padapter, _TRUE, start_addr++, 1, &data[2]);
        rtw_efuse_bt_access(padapter, _TRUE, start_addr++, 1, &data[3]);
        rtw_efuse_bt_access(padapter, _FALSE, tmpaddr, 1, &tmpdata[2]);
        rtw_efuse_bt_access(padapter, _FALSE, tmpaddr + 1, 1, &tmpdata[3]);
        if ((data[2] != tmpdata[2]) || (data[3] != tmpdata[3]))
            badworden &= (~BIT(1));
    }
    if (!(word_en & BIT(2))) {
        tmpaddr = start_addr;
        rtw_efuse_bt_access(padapter, _TRUE, start_addr++, 1, &data[4]);
        rtw_efuse_bt_access(padapter, _TRUE, start_addr++, 1, &data[5]);
        rtw_efuse_bt_access(padapter, _FALSE, tmpaddr, 1, &tmpdata[4]);
        rtw_efuse_bt_access(padapter, _FALSE, tmpaddr + 1, 1, &tmpdata[5]);
        if ((data[4] != tmpdata[4]) || (data[5] != tmpdata[5]))
            badworden &= (~BIT(2));
    }
    if (!(word_en & BIT(3))) {
        tmpaddr = start_addr;
        rtw_efuse_bt_access(padapter, _TRUE, start_addr++, 1, &data[6]);
        rtw_efuse_bt_access(padapter, _TRUE, start_addr++, 1, &data[7]);
        rtw_efuse_bt_access(padapter, _FALSE, tmpaddr, 1, &tmpdata[6]);
        rtw_efuse_bt_access(padapter, _FALSE, tmpaddr + 1, 1, &tmpdata[7]);
 
        if ((data[6] != tmpdata[6]) || (data[7] != tmpdata[7]))
            badworden &= (~BIT(3));
    }
 
    return badworden;
}
 
static void
hal_EfuseConstructPGPkt(
    u8                offset,
    u8                word_en,
    u8                *pData,
    PPGPKT_STRUCT    pTargetPkt)
{
    _rtw_memset(pTargetPkt->data, 0xFF, PGPKT_DATA_SIZE);
    pTargetPkt->offset = offset;
    pTargetPkt->word_en = word_en;
    efuse_WordEnableDataRead(word_en, pData, pTargetPkt->data);
    pTargetPkt->word_cnts = Efuse_CalculateWordCnts(pTargetPkt->word_en);
}
 
static u8
hal_EfusePgPacketWriteData(
    PADAPTER        pAdapter,
    u8                efuseType,
    u16                *pAddr,
    PPGPKT_STRUCT    pTargetPkt,
    u8                bPseudoTest)
{
    u16    efuse_addr;
    u8    badworden;
 
    efuse_addr = *pAddr;
    badworden = Hal_EfuseWordEnableDataWrite(pAdapter, efuse_addr + 1, pTargetPkt->word_en, pTargetPkt->data, bPseudoTest);
    if (badworden != 0x0F) {
        RTW_INFO("%s: Fail!!\n", __FUNCTION__);
        return _FALSE;
    } else
        RTW_INFO("%s: OK!!\n", __FUNCTION__);
 
    return _TRUE;
}
 
/* ***********************************************************
 *                Efuse related code
 * *********************************************************** */
static u8
hal_EfuseSwitchToBank(
    PADAPTER    padapter,
    u8            bank,
    u8            bPseudoTest)
{
    u8 bRet = _FALSE;
    u32 value32 = 0;
#ifdef HAL_EFUSE_MEMORY
    PHAL_DATA_TYPE pHalData = GET_HAL_DATA(padapter);
    PEFUSE_HAL pEfuseHal = &pHalData->EfuseHal;
#endif
 
 
    RTW_INFO("%s: Efuse switch bank to %d\n", __FUNCTION__, bank);
    if (bPseudoTest) {
#ifdef HAL_EFUSE_MEMORY
        pEfuseHal->fakeEfuseBank = bank;
#else
        fakeEfuseBank = bank;
#endif
        bRet = _TRUE;
    } else {
        value32 = rtw_read32(padapter, 0x34);
        bRet = _TRUE;
        switch (bank) {
        case 0:
            value32 = (value32 & ~EFUSE_SEL_MASK) | EFUSE_SEL(EFUSE_WIFI_SEL_0);
            break;
        case 1:
            value32 = (value32 & ~EFUSE_SEL_MASK) | EFUSE_SEL(EFUSE_BT_SEL_0);
            break;
        case 2:
            value32 = (value32 & ~EFUSE_SEL_MASK) | EFUSE_SEL(EFUSE_BT_SEL_1);
            break;
        case 3:
            value32 = (value32 & ~EFUSE_SEL_MASK) | EFUSE_SEL(EFUSE_BT_SEL_2);
            break;
        default:
            value32 = (value32 & ~EFUSE_SEL_MASK) | EFUSE_SEL(EFUSE_WIFI_SEL_0);
            bRet = _FALSE;
            break;
        }
        rtw_write32(padapter, 0x34, value32);
    }
 
    return bRet;
}
 
 
#define EFUSE_CTRL                0x30        /* E-Fuse Control. */
 
/*  11/16/2008 MH Read one byte from real Efuse. */
u8
efuse_OneByteRead(
    IN    PADAPTER    pAdapter,
    IN    u16            addr,
    IN    u8            *data,
    IN    BOOLEAN        bPseudoTest)
{
    u32    tmpidx = 0;
    u8    bResult;
    u8    readbyte;
    HAL_DATA_TYPE    *pHalData = GET_HAL_DATA(pAdapter);
 
    if (IS_HARDWARE_TYPE_8723B(pAdapter) ||
        (IS_HARDWARE_TYPE_8192E(pAdapter) && (!IS_A_CUT(pHalData->version_id))) ||
        (IS_VENDOR_8188E_I_CUT_SERIES(pAdapter)) || (IS_CHIP_VENDOR_SMIC(pHalData->version_id))
       ) {
        /* <20130121, Kordan> For SMIC EFUSE specificatoin. */
        /* 0x34[11]: SW force PGMEN input of efuse to high. (for the bank selected by 0x34[9:8])     */
        /* phy_set_mac_reg(pAdapter, 0x34, BIT11, 0); */
        rtw_write16(pAdapter, 0x34, rtw_read16(pAdapter, 0x34) & (~BIT11));
    }
 
    /* -----------------e-fuse reg ctrl --------------------------------- */
    /* address             */
    rtw_write8(pAdapter, EFUSE_CTRL + 1, (u8)(addr & 0xff));
    rtw_write8(pAdapter, EFUSE_CTRL + 2, ((u8)((addr >> 8) & 0x03)) |
           (rtw_read8(pAdapter, EFUSE_CTRL + 2) & 0xFC));
 
    /* rtw_write8(pAdapter, EFUSE_CTRL+3,  0x72); */ /* read cmd     */
    /* Write bit 32 0 */
    readbyte = rtw_read8(pAdapter, EFUSE_CTRL + 3);
    rtw_write8(pAdapter, EFUSE_CTRL + 3, (readbyte & 0x7f));
 
    while (!(0x80 & rtw_read8(pAdapter, EFUSE_CTRL + 3)) && (tmpidx < 1000)) {
        rtw_mdelay_os(1);
        tmpidx++;
    }
    if (tmpidx < 100) {
        *data = rtw_read8(pAdapter, EFUSE_CTRL);
        bResult = _TRUE;
    } else {
        *data = 0xff;
        bResult = _FALSE;
        RTW_INFO("%s: [ERROR] addr=0x%x bResult=%d time out 1s !!!\n", __FUNCTION__, addr, bResult);
        RTW_INFO("%s: [ERROR] EFUSE_CTRL =0x%08x !!!\n", __FUNCTION__, rtw_read32(pAdapter, EFUSE_CTRL));
    }
 
    return bResult;
}
 
 
static u16
hal_EfuseGetCurrentSize_BT(
    PADAPTER    padapter,
    u8            bPseudoTest)
{
#ifdef HAL_EFUSE_MEMORY
    PHAL_DATA_TYPE    pHalData = GET_HAL_DATA(padapter);
    PEFUSE_HAL        pEfuseHal = &pHalData->EfuseHal;
#endif
    u16 btusedbytes;
    u16    efuse_addr;
    u8    bank, startBank;
    u8    hoffset = 0, hworden = 0;
    u8    efuse_data, word_cnts = 0;
    u16    retU2 = 0;
    u8 bContinual = _TRUE;
 
 
    btusedbytes = fakeBTEfuseUsedBytes;
 
    efuse_addr = (u16)((btusedbytes % EFUSE_BT_REAL_BANK_CONTENT_LEN));
    startBank = (u8)(1 + (btusedbytes / EFUSE_BT_REAL_BANK_CONTENT_LEN));
 
    RTW_INFO("%s: start from bank=%d addr=0x%X\n", __FUNCTION__, startBank, efuse_addr);
    retU2 = EFUSE_BT_REAL_CONTENT_LEN - EFUSE_PROTECT_BYTES_BANK;
 
    for (bank = startBank; bank < 3; bank++) {
        if (hal_EfuseSwitchToBank(padapter, bank, bPseudoTest) == _FALSE) {
            RTW_ERR("%s: switch bank(%d) Fail!!\n", __FUNCTION__, bank);
            /* bank = EFUSE_MAX_BANK; */
            break;
        }
 
        /* only when bank is switched we have to reset the efuse_addr. */
        if (bank != startBank)
            efuse_addr = 0;
 
 
        while (AVAILABLE_EFUSE_ADDR(efuse_addr)) {
            if (rtw_efuse_bt_access(padapter, _FALSE, efuse_addr, 1, &efuse_data) == _FALSE) {
                RTW_ERR("%s: efuse_OneByteRead Fail! addr=0x%X !!\n", __FUNCTION__, efuse_addr);
                /* bank = EFUSE_MAX_BANK; */
                break;
            }
            RTW_INFO("%s: efuse_OneByteRead ! addr=0x%X !efuse_data=0x%X! bank =%d\n", __FUNCTION__, efuse_addr, efuse_data, bank);
 
            if (efuse_data == 0xFF)
                break;
 
            if (EXT_HEADER(efuse_data)) {
                hoffset = GET_HDR_OFFSET_2_0(efuse_data);
                efuse_addr++;
                rtw_efuse_bt_access(padapter, _FALSE, efuse_addr, 1, &efuse_data);
                RTW_INFO("%s: efuse_OneByteRead EXT_HEADER ! addr=0x%X !efuse_data=0x%X! bank =%d\n", __FUNCTION__, efuse_addr, efuse_data, bank);
 
                if (ALL_WORDS_DISABLED(efuse_data)) {
                    efuse_addr++;
                    continue;
                }
 
                /*                hoffset = ((hoffset & 0xE0) >> 5) | ((efuse_data & 0xF0) >> 1); */
                hoffset |= ((efuse_data & 0xF0) >> 1);
                hworden = efuse_data & 0x0F;
            } else {
                hoffset = (efuse_data >> 4) & 0x0F;
                hworden =  efuse_data & 0x0F;
            }
 
            RTW_INFO(FUNC_ADPT_FMT": Offset=%d Worden=%#X\n",
                 FUNC_ADPT_ARG(padapter), hoffset, hworden);
 
            word_cnts = Efuse_CalculateWordCnts(hworden);
            /* read next header */
            efuse_addr += (word_cnts * 2) + 1;
        }
        /* Check if we need to check next bank efuse */
        if (efuse_addr < retU2)
            break;/* don't need to check next bank. */
    }
    retU2 = ((bank - 1) * EFUSE_BT_REAL_BANK_CONTENT_LEN) + efuse_addr;
 
    fakeBTEfuseUsedBytes = retU2;
    RTW_INFO("%s: CurrentSize=%d\n", __FUNCTION__, retU2);
    return retU2;
}
 
 
static u8
hal_BT_EfusePgCheckAvailableAddr(
    PADAPTER    pAdapter,
    u8        bPseudoTest)
{
    u16    max_available = EFUSE_BT_REAL_CONTENT_LEN - EFUSE_PROTECT_BYTES_BANK;
    u16    current_size = 0;
 
     RTW_INFO("%s: max_available=%d\n", __FUNCTION__, max_available);
    current_size = hal_EfuseGetCurrentSize_BT(pAdapter, bPseudoTest);
    if (current_size >= max_available) {
        RTW_INFO("%s: Error!! current_size(%d)>max_available(%d)\n", __FUNCTION__, current_size, max_available);
        return _FALSE;
    }
    return _TRUE;
}
 
u8 EfusePgPacketWrite_BT(
    PADAPTER    pAdapter,
    u8            offset,
    u8            word_en,
    u8            *pData,
    u8            bPseudoTest)
{
    PGPKT_STRUCT targetPkt;
    u16 startAddr = 0;
    u8 efuseType = EFUSE_BT;
 
    if (!hal_BT_EfusePgCheckAvailableAddr(pAdapter, bPseudoTest))
        return _FALSE;
 
    hal_EfuseConstructPGPkt(offset, word_en, pData, &targetPkt);
 
    if (!hal_EfusePartialWriteCheck(pAdapter, efuseType, &startAddr, &targetPkt, bPseudoTest))
        return _FALSE;
 
    if (!hal_EfusePgPacketWriteHeader(pAdapter, efuseType, &startAddr, &targetPkt, bPseudoTest))
        return _FALSE;
 
    if (!hal_EfusePgPacketWriteData(pAdapter, efuseType, &startAddr, &targetPkt, bPseudoTest))
        return _FALSE;
 
    return _TRUE;
}
 
 
#else /* !RTW_HALMAC */
/* ------------------------------------------------------------------------------ */
#define REG_EFUSE_CTRL        0x0030
#define EFUSE_CTRL            REG_EFUSE_CTRL        /* E-Fuse Control. */
/* ------------------------------------------------------------------------------ */
 
VOID efuse_PreUpdateAction(
    PADAPTER    pAdapter,
    pu4Byte    BackupRegs)
{
#if defined(CONFIG_RTL8812A)
    if (IS_HARDWARE_TYPE_8812AU(pAdapter)) {
        /* <20131115, Kordan> Turn off Rx to prevent from being busy when writing the EFUSE. (Asked by Chunchu.)*/
        BackupRegs[0] = phy_query_mac_reg(pAdapter, REG_RCR, bMaskDWord);
        BackupRegs[1] = phy_query_mac_reg(pAdapter, REG_RXFLTMAP0, bMaskDWord);
        BackupRegs[2] = phy_query_mac_reg(pAdapter, REG_RXFLTMAP0+4, bMaskDWord);
        BackupRegs[3] = phy_query_mac_reg(pAdapter, REG_AFE_MISC, bMaskDWord);
 
        PlatformEFIOWrite4Byte(pAdapter, REG_RCR, 0x1);
        PlatformEFIOWrite1Byte(pAdapter, REG_RXFLTMAP0, 0);
        PlatformEFIOWrite1Byte(pAdapter, REG_RXFLTMAP0+1, 0);
        PlatformEFIOWrite1Byte(pAdapter, REG_RXFLTMAP0+2, 0);
        PlatformEFIOWrite1Byte(pAdapter, REG_RXFLTMAP0+3, 0);
        PlatformEFIOWrite1Byte(pAdapter, REG_RXFLTMAP0+4, 0);
        PlatformEFIOWrite1Byte(pAdapter, REG_RXFLTMAP0+5, 0);
 
        /* <20140410, Kordan> 0x11 = 0x4E, lower down LX_SPS0 voltage. (Asked by Chunchu)*/
        phy_set_mac_reg(pAdapter, REG_AFE_MISC, bMaskByte1, 0x4E);
        }
#endif
}
 
VOID efuse_PostUpdateAction(
    PADAPTER    pAdapter,
    pu4Byte    BackupRegs)
{
#if defined(CONFIG_RTL8812A)
    if (IS_HARDWARE_TYPE_8812AU(pAdapter)) {
        /* <20131115, Kordan> Turn on Rx and restore the registers. (Asked by Chunchu.)*/
        phy_set_mac_reg(pAdapter, REG_RCR, bMaskDWord, BackupRegs[0]);
        phy_set_mac_reg(pAdapter, REG_RXFLTMAP0, bMaskDWord, BackupRegs[1]);
        phy_set_mac_reg(pAdapter, REG_RXFLTMAP0+4, bMaskDWord, BackupRegs[2]);
        phy_set_mac_reg(pAdapter, REG_AFE_MISC, bMaskDWord, BackupRegs[3]);
    }
#endif
}
 
 
BOOLEAN
Efuse_Read1ByteFromFakeContent(
    IN        PADAPTER    pAdapter,
    IN        u16        Offset,
    IN OUT    u8        *Value);
BOOLEAN
Efuse_Read1ByteFromFakeContent(
    IN        PADAPTER    pAdapter,
    IN        u16        Offset,
    IN OUT    u8        *Value)
{
    if (Offset >= EFUSE_MAX_HW_SIZE)
        return _FALSE;
    /* DbgPrint("Read fake content, offset = %d\n", Offset); */
    if (fakeEfuseBank == 0)
        *Value = fakeEfuseContent[Offset];
    else
        *Value = fakeBTEfuseContent[fakeEfuseBank - 1][Offset];
    return _TRUE;
}
 
BOOLEAN
Efuse_Write1ByteToFakeContent(
    IN        PADAPTER    pAdapter,
    IN        u16        Offset,
    IN        u8        Value);
BOOLEAN
Efuse_Write1ByteToFakeContent(
    IN        PADAPTER    pAdapter,
    IN        u16        Offset,
    IN        u8        Value)
{
    if (Offset >= EFUSE_MAX_HW_SIZE)
        return _FALSE;
    if (fakeEfuseBank == 0)
        fakeEfuseContent[Offset] = Value;
    else
        fakeBTEfuseContent[fakeEfuseBank - 1][Offset] = Value;
    return _TRUE;
}
 
/*-----------------------------------------------------------------------------
 * Function:    Efuse_PowerSwitch
 *
 * Overview:    When we want to enable write operation, we should change to
 *                pwr on state. When we stop write, we should switch to 500k mode
 *                and disable LDO 2.5V.
 *
 * Input:       NONE
 *
 * Output:      NONE
 *
 * Return:      NONE
 *
 * Revised History:
 * When            Who        Remark
 * 11/17/2008    MHC        Create Version 0.
 *
 *---------------------------------------------------------------------------*/
VOID
Efuse_PowerSwitch(
    IN    PADAPTER    pAdapter,
    IN    u8        bWrite,
    IN    u8        PwrState)
{
    pAdapter->hal_func.EfusePowerSwitch(pAdapter, bWrite, PwrState);
}
 
VOID
BTEfuse_PowerSwitch(
    IN    PADAPTER    pAdapter,
    IN    u8        bWrite,
    IN    u8        PwrState)
{
    if (pAdapter->hal_func.BTEfusePowerSwitch)
        pAdapter->hal_func.BTEfusePowerSwitch(pAdapter, bWrite, PwrState);
}
 
/*-----------------------------------------------------------------------------
 * Function:    efuse_GetCurrentSize
 *
 * Overview:    Get current efuse size!!!
 *
 * Input:       NONE
 *
 * Output:      NONE
 *
 * Return:      NONE
 *
 * Revised History:
 * When            Who        Remark
 * 11/16/2008    MHC        Create Version 0.
 *
 *---------------------------------------------------------------------------*/
u16
Efuse_GetCurrentSize(
    IN PADAPTER        pAdapter,
    IN u8            efuseType,
    IN BOOLEAN        bPseudoTest)
{
    u16 ret = 0;
 
    ret = pAdapter->hal_func.EfuseGetCurrentSize(pAdapter, efuseType, bPseudoTest);
 
    return ret;
}
 
/*
 *    Description:
 *        Execute E-Fuse read byte operation.
 *        Refered from SD1 Richard.
 *
 *    Assumption:
 *        1. Boot from E-Fuse and successfully auto-load.
 *        2. PASSIVE_LEVEL (USB interface)
 *
 *    Created by Roger, 2008.10.21.
 *   */
VOID
ReadEFuseByte(
    PADAPTER    Adapter,
    u16            _offset,
    u8            *pbuf,
    IN BOOLEAN    bPseudoTest)
{
    u32    value32;
    u8    readbyte;
    u16    retry;
    /* u32 start=rtw_get_current_time(); */
 
    if (bPseudoTest) {
        Efuse_Read1ByteFromFakeContent(Adapter, _offset, pbuf);
        return;
    }
    if (IS_HARDWARE_TYPE_8723B(Adapter)) {
        /* <20130121, Kordan> For SMIC S55 EFUSE specificatoin. */
        /* 0x34[11]: SW force PGMEN input of efuse to high. (for the bank selected by 0x34[9:8]) */
        phy_set_mac_reg(Adapter, EFUSE_TEST, BIT11, 0);
    }
    /* Write Address */
    rtw_write8(Adapter, EFUSE_CTRL + 1, (_offset & 0xff));
    readbyte = rtw_read8(Adapter, EFUSE_CTRL + 2);
    rtw_write8(Adapter, EFUSE_CTRL + 2, ((_offset >> 8) & 0x03) | (readbyte & 0xfc));
 
    /* Write bit 32 0 */
    readbyte = rtw_read8(Adapter, EFUSE_CTRL + 3);
    rtw_write8(Adapter, EFUSE_CTRL + 3, (readbyte & 0x7f));
 
    /* Check bit 32 read-ready */
    retry = 0;
    value32 = rtw_read32(Adapter, EFUSE_CTRL);
    /* while(!(((value32 >> 24) & 0xff) & 0x80)  && (retry<10)) */
    while (!(((value32 >> 24) & 0xff) & 0x80)  && (retry < 10000)) {
        value32 = rtw_read32(Adapter, EFUSE_CTRL);
        retry++;
    }
 
    /* 20100205 Joseph: Add delay suggested by SD1 Victor. */
    /* This fix the problem that Efuse read error in high temperature condition. */
    /* Designer says that there shall be some delay after ready bit is set, or the */
    /* result will always stay on last data we read. */
    rtw_udelay_os(50);
    value32 = rtw_read32(Adapter, EFUSE_CTRL);
 
    *pbuf = (u8)(value32 & 0xff);
    /* RTW_INFO("ReadEFuseByte _offset:%08u, in %d ms\n",_offset ,rtw_get_passing_time_ms(start)); */
 
}
 
/*
 *    Description:
 *        1. Execute E-Fuse read byte operation according as map offset and
 *            save to E-Fuse table.
 *        2. Refered from SD1 Richard.
 *
 *    Assumption:
 *        1. Boot from E-Fuse and successfully auto-load.
 *        2. PASSIVE_LEVEL (USB interface)
 *
 *    Created by Roger, 2008.10.21.
 *
 *    2008/12/12 MH    1. Reorganize code flow and reserve bytes. and add description.
 *                    2. Add efuse utilization collect.
 *    2008/12/22 MH    Read Efuse must check if we write section 1 data again!!! Sec1
 *                    write addr must be after sec5.
 *   */
 
VOID
efuse_ReadEFuse(
    PADAPTER    Adapter,
    u8        efuseType,
    u16        _offset,
    u16        _size_byte,
    u8    *pbuf,
    IN    BOOLEAN    bPseudoTest
);
VOID
efuse_ReadEFuse(
    PADAPTER    Adapter,
    u8        efuseType,
    u16        _offset,
    u16        _size_byte,
    u8    *pbuf,
    IN    BOOLEAN    bPseudoTest
)
{
    Adapter->hal_func.ReadEFuse(Adapter, efuseType, _offset, _size_byte, pbuf, bPseudoTest);
}
 
VOID
EFUSE_GetEfuseDefinition(
    IN        PADAPTER    pAdapter,
    IN        u8        efuseType,
    IN        u8        type,
    OUT        void        *pOut,
    IN        BOOLEAN        bPseudoTest
)
{
    pAdapter->hal_func.EFUSEGetEfuseDefinition(pAdapter, efuseType, type, pOut, bPseudoTest);
}
 
 
/*  11/16/2008 MH Read one byte from real Efuse. */
u8
efuse_OneByteRead(
    IN    PADAPTER    pAdapter,
    IN    u16            addr,
    IN    u8            *data,
    IN    BOOLEAN        bPseudoTest)
{
    u32    tmpidx = 0;
    u8    bResult;
    u8    readbyte;
    HAL_DATA_TYPE    *pHalData = GET_HAL_DATA(pAdapter);
 
    /* RTW_INFO("===> EFUSE_OneByteRead(), addr = %x\n", addr); */
    /* RTW_INFO("===> EFUSE_OneByteRead() start, 0x34 = 0x%X\n", rtw_read32(pAdapter, EFUSE_TEST)); */
 
    if (bPseudoTest) {
        bResult = Efuse_Read1ByteFromFakeContent(pAdapter, addr, data);
        return bResult;
    }
 
    if (IS_HARDWARE_TYPE_8723B(pAdapter) ||
        (IS_HARDWARE_TYPE_8192E(pAdapter) && (!IS_A_CUT(pHalData->version_id))) ||
        (IS_VENDOR_8188E_I_CUT_SERIES(pAdapter)) || (IS_CHIP_VENDOR_SMIC(pHalData->version_id))
       ) {
        /* <20130121, Kordan> For SMIC EFUSE specificatoin. */
        /* 0x34[11]: SW force PGMEN input of efuse to high. (for the bank selected by 0x34[9:8])     */
        /* phy_set_mac_reg(pAdapter, 0x34, BIT11, 0); */
        rtw_write16(pAdapter, 0x34, rtw_read16(pAdapter, 0x34) & (~BIT11));
    }
 
    /* -----------------e-fuse reg ctrl --------------------------------- */
    /* address             */
    rtw_write8(pAdapter, EFUSE_CTRL + 1, (u8)(addr & 0xff));
    rtw_write8(pAdapter, EFUSE_CTRL + 2, ((u8)((addr >> 8) & 0x03)) |
           (rtw_read8(pAdapter, EFUSE_CTRL + 2) & 0xFC));
 
    /* rtw_write8(pAdapter, EFUSE_CTRL+3,  0x72); */ /* read cmd     */
    /* Write bit 32 0 */
    readbyte = rtw_read8(pAdapter, EFUSE_CTRL + 3);
    rtw_write8(pAdapter, EFUSE_CTRL + 3, (readbyte & 0x7f));
 
    while (!(0x80 & rtw_read8(pAdapter, EFUSE_CTRL + 3)) && (tmpidx < 1000)) {
        rtw_mdelay_os(1);
        tmpidx++;
    }
    if (tmpidx < 100) {
        *data = rtw_read8(pAdapter, EFUSE_CTRL);
        bResult = _TRUE;
    } else {
        *data = 0xff;
        bResult = _FALSE;
        RTW_INFO("%s: [ERROR] addr=0x%x bResult=%d time out 1s !!!\n", __FUNCTION__, addr, bResult);
        RTW_INFO("%s: [ERROR] EFUSE_CTRL =0x%08x !!!\n", __FUNCTION__, rtw_read32(pAdapter, EFUSE_CTRL));
    }
 
    return bResult;
}
 
/*  11/16/2008 MH Write one byte to reald Efuse. */
u8
efuse_OneByteWrite(
    IN    PADAPTER    pAdapter,
    IN    u16            addr,
    IN    u8            data,
    IN    BOOLEAN        bPseudoTest)
{
    u8    tmpidx = 0;
    u8    bResult = _FALSE;
    u32 efuseValue = 0;
    HAL_DATA_TYPE    *pHalData = GET_HAL_DATA(pAdapter);
 
    /* RTW_INFO("===> EFUSE_OneByteWrite(), addr = %x data=%x\n", addr, data); */
    /* RTW_INFO("===> EFUSE_OneByteWrite() start, 0x34 = 0x%X\n", rtw_read32(pAdapter, EFUSE_TEST)); */
 
    if (bPseudoTest) {
        bResult = Efuse_Write1ByteToFakeContent(pAdapter, addr, data);
        return bResult;
    }
 
    Efuse_PowerSwitch(pAdapter, _TRUE, _TRUE);
 
    /* -----------------e-fuse reg ctrl ---------------------------------     */
    /* address             */
 
 
    efuseValue = rtw_read32(pAdapter, EFUSE_CTRL);
    efuseValue |= (BIT21 | BIT31);
    efuseValue &= ~(0x3FFFF);
    efuseValue |= ((addr << 8 | data) & 0x3FFFF);
 
    /* <20130227, Kordan> 8192E MP chip A-cut had better not set 0x34[11] until B-Cut. */
    if (IS_HARDWARE_TYPE_8723B(pAdapter) ||
        (IS_HARDWARE_TYPE_8192E(pAdapter) && (!IS_A_CUT(pHalData->version_id))) ||
        (IS_VENDOR_8188E_I_CUT_SERIES(pAdapter)) || (IS_CHIP_VENDOR_SMIC(pHalData->version_id))
       ) {
        /* <20130121, Kordan> For SMIC EFUSE specificatoin. */
        /* 0x34[11]: SW force PGMEN input of efuse to high. (for the bank selected by 0x34[9:8]) */
        /* phy_set_mac_reg(pAdapter, 0x34, BIT11, 1); */
        rtw_write16(pAdapter, 0x34, rtw_read16(pAdapter, 0x34) | (BIT11));
        rtw_write32(pAdapter, EFUSE_CTRL, 0x90600000 | ((addr << 8 | data)));
    } else
        rtw_write32(pAdapter, EFUSE_CTRL, efuseValue);
 
    rtw_mdelay_os(1);
 
    while ((0x80 &  rtw_read8(pAdapter, EFUSE_CTRL + 3)) && (tmpidx < 100)) {
        rtw_mdelay_os(1);
        tmpidx++;
    }
 
    if (tmpidx < 100)
        bResult = _TRUE;
    else {
        bResult = _FALSE;
        RTW_INFO("%s: [ERROR] addr=0x%x ,efuseValue=0x%x ,bResult=%d time out 1s !!!\n",
             __FUNCTION__, addr, efuseValue, bResult);
        RTW_INFO("%s: [ERROR] EFUSE_CTRL =0x%08x !!!\n", __FUNCTION__, rtw_read32(pAdapter, EFUSE_CTRL));
    }
 
    /* disable Efuse program enable */
    if (IS_HARDWARE_TYPE_8723B(pAdapter) ||
        (IS_HARDWARE_TYPE_8192E(pAdapter) && (!IS_A_CUT(pHalData->version_id))) ||
        (IS_VENDOR_8188E_I_CUT_SERIES(pAdapter)) || (IS_CHIP_VENDOR_SMIC(pHalData->version_id))
       )
        phy_set_mac_reg(pAdapter, EFUSE_TEST, BIT(11), 0);
 
    Efuse_PowerSwitch(pAdapter, _TRUE, _FALSE);
 
    return bResult;
}
 
int
Efuse_PgPacketRead(IN    PADAPTER    pAdapter,
           IN    u8            offset,
           IN    u8            *data,
           IN    BOOLEAN        bPseudoTest)
{
    int    ret = 0;
 
    ret =  pAdapter->hal_func.Efuse_PgPacketRead(pAdapter, offset, data, bPseudoTest);
 
    return ret;
}
 
int
Efuse_PgPacketWrite(IN    PADAPTER    pAdapter,
            IN    u8            offset,
            IN    u8            word_en,
            IN    u8            *data,
            IN    BOOLEAN        bPseudoTest)
{
    int ret;
 
    ret =  pAdapter->hal_func.Efuse_PgPacketWrite(pAdapter, offset, word_en, data, bPseudoTest);
 
    return ret;
}
 
 
int
Efuse_PgPacketWrite_BT(IN    PADAPTER    pAdapter,
               IN    u8            offset,
               IN    u8            word_en,
               IN    u8            *data,
               IN    BOOLEAN        bPseudoTest)
{
    int ret;
 
    ret =  pAdapter->hal_func.Efuse_PgPacketWrite_BT(pAdapter, offset, word_en, data, bPseudoTest);
 
    return ret;
}
 
 
u8
Efuse_WordEnableDataWrite(IN    PADAPTER    pAdapter,
              IN    u16        efuse_addr,
              IN    u8        word_en,
              IN    u8        *data,
              IN    BOOLEAN        bPseudoTest)
{
    u8    ret = 0;
 
    ret =  pAdapter->hal_func.Efuse_WordEnableDataWrite(pAdapter, efuse_addr, word_en, data, bPseudoTest);
 
    return ret;
}
 
static u8 efuse_read8(PADAPTER padapter, u16 address, u8 *value)
{
    return efuse_OneByteRead(padapter, address, value, _FALSE);
}
 
static u8 efuse_write8(PADAPTER padapter, u16 address, u8 *value)
{
    return efuse_OneByteWrite(padapter, address, *value, _FALSE);
}
 
/*
 * read/wirte raw efuse data
 */
u8 rtw_efuse_access(PADAPTER padapter, u8 bWrite, u16 start_addr, u16 cnts, u8 *data)
{
    int i = 0;
    u16    real_content_len = 0, max_available_size = 0;
    u8 res = _FAIL ;
    u8(*rw8)(PADAPTER, u16, u8 *);
    u32    backupRegs[4] = {0};
 
 
    EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI, TYPE_EFUSE_REAL_CONTENT_LEN, (PVOID)&real_content_len, _FALSE);
    EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI, TYPE_AVAILABLE_EFUSE_BYTES_TOTAL, (PVOID)&max_available_size, _FALSE);
 
    if (start_addr > real_content_len)
        return _FAIL;
 
    if (_TRUE == bWrite) {
        if ((start_addr + cnts) > max_available_size)
            return _FAIL;
        rw8 = &efuse_write8;
    } else
        rw8 = &efuse_read8;
 
    efuse_PreUpdateAction(padapter, backupRegs);
 
    Efuse_PowerSwitch(padapter, bWrite, _TRUE);
 
    /* e-fuse one byte read / write */
    for (i = 0; i < cnts; i++) {
        if (start_addr >= real_content_len) {
            res = _FAIL;
            break;
        }
 
        res = rw8(padapter, start_addr++, data++);
        if (_FAIL == res)
            break;
    }
 
    Efuse_PowerSwitch(padapter, bWrite, _FALSE);
 
    efuse_PostUpdateAction(padapter, backupRegs);
 
    return res;
}
/* ------------------------------------------------------------------------------ */
u16 efuse_GetMaxSize(PADAPTER padapter)
{
    u16    max_size;
 
    max_size = 0;
    EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI , TYPE_AVAILABLE_EFUSE_BYTES_TOTAL, (PVOID)&max_size, _FALSE);
    return max_size;
}
/* ------------------------------------------------------------------------------ */
u8 efuse_GetCurrentSize(PADAPTER padapter, u16 *size)
{
    Efuse_PowerSwitch(padapter, _FALSE, _TRUE);
    *size = Efuse_GetCurrentSize(padapter, EFUSE_WIFI, _FALSE);
    Efuse_PowerSwitch(padapter, _FALSE, _FALSE);
 
    return _SUCCESS;
}
/* ------------------------------------------------------------------------------ */
u16 efuse_bt_GetMaxSize(PADAPTER padapter)
{
    u16    max_size;
 
    max_size = 0;
    EFUSE_GetEfuseDefinition(padapter, EFUSE_BT , TYPE_AVAILABLE_EFUSE_BYTES_TOTAL, (PVOID)&max_size, _FALSE);
    return max_size;
}
 
u8 efuse_bt_GetCurrentSize(PADAPTER padapter, u16 *size)
{
    Efuse_PowerSwitch(padapter, _FALSE, _TRUE);
    *size = Efuse_GetCurrentSize(padapter, EFUSE_BT, _FALSE);
    Efuse_PowerSwitch(padapter, _FALSE, _FALSE);
 
    return _SUCCESS;
}
 
u8 rtw_efuse_map_read(PADAPTER padapter, u16 addr, u16 cnts, u8 *data)
{
    u16    mapLen = 0;
 
    EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI, TYPE_EFUSE_MAP_LEN, (PVOID)&mapLen, _FALSE);
 
    if ((addr + cnts) > mapLen)
        return _FAIL;
 
    Efuse_PowerSwitch(padapter, _FALSE, _TRUE);
 
    efuse_ReadEFuse(padapter, EFUSE_WIFI, addr, cnts, data, _FALSE);
 
    Efuse_PowerSwitch(padapter, _FALSE, _FALSE);
 
    return _SUCCESS;
}
 
u8 rtw_BT_efuse_map_read(PADAPTER padapter, u16 addr, u16 cnts, u8 *data)
{
    u16    mapLen = 0;
 
    EFUSE_GetEfuseDefinition(padapter, EFUSE_BT, TYPE_EFUSE_MAP_LEN, (PVOID)&mapLen, _FALSE);
 
    if ((addr + cnts) > mapLen)
        return _FAIL;
 
    Efuse_PowerSwitch(padapter, _FALSE, _TRUE);
 
    efuse_ReadEFuse(padapter, EFUSE_BT, addr, cnts, data, _FALSE);
 
    Efuse_PowerSwitch(padapter, _FALSE, _FALSE);
 
    return _SUCCESS;
}
 
/* ------------------------------------------------------------------------------ */
u8 rtw_efuse_map_write(PADAPTER padapter, u16 addr, u16 cnts, u8 *data)
{
#define RT_ASSERT_RET(expr)                                                \
    if (!(expr)) {                                                            \
        printk("Assertion failed! %s at ......\n", #expr);                            \
        printk("      ......%s,%s, line=%d\n",__FILE__, __FUNCTION__, __LINE__);    \
        return _FAIL;    \
    }
 
    u8    offset, word_en;
    u8    *map;
    u8    newdata[PGPKT_DATA_SIZE];
    s32    i, j, idx, chk_total_byte;
    u8    ret = _SUCCESS;
    u16    mapLen = 0, startAddr = 0, efuse_max_available_len = 0;
    u32    backupRegs[4] = {0};
    HAL_DATA_TYPE    *pHalData = GET_HAL_DATA(padapter);
    PEFUSE_HAL    pEfuseHal = &pHalData->EfuseHal;
 
 
    EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI, TYPE_EFUSE_MAP_LEN, (PVOID)&mapLen, _FALSE);
    EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI, TYPE_AVAILABLE_EFUSE_BYTES_TOTAL, &efuse_max_available_len, _FALSE);
 
    if ((addr + cnts) > mapLen)
        return _FAIL;
 
    RT_ASSERT_RET(PGPKT_DATA_SIZE == 8); /* have to be 8 byte alignment */
    RT_ASSERT_RET((mapLen & 0x7) == 0); /* have to be PGPKT_DATA_SIZE alignment for memcpy */
 
    map = rtw_zmalloc(mapLen);
    if (map == NULL)
        return _FAIL;
 
    _rtw_memset(map, 0xFF, mapLen);
 
    ret = rtw_efuse_map_read(padapter, 0, mapLen, map);
    if (ret == _FAIL)
        goto exit;
 
    if (padapter->registrypriv.boffefusemask == 0) {
        for (i = 0; i < cnts; i++) {
            if (padapter->registrypriv.bFileMaskEfuse == _TRUE) {
                if (rtw_file_efuse_IsMasked(padapter, addr + i))    /*use file efuse mask. */
                    data[i] = map[addr + i];
            } else {
                if (efuse_IsMasked(padapter, addr + i))
                    data[i] = map[addr + i];
            }
            RTW_INFO("%s , data[%d] = %x, map[addr+i]= %x\n", __func__, i, data[i], map[addr + i]);
        }
    }
    /*Efuse_PowerSwitch(padapter, _TRUE, _TRUE);*/
 
    chk_total_byte = 0;
    idx = 0;
    offset = (addr >> 3);
 
    while (idx < cnts) {
        word_en = 0xF;
        j = (addr + idx) & 0x7;
        for (i = j; i < PGPKT_DATA_SIZE && idx < cnts; i++, idx++) {
            if (data[idx] != map[addr + idx])
                word_en &= ~BIT(i >> 1);
        }
 
        if (word_en != 0xF) {
            chk_total_byte += Efuse_CalculateWordCnts(word_en) * 2;
 
            if (offset >= EFUSE_MAX_SECTION_BASE) /* Over EFUSE_MAX_SECTION 16 for 2 ByteHeader */
                chk_total_byte += 2;
            else
                chk_total_byte += 1;
        }
 
        offset++;
    }
 
    RTW_INFO("Total PG bytes Count = %d\n", chk_total_byte);
    rtw_hal_get_hwreg(padapter, HW_VAR_EFUSE_BYTES, (u8 *)&startAddr);
 
    if (startAddr == 0) {
        startAddr = Efuse_GetCurrentSize(padapter, EFUSE_WIFI, _FALSE);
        RTW_INFO("%s: Efuse_GetCurrentSize startAddr=%#X\n", __func__, startAddr);
    }
    RTW_DBG("%s: startAddr=%#X\n", __func__, startAddr);
 
    if ((startAddr + chk_total_byte) >= efuse_max_available_len) {
        RTW_INFO("%s: startAddr(0x%X) + PG data len %d >= efuse_max_available_len(0x%X)\n",
             __func__, startAddr, chk_total_byte, efuse_max_available_len);
        ret = _FAIL;
        goto exit;
    }
 
    efuse_PreUpdateAction(padapter, backupRegs);
 
    idx = 0;
    offset = (addr >> 3);
    while (idx < cnts) {
        word_en = 0xF;
        j = (addr + idx) & 0x7;
        _rtw_memcpy(newdata, &map[offset << 3], PGPKT_DATA_SIZE);
        for (i = j; i < PGPKT_DATA_SIZE && idx < cnts; i++, idx++) {
            if (data[idx] != map[addr + idx]) {
                word_en &= ~BIT(i >> 1);
                newdata[i] = data[idx];
#ifdef CONFIG_RTL8723B
                if (addr + idx == 0x8) {
                    if (IS_C_CUT(pHalData->version_id) || IS_B_CUT(pHalData->version_id)) {
                        if (pHalData->adjuseVoltageVal == 6) {
                            newdata[i] = map[addr + idx];
                            RTW_INFO(" %s ,\n adjuseVoltageVal = %d ,newdata[%d] = %x\n", __func__, pHalData->adjuseVoltageVal, i, newdata[i]);
                        }
                    }
                }
#endif
            }
        }
 
        if (word_en != 0xF) {
            ret = Efuse_PgPacketWrite(padapter, offset, word_en, newdata, _FALSE);
            RTW_INFO("offset=%x\n", offset);
            RTW_INFO("word_en=%x\n", word_en);
 
            for (i = 0; i < PGPKT_DATA_SIZE; i++)
                RTW_INFO("data=%x \t", newdata[i]);
            if (ret == _FAIL)
                break;
        }
 
        offset++;
    }
 
    /*Efuse_PowerSwitch(padapter, _TRUE, _FALSE);*/
 
    efuse_PostUpdateAction(padapter, backupRegs);
 
exit:
 
    rtw_mfree(map, mapLen);
 
    return ret;
}
 
 
u8 rtw_BT_efuse_map_write(PADAPTER padapter, u16 addr, u16 cnts, u8 *data)
{
#define RT_ASSERT_RET(expr)                                                \
    if (!(expr)) {                                                            \
        printk("Assertion failed! %s at ......\n", #expr);                            \
        printk("      ......%s,%s, line=%d\n",__FILE__, __FUNCTION__, __LINE__);    \
        return _FAIL;    \
    }
 
    u8    offset, word_en;
    u8    *map;
    u8    newdata[PGPKT_DATA_SIZE];
    s32    i = 0, j = 0, idx;
    u8    ret = _SUCCESS;
    u16    mapLen = 0;
 
    EFUSE_GetEfuseDefinition(padapter, EFUSE_BT, TYPE_EFUSE_MAP_LEN, (PVOID)&mapLen, _FALSE);
 
    if ((addr + cnts) > mapLen)
        return _FAIL;
 
    RT_ASSERT_RET(PGPKT_DATA_SIZE == 8); /* have to be 8 byte alignment */
    RT_ASSERT_RET((mapLen & 0x7) == 0); /* have to be PGPKT_DATA_SIZE alignment for memcpy */
 
    map = rtw_zmalloc(mapLen);
    if (map == NULL)
        return _FAIL;
 
    ret = rtw_BT_efuse_map_read(padapter, 0, mapLen, map);
    if (ret == _FAIL)
        goto exit;
    RTW_INFO("OFFSET\tVALUE(hex)\n");
    for (i = 0; i < 1024; i += 16) { /* set 512 because the iwpriv's extra size have limit 0x7FF */
        RTW_INFO("0x%03x\t", i);
        for (j = 0; j < 8; j++)
            RTW_INFO("%02X ", map[i + j]);
        RTW_INFO("\t");
        for (; j < 16; j++)
            RTW_INFO("%02X ", map[i + j]);
        RTW_INFO("\n");
    }
    RTW_INFO("\n");
    Efuse_PowerSwitch(padapter, _TRUE, _TRUE);
 
    idx = 0;
    offset = (addr >> 3);
    while (idx < cnts) {
        word_en = 0xF;
        j = (addr + idx) & 0x7;
        _rtw_memcpy(newdata, &map[offset << 3], PGPKT_DATA_SIZE);
        for (i = j; i < PGPKT_DATA_SIZE && idx < cnts; i++, idx++) {
            if (data[idx] != map[addr + idx]) {
                word_en &= ~BIT(i >> 1);
                newdata[i] = data[idx];
            }
        }
 
        if (word_en != 0xF) {
            RTW_INFO("offset=%x\n", offset);
            RTW_INFO("word_en=%x\n", word_en);
            RTW_INFO("%s: data=", __FUNCTION__);
            for (i = 0; i < PGPKT_DATA_SIZE; i++)
                RTW_INFO("0x%02X ", newdata[i]);
            RTW_INFO("\n");
            ret = Efuse_PgPacketWrite_BT(padapter, offset, word_en, newdata, _FALSE);
            if (ret == _FAIL)
                break;
        }
 
        offset++;
    }
 
    Efuse_PowerSwitch(padapter, _TRUE, _FALSE);
 
exit:
 
    rtw_mfree(map, mapLen);
 
    return ret;
}
 
/*-----------------------------------------------------------------------------
 * Function:    Efuse_ReadAllMap
 *
 * Overview:    Read All Efuse content
 *
 * Input:       NONE
 *
 * Output:      NONE
 *
 * Return:      NONE
 *
 * Revised History:
 * When            Who        Remark
 * 11/11/2008    MHC        Create Version 0.
 *
 *---------------------------------------------------------------------------*/
VOID
Efuse_ReadAllMap(
    IN        PADAPTER    pAdapter,
    IN        u8        efuseType,
    IN OUT    u8        *Efuse,
    IN        BOOLEAN        bPseudoTest);
VOID
Efuse_ReadAllMap(
    IN        PADAPTER    pAdapter,
    IN        u8        efuseType,
    IN OUT    u8        *Efuse,
    IN        BOOLEAN        bPseudoTest)
{
    u16    mapLen = 0;
 
    Efuse_PowerSwitch(pAdapter, _FALSE, _TRUE);
 
    EFUSE_GetEfuseDefinition(pAdapter, efuseType, TYPE_EFUSE_MAP_LEN, (PVOID)&mapLen, bPseudoTest);
 
    efuse_ReadEFuse(pAdapter, efuseType, 0, mapLen, Efuse, bPseudoTest);
 
    Efuse_PowerSwitch(pAdapter, _FALSE, _FALSE);
}
 
/*-----------------------------------------------------------------------------
 * Function:    efuse_ShadowRead1Byte
 *            efuse_ShadowRead2Byte
 *            efuse_ShadowRead4Byte
 *
 * Overview:    Read from efuse init map by one/two/four bytes !!!!!
 *
 * Input:       NONE
 *
 * Output:      NONE
 *
 * Return:      NONE
 *
 * Revised History:
 * When            Who        Remark
 * 11/12/2008    MHC        Create Version 0.
 *
 *---------------------------------------------------------------------------*/
static VOID
efuse_ShadowRead1Byte(
    IN    PADAPTER    pAdapter,
    IN    u16        Offset,
    IN OUT    u8        *Value)
{
    PHAL_DATA_TYPE pHalData = GET_HAL_DATA(pAdapter);
 
    *Value = pHalData->efuse_eeprom_data[Offset];
 
}    /* EFUSE_ShadowRead1Byte */
 
/* ---------------Read Two Bytes */
static VOID
efuse_ShadowRead2Byte(
    IN    PADAPTER    pAdapter,
    IN    u16        Offset,
    IN OUT    u16        *Value)
{
    PHAL_DATA_TYPE pHalData = GET_HAL_DATA(pAdapter);
 
    *Value = pHalData->efuse_eeprom_data[Offset];
    *Value |= pHalData->efuse_eeprom_data[Offset + 1] << 8;
 
}    /* EFUSE_ShadowRead2Byte */
 
/* ---------------Read Four Bytes */
static VOID
efuse_ShadowRead4Byte(
    IN    PADAPTER    pAdapter,
    IN    u16        Offset,
    IN OUT    u32        *Value)
{
    PHAL_DATA_TYPE pHalData = GET_HAL_DATA(pAdapter);
 
    *Value = pHalData->efuse_eeprom_data[Offset];
    *Value |= pHalData->efuse_eeprom_data[Offset + 1] << 8;
    *Value |= pHalData->efuse_eeprom_data[Offset + 2] << 16;
    *Value |= pHalData->efuse_eeprom_data[Offset + 3] << 24;
 
}    /* efuse_ShadowRead4Byte */
 
 
/*-----------------------------------------------------------------------------
 * Function:    efuse_ShadowWrite1Byte
 *            efuse_ShadowWrite2Byte
 *            efuse_ShadowWrite4Byte
 *
 * Overview:    Write efuse modify map by one/two/four byte.
 *
 * Input:       NONE
 *
 * Output:      NONE
 *
 * Return:      NONE
 *
 * Revised History:
 * When            Who        Remark
 * 11/12/2008    MHC        Create Version 0.
 *
 *---------------------------------------------------------------------------*/
#ifdef PLATFORM
static VOID
efuse_ShadowWrite1Byte(
    IN    PADAPTER    pAdapter,
    IN    u16        Offset,
    IN    u8        Value);
#endif /* PLATFORM */
static VOID
efuse_ShadowWrite1Byte(
    IN    PADAPTER    pAdapter,
    IN    u16        Offset,
    IN    u8        Value)
{
    PHAL_DATA_TYPE pHalData = GET_HAL_DATA(pAdapter);
 
    pHalData->efuse_eeprom_data[Offset] = Value;
 
}    /* efuse_ShadowWrite1Byte */
 
/* ---------------Write Two Bytes */
static VOID
efuse_ShadowWrite2Byte(
    IN    PADAPTER    pAdapter,
    IN    u16        Offset,
    IN    u16        Value)
{
 
    PHAL_DATA_TYPE pHalData = GET_HAL_DATA(pAdapter);
 
 
    pHalData->efuse_eeprom_data[Offset] = Value & 0x00FF;
    pHalData->efuse_eeprom_data[Offset + 1] = Value >> 8;
 
}    /* efuse_ShadowWrite1Byte */
 
/* ---------------Write Four Bytes */
static VOID
efuse_ShadowWrite4Byte(
    IN    PADAPTER    pAdapter,
    IN    u16        Offset,
    IN    u32        Value)
{
    PHAL_DATA_TYPE pHalData = GET_HAL_DATA(pAdapter);
 
    pHalData->efuse_eeprom_data[Offset] = (u8)(Value & 0x000000FF);
    pHalData->efuse_eeprom_data[Offset + 1] = (u8)((Value >> 8) & 0x0000FF);
    pHalData->efuse_eeprom_data[Offset + 2] = (u8)((Value >> 16) & 0x00FF);
    pHalData->efuse_eeprom_data[Offset + 3] = (u8)((Value >> 24) & 0xFF);
 
}    /* efuse_ShadowWrite1Byte */
 
 
/*-----------------------------------------------------------------------------
 * Function:    EFUSE_ShadowRead
 *
 * Overview:    Read from efuse init map !!!!!
 *
 * Input:       NONE
 *
 * Output:      NONE
 *
 * Return:      NONE
 *
 * Revised History:
 * When            Who        Remark
 * 11/12/2008    MHC        Create Version 0.
 *
 *---------------------------------------------------------------------------*/
void
EFUSE_ShadowRead(
    IN        PADAPTER    pAdapter,
    IN        u8        Type,
    IN        u16        Offset,
    IN OUT    u32        *Value)
{
    if (Type == 1)
        efuse_ShadowRead1Byte(pAdapter, Offset, (u8 *)Value);
    else if (Type == 2)
        efuse_ShadowRead2Byte(pAdapter, Offset, (u16 *)Value);
    else if (Type == 4)
        efuse_ShadowRead4Byte(pAdapter, Offset, (u32 *)Value);
 
}    /* EFUSE_ShadowRead */
 
/*-----------------------------------------------------------------------------
 * Function:    EFUSE_ShadowWrite
 *
 * Overview:    Write efuse modify map for later update operation to use!!!!!
 *
 * Input:       NONE
 *
 * Output:      NONE
 *
 * Return:      NONE
 *
 * Revised History:
 * When            Who        Remark
 * 11/12/2008    MHC        Create Version 0.
 *
 *---------------------------------------------------------------------------*/
VOID
EFUSE_ShadowWrite(
    IN    PADAPTER    pAdapter,
    IN    u8        Type,
    IN    u16        Offset,
    IN OUT    u32        Value);
VOID
EFUSE_ShadowWrite(
    IN    PADAPTER    pAdapter,
    IN    u8        Type,
    IN    u16        Offset,
    IN OUT    u32        Value)
{
#if (MP_DRIVER == 0)
    return;
#endif
    if (pAdapter->registrypriv.mp_mode == 0)
        return;
 
 
    if (Type == 1)
        efuse_ShadowWrite1Byte(pAdapter, Offset, (u8)Value);
    else if (Type == 2)
        efuse_ShadowWrite2Byte(pAdapter, Offset, (u16)Value);
    else if (Type == 4)
        efuse_ShadowWrite4Byte(pAdapter, Offset, (u32)Value);
 
}    /* EFUSE_ShadowWrite */
 
VOID
Efuse_InitSomeVar(
    IN        PADAPTER    pAdapter
);
VOID
Efuse_InitSomeVar(
    IN        PADAPTER    pAdapter
)
{
    u8 i;
 
    _rtw_memset((PVOID)&fakeEfuseContent[0], 0xff, EFUSE_MAX_HW_SIZE);
    _rtw_memset((PVOID)&fakeEfuseInitMap[0], 0xff, EFUSE_MAX_MAP_LEN);
    _rtw_memset((PVOID)&fakeEfuseModifiedMap[0], 0xff, EFUSE_MAX_MAP_LEN);
 
    for (i = 0; i < EFUSE_MAX_BT_BANK; i++)
        _rtw_memset((PVOID)&BTEfuseContent[i][0], EFUSE_MAX_HW_SIZE, 0xff);
    _rtw_memset((PVOID)&BTEfuseInitMap[0], 0xff, EFUSE_BT_MAX_MAP_LEN);
    _rtw_memset((PVOID)&BTEfuseModifiedMap[0], 0xff, EFUSE_BT_MAX_MAP_LEN);
 
    for (i = 0; i < EFUSE_MAX_BT_BANK; i++)
        _rtw_memset((PVOID)&fakeBTEfuseContent[i][0], 0xff, EFUSE_MAX_HW_SIZE);
    _rtw_memset((PVOID)&fakeBTEfuseInitMap[0], 0xff, EFUSE_BT_MAX_MAP_LEN);
    _rtw_memset((PVOID)&fakeBTEfuseModifiedMap[0], 0xff, EFUSE_BT_MAX_MAP_LEN);
}
#endif /* !RTW_HALMAC */
/*  11/16/2008 MH Add description. Get current efuse area enabled word!!. */
u8
Efuse_CalculateWordCnts(IN u8    word_en)
{
    u8 word_cnts = 0;
    if (!(word_en & BIT(0)))
        word_cnts++; /* 0 : write enable */
    if (!(word_en & BIT(1)))
        word_cnts++;
    if (!(word_en & BIT(2)))
        word_cnts++;
    if (!(word_en & BIT(3)))
        word_cnts++;
    return word_cnts;
}
 
/*-----------------------------------------------------------------------------
 * Function:    efuse_WordEnableDataRead
 *
 * Overview:    Read allowed word in current efuse section data.
 *
 * Input:       NONE
 *
 * Output:      NONE
 *
 * Return:      NONE
 *
 * Revised History:
 * When            Who        Remark
 * 11/16/2008    MHC        Create Version 0.
 * 11/21/2008    MHC        Fix Write bug when we only enable late word.
 *
 *---------------------------------------------------------------------------*/
void
efuse_WordEnableDataRead(IN    u8    word_en,
             IN    u8    *sourdata,
             IN    u8    *targetdata)
{
    if (!(word_en & BIT(0))) {
        targetdata[0] = sourdata[0];
        targetdata[1] = sourdata[1];
    }
    if (!(word_en & BIT(1))) {
        targetdata[2] = sourdata[2];
        targetdata[3] = sourdata[3];
    }
    if (!(word_en & BIT(2))) {
        targetdata[4] = sourdata[4];
        targetdata[5] = sourdata[5];
    }
    if (!(word_en & BIT(3))) {
        targetdata[6] = sourdata[6];
        targetdata[7] = sourdata[7];
    }
}
 
/*-----------------------------------------------------------------------------
 * Function:    EFUSE_ShadowMapUpdate
 *
 * Overview:    Transfer current EFUSE content to shadow init and modify map.
 *
 * Input:       NONE
 *
 * Output:      NONE
 *
 * Return:      NONE
 *
 * Revised History:
 * When            Who        Remark
 * 11/13/2008    MHC        Create Version 0.
 *
 *---------------------------------------------------------------------------*/
void EFUSE_ShadowMapUpdate(
    IN PADAPTER    pAdapter,
    IN u8        efuseType,
    IN BOOLEAN    bPseudoTest)
{
    PHAL_DATA_TYPE pHalData = GET_HAL_DATA(pAdapter);
    u16    mapLen = 0;
#ifdef RTW_HALMAC
    u8 *efuse_map = NULL;
    int err;
 
 
    mapLen = EEPROM_MAX_SIZE;
    efuse_map = pHalData->efuse_eeprom_data;
    /* efuse default content is 0xFF */
    _rtw_memset(efuse_map, 0xFF, EEPROM_MAX_SIZE);
 
    EFUSE_GetEfuseDefinition(pAdapter, efuseType, TYPE_EFUSE_MAP_LEN, (PVOID)&mapLen, bPseudoTest);
    if (!mapLen) {
        RTW_WARN("%s: <ERROR> fail to get efuse size!\n", __FUNCTION__);
        mapLen = EEPROM_MAX_SIZE;
    }
    if (mapLen > EEPROM_MAX_SIZE) {
        RTW_WARN("%s: <ERROR> size of efuse data(%d) is large than expected(%d)!\n",
             __FUNCTION__, mapLen, EEPROM_MAX_SIZE);
        mapLen = EEPROM_MAX_SIZE;
    }
 
    if (pHalData->bautoload_fail_flag == _FALSE) {
        err = rtw_halmac_read_logical_efuse_map(adapter_to_dvobj(pAdapter), efuse_map, mapLen);
        if (err)
            RTW_ERR("%s: <ERROR> fail to get efuse map!\n", __FUNCTION__);
    }
#else /* !RTW_HALMAC */
    EFUSE_GetEfuseDefinition(pAdapter, efuseType, TYPE_EFUSE_MAP_LEN, (PVOID)&mapLen, bPseudoTest);
 
    if (pHalData->bautoload_fail_flag == _TRUE)
        _rtw_memset(pHalData->efuse_eeprom_data, 0xFF, mapLen);
    else {
#ifdef CONFIG_ADAPTOR_INFO_CACHING_FILE
        if (_SUCCESS != retriveAdaptorInfoFile(pAdapter->registrypriv.adaptor_info_caching_file_path, pHalData->efuse_eeprom_data)) {
#endif
 
            Efuse_ReadAllMap(pAdapter, efuseType, pHalData->efuse_eeprom_data, bPseudoTest);
 
#ifdef CONFIG_ADAPTOR_INFO_CACHING_FILE
            storeAdaptorInfoFile(pAdapter->registrypriv.adaptor_info_caching_file_path, pHalData->efuse_eeprom_data);
        }
#endif
    }
 
    /* PlatformMoveMemory((PVOID)&pHalData->EfuseMap[EFUSE_MODIFY_MAP][0], */
    /* (PVOID)&pHalData->EfuseMap[EFUSE_INIT_MAP][0], mapLen); */
#endif /* !RTW_HALMAC */
 
    rtw_dump_cur_efuse(pAdapter);
} /* EFUSE_ShadowMapUpdate */
 
const u8 _mac_hidden_max_bw_to_hal_bw_cap[MAC_HIDDEN_MAX_BW_NUM] = {
    0,
    0,
    (BW_CAP_160M | BW_CAP_80M | BW_CAP_40M | BW_CAP_20M | BW_CAP_10M | BW_CAP_5M),
    (BW_CAP_5M),
    (BW_CAP_10M | BW_CAP_5M),
    (BW_CAP_20M | BW_CAP_10M | BW_CAP_5M),
    (BW_CAP_40M | BW_CAP_20M | BW_CAP_10M | BW_CAP_5M),
    (BW_CAP_80M | BW_CAP_40M | BW_CAP_20M | BW_CAP_10M | BW_CAP_5M),
};
 
const u8 _mac_hidden_proto_to_hal_proto_cap[MAC_HIDDEN_PROTOCOL_NUM] = {
    0,
    0,
    (PROTO_CAP_11N | PROTO_CAP_11G | PROTO_CAP_11B),
    (PROTO_CAP_11AC | PROTO_CAP_11N | PROTO_CAP_11G | PROTO_CAP_11B),
};
 
u8 mac_hidden_wl_func_to_hal_wl_func(u8 func)
{
    u8 wl_func = 0;
 
    if (func & BIT0)
        wl_func |= WL_FUNC_MIRACAST;
    if (func & BIT1)
        wl_func |= WL_FUNC_P2P;
    if (func & BIT2)
        wl_func |= WL_FUNC_TDLS;
    if (func & BIT3)
        wl_func |= WL_FUNC_FTM;
 
    return wl_func;
}
 
#ifdef PLATFORM_LINUX
#ifdef CONFIG_ADAPTOR_INFO_CACHING_FILE
/* #include <rtw_eeprom.h> */
 
int isAdaptorInfoFileValid(void)
{
    return _TRUE;
}
 
int storeAdaptorInfoFile(char *path, u8 *efuse_data)
{
    int ret = _SUCCESS;
 
    if (path && efuse_data) {
        ret = rtw_store_to_file(path, efuse_data, EEPROM_MAX_SIZE_512);
        if (ret == EEPROM_MAX_SIZE)
            ret = _SUCCESS;
        else
            ret = _FAIL;
    } else {
        RTW_INFO("%s NULL pointer\n", __FUNCTION__);
        ret =  _FAIL;
    }
    return ret;
}
 
int retriveAdaptorInfoFile(char *path, u8 *efuse_data)
{
    int ret = _SUCCESS;
    mm_segment_t oldfs;
    struct file *fp;
 
    if (path && efuse_data) {
 
        ret = rtw_retrieve_from_file(path, efuse_data, EEPROM_MAX_SIZE);
 
        if (ret == EEPROM_MAX_SIZE)
            ret = _SUCCESS;
        else
            ret = _FAIL;
 
#if 0
        if (isAdaptorInfoFileValid())
            return 0;
        else
            return _FAIL;
#endif
 
    } else {
        RTW_INFO("%s NULL pointer\n", __FUNCTION__);
        ret = _FAIL;
    }
    return ret;
}
#endif /* CONFIG_ADAPTOR_INFO_CACHING_FILE */
 
u8 rtw_efuse_file_read(PADAPTER padapter, u8 *filepatch, u8 *buf, u32 len)
{
    char *ptmpbuf = NULL, *ptr;
    u8 val8;
    u32 count, i, j;
    int err;
    u32 bufsize = 4096;
 
    ptmpbuf = rtw_zmalloc(bufsize);
    if (ptmpbuf == NULL)
        return _FALSE;
 
    count = rtw_retrieve_from_file(filepatch, ptmpbuf, bufsize);
    if (count <= 100) {
        rtw_mfree(ptmpbuf, bufsize);
        RTW_ERR("%s, filepatch %s, size=%d, FAIL!!\n", __FUNCTION__, filepatch, count);
        return _FALSE;
    }
 
    i = 0;
    j = 0;
    ptr = ptmpbuf;
    while ((j < len) && (i < count)) {
        if (ptmpbuf[i] == '\0')
            break;
    
        ptr = strpbrk(&ptmpbuf[i], " \t\n\r");
        if (ptr) {
            if (ptr == &ptmpbuf[i]) {
                i++;
                continue;
            }
 
            /* Add string terminating null */
            *ptr = 0;
        } else {
            ptr = &ptmpbuf[count-1];
        }
 
        err = sscanf(&ptmpbuf[i], "%hhx", &val8);
        if (err != 1) {
            RTW_WARN("Something wrong to parse efuse file, string=%s\n", &ptmpbuf[i]);
        } else {
            buf[j] = val8;
            RTW_DBG("i=%d, j=%d, 0x%02x\n", i, j, buf[j]);
            j++;
        }
 
        i = ptr - ptmpbuf + 1;
    }
 
    rtw_mfree(ptmpbuf, bufsize);
    RTW_INFO("%s, filepatch %s, size=%d, done\n", __FUNCTION__, filepatch, count);
    return _TRUE;
}
 
#ifdef CONFIG_EFUSE_CONFIG_FILE
u32 rtw_read_efuse_from_file(const char *path, u8 *buf, int map_size)
{
    u32 i;
    u8 c;
    u8 temp[3];
    u8 temp_i;
    u8 end = _FALSE;
    u32 ret = _FAIL;
 
    u8 *file_data = NULL;
    u32 file_size, read_size, pos = 0;
    u8 *map = NULL;
 
    if (rtw_is_file_readable_with_size(path, &file_size) != _TRUE) {
        RTW_PRINT("%s %s is not readable\n", __func__, path);
        goto exit;
    }
 
    file_data = rtw_vmalloc(file_size);
    if (!file_data) {
        RTW_ERR("%s rtw_vmalloc(%d) fail\n", __func__, file_size);
        goto exit;
    }
 
    read_size = rtw_retrieve_from_file(path, file_data, file_size);
    if (read_size == 0) {
        RTW_ERR("%s read from %s fail\n", __func__, path);
        goto exit;
    }
 
    map = rtw_vmalloc(map_size);
    if (!map) {
        RTW_ERR("%s rtw_vmalloc(%d) fail\n", __func__, map_size);
        goto exit;
    }
    _rtw_memset(map, 0xff, map_size);
 
    temp[2] = 0; /* end of string '\0' */
 
    for (i = 0 ; i < map_size ; i++) {
        temp_i = 0;
 
        while (1) {
            if (pos >= read_size) {
                end = _TRUE;
                break;
            }
            c = file_data[pos++];
 
            /* bypass spece or eol or null before first hex digit */
            if (temp_i == 0 && (is_eol(c) == _TRUE || is_space(c) == _TRUE || is_null(c) == _TRUE))
                continue;
 
            if (IsHexDigit(c) == _FALSE) {
                RTW_ERR("%s invalid 8-bit hex format for offset:0x%03x\n", __func__, i);
                goto exit;
            }
 
            temp[temp_i++] = c;
 
            if (temp_i == 2) {
                /* parse value */
                if (sscanf(temp, "%hhx", &map[i]) != 1) {
                    RTW_ERR("%s sscanf fail for offset:0x%03x\n", __func__, i);
                    goto exit;
                }
                break;
            }
        }
 
        if (end == _TRUE) {
            if (temp_i != 0) {
                RTW_ERR("%s incomplete 8-bit hex format for offset:0x%03x\n", __func__, i);
                goto exit;
            }
            break;
        }
    }
 
    RTW_PRINT("efuse file:%s, 0x%03x byte content read\n", path, i);
 
    _rtw_memcpy(buf, map, map_size);
 
    ret = _SUCCESS;
 
exit:
    if (file_data)
        rtw_vmfree(file_data, file_size);
    if (map)
        rtw_vmfree(map, map_size);
 
    return ret;
}
 
u32 rtw_read_macaddr_from_file(const char *path, u8 *buf)
{
    u32 i;
    u8 temp[3];
    u32 ret = _FAIL;
 
    u8 file_data[17];
    u32 read_size, pos = 0;
    u8 addr[ETH_ALEN];
 
    if (rtw_is_file_readable(path) != _TRUE) {
        RTW_PRINT("%s %s is not readable\n", __func__, path);
        goto exit;
    }
 
    read_size = rtw_retrieve_from_file(path, file_data, 17);
    if (read_size != 17) {
        RTW_ERR("%s read from %s fail\n", __func__, path);
        goto exit;
    }
 
    temp[2] = 0; /* end of string '\0' */
 
    for (i = 0 ; i < ETH_ALEN ; i++) {
        if (IsHexDigit(file_data[i * 3]) == _FALSE || IsHexDigit(file_data[i * 3 + 1]) == _FALSE) {
            RTW_ERR("%s invalid 8-bit hex format for address offset:%u\n", __func__, i);
            goto exit;
        }
 
        if (i < ETH_ALEN - 1 && file_data[i * 3 + 2] != ':') {
            RTW_ERR("%s invalid separator after address offset:%u\n", __func__, i);
            goto exit;
        }
 
        temp[0] = file_data[i * 3];
        temp[1] = file_data[i * 3 + 1];
        if (sscanf(temp, "%hhx", &addr[i]) != 1) {
            RTW_ERR("%s sscanf fail for address offset:0x%03x\n", __func__, i);
            goto exit;
        }
    }
 
    _rtw_memcpy(buf, addr, ETH_ALEN);
 
    RTW_PRINT("wifi_mac file: %s\n", path);
#ifdef CONFIG_RTW_DEBUG
    RTW_INFO(MAC_FMT"\n", MAC_ARG(buf));
#endif
 
    ret = _SUCCESS;
 
exit:
    return ret;
}
#endif /* CONFIG_EFUSE_CONFIG_FILE */
 
#endif /* PLATFORM_LINUX */