hc
2024-05-08 f309769f8af08599af39b6de4f675784ce76530d
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
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
/******************************************************************************
 *
 * Copyright(c) 2016 - 2019 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.
 *
 ******************************************************************************/
 
#include "halmac_common_88xx.h"
#include "halmac_88xx_cfg.h"
#include "halmac_init_88xx.h"
#include "halmac_cfg_wmac_88xx.h"
#include "halmac_efuse_88xx.h"
#include "halmac_bb_rf_88xx.h"
#if HALMAC_USB_SUPPORT
#include "halmac_usb_88xx.h"
#endif
#if HALMAC_SDIO_SUPPORT
#include "halmac_sdio_88xx.h"
#endif
#if HALMAC_PCIE_SUPPORT
#include "halmac_pcie_88xx.h"
#endif
#include "halmac_mimo_88xx.h"
 
#if HALMAC_88XX_SUPPORT
 
#define CFG_PARAM_H2C_INFO_SIZE    12
#define ORIGINAL_H2C_CMD_SIZE    8
 
#define WLHDR_PROT_VER    0
 
#define WLHDR_TYPE_MGMT        0
#define WLHDR_TYPE_CTRL        1
#define WLHDR_TYPE_DATA        2
 
/* mgmt frame */
#define WLHDR_SUB_TYPE_ASSOC_REQ    0
#define WLHDR_SUB_TYPE_ASSOC_RSPNS    1
#define WLHDR_SUB_TYPE_REASSOC_REQ    2
#define WLHDR_SUB_TYPE_REASSOC_RSPNS    3
#define WLHDR_SUB_TYPE_PROBE_REQ    4
#define WLHDR_SUB_TYPE_PROBE_RSPNS    5
#define WLHDR_SUB_TYPE_BCN        8
#define WLHDR_SUB_TYPE_DISASSOC        10
#define WLHDR_SUB_TYPE_AUTH        11
#define WLHDR_SUB_TYPE_DEAUTH        12
#define WLHDR_SUB_TYPE_ACTION        13
#define WLHDR_SUB_TYPE_ACTION_NOACK    14
 
/* ctrl frame */
#define WLHDR_SUB_TYPE_BF_RPT_POLL    4
#define WLHDR_SUB_TYPE_NDPA        5
 
/* data frame */
#define WLHDR_SUB_TYPE_DATA        0
#define WLHDR_SUB_TYPE_NULL        4
#define WLHDR_SUB_TYPE_QOS_DATA        8
#define WLHDR_SUB_TYPE_QOS_NULL        12
 
#define LTECOEX_ACCESS_CTRL REG_WL2LTECOEX_INDIRECT_ACCESS_CTRL_V1
 
struct wlhdr_frame_ctrl {
   u16 protocol:2;
   u16 type:2;
   u16 sub_type:4;
   u16 to_ds:1;
   u16 from_ds:1;
   u16 more_frag:1;
   u16 retry:1;
   u16 pwr_mgmt:1;
   u16 more_data:1;
   u16 protect_frame:1;
   u16 order:1;
};
 
static enum halmac_ret_status
parse_c2h_pkt_88xx(struct halmac_adapter *adapter, u8 *buf, u32 size);
 
static enum halmac_ret_status
get_c2h_dbg_88xx(struct halmac_adapter *adapter, u8 *buf, u32 size);
 
static enum halmac_ret_status
get_h2c_ack_88xx(struct halmac_adapter *adapter, u8 *buf, u32 size);
 
static enum halmac_ret_status
get_scan_ch_notify_88xx(struct halmac_adapter *adapter, u8 *buf, u32 size);
 
static enum halmac_ret_status
get_scan_rpt_88xx(struct halmac_adapter *adapter, u8 *buf, u32 size);
 
static enum halmac_ret_status
get_h2c_ack_cfg_param_88xx(struct halmac_adapter *adapter, u8 *buf, u32 size);
 
static enum halmac_ret_status
get_h2c_ack_update_pkt_88xx(struct halmac_adapter *adapter, u8 *buf, u32 size);
 
static enum halmac_ret_status
get_h2c_ack_send_scan_pkt_88xx(struct halmac_adapter *adapter, u8 *buf,
                  u32 size);
 
static enum halmac_ret_status
get_h2c_ack_drop_scan_pkt_88xx(struct halmac_adapter *adapter, u8 *buf,
                  u32 size);
 
static enum halmac_ret_status
get_h2c_ack_update_datapkt_88xx(struct halmac_adapter *adapter, u8 *buf,
               u32 size);
 
static enum halmac_ret_status
get_h2c_ack_run_datapkt_88xx(struct halmac_adapter *adapter, u8 *buf, u32 size);
 
static enum halmac_ret_status
get_h2c_ack_ch_switch_88xx(struct halmac_adapter *adapter, u8 *buf, u32 size);
 
static enum halmac_ret_status
malloc_cfg_param_buf_88xx(struct halmac_adapter *adapter, u8 full_fifo);
 
static enum halmac_cmd_construct_state
cfg_param_cmd_cnstr_state_88xx(struct halmac_adapter *adapter);
 
static enum halmac_ret_status
proc_cfg_param_88xx(struct halmac_adapter *adapter,
           struct halmac_phy_parameter_info *param, u8 full_fifo);
 
static enum halmac_ret_status
send_cfg_param_h2c_88xx(struct halmac_adapter *adapter);
 
static enum halmac_ret_status
cnv_cfg_param_state_88xx(struct halmac_adapter *adapter,
            enum halmac_cmd_construct_state dest_state);
 
static enum halmac_ret_status
add_param_buf_88xx(struct halmac_adapter *adapter,
          struct halmac_phy_parameter_info *param, u8 *buf,
          u8 *end_cmd);
 
static enum halmac_ret_status
gen_cfg_param_h2c_88xx(struct halmac_adapter *adapter, u8 *buff);
 
static enum halmac_ret_status
send_h2c_update_packet_88xx(struct halmac_adapter *adapter,
               enum halmac_packet_id pkt_id, u8 *pkt, u32 size);
 
static enum halmac_ret_status
send_h2c_send_scan_packet_88xx(struct halmac_adapter *adapter,
                  u8 index, u8 *pkt, u32 size);
 
static enum halmac_ret_status
send_h2c_drop_scan_packet_88xx(struct halmac_adapter *adapter,
                  struct halmac_drop_pkt_option *option);
 
static enum halmac_ret_status
send_bt_coex_cmd_88xx(struct halmac_adapter *adapter, u8 *buf, u32 size,
             u8 ack);
 
static enum halmac_ret_status
read_buf_88xx(struct halmac_adapter *adapter, u32 offset, u32 size,
         enum hal_fifo_sel sel, u8 *data);
 
static enum halmac_cmd_construct_state
scan_cmd_cnstr_state_88xx(struct halmac_adapter *adapter);
 
static enum halmac_ret_status
cnv_scan_state_88xx(struct halmac_adapter *adapter,
           enum halmac_cmd_construct_state dest_state);
 
static enum halmac_ret_status
proc_ctrl_ch_switch_88xx(struct halmac_adapter *adapter,
            struct halmac_ch_switch_option *opt);
 
static enum halmac_ret_status
proc_p2pps_88xx(struct halmac_adapter *adapter, struct halmac_p2pps *info);
 
static enum halmac_ret_status
get_cfg_param_status_88xx(struct halmac_adapter *adapter,
             enum halmac_cmd_process_status *proc_status);
 
static enum halmac_ret_status
get_ch_switch_status_88xx(struct halmac_adapter *adapter,
             enum halmac_cmd_process_status *proc_status);
 
static enum halmac_ret_status
get_update_packet_status_88xx(struct halmac_adapter *adapter,
                 enum halmac_cmd_process_status *proc_status);
 
static enum halmac_ret_status
get_send_scan_packet_status_88xx(struct halmac_adapter *adapter,
                enum halmac_cmd_process_status *proc_status);
 
static enum halmac_ret_status
get_drop_scan_packet_status_88xx(struct halmac_adapter *adapter,
                enum halmac_cmd_process_status *proc_status);
 
static enum halmac_ret_status
pwr_sub_seq_parser_88xx(struct halmac_adapter *adapter, u8 cut, u8 intf,
           struct halmac_wlan_pwr_cfg *cmd);
 
static void
pwr_state_88xx(struct halmac_adapter *adapter, enum halmac_mac_power *state);
 
static enum halmac_ret_status
pwr_cmd_polling_88xx(struct halmac_adapter *adapter,
            struct halmac_wlan_pwr_cfg *cmd);
 
static void
get_pq_mapping_88xx(struct halmac_adapter *adapter,
           struct halmac_rqpn_map *mapping);
 
static void
dump_reg_sdio_88xx(struct halmac_adapter *adapter);
 
static enum halmac_ret_status
wlhdr_valid_88xx(struct halmac_adapter *adapter, u8 *buf);
 
static u8
wlhdr_mgmt_valid_88xx(struct halmac_adapter *adapter,
             struct wlhdr_frame_ctrl *wlhdr);
 
static u8
wlhdr_ctrl_valid_88xx(struct halmac_adapter *adapter,
             struct wlhdr_frame_ctrl *wlhdr);
 
static u8
wlhdr_data_valid_88xx(struct halmac_adapter *adapter,
             struct wlhdr_frame_ctrl *wlhdr);
 
static void
dump_reg_88xx(struct halmac_adapter *adapter);
 
static u8
packet_in_nlo_88xx(struct halmac_adapter *adapter,
          enum halmac_packet_id pkt_id);
 
static enum halmac_packet_id
get_real_pkt_id_88xx(struct halmac_adapter *adapter,
            enum halmac_packet_id pkt_id);
 
static u32
get_update_packet_page_size(struct halmac_adapter *adapter, u32 size);
 
/**
 * ofld_func_cfg_88xx() - config offload function
 * @adapter : the adapter of halmac
 * @info : offload function information
 * Author : Ivan Lin
 * Return : enum halmac_ret_status
 * More details of status code can be found in prototype document
 */
enum halmac_ret_status
ofld_func_cfg_88xx(struct halmac_adapter *adapter,
          struct halmac_ofld_func_info *info)
{
   if (adapter->intf == HALMAC_INTERFACE_SDIO &&
       info->rsvd_pg_drv_buf_max_sz > SDIO_TX_MAX_SIZE_88XX)
       return HALMAC_RET_FAIL;
 
   adapter->pltfm_info.malloc_size = info->halmac_malloc_max_sz;
   adapter->pltfm_info.rsvd_pg_size = info->rsvd_pg_drv_buf_max_sz;
 
   return HALMAC_RET_SUCCESS;
}
 
/**
 * dl_drv_rsvd_page_88xx() - download packet to rsvd page
 * @adapter : the adapter of halmac
 * @pg_offset : page offset of driver's rsvd page
 * @halmac_buf : data to be downloaded, tx_desc is not included
 * @halmac_size : data size to be downloaded
 * Author : KaiYuan Chang
 * Return : enum halmac_ret_status
 * More details of status code can be found in prototype document
 */
enum halmac_ret_status
dl_drv_rsvd_page_88xx(struct halmac_adapter *adapter, u8 pg_offset, u8 *buf,
             u32 size)
{
   enum halmac_ret_status status;
   u32 pg_size;
   u32 pg_num = 0;
   u16 pg_addr = 0;
 
   PLTFM_MSG_TRACE("[TRACE]%s ===>\n", __func__);
 
   pg_size = adapter->hw_cfg_info.page_size;
   pg_num = size / pg_size + ((size & (pg_size - 1)) ? 1 : 0);
   if (pg_offset + pg_num > adapter->txff_alloc.rsvd_drv_pg_num) {
       PLTFM_MSG_ERR("[ERR] pkt overflow!!\n");
       return HALMAC_RET_DRV_DL_ERR;
   }
 
   pg_addr = adapter->txff_alloc.rsvd_drv_addr + pg_offset;
 
   status = dl_rsvd_page_88xx(adapter, pg_addr, buf, size);
 
   if (status != HALMAC_RET_SUCCESS) {
       PLTFM_MSG_ERR("[ERR]dl rsvd page fail!!\n");
       return status;
   }
 
   PLTFM_MSG_TRACE("[TRACE]%s <===\n", __func__);
 
   return HALMAC_RET_SUCCESS;
}
 
enum halmac_ret_status
dl_rsvd_page_88xx(struct halmac_adapter *adapter, u16 pg_addr, u8 *buf,
         u32 size)
{
   u8 restore[2];
   u8 value8;
   u16 rsvd_pg_head;
   u32 cnt;
   enum halmac_rsvd_pg_state *state = &adapter->halmac_state.rsvd_pg_state;
   struct halmac_api *api = (struct halmac_api *)adapter->halmac_api;
   enum halmac_ret_status status = HALMAC_RET_SUCCESS;
 
   if (size == 0) {
       PLTFM_MSG_TRACE("[TRACE]pkt size = 0\n");
       return HALMAC_RET_ZERO_LEN_RSVD_PACKET;
   }
 
   if (*state == HALMAC_RSVD_PG_STATE_BUSY)
       return HALMAC_RET_BUSY_STATE;
 
   *state = HALMAC_RSVD_PG_STATE_BUSY;
 
   pg_addr &= BIT_MASK_BCN_HEAD_1_V1;
   HALMAC_REG_W16(REG_FIFOPAGE_CTRL_2, (u16)(pg_addr | BIT(15)));
 
   value8 = HALMAC_REG_R8(REG_CR + 1);
   restore[0] = value8;
   value8 = (u8)(value8 | BIT(0));
   HALMAC_REG_W8(REG_CR + 1, value8);
 
   value8 = HALMAC_REG_R8(REG_FWHW_TXQ_CTRL + 2);
   restore[1] = value8;
   value8 = (u8)(value8 & ~(BIT(6)));
   HALMAC_REG_W8(REG_FWHW_TXQ_CTRL + 2, value8);
 
   if (PLTFM_SEND_RSVD_PAGE(buf, size) == 0) {
       PLTFM_MSG_ERR("[ERR]send rvsd pg(pltfm)!!\n");
       status = HALMAC_RET_DL_RSVD_PAGE_FAIL;
       goto DL_RSVD_PG_END;
   }
 
   cnt = 1000;
   while (!(HALMAC_REG_R8(REG_FIFOPAGE_CTRL_2 + 1) & BIT(7))) {
       PLTFM_DELAY_US(10);
       cnt--;
       if (cnt == 0) {
           PLTFM_MSG_ERR("[ERR]bcn valid!!\n");
           status = HALMAC_RET_POLLING_BCN_VALID_FAIL;
           break;
       }
   }
DL_RSVD_PG_END:
   rsvd_pg_head = adapter->txff_alloc.rsvd_boundary;
   HALMAC_REG_W16(REG_FIFOPAGE_CTRL_2, rsvd_pg_head | BIT(15));
   HALMAC_REG_W8(REG_FWHW_TXQ_CTRL + 2, restore[1]);
   HALMAC_REG_W8(REG_CR + 1, restore[0]);
 
   *state = HALMAC_RSVD_PG_STATE_IDLE;
 
   return status;
}
 
enum halmac_ret_status
get_hw_value_88xx(struct halmac_adapter *adapter, enum halmac_hw_id hw_id,
         void *value)
{
   PLTFM_MSG_TRACE("[TRACE]%s ===>\n", __func__);
 
   switch (hw_id) {
   case HALMAC_HW_RQPN_MAPPING:
       get_pq_mapping_88xx(adapter, (struct halmac_rqpn_map *)value);
       break;
   case HALMAC_HW_EFUSE_SIZE:
       *(u32 *)value = adapter->hw_cfg_info.efuse_size;
       break;
   case HALMAC_HW_EEPROM_SIZE:
       *(u32 *)value = adapter->hw_cfg_info.eeprom_size;
       break;
   case HALMAC_HW_BT_BANK_EFUSE_SIZE:
       *(u32 *)value = adapter->hw_cfg_info.bt_efuse_size;
       break;
   case HALMAC_HW_BT_BANK1_EFUSE_SIZE:
   case HALMAC_HW_BT_BANK2_EFUSE_SIZE:
       *(u32 *)value = 0;
       break;
   case HALMAC_HW_TXFIFO_SIZE:
       *(u32 *)value = adapter->hw_cfg_info.tx_fifo_size;
       break;
   case HALMAC_HW_RXFIFO_SIZE:
       *(u32 *)value = adapter->hw_cfg_info.rx_fifo_size;
       break;
   case HALMAC_HW_RSVD_PG_BNDY:
       *(u16 *)value = adapter->txff_alloc.rsvd_drv_addr;
       break;
   case HALMAC_HW_CAM_ENTRY_NUM:
       *(u8 *)value = adapter->hw_cfg_info.cam_entry_num;
       break;
   case HALMAC_HW_WLAN_EFUSE_AVAILABLE_SIZE:
       get_efuse_available_size_88xx(adapter, (u32 *)value);
       break;
   case HALMAC_HW_IC_VERSION:
       *(u8 *)value = adapter->chip_ver;
       break;
   case HALMAC_HW_PAGE_SIZE:
       *(u32 *)value = adapter->hw_cfg_info.page_size;
       break;
   case HALMAC_HW_TX_AGG_ALIGN_SIZE:
       *(u16 *)value = adapter->hw_cfg_info.tx_align_size;
       break;
   case HALMAC_HW_RX_AGG_ALIGN_SIZE:
       *(u8 *)value = 8;
       break;
   case HALMAC_HW_DRV_INFO_SIZE:
       *(u8 *)value = adapter->drv_info_size;
       break;
   case HALMAC_HW_TXFF_ALLOCATION:
       PLTFM_MEMCPY(value, &adapter->txff_alloc,
                sizeof(struct halmac_txff_allocation));
       break;
   case HALMAC_HW_RSVD_EFUSE_SIZE:
       *(u32 *)value = get_rsvd_efuse_size_88xx(adapter);
       break;
   case HALMAC_HW_FW_HDR_SIZE:
       *(u32 *)value = WLAN_FW_HDR_SIZE;
       break;
   case HALMAC_HW_TX_DESC_SIZE:
       *(u32 *)value = adapter->hw_cfg_info.txdesc_size;
       break;
   case HALMAC_HW_RX_DESC_SIZE:
       *(u32 *)value = adapter->hw_cfg_info.rxdesc_size;
       break;
   case HALMAC_HW_ORI_H2C_SIZE:
       *(u32 *)value = ORIGINAL_H2C_CMD_SIZE;
       break;
   case HALMAC_HW_RSVD_DRV_PGNUM:
       *(u16 *)value = adapter->txff_alloc.rsvd_drv_pg_num;
       break;
   case HALMAC_HW_TX_PAGE_SIZE:
       *(u16 *)value = TX_PAGE_SIZE_88XX;
       break;
   case HALMAC_HW_USB_TXAGG_DESC_NUM:
       *(u8 *)value = adapter->hw_cfg_info.usb_txagg_num;
       break;
   case HALMAC_HW_AC_OQT_SIZE:
       *(u8 *)value = adapter->hw_cfg_info.ac_oqt_size;
       break;
   case HALMAC_HW_NON_AC_OQT_SIZE:
       *(u8 *)value = adapter->hw_cfg_info.non_ac_oqt_size;
       break;
   case HALMAC_HW_AC_QUEUE_NUM:
       *(u8 *)value = adapter->hw_cfg_info.acq_num;
       break;
   case HALMAC_HW_PWR_STATE:
       pwr_state_88xx(adapter, (enum halmac_mac_power *)value);
       break;
   default:
       return HALMAC_RET_PARA_NOT_SUPPORT;
   }
 
   PLTFM_MSG_TRACE("[TRACE]%s <===\n", __func__);
 
   return HALMAC_RET_SUCCESS;
}
 
static void
get_pq_mapping_88xx(struct halmac_adapter *adapter,
           struct halmac_rqpn_map *mapping)
{
   mapping->dma_map_vo = adapter->pq_map[HALMAC_PQ_MAP_VO];
   mapping->dma_map_vi = adapter->pq_map[HALMAC_PQ_MAP_VI];
   mapping->dma_map_be = adapter->pq_map[HALMAC_PQ_MAP_BE];
   mapping->dma_map_bk = adapter->pq_map[HALMAC_PQ_MAP_BK];
   mapping->dma_map_mg = adapter->pq_map[HALMAC_PQ_MAP_MG];
   mapping->dma_map_hi = adapter->pq_map[HALMAC_PQ_MAP_HI];
}
 
/**
 * set_hw_value_88xx() -set hw config value
 * @adapter : the adapter of halmac
 * @hw_id : hw id for driver to config
 * @value : hw value, reference table to get data type
 * Author : KaiYuan Chang / Ivan Lin
 * Return : enum halmac_ret_status
 * More details of status code can be found in prototype document
 */
enum halmac_ret_status
set_hw_value_88xx(struct halmac_adapter *adapter, enum halmac_hw_id hw_id,
         void *value)
{
   enum halmac_ret_status status = HALMAC_RET_SUCCESS;
#if HALMAC_SDIO_SUPPORT
   struct halmac_tx_page_threshold_info *th_info;
#endif
   struct halmac_api *api = (struct halmac_api *)adapter->halmac_api;
 
   PLTFM_MSG_TRACE("[TRACE]%s ===>\n", __func__);
 
   if (!value) {
       PLTFM_MSG_ERR("[ERR]null ptr-set hw value\n");
       return HALMAC_RET_NULL_POINTER;
   }
 
   switch (hw_id) {
#if HALMAC_USB_SUPPORT
   case HALMAC_HW_USB_MODE:
       status = set_usb_mode_88xx(adapter,
                      *(enum halmac_usb_mode *)value);
       if (status != HALMAC_RET_SUCCESS)
           return status;
       break;
#endif
   case HALMAC_HW_BANDWIDTH:
       cfg_bw_88xx(adapter, *(enum halmac_bw *)value);
       break;
   case HALMAC_HW_CHANNEL:
       cfg_ch_88xx(adapter, *(u8 *)value);
       break;
   case HALMAC_HW_PRI_CHANNEL_IDX:
       cfg_pri_ch_idx_88xx(adapter, *(enum halmac_pri_ch_idx *)value);
       break;
   case HALMAC_HW_EN_BB_RF:
       status = enable_bb_rf_88xx(adapter, *(u8 *)value);
       if (status != HALMAC_RET_SUCCESS)
           return status;
       break;
#if HALMAC_SDIO_SUPPORT
   case HALMAC_HW_SDIO_TX_PAGE_THRESHOLD:
       if (adapter->intf == HALMAC_INTERFACE_SDIO) {
           th_info = (struct halmac_tx_page_threshold_info *)value;
           cfg_sdio_tx_page_threshold_88xx(adapter, th_info);
       } else {
           return HALMAC_RET_FAIL;
       }
       break;
#endif
   case HALMAC_HW_RX_SHIFT:
       rx_shift_88xx(adapter, *(u8 *)value);
       break;
   case HALMAC_HW_TXDESC_CHECKSUM:
       tx_desc_chksum_88xx(adapter, *(u8 *)value);
       break;
   case HALMAC_HW_RX_CLK_GATE:
       rx_clk_gate_88xx(adapter, *(u8 *)value);
       break;
   case HALMAC_HW_FAST_EDCA:
       fast_edca_cfg_88xx(adapter,
                  (struct halmac_fast_edca_cfg *)value);
       break;
   case HALMAC_HW_RTS_FULL_BW:
       rts_full_bw_88xx(adapter, *(u8 *)value);
       break;
   case HALMAC_HW_FREE_CNT_EN:
       HALMAC_REG_W8_SET(REG_MISC_CTRL, BIT_EN_FREECNT);
       break;
   case HALMAC_HW_TXFIFO_LIFETIME:
       cfg_txfifo_lt_88xx(adapter,
                  (struct halmac_txfifo_lifetime_cfg *)value);
       break;
   default:
       return HALMAC_RET_PARA_NOT_SUPPORT;
   }
 
   PLTFM_MSG_TRACE("[TRACE]%s <===\n", __func__);
 
   return HALMAC_RET_SUCCESS;
}
 
/**
 * get_watcher_88xx() -get watcher value
 * @adapter : the adapter of halmac
 * @sel : id for driver to config
 * @value : value, reference table to get data type
 * Author :
 * Return : enum halmac_ret_status
 * More details of status code can be found in prototype document
 */
enum halmac_ret_status
get_watcher_88xx(struct halmac_adapter *adapter, enum halmac_watcher_sel sel,
        void *value)
{
   PLTFM_MSG_TRACE("[TRACE]%s ===>\n", __func__);
 
   if (!value) {
       PLTFM_MSG_ERR("[ERR]null ptr-set hw value\n");
       return HALMAC_RET_NULL_POINTER;
   }
 
   switch (sel) {
   case HALMAC_WATCHER_SDIO_RN_FOOL_PROOFING:
       *(u32 *)value = adapter->watcher.get_watcher.sdio_rn_not_align;
       break;
   default:
       return HALMAC_RET_PARA_NOT_SUPPORT;
   }
 
   PLTFM_MSG_TRACE("[TRACE]%s <===\n", __func__);
 
   return HALMAC_RET_SUCCESS;
}
 
enum halmac_ret_status
set_h2c_pkt_hdr_88xx(struct halmac_adapter *adapter, u8 *hdr,
            struct halmac_h2c_header_info *info, u16 *seq_num)
{
   u16 total_size;
 
   PLTFM_MSG_TRACE("[TRACE]%s!!\n", __func__);
 
   total_size = H2C_PKT_HDR_SIZE_88XX + info->content_size;
   FW_OFFLOAD_H2C_SET_TOTAL_LEN(hdr, total_size);
   FW_OFFLOAD_H2C_SET_SUB_CMD_ID(hdr, info->sub_cmd_id);
 
   FW_OFFLOAD_H2C_SET_CATEGORY(hdr, 0x01);
   FW_OFFLOAD_H2C_SET_CMD_ID(hdr, 0xFF);
 
   PLTFM_MUTEX_LOCK(&adapter->h2c_seq_mutex);
   FW_OFFLOAD_H2C_SET_SEQ_NUM(hdr, adapter->h2c_info.seq_num);
   *seq_num = adapter->h2c_info.seq_num;
   (adapter->h2c_info.seq_num)++;
   PLTFM_MUTEX_UNLOCK(&adapter->h2c_seq_mutex);
 
   if (info->ack == 1)
       FW_OFFLOAD_H2C_SET_ACK(hdr, 1);
 
   return HALMAC_RET_SUCCESS;
}
 
enum halmac_ret_status
send_h2c_pkt_88xx(struct halmac_adapter *adapter, u8 *pkt)
{
   u32 cnt = 100;
   enum halmac_ret_status status = HALMAC_RET_SUCCESS;
 
   while (adapter->h2c_info.buf_fs <= H2C_PKT_SIZE_88XX) {
       get_h2c_buf_free_space_88xx(adapter);
       cnt--;
       if (cnt == 0) {
           PLTFM_MSG_ERR("[ERR]h2c free space!!\n");
           return HALMAC_RET_H2C_SPACE_FULL;
       }
   }
 
   cnt = 100;
   do {
       if (PLTFM_SEND_H2C_PKT(pkt, H2C_PKT_SIZE_88XX) == 1)
           break;
       cnt--;
       if (cnt == 0) {
           PLTFM_MSG_ERR("[ERR]pltfm - sned h2c pkt!!\n");
           return HALMAC_RET_SEND_H2C_FAIL;
       }
       PLTFM_DELAY_US(5);
 
   } while (1);
 
   adapter->h2c_info.buf_fs -= H2C_PKT_SIZE_88XX;
 
   return status;
}
 
enum halmac_ret_status
get_h2c_buf_free_space_88xx(struct halmac_adapter *adapter)
{
   u32 hw_wptr;
   u32 fw_rptr;
   struct halmac_h2c_info *info = &adapter->h2c_info;
   struct halmac_api *api = (struct halmac_api *)adapter->halmac_api;
 
   hw_wptr = HALMAC_REG_R32(REG_H2C_PKT_WRITEADDR) & 0x3FFFF;
   fw_rptr = HALMAC_REG_R32(REG_H2C_PKT_READADDR) & 0x3FFFF;
 
   if (hw_wptr >= fw_rptr)
       info->buf_fs = info->buf_size - (hw_wptr - fw_rptr);
   else
       info->buf_fs = fw_rptr - hw_wptr;
 
   return HALMAC_RET_SUCCESS;
}
 
/**
 * get_c2h_info_88xx() - process halmac C2H packet
 * @adapter : the adapter of halmac
 * @buf : RX Packet pointer
 * @size : RX Packet size
 *
 * Note : Don't use any IO or DELAY in this API
 *
 * Author : KaiYuan Chang/Ivan Lin
 *
 * Used to process c2h packet info from RX path. After receiving the packet,
 * user need to call this api and pass the packet pointer.
 *
 * Return : enum halmac_ret_status
 * More details of status code can be found in prototype document
 */
enum halmac_ret_status
get_c2h_info_88xx(struct halmac_adapter *adapter, u8 *buf, u32 size)
{
   enum halmac_ret_status status = HALMAC_RET_SUCCESS;
 
   if (GET_RX_DESC_C2H(buf) == 1) {
       PLTFM_MSG_TRACE("[TRACE]Parse c2h pkt\n");
 
       status = parse_c2h_pkt_88xx(adapter, buf, size);
       if (status != HALMAC_RET_SUCCESS) {
           PLTFM_MSG_ERR("[ERR]Parse c2h pkt\n");
           return status;
       }
   }
 
   return HALMAC_RET_SUCCESS;
}
 
static enum halmac_ret_status
parse_c2h_pkt_88xx(struct halmac_adapter *adapter, u8 *buf, u32 size)
{
   u8 cmd_id;
   u8 sub_cmd_id;
   u8 *c2h_pkt = buf + adapter->hw_cfg_info.rxdesc_size;
   u32 c2h_size = size - adapter->hw_cfg_info.rxdesc_size;
   enum halmac_ret_status status = HALMAC_RET_SUCCESS;
 
   cmd_id = (u8)C2H_HDR_GET_CMD_ID(c2h_pkt);
 
   if (cmd_id != 0xFF) {
       PLTFM_MSG_TRACE("[TRACE]Not 0xFF cmd!!\n");
       return HALMAC_RET_C2H_NOT_HANDLED;
   }
 
   sub_cmd_id = (u8)C2H_HDR_GET_C2H_SUB_CMD_ID(c2h_pkt);
 
   switch (sub_cmd_id) {
   case C2H_SUB_CMD_ID_C2H_DBG:
       status = get_c2h_dbg_88xx(adapter, c2h_pkt, c2h_size);
       break;
   case C2H_SUB_CMD_ID_H2C_ACK_HDR:
       status = get_h2c_ack_88xx(adapter, c2h_pkt, c2h_size);
       break;
   case C2H_SUB_CMD_ID_BT_COEX_INFO:
       status = HALMAC_RET_C2H_NOT_HANDLED;
       break;
   case C2H_SUB_CMD_ID_SCAN_STATUS_RPT:
       status = get_scan_rpt_88xx(adapter, c2h_pkt, c2h_size);
       break;
   case C2H_SUB_CMD_ID_PSD_DATA:
       status = get_psd_data_88xx(adapter, c2h_pkt, c2h_size);
       break;
   case C2H_SUB_CMD_ID_EFUSE_DATA:
       status = get_efuse_data_88xx(adapter, c2h_pkt, c2h_size);
       break;
   case C2H_SUB_CMD_ID_SCAN_CH_NOTIFY:
       status = get_scan_ch_notify_88xx(adapter, c2h_pkt, c2h_size);
       break;
   case C2H_SUB_CMD_ID_DPK_DATA:
       status = get_dpk_data_88xx(adapter, c2h_pkt, c2h_size);
       break;
   default:
       PLTFM_MSG_WARN("[WARN]Sub cmd id!!\n");
       status = HALMAC_RET_C2H_NOT_HANDLED;
       break;
   }
 
   return status;
}
 
static enum halmac_ret_status
get_c2h_dbg_88xx(struct halmac_adapter *adapter, u8 *buf, u32 size)
{
   u8 i;
   u8 next_msg = 0;
   u8 cur_msg = 0;
   u8 msg_len = 0;
   char *c2h_buf = (char *)NULL;
   u8 content_len = 0;
   u8 seq_num = 0;
 
   content_len = (u8)C2H_HDR_GET_LEN((u8 *)buf);
 
   if (content_len > C2H_DBG_CONTENT_MAX_LENGTH) {
       PLTFM_MSG_ERR("[ERR]c2h size > max len!\n");
       return HALMAC_RET_C2H_NOT_HANDLED;
   }
 
   for (i = 0; i < content_len; i++) {
       if (*(buf + C2H_DBG_HDR_LEN + i) == '\n') {
           if ((*(buf + C2H_DBG_HDR_LEN + i + 1) == '\0') ||
               (*(buf + C2H_DBG_HDR_LEN + i + 1) == 0xff)) {
               next_msg = C2H_DBG_HDR_LEN + i + 1;
               goto _ENDFOUND;
           }
       }
   }
 
_ENDFOUND:
   msg_len = next_msg - C2H_DBG_HDR_LEN;
 
   c2h_buf = (char *)PLTFM_MALLOC(msg_len);
   if (!c2h_buf)
       return HALMAC_RET_MALLOC_FAIL;
 
   PLTFM_MEMCPY(c2h_buf, buf + C2H_DBG_HDR_LEN, msg_len);
 
   seq_num = (u8)(*(c2h_buf));
   *(c2h_buf + msg_len - 1) = '\0';
   PLTFM_MSG_ALWAYS("[RTKFW, SEQ=%d]: %s\n",
            seq_num, (char *)(c2h_buf + 1));
   PLTFM_FREE(c2h_buf, msg_len);
 
   while (*(buf + next_msg) != '\0') {
       cur_msg = next_msg;
 
       msg_len = (u8)(*(buf + cur_msg + 3)) - 1;
       next_msg += C2H_DBG_HDR_LEN + msg_len;
 
       c2h_buf = (char *)PLTFM_MALLOC(msg_len);
       if (!c2h_buf)
           return HALMAC_RET_MALLOC_FAIL;
 
       PLTFM_MEMCPY(c2h_buf, buf + cur_msg + C2H_DBG_HDR_LEN, msg_len);
       *(c2h_buf + msg_len - 1) = '\0';
       seq_num = (u8)(*(c2h_buf));
       PLTFM_MSG_ALWAYS("[RTKFW, SEQ=%d]: %s\n",
                seq_num, (char *)(c2h_buf + 1));
       PLTFM_FREE(c2h_buf, msg_len);
   }
 
   return HALMAC_RET_SUCCESS;
}
 
static enum halmac_ret_status
get_h2c_ack_88xx(struct halmac_adapter *adapter, u8 *buf, u32 size)
{
   u8 cmd_id;
   u8 sub_cmd_id;
   u8 fw_rc;
   enum halmac_ret_status status = HALMAC_RET_SUCCESS;
 
   PLTFM_MSG_TRACE("[TRACE]Ack for C2H!!\n");
 
   fw_rc = (u8)H2C_ACK_HDR_GET_H2C_RETURN_CODE(buf);
   if (HALMAC_H2C_RETURN_SUCCESS != (enum halmac_h2c_return_code)fw_rc)
       PLTFM_MSG_TRACE("[TRACE]fw rc = %d\n", fw_rc);
 
   cmd_id = (u8)H2C_ACK_HDR_GET_H2C_CMD_ID(buf);
 
   if (cmd_id != 0xFF) {
       PLTFM_MSG_ERR("[ERR]h2c ack cmd id!!\n");
       return HALMAC_RET_C2H_NOT_HANDLED;
   }
 
   sub_cmd_id = (u8)H2C_ACK_HDR_GET_H2C_SUB_CMD_ID(buf);
 
   switch (sub_cmd_id) {
   case H2C_SUB_CMD_ID_DUMP_PHYSICAL_EFUSE_ACK:
       status = get_h2c_ack_phy_efuse_88xx(adapter, buf, size);
       break;
   case H2C_SUB_CMD_ID_CFG_PARAM_ACK:
       status = get_h2c_ack_cfg_param_88xx(adapter, buf, size);
       break;
   case H2C_SUB_CMD_ID_UPDATE_PKT_ACK:
       status = get_h2c_ack_update_pkt_88xx(adapter, buf, size);
       break;
   case H2C_SUB_CMD_ID_SEND_SCAN_PKT_ACK:
       status = get_h2c_ack_send_scan_pkt_88xx(adapter, buf, size);
       break;
   case H2C_SUB_CMD_ID_DROP_SCAN_PKT_ACK:
       status = get_h2c_ack_drop_scan_pkt_88xx(adapter, buf, size);
       break;
   case H2C_SUB_CMD_ID_UPDATE_DATAPACK_ACK:
       status = get_h2c_ack_update_datapkt_88xx(adapter, buf, size);
       break;
   case H2C_SUB_CMD_ID_RUN_DATAPACK_ACK:
       status = get_h2c_ack_run_datapkt_88xx(adapter, buf, size);
       break;
   case H2C_SUB_CMD_ID_CH_SWITCH_ACK:
       status = get_h2c_ack_ch_switch_88xx(adapter, buf, size);
       break;
   case H2C_SUB_CMD_ID_IQK_ACK:
       status = get_h2c_ack_iqk_88xx(adapter, buf, size);
       break;
   case H2C_SUB_CMD_ID_PWR_TRK_ACK:
       status = get_h2c_ack_pwr_trk_88xx(adapter, buf, size);
       break;
   case H2C_SUB_CMD_ID_PSD_ACK:
       break;
   case H2C_SUB_CMD_ID_FW_SNDING_ACK:
       status = get_h2c_ack_fw_snding_88xx(adapter, buf, size);
       break;
   case H2C_SUB_CMD_ID_DPK_ACK:
       status = get_h2c_ack_dpk_88xx(adapter, buf, size);
       break;
   default:
       status = HALMAC_RET_C2H_NOT_HANDLED;
       break;
   }
 
   return status;
}
 
static enum halmac_ret_status
get_scan_ch_notify_88xx(struct halmac_adapter *adapter, u8 *buf, u32 size)
{
   struct halmac_scan_rpt_info *scan_rpt_info = &adapter->scan_rpt_info;
 
   PLTFM_MSG_TRACE("[TRACE]scan mode:%d\n", adapter->ch_sw_info.scan_mode);
 
   if (adapter->ch_sw_info.scan_mode == 1) {
       if (scan_rpt_info->avl_buf_size < 12) {
           PLTFM_MSG_ERR("[ERR]ch_notify buffer full!!\n");
           return HALMAC_RET_CH_SW_NO_BUF;
       }
 
       SCAN_CH_NOTIFY_SET_CH_NUM(scan_rpt_info->buf_wptr,
                     (u8)SCAN_CH_NOTIFY_GET_CH_NUM(buf));
       SCAN_CH_NOTIFY_SET_NOTIFY_ID(scan_rpt_info->buf_wptr,
                        SCAN_CH_NOTIFY_GET_NOTIFY_ID(buf));
       SCAN_CH_NOTIFY_SET_STATUS(scan_rpt_info->buf_wptr,
                     (u8)SCAN_CH_NOTIFY_GET_STATUS(buf));
       SCAN_CH_NOTIFY_SET_TSF_0(scan_rpt_info->buf_wptr,
                    (u8)SCAN_CH_NOTIFY_GET_TSF_0(buf));
       SCAN_CH_NOTIFY_SET_TSF_1(scan_rpt_info->buf_wptr,
                    (u8)SCAN_CH_NOTIFY_GET_TSF_1(buf));
       SCAN_CH_NOTIFY_SET_TSF_2(scan_rpt_info->buf_wptr,
                    (u8)SCAN_CH_NOTIFY_GET_TSF_2(buf));
       SCAN_CH_NOTIFY_SET_TSF_3(scan_rpt_info->buf_wptr,
                    (u8)SCAN_CH_NOTIFY_GET_TSF_3(buf));
       SCAN_CH_NOTIFY_SET_TSF_4(scan_rpt_info->buf_wptr,
                    (u8)SCAN_CH_NOTIFY_GET_TSF_4(buf));
       SCAN_CH_NOTIFY_SET_TSF_5(scan_rpt_info->buf_wptr,
                    (u8)SCAN_CH_NOTIFY_GET_TSF_5(buf));
       SCAN_CH_NOTIFY_SET_TSF_6(scan_rpt_info->buf_wptr,
                    (u8)SCAN_CH_NOTIFY_GET_TSF_6(buf));
       SCAN_CH_NOTIFY_SET_TSF_7(scan_rpt_info->buf_wptr,
                    (u8)SCAN_CH_NOTIFY_GET_TSF_7(buf));
 
       scan_rpt_info->avl_buf_size = scan_rpt_info->avl_buf_size - 12;
       scan_rpt_info->total_size = scan_rpt_info->total_size + 12;
       scan_rpt_info->buf_wptr = scan_rpt_info->buf_wptr + 12;
   }
 
   return HALMAC_RET_SUCCESS;
}
 
static enum halmac_ret_status
get_scan_rpt_88xx(struct halmac_adapter *adapter, u8 *buf, u32 size)
{
   u8 fw_rc;
   enum halmac_cmd_process_status proc_status;
   struct halmac_scan_rpt_info *scan_rpt_info = &adapter->scan_rpt_info;
 
   fw_rc = (u8)SCAN_STATUS_RPT_GET_H2C_RETURN_CODE(buf);
   proc_status = (HALMAC_H2C_RETURN_SUCCESS ==
       (enum halmac_h2c_return_code)fw_rc) ?
       HALMAC_CMD_PROCESS_DONE : HALMAC_CMD_PROCESS_ERROR;
 
   PLTFM_EVENT_SIG(HALMAC_FEATURE_CHANNEL_SWITCH, proc_status, NULL, 0);
 
   adapter->halmac_state.scan_state.proc_status = proc_status;
 
   if (adapter->ch_sw_info.scan_mode == 1) {
       scan_rpt_info->rpt_tsf_low =
           ((SCAN_STATUS_RPT_GET_TSF_3(buf) << 24) |
           (SCAN_STATUS_RPT_GET_TSF_2(buf) << 16) |
           (SCAN_STATUS_RPT_GET_TSF_1(buf) << 8) |
           (SCAN_STATUS_RPT_GET_TSF_0(buf)));
       scan_rpt_info->rpt_tsf_high =
           ((SCAN_STATUS_RPT_GET_TSF_7(buf) << 24) |
           (SCAN_STATUS_RPT_GET_TSF_6(buf) << 16) |
           (SCAN_STATUS_RPT_GET_TSF_5(buf) << 8) |
           (SCAN_STATUS_RPT_GET_TSF_4(buf)));
   }
 
   PLTFM_MSG_TRACE("[TRACE]scan : %X\n", proc_status);
 
   return HALMAC_RET_SUCCESS;
}
 
static enum halmac_ret_status
get_h2c_ack_cfg_param_88xx(struct halmac_adapter *adapter, u8 *buf, u32 size)
{
   u8 seq_num;
   u8 fw_rc;
   u32 offset_accum;
   u32 value_accum;
   struct halmac_cfg_param_state *state =
       &adapter->halmac_state.cfg_param_state;
   enum halmac_cmd_process_status proc_status =
       HALMAC_CMD_PROCESS_UNDEFINE;
 
   seq_num = (u8)H2C_ACK_HDR_GET_H2C_SEQ(buf);
   PLTFM_MSG_TRACE("[TRACE]Seq num : h2c->%d c2h->%d\n",
           state->seq_num, seq_num);
   if (seq_num != state->seq_num) {
       PLTFM_MSG_ERR("[ERR]Seq num mismatch : h2c->%d c2h->%d\n",
                 state->seq_num, seq_num);
       return HALMAC_RET_SUCCESS;
   }
 
   if (state->proc_status != HALMAC_CMD_PROCESS_SENDING) {
       PLTFM_MSG_ERR("[ERR]not cmd sending\n");
       return HALMAC_RET_SUCCESS;
   }
 
   fw_rc = (u8)H2C_ACK_HDR_GET_H2C_RETURN_CODE(buf);
   state->fw_rc = fw_rc;
   offset_accum = CFG_PARAM_ACK_GET_OFFSET_ACCUMULATION(buf);
   value_accum = CFG_PARAM_ACK_GET_VALUE_ACCUMULATION(buf);
 
   if (offset_accum != adapter->cfg_param_info.offset_accum ||
       value_accum != adapter->cfg_param_info.value_accum) {
       PLTFM_MSG_ERR("[ERR][C2H]offset_accu : %x, value_accu : %xn",
                 offset_accum, value_accum);
       PLTFM_MSG_ERR("[ERR][Ada]offset_accu : %x, value_accu : %x\n",
                 adapter->cfg_param_info.offset_accum,
                 adapter->cfg_param_info.value_accum);
       proc_status = HALMAC_CMD_PROCESS_ERROR;
   }
 
   if ((enum halmac_h2c_return_code)fw_rc == HALMAC_H2C_RETURN_SUCCESS &&
       proc_status != HALMAC_CMD_PROCESS_ERROR) {
       proc_status = HALMAC_CMD_PROCESS_DONE;
       state->proc_status = proc_status;
       PLTFM_EVENT_SIG(HALMAC_FEATURE_CFG_PARA, proc_status, NULL, 0);
   } else {
       proc_status = HALMAC_CMD_PROCESS_ERROR;
       state->proc_status = proc_status;
       PLTFM_EVENT_SIG(HALMAC_FEATURE_CFG_PARA, proc_status,
               &fw_rc, 1);
   }
 
   return HALMAC_RET_SUCCESS;
}
 
static enum halmac_ret_status
get_h2c_ack_update_pkt_88xx(struct halmac_adapter *adapter, u8 *buf, u32 size)
{
   u8 seq_num;
   u8 fw_rc;
   struct halmac_update_pkt_state *state =
       &adapter->halmac_state.update_pkt_state;
   enum halmac_cmd_process_status proc_status;
 
   seq_num = (u8)H2C_ACK_HDR_GET_H2C_SEQ(buf);
   PLTFM_MSG_TRACE("[TRACE]Seq num : h2c->%d c2h->%d\n",
           state->seq_num, seq_num);
   if (seq_num != state->seq_num) {
       PLTFM_MSG_ERR("[ERR]Seq num mismatch : h2c->%d c2h->%d\n",
                 state->seq_num, seq_num);
       return HALMAC_RET_SUCCESS;
   }
 
   if (state->proc_status != HALMAC_CMD_PROCESS_SENDING) {
       PLTFM_MSG_ERR("[ERR]not cmd sending\n");
       return HALMAC_RET_SUCCESS;
   }
 
   fw_rc = (u8)H2C_ACK_HDR_GET_H2C_RETURN_CODE(buf);
   state->fw_rc = fw_rc;
 
   if (HALMAC_H2C_RETURN_SUCCESS == (enum halmac_h2c_return_code)fw_rc) {
       proc_status = HALMAC_CMD_PROCESS_DONE;
       state->proc_status = proc_status;
       PLTFM_EVENT_SIG(HALMAC_FEATURE_UPDATE_PACKET, proc_status,
               NULL, 0);
   } else {
       proc_status = HALMAC_CMD_PROCESS_ERROR;
       state->proc_status = proc_status;
       PLTFM_EVENT_SIG(HALMAC_FEATURE_UPDATE_PACKET, proc_status,
               &state->fw_rc, 1);
   }
 
   return HALMAC_RET_SUCCESS;
}
 
static enum halmac_ret_status
get_h2c_ack_send_scan_pkt_88xx(struct halmac_adapter *adapter,
                  u8 *buf, u32 size)
{
   u8 seq_num;
   u8 fw_rc;
   struct halmac_scan_pkt_state *state =
       &adapter->halmac_state.scan_pkt_state;
   enum halmac_cmd_process_status proc_status;
 
   seq_num = (u8)H2C_ACK_HDR_GET_H2C_SEQ(buf);
   PLTFM_MSG_TRACE("[TRACE]Seq num : h2c->%d c2h->%d\n",
           state->seq_num, seq_num);
   if (seq_num != state->seq_num) {
       PLTFM_MSG_ERR("[ERR]Seq num mismatch : h2c->%d c2h->%d\n",
                 state->seq_num, seq_num);
       return HALMAC_RET_SUCCESS;
   }
 
   if (state->proc_status != HALMAC_CMD_PROCESS_SENDING) {
       PLTFM_MSG_ERR("[ERR]not cmd sending\n");
       return HALMAC_RET_SUCCESS;
   }
 
   fw_rc = (u8)H2C_ACK_HDR_GET_H2C_RETURN_CODE(buf);
   state->fw_rc = fw_rc;
 
   if (HALMAC_H2C_RETURN_SUCCESS == (enum halmac_h2c_return_code)fw_rc) {
       proc_status = HALMAC_CMD_PROCESS_DONE;
       state->proc_status = proc_status;
       PLTFM_EVENT_SIG(HALMAC_FEATURE_SEND_SCAN_PACKET, proc_status,
               NULL, 0);
   } else {
       proc_status = HALMAC_CMD_PROCESS_ERROR;
       state->proc_status = proc_status;
       PLTFM_EVENT_SIG(HALMAC_FEATURE_SEND_SCAN_PACKET, proc_status,
               &state->fw_rc, 1);
   }
 
   return HALMAC_RET_SUCCESS;
}
 
static enum halmac_ret_status
get_h2c_ack_drop_scan_pkt_88xx(struct halmac_adapter *adapter,
                  u8 *buf, u32 size)
{
   u8 seq_num;
   u8 fw_rc;
   struct halmac_drop_pkt_state *state =
       &adapter->halmac_state.drop_pkt_state;
   enum halmac_cmd_process_status proc_status;
 
   seq_num = (u8)H2C_ACK_HDR_GET_H2C_SEQ(buf);
   PLTFM_MSG_TRACE("[TRACE]Seq num : h2c->%d c2h->%d\n",
           state->seq_num, seq_num);
   if (seq_num != state->seq_num) {
       PLTFM_MSG_ERR("[ERR]Seq num mismatch : h2c->%d c2h->%d\n",
                 state->seq_num, seq_num);
       return HALMAC_RET_SUCCESS;
   }
 
   if (state->proc_status != HALMAC_CMD_PROCESS_SENDING) {
       PLTFM_MSG_ERR("[ERR]not cmd sending\n");
       return HALMAC_RET_SUCCESS;
   }
 
   fw_rc = (u8)H2C_ACK_HDR_GET_H2C_RETURN_CODE(buf);
   state->fw_rc = fw_rc;
 
   if (HALMAC_H2C_RETURN_SUCCESS == (enum halmac_h2c_return_code)fw_rc) {
       proc_status = HALMAC_CMD_PROCESS_DONE;
       state->proc_status = proc_status;
       PLTFM_EVENT_SIG(HALMAC_FEATURE_DROP_SCAN_PACKET, proc_status,
               NULL, 0);
   } else {
       proc_status = HALMAC_CMD_PROCESS_ERROR;
       state->proc_status = proc_status;
       PLTFM_EVENT_SIG(HALMAC_FEATURE_DROP_SCAN_PACKET, proc_status,
               &state->fw_rc, 1);
   }
 
   return HALMAC_RET_SUCCESS;
}
 
static enum halmac_ret_status
get_h2c_ack_update_datapkt_88xx(struct halmac_adapter *adapter, u8 *buf,
               u32 size)
{
   return HALMAC_RET_NOT_SUPPORT;
}
 
static enum halmac_ret_status
get_h2c_ack_run_datapkt_88xx(struct halmac_adapter *adapter, u8 *buf, u32 size)
{
   return HALMAC_RET_NOT_SUPPORT;
}
 
static enum halmac_ret_status
get_h2c_ack_ch_switch_88xx(struct halmac_adapter *adapter, u8 *buf, u32 size)
{
   u8 seq_num;
   u8 fw_rc;
   struct halmac_scan_state *state = &adapter->halmac_state.scan_state;
   struct halmac_scan_rpt_info *scan_rpt_info = &adapter->scan_rpt_info;
   enum halmac_cmd_process_status proc_status;
 
   seq_num = (u8)H2C_ACK_HDR_GET_H2C_SEQ(buf);
   PLTFM_MSG_TRACE("[TRACE]Seq num : h2c->%d c2h->%d\n",
           state->seq_num, seq_num);
   if (seq_num != state->seq_num) {
       PLTFM_MSG_ERR("[ERR]Seq num mismatch : h2c->%d c2h->%d\n",
                 state->seq_num, seq_num);
       return HALMAC_RET_SUCCESS;
   }
 
   if (state->proc_status != HALMAC_CMD_PROCESS_SENDING) {
       PLTFM_MSG_ERR("[ERR]not cmd sending\n");
       return HALMAC_RET_SUCCESS;
   }
 
   fw_rc = (u8)H2C_ACK_HDR_GET_H2C_RETURN_CODE(buf);
   state->fw_rc = fw_rc;
 
   if (adapter->ch_sw_info.scan_mode == 1) {
       scan_rpt_info->ack_tsf_low =
           ((CH_SWITCH_ACK_GET_TSF_3(buf) << 24) |
           (CH_SWITCH_ACK_GET_TSF_2(buf) << 16) |
           (CH_SWITCH_ACK_GET_TSF_1(buf) << 8) |
           (CH_SWITCH_ACK_GET_TSF_0(buf)));
       scan_rpt_info->ack_tsf_high =
           ((CH_SWITCH_ACK_GET_TSF_7(buf) << 24) |
           (CH_SWITCH_ACK_GET_TSF_6(buf) << 16) |
           (CH_SWITCH_ACK_GET_TSF_5(buf) << 8) |
           (CH_SWITCH_ACK_GET_TSF_4(buf)));
   }
 
   if ((enum halmac_h2c_return_code)fw_rc == HALMAC_H2C_RETURN_SUCCESS) {
       proc_status = HALMAC_CMD_PROCESS_RCVD;
       state->proc_status = proc_status;
       PLTFM_EVENT_SIG(HALMAC_FEATURE_CHANNEL_SWITCH, proc_status,
               NULL, 0);
   } else {
       proc_status = HALMAC_CMD_PROCESS_ERROR;
       state->proc_status = proc_status;
       PLTFM_EVENT_SIG(HALMAC_FEATURE_CHANNEL_SWITCH, proc_status,
               &fw_rc, 1);
   }
 
   return HALMAC_RET_SUCCESS;
}
 
/**
 * mac_debug_88xx_v1() - read some registers for debug
 * @adapter
 * Author : KaiYuan Chang/Ivan Lin
 * Return : enum halmac_ret_status
 */
enum halmac_ret_status
mac_debug_88xx(struct halmac_adapter *adapter)
{
   PLTFM_MSG_TRACE("[TRACE]%s ===>\n", __func__);
 
   if (adapter->intf == HALMAC_INTERFACE_SDIO)
       dump_reg_sdio_88xx(adapter);
   else
       dump_reg_88xx(adapter);
 
   PLTFM_MSG_TRACE("[TRACE]%s <===\n", __func__);
 
   return HALMAC_RET_SUCCESS;
}
 
static void
dump_reg_sdio_88xx(struct halmac_adapter *adapter)
{
   u8 tmp8;
   u32 i;
 
   /* Dump CCCR, it needs new platform api */
 
   /*Dump SDIO Local Register, use CMD52*/
   for (i = 0x10250000; i < 0x102500ff; i++) {
       tmp8 = PLTFM_SDIO_CMD52_R(i);
       PLTFM_MSG_TRACE("[TRACE]dbg-sdio[%x]=%x\n", i, tmp8);
   }
 
   /*Dump MAC Register*/
   for (i = 0x0000; i < 0x17ff; i++) {
       tmp8 = PLTFM_SDIO_CMD52_R(i);
       PLTFM_MSG_TRACE("[TRACE]dbg-mac[%x]=%x\n", i, tmp8);
   }
 
   tmp8 = PLTFM_SDIO_CMD52_R(REG_SDIO_CRC_ERR_IDX);
   if (tmp8)
       PLTFM_MSG_ERR("[ERR]sdio crc=%x\n", tmp8);
 
   /*Check RX Fifo status*/
   i = REG_RXFF_PTR_V1;
   tmp8 = PLTFM_SDIO_CMD52_R(i);
   PLTFM_MSG_TRACE("[TRACE]dbg-mac[%x]=%x\n", i, tmp8);
   i = REG_RXFF_WTR_V1;
   tmp8 = PLTFM_SDIO_CMD52_R(i);
   PLTFM_MSG_TRACE("[TRACE]dbg-mac[%x]=%x\n", i, tmp8);
   i = REG_RXFF_PTR_V1;
   tmp8 = PLTFM_SDIO_CMD52_R(i);
   PLTFM_MSG_TRACE("[TRACE]dbg-mac[%x]=%x\n", i, tmp8);
   i = REG_RXFF_WTR_V1;
   tmp8 = PLTFM_SDIO_CMD52_R(i);
   PLTFM_MSG_TRACE("[TRACE]dbg-mac[%x]=%x\n", i, tmp8);
}
 
static void
dump_reg_88xx(struct halmac_adapter *adapter)
{
   u32 tmp32;
   u32 i;
   struct halmac_api *api = (struct halmac_api *)adapter->halmac_api;
 
   /*Dump MAC Register*/
   for (i = 0x0000; i < 0x17fc; i += 4) {
       tmp32 = HALMAC_REG_R32(i);
       PLTFM_MSG_TRACE("[TRACE]dbg-mac[%x]=%x\n", i, tmp32);
   }
 
   /*Check RX Fifo status*/
   i = REG_RXFF_PTR_V1;
   tmp32 = HALMAC_REG_R32(i);
   PLTFM_MSG_TRACE("[TRACE]dbg-mac[%x]=%x\n", i, tmp32);
   i = REG_RXFF_WTR_V1;
   tmp32 = HALMAC_REG_R32(i);
   PLTFM_MSG_TRACE("[TRACE]dbg-mac[%x]=%x\n", i, tmp32);
   i = REG_RXFF_PTR_V1;
   tmp32 = HALMAC_REG_R32(i);
   PLTFM_MSG_TRACE("[TRACE]dbg-mac[%x]=%x\n", i, tmp32);
   i = REG_RXFF_WTR_V1;
   tmp32 = HALMAC_REG_R32(i);
   PLTFM_MSG_TRACE("[TRACE]dbg-mac[%x]=%x\n", i, tmp32);
}
 
/**
 * cfg_parameter_88xx() - config parameter by FW
 * @adapter : the adapter of halmac
 * @info : cmd id, content
 * @full_fifo : parameter information
 *
 * If msk_en = 1, the format of array is {reg_info, mask, value}.
 * If msk_en =_FAUSE, the format of array is {reg_info, value}
 * The format of reg_info is
 * reg_info[31]=rf_reg, 0: MAC_BB reg, 1: RF reg
 * reg_info[27:24]=rf_path, 0: path_A, 1: path_B
 * if rf_reg=0(MAC_BB reg), rf_path is meaningless.
 * ref_info[15:0]=offset
 *
 * Example: msk_en = 0
 * {0x8100000a, 0x00001122}
 * =>Set RF register, path_B, offset 0xA to 0x00001122
 * {0x00000824, 0x11224433}
 * =>Set MAC_BB register, offset 0x800 to 0x11224433
 *
 * Note : full fifo mode only for init flow
 *
 * Author : KaiYuan Chang/Ivan Lin
 * Return : enum halmac_ret_status
 * More details of status code can be found in prototype document
 */
enum halmac_ret_status
cfg_parameter_88xx(struct halmac_adapter *adapter,
          struct halmac_phy_parameter_info *info, u8 full_fifo)
{
   enum halmac_ret_status status = HALMAC_RET_SUCCESS;
   enum halmac_cmd_process_status *proc_status;
   enum halmac_cmd_construct_state cmd_state;
 
   proc_status = &adapter->halmac_state.cfg_param_state.proc_status;
 
   if (halmac_fw_validate(adapter) != HALMAC_RET_SUCCESS)
       return HALMAC_RET_NO_DLFW;
 
   if (adapter->fw_ver.h2c_version < 4)
       return HALMAC_RET_FW_NO_SUPPORT;
 
   if (*proc_status == HALMAC_CMD_PROCESS_SENDING) {
       PLTFM_MSG_TRACE("[TRACE]Wait event(para)\n");
       return HALMAC_RET_BUSY_STATE;
   }
 
   cmd_state = cfg_param_cmd_cnstr_state_88xx(adapter);
   if (cmd_state != HALMAC_CMD_CNSTR_IDLE &&
       cmd_state != HALMAC_CMD_CNSTR_CNSTR) {
       PLTFM_MSG_TRACE("[TRACE]Not idle(para)\n");
       return HALMAC_RET_BUSY_STATE;
   }
 
   *proc_status = HALMAC_CMD_PROCESS_IDLE;
 
   status = proc_cfg_param_88xx(adapter, info, full_fifo);
 
   if (status != HALMAC_RET_SUCCESS && status != HALMAC_RET_PARA_SENDING) {
       PLTFM_MSG_ERR("[ERR]send param h2c\n");
       return status;
   }
 
   return status;
}
 
static enum halmac_cmd_construct_state
cfg_param_cmd_cnstr_state_88xx(struct halmac_adapter *adapter)
{
   return adapter->halmac_state.cfg_param_state.cmd_cnstr_state;
}
 
static enum halmac_ret_status
proc_cfg_param_88xx(struct halmac_adapter *adapter,
           struct halmac_phy_parameter_info *param, u8 full_fifo)
{
   u8 end_cmd = 0;
   u32 rsvd_size;
   enum halmac_ret_status status = HALMAC_RET_SUCCESS;
   struct halmac_cfg_param_info *info = &adapter->cfg_param_info;
   enum halmac_cmd_process_status *proc_status;
 
   proc_status = &adapter->halmac_state.cfg_param_state.proc_status;
 
   status = malloc_cfg_param_buf_88xx(adapter, full_fifo);
   if (status != HALMAC_RET_SUCCESS)
       return status;
 
   if (cnv_cfg_param_state_88xx(adapter, HALMAC_CMD_CNSTR_CNSTR) !=
       HALMAC_RET_SUCCESS) {
       PLTFM_FREE(info->buf, info->buf_size);
       info->buf = NULL;
       info->buf_wptr = NULL;
       return HALMAC_RET_ERROR_STATE;
   }
 
   add_param_buf_88xx(adapter, param, info->buf_wptr, &end_cmd);
   if (param->cmd_id != HALMAC_PARAMETER_CMD_END) {
       info->num++;
       info->buf_wptr += CFG_PARAM_H2C_INFO_SIZE;
       info->avl_buf_size -= CFG_PARAM_H2C_INFO_SIZE;
   }
 
   rsvd_size = info->avl_buf_size - adapter->hw_cfg_info.txdesc_size;
   if (rsvd_size > CFG_PARAM_H2C_INFO_SIZE && end_cmd == 0)
       return HALMAC_RET_SUCCESS;
 
   if (info->num == 0) {
       PLTFM_FREE(info->buf, info->buf_size);
       info->buf = NULL;
       info->buf_wptr = NULL;
       PLTFM_MSG_TRACE("[TRACE]param num = 0!!\n");
 
       *proc_status = HALMAC_CMD_PROCESS_DONE;
       PLTFM_EVENT_SIG(HALMAC_FEATURE_CFG_PARA, *proc_status, NULL, 0);
 
       reset_ofld_feature_88xx(adapter, HALMAC_FEATURE_CFG_PARA);
 
       return HALMAC_RET_SUCCESS;
   }
 
   status = send_cfg_param_h2c_88xx(adapter);
   if (status != HALMAC_RET_SUCCESS) {
       if (info->buf) {
           PLTFM_FREE(info->buf, info->buf_size);
           info->buf = NULL;
           info->buf_wptr = NULL;
       }
       return status;
   }
 
   if (end_cmd == 0) {
       PLTFM_MSG_TRACE("[TRACE]send h2c-buf full\n");
       return HALMAC_RET_PARA_SENDING;
   }
 
   return status;
}
 
static enum halmac_ret_status
send_cfg_param_h2c_88xx(struct halmac_adapter *adapter)
{
   u8 h2c_buf[H2C_PKT_SIZE_88XX] = { 0 };
   u16 pg_addr;
   u16 seq_num = 0;
   u32 info_size;
   struct halmac_h2c_header_info hdr_info;
   enum halmac_ret_status status = HALMAC_RET_SUCCESS;
   struct halmac_cfg_param_info *info = &adapter->cfg_param_info;
   enum halmac_cmd_process_status *proc_status;
 
   proc_status = &adapter->halmac_state.cfg_param_state.proc_status;
 
   if (cnv_cfg_param_state_88xx(adapter, HALMAC_CMD_CNSTR_H2C_SENT) !=
       HALMAC_RET_SUCCESS)
       return HALMAC_RET_ERROR_STATE;
 
   *proc_status = HALMAC_CMD_PROCESS_SENDING;
 
   if (info->full_fifo_mode == 1)
       pg_addr = 0;
   else
       pg_addr = adapter->txff_alloc.rsvd_h2c_info_addr;
 
   info_size = info->num * CFG_PARAM_H2C_INFO_SIZE;
 
   status = dl_rsvd_page_88xx(adapter, pg_addr, info->buf, info_size);
   if (status != HALMAC_RET_SUCCESS) {
       PLTFM_MSG_ERR("[ERR]dl rsvd pg!!\n");
       goto CFG_PARAM_H2C_FAIL;
   }
 
   gen_cfg_param_h2c_88xx(adapter, h2c_buf);
 
   hdr_info.sub_cmd_id = SUB_CMD_ID_CFG_PARAM;
   hdr_info.content_size = 4;
   hdr_info.ack = 1;
   set_h2c_pkt_hdr_88xx(adapter, h2c_buf, &hdr_info, &seq_num);
 
   adapter->halmac_state.cfg_param_state.seq_num = seq_num;
 
   status = send_h2c_pkt_88xx(adapter, h2c_buf);
 
   if (status != HALMAC_RET_SUCCESS) {
       PLTFM_MSG_ERR("[ERR]send h2c!!\n");
       reset_ofld_feature_88xx(adapter, HALMAC_FEATURE_CFG_PARA);
   }
 
CFG_PARAM_H2C_FAIL:
   PLTFM_FREE(info->buf, info->buf_size);
   info->buf = NULL;
   info->buf_wptr = NULL;
 
   if (cnv_cfg_param_state_88xx(adapter, HALMAC_CMD_CNSTR_IDLE) !=
       HALMAC_RET_SUCCESS)
       return HALMAC_RET_ERROR_STATE;
 
   return status;
}
 
static enum halmac_ret_status
cnv_cfg_param_state_88xx(struct halmac_adapter *adapter,
            enum halmac_cmd_construct_state dest_state)
{
   enum halmac_cmd_construct_state *state;
 
   state = &adapter->halmac_state.cfg_param_state.cmd_cnstr_state;
 
   if ((*state != HALMAC_CMD_CNSTR_IDLE) &&
       (*state != HALMAC_CMD_CNSTR_CNSTR) &&
       (*state != HALMAC_CMD_CNSTR_H2C_SENT))
       return HALMAC_RET_ERROR_STATE;
 
   if (dest_state == HALMAC_CMD_CNSTR_IDLE) {
       if (*state == HALMAC_CMD_CNSTR_CNSTR)
           return HALMAC_RET_ERROR_STATE;
   } else if (dest_state == HALMAC_CMD_CNSTR_CNSTR) {
       if (*state == HALMAC_CMD_CNSTR_H2C_SENT)
           return HALMAC_RET_ERROR_STATE;
   } else if (dest_state == HALMAC_CMD_CNSTR_H2C_SENT) {
       if ((*state == HALMAC_CMD_CNSTR_IDLE) ||
           (*state == HALMAC_CMD_CNSTR_H2C_SENT))
           return HALMAC_RET_ERROR_STATE;
   }
 
   *state = dest_state;
 
   return HALMAC_RET_SUCCESS;
}
 
static enum halmac_ret_status
add_param_buf_88xx(struct halmac_adapter *adapter,
          struct halmac_phy_parameter_info *param, u8 *buf,
          u8 *end_cmd)
{
   struct halmac_cfg_param_info *info = &adapter->cfg_param_info;
   union halmac_parameter_content *content = &param->content;
 
   *end_cmd = 0;
 
   PARAM_INFO_SET_LEN(buf, CFG_PARAM_H2C_INFO_SIZE);
   PARAM_INFO_SET_IO_CMD(buf, param->cmd_id);
 
   switch (param->cmd_id) {
   case HALMAC_PARAMETER_CMD_BB_W8:
   case HALMAC_PARAMETER_CMD_BB_W16:
   case HALMAC_PARAMETER_CMD_BB_W32:
   case HALMAC_PARAMETER_CMD_MAC_W8:
   case HALMAC_PARAMETER_CMD_MAC_W16:
   case HALMAC_PARAMETER_CMD_MAC_W32:
       PARAM_INFO_SET_IO_ADDR(buf, content->MAC_REG_W.offset);
       PARAM_INFO_SET_DATA(buf, content->MAC_REG_W.value);
       PARAM_INFO_SET_MASK(buf, content->MAC_REG_W.msk);
       PARAM_INFO_SET_MSK_EN(buf, content->MAC_REG_W.msk_en);
       info->value_accum += content->MAC_REG_W.value;
       info->offset_accum += content->MAC_REG_W.offset;
       break;
   case HALMAC_PARAMETER_CMD_RF_W:
       /*In rf register, the address is only 1 byte*/
       PARAM_INFO_SET_RF_ADDR(buf, content->RF_REG_W.offset);
       PARAM_INFO_SET_RF_PATH(buf, content->RF_REG_W.rf_path);
       PARAM_INFO_SET_DATA(buf, content->RF_REG_W.value);
       PARAM_INFO_SET_MASK(buf, content->RF_REG_W.msk);
       PARAM_INFO_SET_MSK_EN(buf, content->RF_REG_W.msk_en);
       info->value_accum += content->RF_REG_W.value;
       info->offset_accum += (content->RF_REG_W.offset +
                   (content->RF_REG_W.rf_path << 8));
       break;
   case HALMAC_PARAMETER_CMD_DELAY_US:
   case HALMAC_PARAMETER_CMD_DELAY_MS:
       PARAM_INFO_SET_DELAY_VAL(buf, content->DELAY_TIME.delay_time);
       break;
   case HALMAC_PARAMETER_CMD_END:
       *end_cmd = 1;
       break;
   default:
       PLTFM_MSG_ERR("[ERR]cmd id!!\n");
       break;
   }
 
   return HALMAC_RET_SUCCESS;
}
 
static enum halmac_ret_status
gen_cfg_param_h2c_88xx(struct halmac_adapter *adapter, u8 *buff)
{
   struct halmac_cfg_param_info *info = &adapter->cfg_param_info;
   u16 h2c_info_addr = adapter->txff_alloc.rsvd_h2c_info_addr;
   u16 rsvd_pg_addr = adapter->txff_alloc.rsvd_boundary;
 
   CFG_PARAM_SET_NUM(buff, info->num);
 
   if (info->full_fifo_mode == 1) {
       CFG_PARAM_SET_INIT_CASE(buff, 0x1);
       CFG_PARAM_SET_LOC(buff, 0);
   } else {
       CFG_PARAM_SET_INIT_CASE(buff, 0x0);
       CFG_PARAM_SET_LOC(buff, h2c_info_addr - rsvd_pg_addr);
   }
 
   return HALMAC_RET_SUCCESS;
}
 
static enum halmac_ret_status
malloc_cfg_param_buf_88xx(struct halmac_adapter *adapter, u8 full_fifo)
{
   struct halmac_cfg_param_info *info = &adapter->cfg_param_info;
   struct halmac_pltfm_cfg_info *pltfm_info = &adapter->pltfm_info;
 
   if (info->buf)
       return HALMAC_RET_SUCCESS;
 
   if (full_fifo == 1)
       info->buf_size = pltfm_info->malloc_size;
   else
       info->buf_size = CFG_PARAM_RSVDPG_SIZE;
 
   if (info->buf_size > pltfm_info->rsvd_pg_size)
       info->buf_size = pltfm_info->rsvd_pg_size;
 
   info->buf = smart_malloc_88xx(adapter, info->buf_size, &info->buf_size);
   if (info->buf) {
       PLTFM_MEMSET(info->buf, 0x00, info->buf_size);
       info->full_fifo_mode = full_fifo;
       info->buf_wptr = info->buf;
       info->num = 0;
       info->avl_buf_size = info->buf_size;
       info->value_accum = 0;
       info->offset_accum = 0;
   } else {
       return HALMAC_RET_MALLOC_FAIL;
   }
 
   return HALMAC_RET_SUCCESS;
}
 
/**
 * update_packet_88xx() - send specific packet to FW
 * @adapter : the adapter of halmac
 * @pkt_id : packet id, to know the purpose of this packet
 * @pkt : packet
 * @size : packet size
 *
 * Note : TX_DESC is not included in the pkt
 *
 * Author : KaiYuan Chang/Ivan Lin
 * Return : enum halmac_ret_status
 * More details of status code can be found in prototype document
 */
enum halmac_ret_status
update_packet_88xx(struct halmac_adapter *adapter, enum halmac_packet_id pkt_id,
          u8 *pkt, u32 size)
{
   enum halmac_ret_status status = HALMAC_RET_SUCCESS;
   enum halmac_cmd_process_status *proc_status =
       &adapter->halmac_state.update_pkt_state.proc_status;
   u8 *used_page = &adapter->halmac_state.update_pkt_state.used_page;
 
   if (halmac_fw_validate(adapter) != HALMAC_RET_SUCCESS)
       return HALMAC_RET_NO_DLFW;
 
   if (adapter->fw_ver.h2c_version < 4)
       return HALMAC_RET_FW_NO_SUPPORT;
 
   if (size > UPDATE_PKT_RSVDPG_SIZE)
       return HALMAC_RET_RSVD_PG_OVERFLOW_FAIL;
 
   PLTFM_MSG_TRACE("[TRACE]%s ===>\n", __func__);
 
   if (*proc_status == HALMAC_CMD_PROCESS_SENDING) {
       PLTFM_MSG_TRACE("[TRACE]Wait event(upd)\n");
       return HALMAC_RET_BUSY_STATE;
   }
 
   *proc_status = HALMAC_CMD_PROCESS_SENDING;
 
   status = send_h2c_update_packet_88xx(adapter, pkt_id, pkt, size);
   if (status != HALMAC_RET_SUCCESS) {
       PLTFM_MSG_ERR("[ERR]send h2c!!\n");
       PLTFM_MSG_ERR("[ERR]pkt id : %X!!\n", pkt_id);
       return status;
   }
 
   *used_page = (u8)get_update_packet_page_size(adapter, size);
 
   if (packet_in_nlo_88xx(adapter, pkt_id)) {
       *proc_status = HALMAC_CMD_PROCESS_DONE;
       adapter->nlo_flag = 1;
   }
 
   PLTFM_MSG_TRACE("[TRACE]%s <===\n", __func__);
 
   return HALMAC_RET_SUCCESS;
}
 
static enum halmac_ret_status
send_h2c_update_packet_88xx(struct halmac_adapter *adapter,
               enum halmac_packet_id pkt_id, u8 *pkt, u32 size)
{
   u8 h2c_buf[H2C_PKT_SIZE_88XX] = { 0 };
   u16 seq_num = 0;
   u16 pg_addr = adapter->txff_alloc.rsvd_h2c_info_addr;
   u16 pg_offset;
   struct halmac_h2c_header_info hdr_info;
   enum halmac_ret_status status = HALMAC_RET_SUCCESS;
   enum halmac_packet_id real_pkt_id;
 
   status = dl_rsvd_page_88xx(adapter, pg_addr, pkt, size);
   if (status != HALMAC_RET_SUCCESS) {
       PLTFM_MSG_ERR("[ERR]dl rsvd pg!!\n");
       return status;
   }
 
   real_pkt_id = get_real_pkt_id_88xx(adapter, pkt_id);
   pg_offset = pg_addr - adapter->txff_alloc.rsvd_boundary;
   UPDATE_PKT_SET_SIZE(h2c_buf, size + adapter->hw_cfg_info.txdesc_size);
   UPDATE_PKT_SET_ID(h2c_buf, real_pkt_id);
   UPDATE_PKT_SET_LOC(h2c_buf, pg_offset);
 
   hdr_info.sub_cmd_id = SUB_CMD_ID_UPDATE_PKT;
   hdr_info.content_size = 4;
   if (packet_in_nlo_88xx(adapter, pkt_id))
       hdr_info.ack = 0;
   else
       hdr_info.ack = 1;
   set_h2c_pkt_hdr_88xx(adapter, h2c_buf, &hdr_info, &seq_num);
   adapter->halmac_state.update_pkt_state.seq_num = seq_num;
 
   status = send_h2c_pkt_88xx(adapter, h2c_buf);
 
   if (status != HALMAC_RET_SUCCESS) {
       PLTFM_MSG_ERR("[ERR]send h2c!!\n");
       reset_ofld_feature_88xx(adapter, HALMAC_FEATURE_UPDATE_PACKET);
       return status;
   }
 
   return status;
}
 
enum halmac_ret_status
send_scan_packet_88xx(struct halmac_adapter *adapter, u8 index,
             u8 *pkt, u32 size)
{
   enum halmac_ret_status status = HALMAC_RET_SUCCESS;
   enum halmac_cmd_process_status *proc_status =
       &adapter->halmac_state.scan_pkt_state.proc_status;
 
   if (halmac_fw_validate(adapter) != HALMAC_RET_SUCCESS)
       return HALMAC_RET_NO_DLFW;
 
   if (adapter->fw_ver.h2c_version < 13)
       return HALMAC_RET_FW_NO_SUPPORT;
 
   if (size > UPDATE_PKT_RSVDPG_SIZE)
       return HALMAC_RET_RSVD_PG_OVERFLOW_FAIL;
 
   PLTFM_MSG_TRACE("[TRACE]%s ===>\n", __func__);
 
   if (*proc_status == HALMAC_CMD_PROCESS_SENDING) {
       PLTFM_MSG_TRACE("[TRACE]Wait event(send_scan)\n");
       return HALMAC_RET_BUSY_STATE;
   }
 
   *proc_status = HALMAC_CMD_PROCESS_SENDING;
 
   status = send_h2c_send_scan_packet_88xx(adapter, index, pkt, size);
   if (status != HALMAC_RET_SUCCESS)
       return status;
 
   PLTFM_MSG_TRACE("[TRACE]%s <===\n", __func__);
 
   return HALMAC_RET_SUCCESS;
}
 
static enum halmac_ret_status
send_h2c_send_scan_packet_88xx(struct halmac_adapter *adapter,
                  u8 index, u8 *pkt, u32 size)
{
   u8 h2c_buf[H2C_PKT_SIZE_88XX] = { 0 };
   u16 seq_num = 0;
   u16 pg_addr = adapter->txff_alloc.rsvd_h2c_info_addr;
   u16 pg_offset;
   struct halmac_h2c_header_info hdr_info;
   enum halmac_ret_status status = HALMAC_RET_SUCCESS;
 
   status = dl_rsvd_page_88xx(adapter, pg_addr, pkt, size);
   if (status != HALMAC_RET_SUCCESS) {
       PLTFM_MSG_ERR("[ERR]dl rsvd pg!!\n");
       return status;
   }
 
   pg_offset = pg_addr - adapter->txff_alloc.rsvd_boundary;
   SEND_SCAN_PKT_SET_SIZE(h2c_buf, size +
                  adapter->hw_cfg_info.txdesc_size);
   SEND_SCAN_PKT_SET_INDEX(h2c_buf, index);
   SEND_SCAN_PKT_SET_LOC(h2c_buf, pg_offset);
 
   hdr_info.sub_cmd_id = SUB_CMD_ID_SEND_SCAN_PKT;
   hdr_info.content_size = 8;
   hdr_info.ack = 1;
   set_h2c_pkt_hdr_88xx(adapter, h2c_buf, &hdr_info, &seq_num);
   adapter->halmac_state.scan_pkt_state.seq_num = seq_num;
 
   status = send_h2c_pkt_88xx(adapter, h2c_buf);
 
   if (status != HALMAC_RET_SUCCESS) {
       PLTFM_MSG_ERR("[ERR]send h2c!!\n");
       reset_ofld_feature_88xx(adapter,
                   HALMAC_FEATURE_SEND_SCAN_PACKET);
       return status;
   }
 
   return status;
}
 
enum halmac_ret_status
drop_scan_packet_88xx(struct halmac_adapter *adapter,
             struct halmac_drop_pkt_option *option)
{
   enum halmac_ret_status status = HALMAC_RET_SUCCESS;
   enum halmac_cmd_process_status *proc_status =
       &adapter->halmac_state.drop_pkt_state.proc_status;
 
   if (halmac_fw_validate(adapter) != HALMAC_RET_SUCCESS)
       return HALMAC_RET_NO_DLFW;
 
   if (adapter->fw_ver.h2c_version < 13)
       return HALMAC_RET_FW_NO_SUPPORT;
 
   PLTFM_MSG_TRACE("[TRACE]%s ===>\n", __func__);
 
   if (*proc_status == HALMAC_CMD_PROCESS_SENDING) {
       PLTFM_MSG_TRACE("[TRACE]Wait event(drop_scan)\n");
       return HALMAC_RET_BUSY_STATE;
   }
 
   *proc_status = HALMAC_CMD_PROCESS_SENDING;
 
   status = send_h2c_drop_scan_packet_88xx(adapter, option);
   if (status != HALMAC_RET_SUCCESS)
       return status;
 
   PLTFM_MSG_TRACE("[TRACE]%s <===\n", __func__);
 
   return HALMAC_RET_SUCCESS;
}
 
static enum halmac_ret_status
send_h2c_drop_scan_packet_88xx(struct halmac_adapter *adapter,
                  struct halmac_drop_pkt_option *option)
{
   u8 h2c_buf[H2C_PKT_SIZE_88XX] = { 0 };
   u16 seq_num = 0;
   struct halmac_h2c_header_info hdr_info;
   enum halmac_ret_status status = HALMAC_RET_SUCCESS;
 
   PLTFM_MSG_TRACE("[TRACE]%s\n", __func__);
 
   DROP_SCAN_PKT_SET_DROP_ALL(h2c_buf, option->drop_all);
   DROP_SCAN_PKT_SET_DROP_SINGLE(h2c_buf, option->drop_single);
   DROP_SCAN_PKT_SET_DROP_IDX(h2c_buf, option->drop_index);
 
   hdr_info.sub_cmd_id = SUB_CMD_ID_DROP_SCAN_PKT;
   hdr_info.content_size = 8;
   hdr_info.ack = 1;
   set_h2c_pkt_hdr_88xx(adapter, h2c_buf, &hdr_info, &seq_num);
   adapter->halmac_state.drop_pkt_state.seq_num = seq_num;
 
   status = send_h2c_pkt_88xx(adapter, h2c_buf);
 
   if (status != HALMAC_RET_SUCCESS) {
       PLTFM_MSG_ERR("[ERR]send h2c!!\n");
       reset_ofld_feature_88xx(adapter,
                   HALMAC_FEATURE_DROP_SCAN_PACKET);
       return status;
   }
 
   return status;
}
 
enum halmac_ret_status
bcn_ie_filter_88xx(struct halmac_adapter *adapter,
          struct halmac_bcn_ie_info *info)
{
   return HALMAC_RET_NOT_SUPPORT;
}
 
enum halmac_ret_status
update_datapack_88xx(struct halmac_adapter *adapter,
            enum halmac_data_type data_type,
            struct halmac_phy_parameter_info *info)
{
   return HALMAC_RET_NOT_SUPPORT;
}
 
enum halmac_ret_status
run_datapack_88xx(struct halmac_adapter *adapter,
         enum halmac_data_type data_type)
{
   return HALMAC_RET_NOT_SUPPORT;
}
 
enum halmac_ret_status
send_bt_coex_88xx(struct halmac_adapter *adapter, u8 *buf, u32 size, u8 ack)
{
   enum halmac_ret_status status = HALMAC_RET_SUCCESS;
 
   if (halmac_fw_validate(adapter) != HALMAC_RET_SUCCESS)
       return HALMAC_RET_NO_DLFW;
 
   PLTFM_MSG_TRACE("[TRACE]%s ===>\n", __func__);
 
   status = send_bt_coex_cmd_88xx(adapter, buf, size, ack);
 
   if (status != HALMAC_RET_SUCCESS) {
       PLTFM_MSG_ERR("[ERR]bt coex cmd!!\n");
       return status;
   }
 
   PLTFM_MSG_TRACE("[TRACE]%s <===\n", __func__);
 
   return HALMAC_RET_SUCCESS;
}
 
static enum halmac_ret_status
send_bt_coex_cmd_88xx(struct halmac_adapter *adapter, u8 *buf, u32 size,
             u8 ack)
{
   u8 h2c_buf[H2C_PKT_SIZE_88XX] = { 0 };
   u16 seq_num = 0;
   struct halmac_h2c_header_info hdr_info;
   enum halmac_ret_status status = HALMAC_RET_SUCCESS;
 
   PLTFM_MSG_TRACE("[TRACE]%s ===>\n", __func__);
 
   PLTFM_MEMCPY(h2c_buf + 8, buf, size);
 
   hdr_info.sub_cmd_id = SUB_CMD_ID_BT_COEX;
   hdr_info.content_size = (u16)size;
   hdr_info.ack = ack;
   set_h2c_pkt_hdr_88xx(adapter, h2c_buf, &hdr_info, &seq_num);
 
   status = send_h2c_pkt_88xx(adapter, h2c_buf);
 
   if (status != HALMAC_RET_SUCCESS) {
       PLTFM_MSG_ERR("[ERR]send h2c!!\n");
       return status;
   }
 
   return HALMAC_RET_SUCCESS;
}
 
/**
 * dump_fifo_88xx() - dump fifo data
 * @adapter : the adapter of halmac
 * @sel : FIFO selection
 * @start_addr : start address of selected FIFO
 * @size : dump size of selected FIFO
 * @data : FIFO data
 *
 * Note : before dump fifo, user need to call halmac_get_fifo_size to
 * get fifo size. Then input this size to halmac_dump_fifo.
 *
 * Author : Ivan Lin/KaiYuan Chang
 * Return : enum halmac_ret_status
 * More details of status code can be found in prototype document
 */
enum halmac_ret_status
dump_fifo_88xx(struct halmac_adapter *adapter, enum hal_fifo_sel sel,
          u32 start_addr, u32 size, u8 *data)
{
   enum halmac_ret_status status = HALMAC_RET_SUCCESS;
   u8 tmp8;
   u8 enable;
   struct halmac_api *api = (struct halmac_api *)adapter->halmac_api;
 
   PLTFM_MSG_TRACE("[TRACE]%s ===>\n", __func__);
 
   if (sel == HAL_FIFO_SEL_TX &&
       (start_addr + size) > adapter->hw_cfg_info.tx_fifo_size) {
       PLTFM_MSG_ERR("[ERR]size overflow!!\n");
       return HALMAC_RET_DUMP_FIFOSIZE_INCORRECT;
   }
 
   if (sel == HAL_FIFO_SEL_RX &&
       (start_addr + size) > adapter->hw_cfg_info.rx_fifo_size) {
       PLTFM_MSG_ERR("[ERR]size overflow!!\n");
       return HALMAC_RET_DUMP_FIFOSIZE_INCORRECT;
   }
 
   if ((size & (4 - 1)) != 0) {
       PLTFM_MSG_ERR("[ERR]not 4byte alignment!!\n");
       return HALMAC_RET_DUMP_FIFOSIZE_INCORRECT;
   }
 
   if (!data)
       return HALMAC_RET_NULL_POINTER;
 
   tmp8 = HALMAC_REG_R8(REG_RCR + 2);
   enable = 0;
   status = api->halmac_set_hw_value(adapter, HALMAC_HW_RX_CLK_GATE,
                     &enable);
   if (status != HALMAC_RET_SUCCESS)
       return status;
   status = read_buf_88xx(adapter, start_addr, size, sel, data);
 
   HALMAC_REG_W8(REG_RCR + 2, tmp8);
 
   if (status != HALMAC_RET_SUCCESS) {
       PLTFM_MSG_ERR("[ERR]read buf!!\n");
       return status;
   }
 
   PLTFM_MSG_TRACE("[TRACE]%s <===\n", __func__);
 
   return HALMAC_RET_SUCCESS;
}
 
static enum halmac_ret_status
read_buf_88xx(struct halmac_adapter *adapter, u32 offset, u32 size,
         enum hal_fifo_sel sel, u8 *data)
{
   u32 start_pg;
   u32 value32;
   u32 i;
   u32 residue;
   u32 cnt = 0;
   struct halmac_api *api = (struct halmac_api *)adapter->halmac_api;
 
   if (sel == HAL_FIFO_SEL_RSVD_PAGE)
       offset += (adapter->txff_alloc.rsvd_boundary <<
              TX_PAGE_SIZE_SHIFT_88XX);
 
   start_pg = offset >> 12;
   residue = offset & (4096 - 1);
 
   if (sel == HAL_FIFO_SEL_TX || sel == HAL_FIFO_SEL_RSVD_PAGE)
       start_pg += 0x780;
   else if (sel == HAL_FIFO_SEL_RX)
       start_pg += 0x700;
   else if (sel == HAL_FIFO_SEL_REPORT)
       start_pg += 0x660;
   else if (sel == HAL_FIFO_SEL_LLT)
       start_pg += 0x650;
   else if (sel == HAL_FIFO_SEL_RXBUF_FW)
       start_pg += 0x680;
   else
       return HALMAC_RET_NOT_SUPPORT;
 
   value32 = HALMAC_REG_R16(REG_PKTBUF_DBG_CTRL) & 0xF000;
 
   do {
       HALMAC_REG_W16(REG_PKTBUF_DBG_CTRL, (u16)(start_pg | value32));
 
       for (i = 0x8000 + residue; i <= 0x8FFF; i += 4) {
           *(u32 *)(data + cnt) = HALMAC_REG_R32(i);
           *(u32 *)(data + cnt) =
               rtk_le32_to_cpu(*(u32 *)(data + cnt));
           cnt += 4;
           if (size == cnt)
               goto HALMAC_BUF_READ_OK;
       }
 
       residue = 0;
       start_pg++;
   } while (1);
 
HALMAC_BUF_READ_OK:
   HALMAC_REG_W16(REG_PKTBUF_DBG_CTRL, (u16)value32);
 
   return HALMAC_RET_SUCCESS;
}
 
/**
 * get_fifo_size_88xx() - get fifo size
 * @adapter : the adapter of halmac
 * @sel : FIFO selection
 * Author : Ivan Lin/KaiYuan Chang
 * Return : u32
 * More details of status code can be found in prototype document
 */
u32
get_fifo_size_88xx(struct halmac_adapter *adapter, enum hal_fifo_sel sel)
{
   u32 size = 0;
 
   if (sel == HAL_FIFO_SEL_TX)
       size = adapter->hw_cfg_info.tx_fifo_size;
   else if (sel == HAL_FIFO_SEL_RX)
       size = adapter->hw_cfg_info.rx_fifo_size;
   else if (sel == HAL_FIFO_SEL_RSVD_PAGE)
       size = adapter->hw_cfg_info.tx_fifo_size -
              (adapter->txff_alloc.rsvd_boundary <<
           TX_PAGE_SIZE_SHIFT_88XX);
   else if (sel == HAL_FIFO_SEL_REPORT)
       size = 65536;
   else if (sel == HAL_FIFO_SEL_LLT)
       size = 65536;
   else if (sel == HAL_FIFO_SEL_RXBUF_FW)
       size = RX_BUF_FW_88XX;
 
   return size;
}
 
enum halmac_ret_status
set_h2c_header_88xx(struct halmac_adapter *adapter, u8 *hdr, u16 *seq, u8 ack)
{
   PLTFM_MSG_TRACE("[TRACE]%s!!\n", __func__);
 
   H2C_CMD_HEADER_SET_CATEGORY(hdr, 0x00);
   H2C_CMD_HEADER_SET_TOTAL_LEN(hdr, 16);
 
   PLTFM_MUTEX_LOCK(&adapter->h2c_seq_mutex);
   H2C_CMD_HEADER_SET_SEQ_NUM(hdr, adapter->h2c_info.seq_num);
   *seq = adapter->h2c_info.seq_num;
   (adapter->h2c_info.seq_num)++;
   PLTFM_MUTEX_UNLOCK(&adapter->h2c_seq_mutex);
 
   if (ack == 1)
       H2C_CMD_HEADER_SET_ACK(hdr, 1);
 
   return HALMAC_RET_SUCCESS;
}
 
/**
 * add_ch_info_88xx() -add channel information
 * @adapter : the adapter of halmac
 * @info : channel information
 * Author : KaiYuan Chang/Ivan Lin
 * Return : enum halmac_ret_status
 * More details of status code can be found in prototype document
 */
enum halmac_ret_status
add_ch_info_88xx(struct halmac_adapter *adapter, struct halmac_ch_info *info)
{
   struct halmac_ch_sw_info *ch_sw_info = &adapter->ch_sw_info;
   enum halmac_cmd_construct_state state;
 
   PLTFM_MSG_TRACE("[TRACE]%s ===>\n", __func__);
 
   if (adapter->halmac_state.dlfw_state != HALMAC_GEN_INFO_SENT) {
       PLTFM_MSG_ERR("[ERR]gen info\n");
       return HALMAC_RET_GEN_INFO_NOT_SENT;
   }
 
   state = scan_cmd_cnstr_state_88xx(adapter);
   if (state != HALMAC_CMD_CNSTR_BUF_CLR &&
       state != HALMAC_CMD_CNSTR_CNSTR) {
       PLTFM_MSG_WARN("[WARN]cmd state (scan)\n");
       return HALMAC_RET_ERROR_STATE;
   }
 
   if (!ch_sw_info->buf) {
       ch_sw_info->buf = (u8 *)PLTFM_MALLOC(SCAN_INFO_RSVDPG_SIZE);
       if (!ch_sw_info->buf)
           return HALMAC_RET_NULL_POINTER;
       ch_sw_info->buf_wptr = ch_sw_info->buf;
       ch_sw_info->buf_size = SCAN_INFO_RSVDPG_SIZE;
       ch_sw_info->avl_buf_size = SCAN_INFO_RSVDPG_SIZE;
       ch_sw_info->total_size = 0;
       ch_sw_info->extra_info_en = 0;
       ch_sw_info->ch_num = 0;
   }
 
   if (ch_sw_info->extra_info_en == 1) {
       PLTFM_MSG_ERR("[ERR]extra info = 1!!\n");
       return HALMAC_RET_CH_SW_SEQ_WRONG;
   }
 
   if (ch_sw_info->avl_buf_size < 4) {
       PLTFM_MSG_ERR("[ERR]buf full!!\n");
       return HALMAC_RET_CH_SW_NO_BUF;
   }
 
   if (cnv_scan_state_88xx(adapter, HALMAC_CMD_CNSTR_CNSTR) !=
       HALMAC_RET_SUCCESS)
       return HALMAC_RET_ERROR_STATE;
 
   CH_INFO_SET_CH(ch_sw_info->buf_wptr, info->channel);
   CH_INFO_SET_PRI_CH_IDX(ch_sw_info->buf_wptr, info->pri_ch_idx);
   CH_INFO_SET_BW(ch_sw_info->buf_wptr, info->bw);
   CH_INFO_SET_TIMEOUT(ch_sw_info->buf_wptr, info->timeout);
   CH_INFO_SET_ACTION_ID(ch_sw_info->buf_wptr, info->action_id);
   CH_INFO_SET_EXTRA_INFO(ch_sw_info->buf_wptr, info->extra_info);
 
   ch_sw_info->avl_buf_size = ch_sw_info->avl_buf_size - 4;
   ch_sw_info->total_size = ch_sw_info->total_size + 4;
   ch_sw_info->ch_num++;
   ch_sw_info->extra_info_en = info->extra_info;
   ch_sw_info->buf_wptr = ch_sw_info->buf_wptr + 4;
 
   PLTFM_MSG_TRACE("[TRACE]%s <===\n", __func__);
 
   return HALMAC_RET_SUCCESS;
}
 
static enum halmac_cmd_construct_state
scan_cmd_cnstr_state_88xx(struct halmac_adapter *adapter)
{
   return adapter->halmac_state.scan_state.cmd_cnstr_state;
}
 
static enum halmac_ret_status
cnv_scan_state_88xx(struct halmac_adapter *adapter,
           enum halmac_cmd_construct_state dest_state)
{
   enum halmac_cmd_construct_state *state;
 
   state = &adapter->halmac_state.scan_state.cmd_cnstr_state;
 
   if (dest_state == HALMAC_CMD_CNSTR_IDLE) {
       if ((*state == HALMAC_CMD_CNSTR_BUF_CLR) ||
           (*state == HALMAC_CMD_CNSTR_CNSTR))
           return HALMAC_RET_ERROR_STATE;
   } else if (dest_state == HALMAC_CMD_CNSTR_BUF_CLR) {
       if (*state == HALMAC_CMD_CNSTR_H2C_SENT)
           return HALMAC_RET_ERROR_STATE;
   } else if (dest_state == HALMAC_CMD_CNSTR_CNSTR) {
       if ((*state == HALMAC_CMD_CNSTR_IDLE) ||
           (*state == HALMAC_CMD_CNSTR_H2C_SENT))
           return HALMAC_RET_ERROR_STATE;
   } else if (dest_state == HALMAC_CMD_CNSTR_H2C_SENT) {
       if ((*state != HALMAC_CMD_CNSTR_CNSTR) &&
           (*state != HALMAC_CMD_CNSTR_BUF_CLR))
           return HALMAC_RET_ERROR_STATE;
   }
 
   *state = dest_state;
 
   return HALMAC_RET_SUCCESS;
}
 
/**
 * add_extra_ch_info_88xx() -add extra channel information
 * @adapter : the adapter of halmac
 * @info : extra channel information
 * Author : KaiYuan Chang/Ivan Lin
 * Return : enum halmac_ret_status
 * More details of status code can be found in prototype document
 */
enum halmac_ret_status
add_extra_ch_info_88xx(struct halmac_adapter *adapter,
              struct halmac_ch_extra_info *info)
{
   struct halmac_ch_sw_info *ch_sw_info = &adapter->ch_sw_info;
 
   PLTFM_MSG_TRACE("[TRACE]%s ===>\n", __func__);
 
   if (!ch_sw_info->buf) {
       PLTFM_MSG_ERR("[ERR]buf = null!!\n");
       return HALMAC_RET_CH_SW_SEQ_WRONG;
   }
 
   if (ch_sw_info->extra_info_en == 0) {
       PLTFM_MSG_ERR("[ERR]extra info = 0!!\n");
       return HALMAC_RET_CH_SW_SEQ_WRONG;
   }
 
   if (ch_sw_info->avl_buf_size < (u32)(info->extra_info_size + 2)) {
       PLTFM_MSG_ERR("[ERR]no available buffer!!\n");
       return HALMAC_RET_CH_SW_NO_BUF;
   }
 
   if (scan_cmd_cnstr_state_88xx(adapter) != HALMAC_CMD_CNSTR_CNSTR) {
       PLTFM_MSG_WARN("[WARN]cmd state (ex scan)\n");
       return HALMAC_RET_ERROR_STATE;
   }
 
   if (cnv_scan_state_88xx(adapter, HALMAC_CMD_CNSTR_CNSTR) !=
       HALMAC_RET_SUCCESS)
       return HALMAC_RET_ERROR_STATE;
 
   CH_EXTRA_INFO_SET_ID(ch_sw_info->buf_wptr, info->extra_action_id);
   CH_EXTRA_INFO_SET_INFO(ch_sw_info->buf_wptr, info->extra_info);
   CH_EXTRA_INFO_SET_SIZE(ch_sw_info->buf_wptr, info->extra_info_size);
   PLTFM_MEMCPY(ch_sw_info->buf_wptr + 2, info->extra_info_data,
            info->extra_info_size);
 
   ch_sw_info->avl_buf_size -= (2 + info->extra_info_size);
   ch_sw_info->total_size += (2 + info->extra_info_size);
   ch_sw_info->extra_info_en = info->extra_info;
   ch_sw_info->buf_wptr += (2 + info->extra_info_size);
 
   PLTFM_MSG_TRACE("[TRACE]%s <===\n", __func__);
 
   return HALMAC_RET_SUCCESS;
}
 
/**
 * ctrl_ch_switch_88xx() -send channel switch cmd
 * @adapter : the adapter of halmac
 * @opt : channel switch config
 * Author : KaiYuan Chang/Ivan Lin
 * Return : enum halmac_ret_status
 * More details of status code can be found in prototype document
 */
enum halmac_ret_status
ctrl_ch_switch_88xx(struct halmac_adapter *adapter,
           struct halmac_ch_switch_option *opt)
{
   enum halmac_ret_status status = HALMAC_RET_SUCCESS;
   enum halmac_cmd_construct_state state;
   enum halmac_cmd_process_status *proc_status;
 
   proc_status = &adapter->halmac_state.scan_state.proc_status;
 
   if (halmac_fw_validate(adapter) != HALMAC_RET_SUCCESS)
       return HALMAC_RET_NO_DLFW;
 
   if (adapter->fw_ver.h2c_version < 4)
       return HALMAC_RET_FW_NO_SUPPORT;
 
   if (adapter->ch_sw_info.total_size +
       (adapter->halmac_state.update_pkt_state.used_page <<
       TX_PAGE_SIZE_SHIFT_88XX) >
       (u32)adapter->txff_alloc.rsvd_pg_num << TX_PAGE_SIZE_SHIFT_88XX)
       return HALMAC_RET_RSVD_PG_OVERFLOW_FAIL;
 
   PLTFM_MSG_TRACE("[TRACE]%s ===>\n", __func__);
 
   if (opt->switch_en == 0)
       *proc_status = HALMAC_CMD_PROCESS_IDLE;
 
   if ((*proc_status == HALMAC_CMD_PROCESS_SENDING) ||
       (*proc_status == HALMAC_CMD_PROCESS_RCVD)) {
       PLTFM_MSG_TRACE("[TRACE]Wait event(scan)\n");
       return HALMAC_RET_BUSY_STATE;
   }
 
   state = scan_cmd_cnstr_state_88xx(adapter);
   if (opt->switch_en == 1) {
       if (state != HALMAC_CMD_CNSTR_CNSTR) {
           PLTFM_MSG_ERR("[ERR]state(en = 1)\n");
           return HALMAC_RET_ERROR_STATE;
       }
   } else {
       if (state != HALMAC_CMD_CNSTR_BUF_CLR) {
           PLTFM_MSG_ERR("[ERR]state(en = 0)\n");
           return HALMAC_RET_ERROR_STATE;
       }
   }
 
   status = proc_ctrl_ch_switch_88xx(adapter, opt);
   if (status != HALMAC_RET_SUCCESS) {
       PLTFM_MSG_ERR("[ERR]ctrl ch sw!!\n");
       return status;
   }
 
   PLTFM_MSG_TRACE("[TRACE]%s <===\n", __func__);
 
   return HALMAC_RET_SUCCESS;
}
 
static enum halmac_ret_status
proc_ctrl_ch_switch_88xx(struct halmac_adapter *adapter,
            struct halmac_ch_switch_option *opt)
{
   u8 h2c_buf[H2C_PKT_SIZE_88XX] = { 0 };
   u16 seq_num = 0;
   u16 pg_addr = adapter->txff_alloc.rsvd_h2c_info_addr;
   struct halmac_h2c_header_info hdr_info;
   struct halmac_scan_rpt_info *scan_rpt_info = &adapter->scan_rpt_info;
   enum halmac_ret_status status = HALMAC_RET_SUCCESS;
   enum halmac_cmd_process_status *proc_status;
 
   proc_status = &adapter->halmac_state.scan_state.proc_status;
 
   PLTFM_MSG_TRACE("[TRACE]%s ===>\n", __func__);
 
   if (adapter->halmac_state.update_pkt_state.used_page > 0 &&
       opt->nlo_en == 1 && adapter->nlo_flag != 1)
       PLTFM_MSG_WARN("[WARN]probe req is NOT nlo pkt!!\n");
 
   if (cnv_scan_state_88xx(adapter, HALMAC_CMD_CNSTR_H2C_SENT) !=
       HALMAC_RET_SUCCESS)
       return HALMAC_RET_ERROR_STATE;
 
   *proc_status = HALMAC_CMD_PROCESS_SENDING;
 
   if (opt->switch_en != 0) {
       pg_addr += adapter->halmac_state.update_pkt_state.used_page;
       status = dl_rsvd_page_88xx(adapter, pg_addr,
                      adapter->ch_sw_info.buf,
                      adapter->ch_sw_info.total_size);
       if (status != HALMAC_RET_SUCCESS) {
           PLTFM_MSG_ERR("[ERR]dl rsvd pg!!\n");
           return status;
       }
       adapter->halmac_state.update_pkt_state.used_page = 0;
   }
 
   CH_SWITCH_SET_START(h2c_buf, opt->switch_en);
   CH_SWITCH_SET_CH_NUM(h2c_buf, adapter->ch_sw_info.ch_num);
   CH_SWITCH_SET_INFO_LOC(h2c_buf,
                  pg_addr - adapter->txff_alloc.rsvd_boundary);
   CH_SWITCH_SET_DEST_CH_EN(h2c_buf, opt->dest_ch_en);
   CH_SWITCH_SET_DEST_CH(h2c_buf, opt->dest_ch);
   CH_SWITCH_SET_PRI_CH_IDX(h2c_buf, opt->dest_pri_ch_idx);
   CH_SWITCH_SET_ABSOLUTE_TIME(h2c_buf, opt->absolute_time_en);
   CH_SWITCH_SET_TSF_LOW(h2c_buf, opt->tsf_low);
   CH_SWITCH_SET_PERIODIC_OPT(h2c_buf, opt->periodic_option);
   CH_SWITCH_SET_NORMAL_CYCLE(h2c_buf, opt->normal_cycle);
   CH_SWITCH_SET_NORMAL_PERIOD(h2c_buf, opt->normal_period);
   CH_SWITCH_SET_SLOW_PERIOD(h2c_buf, opt->phase_2_period);
   CH_SWITCH_SET_NORMAL_PERIOD_SEL(h2c_buf, opt->normal_period_sel);
   CH_SWITCH_SET_SLOW_PERIOD_SEL(h2c_buf, opt->phase_2_period_sel);
   CH_SWITCH_SET_SCAN_MODE(h2c_buf, opt->scan_mode_en);
   CH_SWITCH_SET_INFO_SIZE(h2c_buf, adapter->ch_sw_info.total_size);
 
   hdr_info.sub_cmd_id = SUB_CMD_ID_CH_SWITCH;
   hdr_info.content_size = 20;
   if (opt->nlo_en == 1)
       hdr_info.ack = 0;
   else
       hdr_info.ack = 1;
 
   if (opt->scan_mode_en == 1) {
       adapter->ch_sw_info.scan_mode = 1;
       if (!scan_rpt_info->buf) {
           scan_rpt_info->buf =
               (u8 *)PLTFM_MALLOC(SCAN_INFO_RSVDPG_SIZE);
           if (!scan_rpt_info->buf)
               return HALMAC_RET_NULL_POINTER;
       } else {
           PLTFM_MEMSET(scan_rpt_info->buf, 0,
                    SCAN_INFO_RSVDPG_SIZE);
       }
       scan_rpt_info->buf_wptr = scan_rpt_info->buf;
       scan_rpt_info->buf_size = SCAN_INFO_RSVDPG_SIZE;
       scan_rpt_info->avl_buf_size = SCAN_INFO_RSVDPG_SIZE;
       scan_rpt_info->total_size = 0;
       scan_rpt_info->ack_tsf_high = 0;
       scan_rpt_info->ack_tsf_low = 0;
       scan_rpt_info->rpt_tsf_high = 0;
       scan_rpt_info->rpt_tsf_low = 0;
   } else {
       adapter->ch_sw_info.scan_mode = 0;
       if (!scan_rpt_info->buf)
           PLTFM_FREE(scan_rpt_info->buf, scan_rpt_info->buf_size);
       scan_rpt_info->buf_wptr = NULL;
       scan_rpt_info->buf_size = 0;
       scan_rpt_info->avl_buf_size = 0;
       scan_rpt_info->total_size = 0;
   }
 
   set_h2c_pkt_hdr_88xx(adapter, h2c_buf, &hdr_info, &seq_num);
   adapter->halmac_state.scan_state.seq_num = seq_num;
 
   status = send_h2c_pkt_88xx(adapter, h2c_buf);
 
   if (status != HALMAC_RET_SUCCESS) {
       PLTFM_MSG_ERR("[ERR]send h2c!!\n");
       reset_ofld_feature_88xx(adapter, HALMAC_FEATURE_CHANNEL_SWITCH);
   }
   PLTFM_FREE(adapter->ch_sw_info.buf, adapter->ch_sw_info.buf_size);
   adapter->ch_sw_info.buf = NULL;
   adapter->ch_sw_info.buf_wptr = NULL;
   adapter->ch_sw_info.extra_info_en = 0;
   adapter->ch_sw_info.buf_size = 0;
   adapter->ch_sw_info.avl_buf_size = 0;
   adapter->ch_sw_info.total_size = 0;
   adapter->ch_sw_info.ch_num = 0;
 
   if (cnv_scan_state_88xx(adapter, HALMAC_CMD_CNSTR_IDLE) !=
       HALMAC_RET_SUCCESS)
       return HALMAC_RET_ERROR_STATE;
 
   adapter->nlo_flag = 0;
 
   return status;
}
 
/**
 * clear_ch_info_88xx() -clear channel information
 * @adapter : the adapter of halmac
 * Author : KaiYuan Chang/Ivan Lin
 * Return : enum halmac_ret_status
 * More details of status code can be found in prototype document
 */
enum halmac_ret_status
clear_ch_info_88xx(struct halmac_adapter *adapter)
{
   PLTFM_MSG_TRACE("[TRACE]%s ===>\n", __func__);
 
   if (scan_cmd_cnstr_state_88xx(adapter) == HALMAC_CMD_CNSTR_H2C_SENT) {
       PLTFM_MSG_WARN("[WARN]state(clear)\n");
       return HALMAC_RET_ERROR_STATE;
   }
 
   if (cnv_scan_state_88xx(adapter, HALMAC_CMD_CNSTR_BUF_CLR) !=
       HALMAC_RET_SUCCESS)
       return HALMAC_RET_ERROR_STATE;
 
   PLTFM_FREE(adapter->ch_sw_info.buf, adapter->ch_sw_info.buf_size);
   adapter->ch_sw_info.buf = NULL;
   adapter->ch_sw_info.buf_wptr = NULL;
   adapter->ch_sw_info.extra_info_en = 0;
   adapter->ch_sw_info.buf_size = 0;
   adapter->ch_sw_info.avl_buf_size = 0;
   adapter->ch_sw_info.total_size = 0;
   adapter->ch_sw_info.ch_num = 0;
 
   PLTFM_MSG_TRACE("[TRACE]%s <===\n", __func__);
 
   return HALMAC_RET_SUCCESS;
}
 
/**
 * chk_txdesc_88xx() -check if the tx packet format is incorrect
 * @adapter : the adapter of halmac
 * @buf : tx Packet buffer, tx desc is included
 * @size : tx packet size
 * Author : KaiYuan Chang
 * Return : enum halmac_ret_status
 * More details of status code can be found in prototype document
 */
enum halmac_ret_status
chk_txdesc_88xx(struct halmac_adapter *adapter, u8 *buf, u32 size)
{
   u32 mac_clk = 0;
   u8 value8;
   enum halmac_ret_status status = HALMAC_RET_SUCCESS;
   struct halmac_api *api = (struct halmac_api *)adapter->halmac_api;
 
   PLTFM_MSG_TRACE("[TRACE]%s ===>\n", __func__);
 
   if (GET_TX_DESC_BMC(buf) == 1 && GET_TX_DESC_AGG_EN(buf) == 1)
       PLTFM_MSG_ERR("[ERR]txdesc - agg + bmc\n");
 
   if (size < (GET_TX_DESC_TXPKTSIZE(buf) +
           adapter->hw_cfg_info.txdesc_size +
           (GET_TX_DESC_PKT_OFFSET(buf) << 3))) {
       PLTFM_MSG_ERR("[ERR]txdesc - total size\n");
       status = HALMAC_RET_TXDESC_SET_FAIL;
   }
 
   if (wlhdr_valid_88xx(adapter, buf) != HALMAC_RET_SUCCESS) {
       PLTFM_MSG_ERR("[ERR]wlhdr\n");
       status = HALMAC_RET_WLHDR_FAIL;
   }
 
   if (GET_TX_DESC_AMSDU_PAD_EN(buf) != 0) {
       PLTFM_MSG_ERR("[ERR]txdesc - amsdu_pad\n");
       status = HALMAC_RET_TXDESC_SET_FAIL;
   }
 
   if (GET_TX_DESC_USE_MAX_TIME_EN(buf) == 1) {
       value8 = (u8)GET_TX_DESC_AMPDU_MAX_TIME(buf);
       if (value8 > HALMAC_REG_R8(REG_AMPDU_MAX_TIME_V1)) {
           PLTFM_MSG_ERR("[ERR]txdesc - ampdu_max_time\n");
           status = HALMAC_RET_TXDESC_SET_FAIL;
       }
   }
 
   switch (BIT_GET_MAC_CLK_SEL(HALMAC_REG_R32(REG_AFE_CTRL1))) {
   case 0x0:
       mac_clk = 80;
       break;
   case 0x1:
       mac_clk = 40;
       break;
   case 0x2:
       mac_clk = 20;
       break;
   case 0x3:
       mac_clk = 10;
       break;
   }
 
   PLTFM_MSG_ALWAYS("MAC clock : 0x%XM\n", mac_clk);
   PLTFM_MSG_ALWAYS("mac agg en : 0x%X\n", GET_TX_DESC_AGG_EN(buf));
   PLTFM_MSG_ALWAYS("mac agg num : 0x%X\n", GET_TX_DESC_MAX_AGG_NUM(buf));
 
   PLTFM_MSG_TRACE("[TRACE]%s <===\n", __func__);
 
   return status;
}
 
static enum halmac_ret_status
wlhdr_valid_88xx(struct halmac_adapter *adapter, u8 *buf)
{
   u32 txdesc_size = adapter->hw_cfg_info.txdesc_size +
                       GET_TX_DESC_PKT_OFFSET(buf);
   enum halmac_ret_status status = HALMAC_RET_SUCCESS;
   struct wlhdr_frame_ctrl *wlhdr;
 
   wlhdr = (struct wlhdr_frame_ctrl *)(buf + txdesc_size);
 
   if (wlhdr->protocol != WLHDR_PROT_VER) {
       PLTFM_MSG_ERR("[ERR]prot ver!!\n");
       return HALMAC_RET_WLHDR_FAIL;
   }
 
   switch (wlhdr->type) {
   case WLHDR_TYPE_MGMT:
       if (wlhdr_mgmt_valid_88xx(adapter, wlhdr) != 1)
           status = HALMAC_RET_WLHDR_FAIL;
       break;
   case WLHDR_TYPE_CTRL:
       if (wlhdr_ctrl_valid_88xx(adapter, wlhdr) != 1)
           status = HALMAC_RET_WLHDR_FAIL;
       break;
   case WLHDR_TYPE_DATA:
       if (wlhdr_data_valid_88xx(adapter, wlhdr) != 1)
           status = HALMAC_RET_WLHDR_FAIL;
       break;
   default:
       PLTFM_MSG_ERR("[ERR]undefined type!!\n");
       status = HALMAC_RET_WLHDR_FAIL;
       break;
   }
 
   return status;
}
 
static u8
wlhdr_mgmt_valid_88xx(struct halmac_adapter *adapter,
             struct wlhdr_frame_ctrl *wlhdr)
{
   u8 state;
 
   switch (wlhdr->sub_type) {
   case WLHDR_SUB_TYPE_ASSOC_REQ:
   case WLHDR_SUB_TYPE_ASSOC_RSPNS:
   case WLHDR_SUB_TYPE_REASSOC_REQ:
   case WLHDR_SUB_TYPE_REASSOC_RSPNS:
   case WLHDR_SUB_TYPE_PROBE_REQ:
   case WLHDR_SUB_TYPE_PROBE_RSPNS:
   case WLHDR_SUB_TYPE_BCN:
   case WLHDR_SUB_TYPE_DISASSOC:
   case WLHDR_SUB_TYPE_AUTH:
   case WLHDR_SUB_TYPE_DEAUTH:
   case WLHDR_SUB_TYPE_ACTION:
   case WLHDR_SUB_TYPE_ACTION_NOACK:
       state = 1;
       break;
   default:
       PLTFM_MSG_ERR("[ERR]mgmt invalid!!\n");
       state = 0;
       break;
   }
 
   return state;
}
 
static u8
wlhdr_ctrl_valid_88xx(struct halmac_adapter *adapter,
             struct wlhdr_frame_ctrl *wlhdr)
{
   u8 state;
 
   switch (wlhdr->sub_type) {
   case WLHDR_SUB_TYPE_BF_RPT_POLL:
   case WLHDR_SUB_TYPE_NDPA:
       state = 1;
       break;
   default:
       PLTFM_MSG_ERR("[ERR]ctrl invalid!!\n");
       state = 0;
       break;
   }
 
   return state;
}
 
static u8
wlhdr_data_valid_88xx(struct halmac_adapter *adapter,
             struct wlhdr_frame_ctrl *wlhdr)
{
   u8 state;
 
   switch (wlhdr->sub_type) {
   case WLHDR_SUB_TYPE_DATA:
   case WLHDR_SUB_TYPE_NULL:
   case WLHDR_SUB_TYPE_QOS_DATA:
   case WLHDR_SUB_TYPE_QOS_NULL:
       state = 1;
       break;
   default:
       PLTFM_MSG_ERR("[ERR]data invalid!!\n");
       state = 0;
       break;
   }
 
   return state;
}
 
/**
 * get_version_88xx() - get HALMAC version
 * @ver : return version of major, prototype, minor and patch information
 * Author : KaiYuan Chang / Ivan Lin
 * Return : enum halmac_ret_status
 * More details of status code can be found in prototype document
 */
enum halmac_ret_status
get_version_88xx(struct halmac_adapter *adapter, struct halmac_ver *ver)
{
   PLTFM_MSG_TRACE("[TRACE]%s ===>\n", __func__);
 
   ver->major_ver = (u8)HALMAC_MAJOR_VER;
   ver->prototype_ver = (u8)HALMAC_PROTOTYPE_VER;
   ver->minor_ver = (u8)HALMAC_MINOR_VER;
   ver->patch_ver = (u8)HALMAC_PATCH_VER;
 
   PLTFM_MSG_TRACE("[TRACE]%s <===\n", __func__);
 
   return HALMAC_RET_SUCCESS;
}
 
enum halmac_ret_status
p2pps_88xx(struct halmac_adapter *adapter, struct halmac_p2pps *info)
{
   enum halmac_ret_status status = HALMAC_RET_SUCCESS;
 
   if (halmac_fw_validate(adapter) != HALMAC_RET_SUCCESS)
       return HALMAC_RET_NO_DLFW;
 
   if (adapter->fw_ver.h2c_version < 6)
       return HALMAC_RET_FW_NO_SUPPORT;
 
   status = proc_p2pps_88xx(adapter, info);
   if (status != HALMAC_RET_SUCCESS) {
       PLTFM_MSG_ERR("[ERR]p2pps!!\n");
       return status;
   }
 
   return HALMAC_RET_SUCCESS;
}
 
static enum halmac_ret_status
proc_p2pps_88xx(struct halmac_adapter *adapter, struct halmac_p2pps *info)
{
   u8 h2c_buf[H2C_PKT_SIZE_88XX] = { 0 };
   u16 seq_num = 0;
   struct halmac_h2c_header_info hdr_info;
   enum halmac_ret_status status = HALMAC_RET_SUCCESS;
 
   P2PPS_SET_OFFLOAD_EN(h2c_buf, info->offload_en);
   P2PPS_SET_ROLE(h2c_buf, info->role);
   P2PPS_SET_CTWINDOW_EN(h2c_buf, info->ctwindow_en);
   P2PPS_SET_NOA_EN(h2c_buf, info->noa_en);
   P2PPS_SET_NOA_SEL(h2c_buf, info->noa_sel);
   P2PPS_SET_ALLSTASLEEP(h2c_buf, info->all_sta_sleep);
   P2PPS_SET_DISCOVERY(h2c_buf, info->discovery);
   P2PPS_SET_DISABLE_CLOSERF(h2c_buf, info->disable_close_rf);
   P2PPS_SET_P2P_PORT_ID(h2c_buf, info->p2p_port_id);
   P2PPS_SET_P2P_GROUP(h2c_buf, info->p2p_group);
   P2PPS_SET_P2P_MACID(h2c_buf, info->p2p_macid);
 
   P2PPS_SET_CTWINDOW_LENGTH(h2c_buf, info->ctwindow_length);
 
   P2PPS_SET_NOA_DURATION_PARA(h2c_buf, info->noa_duration_para);
   P2PPS_SET_NOA_INTERVAL_PARA(h2c_buf, info->noa_interval_para);
   P2PPS_SET_NOA_START_TIME_PARA(h2c_buf, info->noa_start_time_para);
   P2PPS_SET_NOA_COUNT_PARA(h2c_buf, info->noa_count_para);
 
   hdr_info.sub_cmd_id = SUB_CMD_ID_P2PPS;
   hdr_info.content_size = 24;
   hdr_info.ack = 0;
   set_h2c_pkt_hdr_88xx(adapter, h2c_buf, &hdr_info, &seq_num);
 
   status = send_h2c_pkt_88xx(adapter, h2c_buf);
 
   if (status != HALMAC_RET_SUCCESS)
       PLTFM_MSG_ERR("[ERR]send h2c!!\n");
 
   return status;
}
 
/**
 * query_status_88xx() -query the offload feature status
 * @adapter : the adapter of halmac
 * @feature_id : feature_id
 * @proc_status : feature_status
 * @data : data buffer
 * @size : data size
 *
 * Note :
 * If user wants to know the data size, user can allocate zero
 * size buffer first. If this size less than the data size, halmac
 * will return  HALMAC_RET_BUFFER_TOO_SMALL. User need to
 * re-allocate data buffer with correct data size.
 *
 * Author : Ivan Lin/KaiYuan Chang
 * Return : enum halmac_ret_status
 * More details of status code can be found in prototype document
 */
enum halmac_ret_status
query_status_88xx(struct halmac_adapter *adapter,
         enum halmac_feature_id feature_id,
         enum halmac_cmd_process_status *proc_status, u8 *data,
         u32 *size)
{
   enum halmac_ret_status status = HALMAC_RET_SUCCESS;
 
   if (!proc_status)
       return HALMAC_RET_NULL_POINTER;
 
   switch (feature_id) {
   case HALMAC_FEATURE_CFG_PARA:
       status = get_cfg_param_status_88xx(adapter, proc_status);
       break;
   case HALMAC_FEATURE_DUMP_PHYSICAL_EFUSE:
       status = get_dump_phy_efuse_status_88xx(adapter, proc_status,
                           data, size);
       break;
   case HALMAC_FEATURE_DUMP_LOGICAL_EFUSE:
       status = get_dump_log_efuse_status_88xx(adapter, proc_status,
                           data, size);
       break;
   case HALMAC_FEATURE_DUMP_LOGICAL_EFUSE_MASK:
       status = get_dump_log_efuse_mask_status_88xx(adapter,
                                proc_status,
                                data, size);
       break;
   case HALMAC_FEATURE_CHANNEL_SWITCH:
       status = get_ch_switch_status_88xx(adapter, proc_status);
       break;
   case HALMAC_FEATURE_UPDATE_PACKET:
       status = get_update_packet_status_88xx(adapter, proc_status);
       break;
   case HALMAC_FEATURE_SEND_SCAN_PACKET:
       status = get_send_scan_packet_status_88xx(adapter, proc_status);
       break;
   case HALMAC_FEATURE_DROP_SCAN_PACKET:
       status = get_drop_scan_packet_status_88xx(adapter, proc_status);
       break;
   case HALMAC_FEATURE_IQK:
       status = get_iqk_status_88xx(adapter, proc_status);
       break;
   case HALMAC_FEATURE_POWER_TRACKING:
       status = get_pwr_trk_status_88xx(adapter, proc_status);
       break;
   case HALMAC_FEATURE_PSD:
       status = get_psd_status_88xx(adapter, proc_status, data, size);
       break;
   case HALMAC_FEATURE_FW_SNDING:
       status = get_fw_snding_status_88xx(adapter, proc_status);
       break;
   case HALMAC_FEATURE_DPK:
       status = get_dpk_status_88xx(adapter, proc_status, data, size);
       break;
   default:
       return HALMAC_RET_INVALID_FEATURE_ID;
   }
 
   return status;
}
 
static enum halmac_ret_status
get_cfg_param_status_88xx(struct halmac_adapter *adapter,
             enum halmac_cmd_process_status *proc_status)
{
   *proc_status = adapter->halmac_state.cfg_param_state.proc_status;
 
   return HALMAC_RET_SUCCESS;
}
 
static enum halmac_ret_status
get_ch_switch_status_88xx(struct halmac_adapter *adapter,
             enum halmac_cmd_process_status *proc_status)
{
   *proc_status = adapter->halmac_state.scan_state.proc_status;
 
   return HALMAC_RET_SUCCESS;
}
 
static enum halmac_ret_status
get_update_packet_status_88xx(struct halmac_adapter *adapter,
                 enum halmac_cmd_process_status *proc_status)
{
   *proc_status = adapter->halmac_state.update_pkt_state.proc_status;
 
   return HALMAC_RET_SUCCESS;
}
 
static enum halmac_ret_status
get_send_scan_packet_status_88xx(struct halmac_adapter *adapter,
                enum halmac_cmd_process_status *proc_status)
{
   *proc_status = adapter->halmac_state.scan_pkt_state.proc_status;
 
   return HALMAC_RET_SUCCESS;
}
 
static enum halmac_ret_status
get_drop_scan_packet_status_88xx(struct halmac_adapter *adapter,
                enum halmac_cmd_process_status *proc_status)
{
   *proc_status = adapter->halmac_state.drop_pkt_state.proc_status;
 
   return HALMAC_RET_SUCCESS;
}
 
/**
 * cfg_drv_rsvd_pg_num_88xx() -config reserved page number for driver
 * @adapter : the adapter of halmac
 * @pg_num : page number
 * Author : KaiYuan Chang
 * Return : enum halmac_ret_status
 * More details of status code can be found in prototype document
 */
enum halmac_ret_status
cfg_drv_rsvd_pg_num_88xx(struct halmac_adapter *adapter,
            enum halmac_drv_rsvd_pg_num pg_num)
{
   if (adapter->api_registry.cfg_drv_rsvd_pg_en == 0)
       return HALMAC_RET_NOT_SUPPORT;
 
   PLTFM_MSG_TRACE("[TRACE]%s ===>\n", __func__);
   PLTFM_MSG_TRACE("[TRACE]pg_num = %d\n", pg_num);
 
   switch (pg_num) {
   case HALMAC_RSVD_PG_NUM8:
       adapter->txff_alloc.rsvd_drv_pg_num = 8;
       break;
   case HALMAC_RSVD_PG_NUM16:
       adapter->txff_alloc.rsvd_drv_pg_num = 16;
       break;
   case HALMAC_RSVD_PG_NUM24:
       adapter->txff_alloc.rsvd_drv_pg_num = 24;
       break;
   case HALMAC_RSVD_PG_NUM32:
       adapter->txff_alloc.rsvd_drv_pg_num = 32;
       break;
   case HALMAC_RSVD_PG_NUM64:
       adapter->txff_alloc.rsvd_drv_pg_num = 64;
       break;
   case HALMAC_RSVD_PG_NUM128:
       adapter->txff_alloc.rsvd_drv_pg_num = 128;
       break;
   case HALMAC_RSVD_PG_NUM256:
       adapter->txff_alloc.rsvd_drv_pg_num = 256;
       break;
   case HALMAC_RSVD_PG_NUM512:
       adapter->txff_alloc.rsvd_drv_pg_num = 512;
       break;
   case HALMAC_RSVD_PG_NUM1024:
       adapter->txff_alloc.rsvd_drv_pg_num = 1024;
       break;
   case HALMAC_RSVD_PG_NUM1460:
       adapter->txff_alloc.rsvd_drv_pg_num = 1460;
       break;
   }
 
   PLTFM_MSG_TRACE("[TRACE]%s <===\n", __func__);
 
   return HALMAC_RET_SUCCESS;
}
 
/**
 * (debug API)h2c_lb_88xx() - send h2c loopback packet
 * @adapter : the adapter of halmac
 * Author : KaiYuan Chang/Ivan Lin
 * Return : enum halmac_ret_status
 * More details of status code can be found in prototype document
 */
enum halmac_ret_status
h2c_lb_88xx(struct halmac_adapter *adapter)
{
   return HALMAC_RET_SUCCESS;
}
 
enum halmac_ret_status
pwr_seq_parser_88xx(struct halmac_adapter *adapter,
           struct halmac_wlan_pwr_cfg **cmd_seq)
{
   u8 cut;
   u8 intf;
   u32 idx = 0;
   enum halmac_ret_status status = HALMAC_RET_SUCCESS;
   struct halmac_wlan_pwr_cfg *cmd;
 
   switch (adapter->chip_ver) {
   case HALMAC_CHIP_VER_A_CUT:
       cut = HALMAC_PWR_CUT_A_MSK;
       break;
   case HALMAC_CHIP_VER_B_CUT:
       cut = HALMAC_PWR_CUT_B_MSK;
       break;
   case HALMAC_CHIP_VER_C_CUT:
       cut = HALMAC_PWR_CUT_C_MSK;
       break;
   case HALMAC_CHIP_VER_D_CUT:
       cut = HALMAC_PWR_CUT_D_MSK;
       break;
   case HALMAC_CHIP_VER_E_CUT:
       cut = HALMAC_PWR_CUT_E_MSK;
       break;
   case HALMAC_CHIP_VER_F_CUT:
       cut = HALMAC_PWR_CUT_F_MSK;
       break;
   case HALMAC_CHIP_VER_TEST:
       cut = HALMAC_PWR_CUT_TESTCHIP_MSK;
       break;
   default:
       PLTFM_MSG_ERR("[ERR]chip info!!\n");
       return HALMAC_RET_SWITCH_CASE_ERROR;
   }
 
   switch (adapter->intf) {
   case HALMAC_INTERFACE_PCIE:
   case HALMAC_INTERFACE_AXI:
       intf = HALMAC_PWR_INTF_PCI_MSK;
       break;
   case HALMAC_INTERFACE_USB:
       intf = HALMAC_PWR_INTF_USB_MSK;
       break;
   case HALMAC_INTERFACE_SDIO:
       intf = HALMAC_PWR_INTF_SDIO_MSK;
       break;
   default:
       PLTFM_MSG_ERR("[ERR]interface!!\n");
       return HALMAC_RET_SWITCH_CASE_ERROR;
   }
 
   do {
       cmd = cmd_seq[idx];
 
       if (!cmd)
           break;
 
       status = pwr_sub_seq_parser_88xx(adapter, cut, intf, cmd);
       if (status != HALMAC_RET_SUCCESS) {
           PLTFM_MSG_ERR("[ERR]pwr sub seq!!\n");
           return status;
       }
 
       idx++;
   } while (1);
 
   return status;
}
 
static enum halmac_ret_status
pwr_sub_seq_parser_88xx(struct halmac_adapter *adapter, u8 cut, u8 intf,
           struct halmac_wlan_pwr_cfg *cmd)
{
   u8 value;
   u32 offset;
   struct halmac_api *api = (struct halmac_api *)adapter->halmac_api;
 
   do {
       if ((cmd->interface_msk & intf) && (cmd->cut_msk & cut)) {
           switch (cmd->cmd) {
           case HALMAC_PWR_CMD_WRITE:
               offset = cmd->offset;
 
               if (cmd->base == HALMAC_PWR_ADDR_SDIO)
                   offset |= SDIO_LOCAL_OFFSET;
 
               value = HALMAC_REG_R8(offset);
               value = (u8)(value & (u8)(~(cmd->msk)));
               value = (u8)(value | (cmd->value & cmd->msk));
 
               HALMAC_REG_W8(offset, value);
               break;
           case HALMAC_PWR_CMD_POLLING:
               if (pwr_cmd_polling_88xx(adapter, cmd) !=
                   HALMAC_RET_SUCCESS)
                   return HALMAC_RET_PWRSEQ_POLLING_FAIL;
               break;
           case HALMAC_PWR_CMD_DELAY:
               if (cmd->value == HALMAC_PWR_DELAY_US)
                   PLTFM_DELAY_US(cmd->offset);
               else
                   PLTFM_DELAY_US(1000 * cmd->offset);
               break;
           case HALMAC_PWR_CMD_READ:
               break;
           case HALMAC_PWR_CMD_END:
               return HALMAC_RET_SUCCESS;
           default:
               return HALMAC_RET_PWRSEQ_CMD_INCORRECT;
           }
       }
       cmd++;
   } while (1);
 
   return HALMAC_RET_SUCCESS;
}
 
static enum halmac_ret_status
pwr_cmd_polling_88xx(struct halmac_adapter *adapter,
            struct halmac_wlan_pwr_cfg *cmd)
{
   u8 value;
   u8 flg;
   u8 poll_bit;
   u32 offset;
   u32 cnt;
   static u32 stats;
   enum halmac_interface intf;
   struct halmac_api *api = (struct halmac_api *)adapter->halmac_api;
 
   poll_bit = 0;
   cnt = HALMAC_PWR_POLLING_CNT;
   flg = 0;
   intf = adapter->intf;
 
   if (cmd->base == HALMAC_PWR_ADDR_SDIO)
       offset = cmd->offset | SDIO_LOCAL_OFFSET;
   else
       offset = cmd->offset;
 
   do {
       cnt--;
       value = HALMAC_REG_R8(offset);
       value = (u8)(value & cmd->msk);
 
       if (value == (cmd->value & cmd->msk)) {
           poll_bit = 1;
       } else {
           if (cnt == 0) {
               if (intf == HALMAC_INTERFACE_PCIE && flg == 0) {
                   /* PCIE + USB package */
                   /* power bit polling timeout issue */
                   stats++;
                   PLTFM_MSG_WARN("[WARN]PCIE stats:%d\n",
                              stats);
                   value = HALMAC_REG_R8(REG_SYS_PW_CTRL);
                   value |= BIT(3);
                   HALMAC_REG_W8(REG_SYS_PW_CTRL, value);
                   value &= ~BIT(3);
                   HALMAC_REG_W8(REG_SYS_PW_CTRL, value);
                   poll_bit = 0;
                   cnt = HALMAC_PWR_POLLING_CNT;
                   flg = 1;
               } else {
                   PLTFM_MSG_ERR("[ERR]polling to!!\n");
                   PLTFM_MSG_ERR("[ERR]cmd offset:%X\n",
                             cmd->offset);
                   PLTFM_MSG_ERR("[ERR]cmd value:%X\n",
                             cmd->value);
                   PLTFM_MSG_ERR("[ERR]cmd msk:%X\n",
                             cmd->msk);
                   PLTFM_MSG_ERR("[ERR]offset = %X\n",
                             offset);
                   PLTFM_MSG_ERR("[ERR]value = %X\n",
                             value);
                   return HALMAC_RET_PWRSEQ_POLLING_FAIL;
               }
           } else {
               PLTFM_DELAY_US(50);
           }
       }
   } while (!poll_bit);
 
   return HALMAC_RET_SUCCESS;
}
 
enum halmac_ret_status
parse_intf_phy_88xx(struct halmac_adapter *adapter,
           struct halmac_intf_phy_para *param,
           enum halmac_intf_phy_platform pltfm,
           enum hal_intf_phy intf_phy)
{
   u16 value;
   u16 cur_cut;
   u16 offset;
   u16 ip_sel;
   struct halmac_intf_phy_para *cur_param;
   struct halmac_api *api = (struct halmac_api *)adapter->halmac_api;
   enum halmac_ret_status result = HALMAC_RET_SUCCESS;
 
   switch (adapter->chip_ver) {
   case HALMAC_CHIP_VER_A_CUT:
       cur_cut = (u16)HALMAC_INTF_PHY_CUT_A;
       break;
   case HALMAC_CHIP_VER_B_CUT:
       cur_cut = (u16)HALMAC_INTF_PHY_CUT_B;
       break;
   case HALMAC_CHIP_VER_C_CUT:
       cur_cut = (u16)HALMAC_INTF_PHY_CUT_C;
       break;
   case HALMAC_CHIP_VER_D_CUT:
       cur_cut = (u16)HALMAC_INTF_PHY_CUT_D;
       break;
   case HALMAC_CHIP_VER_E_CUT:
       cur_cut = (u16)HALMAC_INTF_PHY_CUT_E;
       break;
   case HALMAC_CHIP_VER_F_CUT:
       cur_cut = (u16)HALMAC_INTF_PHY_CUT_F;
       break;
   case HALMAC_CHIP_VER_TEST:
       cur_cut = (u16)HALMAC_INTF_PHY_CUT_TESTCHIP;
       break;
   default:
       return HALMAC_RET_FAIL;
   }
 
   cur_param = param;
 
   do {
       if ((cur_param->cut & cur_cut) &&
           (cur_param->plaform & (u16)pltfm)) {
           offset =  cur_param->offset;
           value = cur_param->value;
           ip_sel = cur_param->ip_sel;
 
           if (offset == 0xFFFF)
               break;
 
           if (ip_sel == HALMAC_IP_SEL_MAC) {
               HALMAC_REG_W8((u32)offset, (u8)value);
           } else if (intf_phy == HAL_INTF_PHY_USB2 ||
                  intf_phy == HAL_INTF_PHY_USB3) {
#if HALMAC_USB_SUPPORT
               if (offset > 0x100)
                   usb_page_switch_88xx(adapter,
                                intf_phy,
                                1);
               else
                   usb_page_switch_88xx(adapter,
                                intf_phy,
                                0);
               offset = offset & 0xFF;
               result = usbphy_write_88xx(adapter, (u8)offset,
                              value, intf_phy);
               if (result != HALMAC_RET_SUCCESS)
                   PLTFM_MSG_ERR("[ERR]usb phy!!\n");
#endif
           } else if (intf_phy == HAL_INTF_PHY_PCIE_GEN1 ||
                  intf_phy == HAL_INTF_PHY_PCIE_GEN2) {
#if HALMAC_PCIE_SUPPORT
               if (ip_sel == HALMAC_IP_INTF_PHY)
                   result = mdio_write_88xx(adapter,
                                (u8)offset,
                                value,
                                intf_phy);
               else
                   result = dbi_w8_88xx(adapter, offset,
                                (u8)value);
               if (result != HALMAC_RET_SUCCESS)
                   PLTFM_MSG_ERR("[ERR]mdio/dbi!!\n");
#endif
           } else {
               PLTFM_MSG_ERR("[ERR]intf phy sel!!\n");
           }
       }
       cur_param++;
   } while (1);
 
   return result;
}
 
/**
 * txfifo_is_empty_88xx() -check if txfifo is empty
 * @adapter : the adapter of halmac
 * @chk_num : check number
 * Author : Ivan Lin
 * Return : enum halmac_ret_status
 * More details of status code can be found in prototype document
 */
enum halmac_ret_status
txfifo_is_empty_88xx(struct halmac_adapter *adapter, u32 chk_num)
{
   u32 cnt;
   struct halmac_api *api = (struct halmac_api *)adapter->halmac_api;
 
   PLTFM_MSG_TRACE("[TRACE]%s ===>\n", __func__);
 
   cnt = (chk_num <= 10) ? 10 : chk_num;
   do {
       if (HALMAC_REG_R8(REG_TXPKT_EMPTY) != 0xFF)
           return HALMAC_RET_TXFIFO_NO_EMPTY;
 
       if ((HALMAC_REG_R8(REG_TXPKT_EMPTY + 1) & 0x06) != 0x06)
           return HALMAC_RET_TXFIFO_NO_EMPTY;
       cnt--;
 
   } while (cnt != 0);
 
   PLTFM_MSG_TRACE("[TRACE]%s <===\n", __func__);
 
   return HALMAC_RET_SUCCESS;
}
 
/**
 * (internal use)
 * smart_malloc_88xx() - adapt malloc size
 * @adapter : the adapter of halmac
 * @size : expected malloc size
 * @pNew_size : real malloc size
 * Author : Ivan Lin
 * Return : address pointer
 */
u8*
smart_malloc_88xx(struct halmac_adapter *adapter, u32 size, u32 *new_size)
{
   u8 retry_num;
   u8 *malloc_buf = NULL;
 
   for (retry_num = 0; retry_num < 5; retry_num++) {
       malloc_buf = (u8 *)PLTFM_MALLOC(size);
 
       if (malloc_buf) {
           *new_size = size;
           return malloc_buf;
       }
 
       size = size >> 1;
 
       if (size == 0)
           break;
   }
 
   PLTFM_MSG_ERR("[ERR]adptive malloc!!\n");
 
   return NULL;
}
 
/**
 * (internal use)
 * ltecoex_reg_read_88xx() - read ltecoex register
 * @adapter : the adapter of halmac
 * @offset : offset
 * @pValue : value
 * Author : Ivan Lin
 * Return : enum halmac_ret_status
 */
enum halmac_ret_status
ltecoex_reg_read_88xx(struct halmac_adapter *adapter, u16 offset, u32 *value)
{
   u32 cnt;
   struct halmac_api *api = (struct halmac_api *)adapter->halmac_api;
 
   cnt = 10000;
   while ((HALMAC_REG_R8(LTECOEX_ACCESS_CTRL + 3) & BIT(5)) == 0) {
       if (cnt == 0) {
           PLTFM_MSG_ERR("[ERR]lte ready(R)\n");
           return HALMAC_RET_LTECOEX_READY_FAIL;
       }
       cnt--;
       PLTFM_DELAY_US(50);
   }
 
   HALMAC_REG_W32(LTECOEX_ACCESS_CTRL, 0x800F0000 | offset);
   *value = HALMAC_REG_R32(REG_WL2LTECOEX_INDIRECT_ACCESS_READ_DATA_V1);
 
   return HALMAC_RET_SUCCESS;
}
 
/**
 * (internal use)
 * ltecoex_reg_write_88xx() - write ltecoex register
 * @adapter : the adapter of halmac
 * @offset : offset
 * @value : value
 * Author : Ivan Lin
 * Return : enum halmac_ret_status
 */
enum halmac_ret_status
ltecoex_reg_write_88xx(struct halmac_adapter *adapter, u16 offset, u32 value)
{
   u32 cnt;
   struct halmac_api *api = (struct halmac_api *)adapter->halmac_api;
 
   cnt = 10000;
   while ((HALMAC_REG_R8(LTECOEX_ACCESS_CTRL + 3) & BIT(5)) == 0) {
       if (cnt == 0) {
           PLTFM_MSG_ERR("[ERR]lte ready(W)\n");
           return HALMAC_RET_LTECOEX_READY_FAIL;
       }
       cnt--;
       PLTFM_DELAY_US(50);
   }
 
   HALMAC_REG_W32(REG_WL2LTECOEX_INDIRECT_ACCESS_WRITE_DATA_V1, value);
   HALMAC_REG_W32(LTECOEX_ACCESS_CTRL, 0xC00F0000 | offset);
 
   return HALMAC_RET_SUCCESS;
}
 
static void
pwr_state_88xx(struct halmac_adapter *adapter, enum halmac_mac_power *state)
{
   struct halmac_api *api = (struct halmac_api *)adapter->halmac_api;
 
   if ((HALMAC_REG_R8(REG_SYS_FUNC_EN + 1) & BIT(3)) == 0)
       *state = HALMAC_MAC_POWER_OFF;
   else
       *state = HALMAC_MAC_POWER_ON;
}
 
static u8
packet_in_nlo_88xx(struct halmac_adapter *adapter,
          enum halmac_packet_id pkt_id)
{
   enum halmac_packet_id nlo_pkt = HALMAC_PACKET_PROBE_REQ_NLO;
 
   if (pkt_id >= nlo_pkt)
       return 1;
   else
       return 0;
}
 
static enum halmac_packet_id
get_real_pkt_id_88xx(struct halmac_adapter *adapter,
            enum halmac_packet_id pkt_id)
{
   enum halmac_packet_id real_pkt_id;
 
   PLTFM_MSG_TRACE("[TRACE]%s ===>\n", __func__);
 
   switch (pkt_id) {
   case HALMAC_PACKET_PROBE_REQ_NLO:
       real_pkt_id = HALMAC_PACKET_PROBE_REQ;
       break;
   case HALMAC_PACKET_SYNC_BCN_NLO:
       real_pkt_id = HALMAC_PACKET_SYNC_BCN;
       break;
   case HALMAC_PACKET_DISCOVERY_BCN_NLO:
       real_pkt_id = HALMAC_PACKET_DISCOVERY_BCN;
       break;
   default:
       real_pkt_id = pkt_id;
   }
   PLTFM_MSG_TRACE("[TRACE]%s <===\n", __func__);
   return real_pkt_id;
}
 
static u32
get_update_packet_page_size(struct halmac_adapter *adapter, u32 size)
{
   struct halmac_api *api = (struct halmac_api *)adapter->halmac_api;
   u32 txdesc_size;
   u32 total;
 
   api->halmac_get_hw_value(adapter, HALMAC_HW_TX_DESC_SIZE, &txdesc_size);
 
   total = size + txdesc_size;
   return (total & 0x7f) > 0 ?
       (total >> TX_PAGE_SIZE_SHIFT_88XX) + 1 :
       total >> TX_PAGE_SIZE_SHIFT_88XX;
}
 
#endif /* HALMAC_88XX_SUPPORT */