summaryrefslogtreecommitdiff
path: root/drivers/net/wireless/sd8797/mlan/mlan_cmdevt.c
blob: f3db9e6625652c0f29f13e5cb26a76299671434e (plain)
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
/**
 * @file mlan_cmdevt.c
 *
 *  @brief This file contains the handling of CMD/EVENT in MLAN
 *
 *  Copyright (C) 2009-2011, Marvell International Ltd. 
 *
 *  This software file (the "File") is distributed by Marvell International
 *  Ltd. under the terms of the GNU General Public License Version 2, June 1991
 *  (the "License").  You may use, redistribute and/or modify this File in
 *  accordance with the terms and conditions of the License, a copy of which
 *  is available by writing to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
 *  worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
 *  
 *  THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
 *  IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
 *  ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
 *  this warranty disclaimer.
 */

/*************************************************************
Change Log:
    05/12/2009: initial version
************************************************************/
#include "mlan.h"
#ifdef STA_SUPPORT
#include "mlan_join.h"
#endif
#include "mlan_util.h"
#include "mlan_fw.h"
#include "mlan_main.h"
#include "mlan_wmm.h"
#include "mlan_11n.h"
#include "mlan_11h.h"
#include "mlan_sdio.h"
/********************************************************
                Local Variables
********************************************************/

/*******************************************************
                Global Variables
********************************************************/

/********************************************************
                Local Functions
********************************************************/

/**
 *  @brief This function convert a given character to hex
 *
 *  @param chr        Character to be converted
 *
 *  @return           The converted hex if chr is a valid hex, else 0
 */
static t_u32
wlan_hexval(t_u8 chr)
{
    if (chr >= '0' && chr <= '9')
        return chr - '0';
    if (chr >= 'A' && chr <= 'F')
        return chr - 'A' + 10;
    if (chr >= 'a' && chr <= 'f')
        return chr - 'a' + 10;

    return 0;
}

/**
 *  @brief This function convert a given string to hex
 *
 *  @param a            A pointer to string to be converted
 *
 *  @return             The converted hex value if param a is a valid hex, else 0
 */
int
wlan_atox(t_u8 * a)
{
    int i = 0;

    ENTER();

    while (wlan_isxdigit(*a))
        i = i * 16 + wlan_hexval(*a++);

    LEAVE();
    return i;
}

/**
 *  @brief This function parse cal data from ASCII to hex
 *
 *  @param src          A pointer to source data
 *  @param len          Source dara length
 *  @param dst          A pointer to a buf to store the parsed data
 *
 *  @return             The parsed hex data length
 */
static t_u32
wlan_parse_cal_cfg(t_u8 * src, t_size len, t_u8 * dst)
{
    t_u8 *ptr;
    t_u8 *dptr;

    ENTER();
    ptr = src;
    dptr = dst;

    while (ptr - src < len) {
        while (*ptr && (wlan_isspace(*ptr) || *ptr == '\t')) {
            ptr++;
        }

        if (wlan_isxdigit(*ptr)) {
            *dptr++ = wlan_atox(ptr);
            ptr += 2;
        } else {
            ptr++;
        }
    }
    LEAVE();
    return (dptr - dst);
}

/**
 *  @brief This function initializes the command node.
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param pcmd_node    A pointer to cmd_ctrl_node structure
 *  @param cmd_oid      Cmd oid: treated as sub command
 *  @param pioctl_buf   A pointer to MLAN IOCTL Request buffer
 *  @param pdata_buf    A pointer to information buffer
 *
 *  @return             N/A
 */
static void
wlan_init_cmd_node(IN pmlan_private pmpriv,
                   IN cmd_ctrl_node * pcmd_node,
                   IN t_u32 cmd_oid,
                   IN t_void * pioctl_buf, IN t_void * pdata_buf)
{
    mlan_adapter *pmadapter = pmpriv->adapter;

    ENTER();

    if (pcmd_node == MNULL) {
        LEAVE();
        return;
    }
    pcmd_node->priv = pmpriv;
    pcmd_node->cmd_oid = cmd_oid;
    pcmd_node->pioctl_buf = pioctl_buf;
    pcmd_node->pdata_buf = pdata_buf;

    pcmd_node->cmdbuf = pcmd_node->pmbuf;

    /* Make sure head_ptr for cmd buf is Align */
    pcmd_node->cmdbuf->data_offset = 0;
    memset(pmadapter, pcmd_node->cmdbuf->pbuf, 0, MRVDRV_SIZE_OF_CMD_BUFFER);

    /* Prepare mlan_buffer for command sending */
    pcmd_node->cmdbuf->buf_type = MLAN_BUF_TYPE_CMD;
    pcmd_node->cmdbuf->data_offset += INTF_HEADER_LEN;

    LEAVE();
}

/**
 *  @brief This function gets a free command node if available in
 *              command free queue.
 *
 *  @param pmadapter        A pointer to mlan_adapter structure
 *
 *  @return cmd_ctrl_node   A pointer to cmd_ctrl_node structure or MNULL
 */
static cmd_ctrl_node *
wlan_get_cmd_node(mlan_adapter * pmadapter)
{
    cmd_ctrl_node *pcmd_node;

    ENTER();

    if (pmadapter == MNULL) {
        LEAVE();
        return MNULL;
    }

    if (util_peek_list(pmadapter->pmoal_handle, &pmadapter->cmd_free_q,
                       pmadapter->callbacks.moal_spin_lock,
                       pmadapter->callbacks.moal_spin_unlock)) {
        pcmd_node = (cmd_ctrl_node *) util_dequeue_list(pmadapter->pmoal_handle,
                                                        &pmadapter->cmd_free_q,
                                                        pmadapter->callbacks.
                                                        moal_spin_lock,
                                                        pmadapter->callbacks.
                                                        moal_spin_unlock);
    } else {
        PRINTM(MERROR, "GET_CMD_NODE: cmd_ctrl_node is not available\n");
        pcmd_node = MNULL;
    }

    LEAVE();
    return pcmd_node;
}

/**
 *  @brief This function cleans command node.
 *
 *  @param pmadapter    A pointer to mlan_adapter structure
 *  @param pcmd_node    A pointer to cmd_ctrl_node structure
 *
 *  @return             N/A
 */
static t_void
wlan_clean_cmd_node(pmlan_adapter pmadapter, cmd_ctrl_node * pcmd_node)
{
    ENTER();

    if (pcmd_node == MNULL) {
        LEAVE();
        return;
    }
    pcmd_node->cmd_oid = 0;
    pcmd_node->cmd_flag = 0;
    pcmd_node->pioctl_buf = MNULL;
    pcmd_node->pdata_buf = MNULL;

    if (pcmd_node->respbuf) {
        wlan_free_mlan_buffer(pmadapter, pcmd_node->respbuf);
        pcmd_node->respbuf = MNULL;
    }

    LEAVE();
    return;
}

/**
 *  @brief This function will return the pointer to the first entry in
 *  		pending cmd which matches the given pioctl_req
 *
 *  @param pmadapter    A pointer to mlan_adapter
 *  @param pioctl_req   A pointer to mlan_ioctl_req buf
 *
 *  @return 	   A pointer to first entry match pioctl_req
 */
static cmd_ctrl_node *
wlan_get_pending_ioctl_cmd(pmlan_adapter pmadapter, pmlan_ioctl_req pioctl_req)
{
    cmd_ctrl_node *pcmd_node = MNULL;

    ENTER();

    if (!
        (pcmd_node =
         (cmd_ctrl_node *) util_peek_list(pmadapter->pmoal_handle,
                                          &pmadapter->cmd_pending_q,
                                          pmadapter->callbacks.moal_spin_lock,
                                          pmadapter->callbacks.
                                          moal_spin_unlock))) {
        LEAVE();
        return MNULL;
    }
    while (pcmd_node != (cmd_ctrl_node *) & pmadapter->cmd_pending_q) {
        if (pcmd_node->pioctl_buf == pioctl_req) {
            LEAVE();
            return pcmd_node;
        }
        pcmd_node = pcmd_node->pnext;
    }
    LEAVE();
    return MNULL;
}

/**
 *  @brief This function handles the command response of host_cmd
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param resp         A pointer to HostCmd_DS_COMMAND
 *  @param pioctl_buf   A pointer to mlan_ioctl_req structure
 *
 *  @return        MLAN_STATUS_SUCCESS
 */
static mlan_status
wlan_ret_host_cmd(IN pmlan_private pmpriv,
                  IN HostCmd_DS_COMMAND * resp, IN mlan_ioctl_req * pioctl_buf)
{
    mlan_ds_misc_cfg *misc;
    t_u16 size = wlan_le16_to_cpu(resp->size);

    ENTER();

    PRINTM(MINFO, "host command response size = %d\n", size);
    size = MIN(size, MRVDRV_SIZE_OF_CMD_BUFFER);
    if (pioctl_buf) {
        misc = (mlan_ds_misc_cfg *) pioctl_buf->pbuf;
        misc->param.hostcmd.len = size;
        memcpy(pmpriv->adapter, misc->param.hostcmd.cmd, (void *) resp, size);
    }

    LEAVE();
    return MLAN_STATUS_SUCCESS;
}

/**
 *  @brief This function sends host command to firmware.
 *
 *  @param pmpriv     	A pointer to mlan_private structure
 *  @param cmd      	A pointer to HostCmd_DS_COMMAND structure
 *  @param pdata_buf	A pointer to data buffer
 *  @return         	MLAN_STATUS_SUCCESS
 */
static mlan_status
wlan_cmd_host_cmd(IN pmlan_private pmpriv,
                  IN HostCmd_DS_COMMAND * cmd, IN t_void * pdata_buf)
{
    mlan_ds_misc_cmd *pcmd_ptr = (mlan_ds_misc_cmd *) pdata_buf;

    ENTER();

    /* Copy the HOST command to command buffer */
    memcpy(pmpriv->adapter, (void *) cmd, pcmd_ptr->cmd,
           MIN(MRVDRV_SIZE_OF_CMD_BUFFER, pcmd_ptr->len));
    PRINTM(MINFO, "Host command size = %d\n", pcmd_ptr->len);
    LEAVE();
    return MLAN_STATUS_SUCCESS;
}

/**
 *  @brief This function downloads a command to firmware.
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param pcmd_node    A pointer to cmd_ctrl_node structure
 *
 *  @return             MLAN_STATUS_SUCCESS or MLAN_STATUS_FAILURE
 */
static mlan_status
wlan_dnld_cmd_to_fw(IN mlan_private * pmpriv, IN cmd_ctrl_node * pcmd_node)
{

    mlan_adapter *pmadapter = pmpriv->adapter;
    mlan_callbacks *pcb = (mlan_callbacks *) & pmadapter->callbacks;
    mlan_status ret = MLAN_STATUS_SUCCESS;
    HostCmd_DS_COMMAND *pcmd;
    mlan_ioctl_req *pioctl_buf = MNULL;
    t_u16 cmd_code;
    t_u16 cmd_size;
    t_u32 sec, usec;

    ENTER();

    if (pcmd_node)
        if (pcmd_node->pioctl_buf != MNULL)
            pioctl_buf = (mlan_ioctl_req *) pcmd_node->pioctl_buf;
    if (!pmadapter || !pcmd_node) {
        if (pioctl_buf)
            pioctl_buf->status_code = MLAN_ERROR_CMD_DNLD_FAIL;
        ret = MLAN_STATUS_FAILURE;
        goto done;
    }

    pcmd =
        (HostCmd_DS_COMMAND *) (pcmd_node->cmdbuf->pbuf +
                                pcmd_node->cmdbuf->data_offset);

    /* Sanity test */
    if (pcmd == MNULL || pcmd->size == 0) {
        PRINTM(MERROR, "DNLD_CMD: pcmd is null or command size is zero, "
               "Not sending\n");
        if (pioctl_buf)
            pioctl_buf->status_code = MLAN_ERROR_CMD_DNLD_FAIL;
        wlan_insert_cmd_to_free_q(pmadapter, pcmd_node);
        ret = MLAN_STATUS_FAILURE;
        goto done;
    }

    /* Set command sequence number */
    pmadapter->seq_num++;
    pcmd->seq_num =
        wlan_cpu_to_le16(HostCmd_SET_SEQ_NO_BSS_INFO
                         (pmadapter->seq_num, pcmd_node->priv->bss_num,
                          pcmd_node->priv->bss_type));

    wlan_request_cmd_lock(pmadapter);
    pmadapter->curr_cmd = pcmd_node;
    wlan_release_cmd_lock(pmadapter);

    cmd_code = wlan_le16_to_cpu(pcmd->command);
    cmd_size = wlan_le16_to_cpu(pcmd->size);

    pcmd_node->cmdbuf->data_len = cmd_size;

    pmadapter->callbacks.moal_get_system_time(pmadapter->pmoal_handle, &sec,
                                              &usec);
    PRINTM_NETINTF(MCMND, pmpriv);
    PRINTM(MCMND, "DNLD_CMD (%lu.%06lu): 0x%x, act 0x%x, len %d, seqno 0x%x\n",
           sec, usec, cmd_code,
           wlan_le16_to_cpu(*(t_u16 *) ((t_u8 *) pcmd + S_DS_GEN)), cmd_size,
           wlan_le16_to_cpu(pcmd->seq_num));
    DBG_HEXDUMP(MCMD_D, "DNLD_CMD", (t_u8 *) pcmd, cmd_size);

    /* Send the command to lower layer */

    pcmd_node->cmdbuf->data_offset -= INTF_HEADER_LEN;
    pcmd_node->cmdbuf->data_len += INTF_HEADER_LEN;
    /* Extra header for SDIO is added here */
    ret =
        wlan_sdio_host_to_card(pmadapter, MLAN_TYPE_CMD, pcmd_node->cmdbuf,
                               MNULL);

    if (ret == MLAN_STATUS_FAILURE) {
        PRINTM(MERROR, "DNLD_CMD: Host to Card Failed\n");
        if (pioctl_buf)
            pioctl_buf->status_code = MLAN_ERROR_CMD_DNLD_FAIL;
        wlan_insert_cmd_to_free_q(pmadapter, pmadapter->curr_cmd);

        wlan_request_cmd_lock(pmadapter);
        pmadapter->curr_cmd = MNULL;
        wlan_release_cmd_lock(pmadapter);

        pmadapter->dbg.num_cmd_host_to_card_failure++;
        ret = MLAN_STATUS_FAILURE;
        goto done;
    }

    /* Save the last command id and action to debug log */
    pmadapter->dbg.last_cmd_index =
        (pmadapter->dbg.last_cmd_index + 1) % DBG_CMD_NUM;
    pmadapter->dbg.last_cmd_id[pmadapter->dbg.last_cmd_index] = cmd_code;
    pmadapter->dbg.last_cmd_act[pmadapter->dbg.last_cmd_index] =
        wlan_le16_to_cpu(*(t_u16 *) ((t_u8 *) pcmd + S_DS_GEN));

    /* Clear BSS_NO_BITS from HostCmd */
    cmd_code &= HostCmd_CMD_ID_MASK;
    /* soft_reset command has no command response, we should return here */
    if (cmd_code == HostCmd_CMD_SOFT_RESET) {
        PRINTM(MCMND, "DNLD_CMD: SoftReset\n");
        if (pioctl_buf) {
            wlan_request_cmd_lock(pmadapter);
            pmadapter->curr_cmd->pioctl_buf = MNULL;
            wlan_release_cmd_lock(pmadapter);
            pcb->moal_ioctl_complete(pmadapter->pmoal_handle, pioctl_buf,
                                     MLAN_STATUS_SUCCESS);
        }
        goto done;
    }

    /* Setup the timer after transmit command */
    pcb->moal_start_timer(pmadapter->pmoal_handle, pmadapter->pmlan_cmd_timer,
                          MFALSE, MRVDRV_TIMER_60S);

    pmadapter->cmd_timer_is_set = MTRUE;

    ret = MLAN_STATUS_SUCCESS;

  done:
    LEAVE();
    return ret;
}

/**
 *  @brief This function sends sleep confirm command to firmware.
 *
 *  @param pmadapter  A pointer to mlan_adapter structure
 *
 *  @return        MLAN_STATUS_SUCCESS or MLAN_STATUS_FAILURE
 */
static mlan_status
wlan_dnld_sleep_confirm_cmd(mlan_adapter * pmadapter)
{
    mlan_status ret = MLAN_STATUS_SUCCESS;
    static t_u32 i = 0;
    t_u16 cmd_len = 0;
    opt_sleep_confirm_buffer *sleep_cfm_buf =
        (opt_sleep_confirm_buffer *) (pmadapter->psleep_cfm->pbuf +
                                      pmadapter->psleep_cfm->data_offset);

    ENTER();

    cmd_len = sizeof(OPT_Confirm_Sleep);
    pmadapter->seq_num++;
    sleep_cfm_buf->ps_cfm_sleep.seq_num =
        wlan_cpu_to_le16(HostCmd_SET_SEQ_NO_BSS_INFO
                         (pmadapter->seq_num,
                          (wlan_get_priv(pmadapter, MLAN_BSS_ROLE_ANY))->
                          bss_num,
                          (wlan_get_priv(pmadapter, MLAN_BSS_ROLE_ANY))->
                          bss_type));
    DBG_HEXDUMP(MCMD_D, "SLEEP_CFM", &sleep_cfm_buf->ps_cfm_sleep,
                sizeof(OPT_Confirm_Sleep));

    /* Send sleep confirm command to firmware */

    pmadapter->psleep_cfm->data_len = cmd_len + INTF_HEADER_LEN;
    ret = wlan_sdio_host_to_card(pmadapter, MLAN_TYPE_CMD,
                                 pmadapter->psleep_cfm, MNULL);

    if (ret == MLAN_STATUS_FAILURE) {
        PRINTM(MERROR, "SLEEP_CFM: failed\n");
        pmadapter->dbg.num_cmd_sleep_cfm_host_to_card_failure++;
        goto done;
    } else {
        if (GET_BSS_ROLE(wlan_get_priv(pmadapter, MLAN_BSS_ROLE_ANY)) ==
            MLAN_BSS_ROLE_UAP)
            pmadapter->ps_state = PS_STATE_SLEEP_CFM;
#ifdef STA_SUPPORT
        if (GET_BSS_ROLE(wlan_get_priv(pmadapter, MLAN_BSS_ROLE_ANY)) ==
            MLAN_BSS_ROLE_STA) {
            if (!sleep_cfm_buf->ps_cfm_sleep.sleep_cfm.resp_ctrl) {
                /* Response is not needed for sleep confirm command */
                pmadapter->ps_state = PS_STATE_SLEEP;
            } else {
                pmadapter->ps_state = PS_STATE_SLEEP_CFM;
            }

            if (!sleep_cfm_buf->ps_cfm_sleep.sleep_cfm.resp_ctrl
                && (pmadapter->is_hs_configured &&
                    !pmadapter->sleep_period.period)) {
                pmadapter->pm_wakeup_card_req = MTRUE;
                wlan_host_sleep_activated_event(wlan_get_priv
                                                (pmadapter, MLAN_BSS_ROLE_STA),
                                                MTRUE);
            }
        }
#endif /* STA_SUPPORT */

#define NUM_SC_PER_LINE         16
        if (++i % NUM_SC_PER_LINE == 0)
            PRINTM(MEVENT, "+\n");
        else
            PRINTM(MEVENT, "+");
    }

  done:
    LEAVE();
    return ret;
}

/********************************************************
                Global Functions
********************************************************/

/**
 *  @brief Event handler
 *
 *  @param priv		A pointer to mlan_private structure
 *  @param event_id	Event ID
 *  @param pmevent	Event buffer
 *
 *  @return		MLAN_STATUS_SUCCESS
 */
mlan_status
wlan_recv_event(pmlan_private priv, mlan_event_id event_id, t_void * pmevent)
{
    pmlan_callbacks pcb = &priv->adapter->callbacks;

    ENTER();

    if (pmevent)
        /* The caller has provided the event. */
        pcb->moal_recv_event(priv->adapter->pmoal_handle,
                             (pmlan_event) pmevent);
    else {
        mlan_event mevent;

        memset(priv->adapter, &mevent, 0, sizeof(mlan_event));
        mevent.bss_index = priv->bss_index;
        mevent.event_id = event_id;
        mevent.event_len = 0;

        pcb->moal_recv_event(priv->adapter->pmoal_handle, &mevent);
    }

    LEAVE();
    return MLAN_STATUS_SUCCESS;
}

/**
 *  @brief This function allocates the command buffer and links
 *              it to command free queue.
 *
 *  @param pmadapter    A pointer to mlan_adapter structure
 *
 *  @return             MLAN_STATUS_SUCCESS or MLAN_STATUS_FAILURE
 */
mlan_status
wlan_alloc_cmd_buffer(IN mlan_adapter * pmadapter)
{
    mlan_status ret = MLAN_STATUS_SUCCESS;
    mlan_callbacks *pcb = (mlan_callbacks *) & pmadapter->callbacks;
    cmd_ctrl_node *pcmd_array = MNULL;
    t_u32 buf_size;
    t_u32 i;

    ENTER();

    /* Allocate and initialize cmd_ctrl_node */
    buf_size = sizeof(cmd_ctrl_node) * MRVDRV_NUM_OF_CMD_BUFFER;
    ret =
        pcb->moal_malloc(pmadapter->pmoal_handle, buf_size,
                         MLAN_MEM_DEF | MLAN_MEM_DMA, (t_u8 **) & pcmd_array);
    if (ret != MLAN_STATUS_SUCCESS || !pcmd_array) {
        PRINTM(MERROR, "ALLOC_CMD_BUF: Failed to allocate pcmd_array\n");
        ret = MLAN_STATUS_FAILURE;
        goto done;
    }

    pmadapter->cmd_pool = pcmd_array;
    memset(pmadapter, pmadapter->cmd_pool, 0, buf_size);

    /* Allocate and initialize command buffers */
    for (i = 0; i < MRVDRV_NUM_OF_CMD_BUFFER; i++) {
        if (!(pcmd_array[i].pmbuf = wlan_alloc_mlan_buffer(pmadapter,
                                                           MRVDRV_SIZE_OF_CMD_BUFFER,
                                                           0, MTRUE))) {
            PRINTM(MERROR,
                   "ALLOC_CMD_BUF: Failed to allocate command buffer\n");
            ret = MLAN_STATUS_FAILURE;
            goto done;
        }
    }

    for (i = 0; i < MRVDRV_NUM_OF_CMD_BUFFER; i++) {
        wlan_insert_cmd_to_free_q(pmadapter, &pcmd_array[i]);
    }
    ret = MLAN_STATUS_SUCCESS;
  done:
    LEAVE();
    return ret;
}

/**
 *  @brief This function frees the command buffer.
 *
 *  @param pmadapter    A pointer to mlan_adapter structure
 *
 *  @return             MLAN_STATUS_SUCCESS
 */
mlan_status
wlan_free_cmd_buffer(IN mlan_adapter * pmadapter)
{
    mlan_callbacks *pcb = (mlan_callbacks *) & pmadapter->callbacks;
    cmd_ctrl_node *pcmd_array;
    t_u32 i;

    ENTER();

    /* Need to check if cmd pool is allocated or not */
    if (pmadapter->cmd_pool == MNULL) {
        PRINTM(MINFO, "FREE_CMD_BUF: cmd_pool is Null\n");
        goto done;
    }

    pcmd_array = pmadapter->cmd_pool;

    /* Release shared memory buffers */
    for (i = 0; i < MRVDRV_NUM_OF_CMD_BUFFER; i++) {
        if (pcmd_array[i].pmbuf) {
            PRINTM(MINFO, "Free all the command buffer.\n");
            wlan_free_mlan_buffer(pmadapter, pcmd_array[i].pmbuf);
            pcmd_array[i].pmbuf = MNULL;
        }
        if (pcmd_array[i].respbuf) {
            wlan_free_mlan_buffer(pmadapter, pcmd_array[i].respbuf);
            pcmd_array[i].respbuf = MNULL;
        }
    }
    /* Release cmd_ctrl_node */
    if (pmadapter->cmd_pool) {
        PRINTM(MINFO, "Free command pool.\n");
        pcb->moal_mfree(pmadapter->pmoal_handle, (t_u8 *) pmadapter->cmd_pool);
        pmadapter->cmd_pool = MNULL;
    }

  done:
    LEAVE();
    return MLAN_STATUS_SUCCESS;
}

/**
 *  @brief This function handles events generated by firmware
 *
 *  @param pmadapter		A pointer to mlan_adapter structure
 *
 *  @return		MLAN_STATUS_SUCCESS or MLAN_STATUS_FAILURE
 */
mlan_status
wlan_process_event(pmlan_adapter pmadapter)
{
    mlan_status ret = MLAN_STATUS_SUCCESS;
    pmlan_private priv = wlan_get_priv(pmadapter, MLAN_BSS_ROLE_ANY);
    pmlan_buffer pmbuf = pmadapter->pmlan_buffer_event;
    t_u32 eventcause = pmadapter->event_cause;
    t_u32 in_ts_sec;
    t_u32 in_ts_usec;
    ENTER();

    /* Save the last event to debug log */
    pmadapter->dbg.last_event_index =
        (pmadapter->dbg.last_event_index + 1) % DBG_CMD_NUM;
    pmadapter->dbg.last_event[pmadapter->dbg.last_event_index] =
        (t_u16) eventcause;

    if ((eventcause & EVENT_ID_MASK) == EVENT_RADAR_DETECTED) {
        if (wlan_11h_dfs_event_preprocessing(pmadapter) == MLAN_STATUS_SUCCESS) {
            memcpy(pmadapter, (t_u8 *) & eventcause,
                   pmbuf->pbuf + pmbuf->data_offset, sizeof(eventcause));
        }
    }
    /* Get BSS number and corresponding priv */
    priv =
        wlan_get_priv_by_id(pmadapter, EVENT_GET_BSS_NUM(eventcause),
                            EVENT_GET_BSS_TYPE(eventcause));
    if (!priv)
        priv = wlan_get_priv(pmadapter, MLAN_BSS_ROLE_ANY);

    /* Clear BSS_NO_BITS from event */
    eventcause &= EVENT_ID_MASK;
    pmadapter->event_cause = eventcause;

    if (pmbuf) {
        pmbuf->bss_index = priv->bss_index;
        memcpy(pmadapter,
               pmbuf->pbuf + pmbuf->data_offset,
               (t_u8 *) & eventcause, sizeof(eventcause));
    }

    if (MTRUE && (eventcause != EVENT_PS_SLEEP && eventcause != EVENT_PS_AWAKE)
        ) {
        pmadapter->callbacks.moal_get_system_time(pmadapter->pmoal_handle,
                                                  &in_ts_sec, &in_ts_usec);
        PRINTM_NETINTF(MEVENT, priv);
        PRINTM(MEVENT, "%lu.%06lu : Event: 0x%x\n", in_ts_sec, in_ts_usec,
               eventcause);
    }

    ret = priv->ops.process_event(priv);

    pmadapter->event_cause = 0;
    pmadapter->pmlan_buffer_event = MNULL;
    if (pmbuf) {
        wlan_free_mlan_buffer(pmadapter, pmbuf);
    }

    LEAVE();
    return ret;
}

/**
 *  @brief This function requests a lock on command queue.
 *
 *  @param pmadapter    A pointer to mlan_adapter structure
 *
 *  @return             N/A
 */
t_void
wlan_request_cmd_lock(IN mlan_adapter * pmadapter)
{
    mlan_callbacks *pcb = (mlan_callbacks *) & pmadapter->callbacks;

    ENTER();

    /* Call MOAL spin lock callback function */
    pcb->moal_spin_lock(pmadapter->pmoal_handle, pmadapter->pmlan_cmd_lock);

    LEAVE();
    return;
}

/**
 *  @brief This function releases a lock on command queue.
 *
 *  @param pmadapter    A pointer to mlan_adapter structure
 *
 *  @return             N/A
 */
t_void
wlan_release_cmd_lock(IN mlan_adapter * pmadapter)
{
    mlan_callbacks *pcb = (mlan_callbacks *) & pmadapter->callbacks;

    ENTER();

    /* Call MOAL spin unlock callback function */
    pcb->moal_spin_unlock(pmadapter->pmoal_handle, pmadapter->pmlan_cmd_lock);

    LEAVE();
    return;
}

/**
 *  @brief This function prepare the command before sending to firmware.
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param cmd_no       Command number
 *  @param cmd_action   Command action: GET or SET
 *  @param cmd_oid      Cmd oid: treated as sub command
 *  @param pioctl_buf   A pointer to MLAN IOCTL Request buffer
 *  @param pdata_buf    A pointer to information buffer
 *
 *  @return             MLAN_STATUS_SUCCESS or MLAN_STATUS_FAILURE
 */
mlan_status
wlan_prepare_cmd(IN mlan_private * pmpriv,
                 IN t_u16 cmd_no,
                 IN t_u16 cmd_action,
                 IN t_u32 cmd_oid,
                 IN t_void * pioctl_buf, IN t_void * pdata_buf)
{
    mlan_status ret = MLAN_STATUS_SUCCESS;
    mlan_adapter *pmadapter = pmpriv->adapter;
    cmd_ctrl_node *pcmd_node = MNULL;
    HostCmd_DS_COMMAND *cmd_ptr = MNULL;
    pmlan_ioctl_req pioctl_req = (mlan_ioctl_req *) pioctl_buf;

    ENTER();

    /* Sanity test */
    if (!pmadapter || pmadapter->surprise_removed) {
        PRINTM(MERROR, "PREP_CMD: Card is Removed\n");
        if (pioctl_req)
            pioctl_req->status_code = MLAN_ERROR_FW_NOT_READY;
        ret = MLAN_STATUS_FAILURE;
        goto done;
    }

    if (pmadapter->hw_status == WlanHardwareStatusReset) {
        if ((cmd_no != HostCmd_CMD_FUNC_INIT)
            ) {
            PRINTM(MERROR, "PREP_CMD: FW is in reset state\n");
            if (pioctl_req)
                pioctl_req->status_code = MLAN_ERROR_FW_NOT_READY;
            ret = MLAN_STATUS_FAILURE;
            goto done;
        }
    }

    /* Get a new command node */
    pcmd_node = wlan_get_cmd_node(pmadapter);

    if (pcmd_node == MNULL) {
        PRINTM(MERROR, "PREP_CMD: No free cmd node\n");
        if (pioctl_req)
            pioctl_req->status_code = MLAN_ERROR_NO_MEM;
        ret = MLAN_STATUS_FAILURE;
        goto done;
    }

    /* Initialize the command node */
    wlan_init_cmd_node(pmpriv, pcmd_node, cmd_oid, pioctl_buf, pdata_buf);

    if (pcmd_node->cmdbuf == MNULL) {
        PRINTM(MERROR, "PREP_CMD: No free cmd buf\n");
        if (pioctl_req)
            pioctl_req->status_code = MLAN_ERROR_NO_MEM;
        ret = MLAN_STATUS_FAILURE;
        goto done;
    }

    cmd_ptr =
        (HostCmd_DS_COMMAND *) (pcmd_node->cmdbuf->pbuf +
                                pcmd_node->cmdbuf->data_offset);
    cmd_ptr->command = cmd_no;
    cmd_ptr->result = 0;

    /* Prepare command */
    if (cmd_no)
        ret =
            pmpriv->ops.prepare_cmd(pmpriv, cmd_no, cmd_action, cmd_oid,
                                    pioctl_buf, pdata_buf, cmd_ptr);
    else {
        ret = wlan_cmd_host_cmd(pmpriv, cmd_ptr, pdata_buf);
        pcmd_node->cmd_flag |= CMD_F_HOSTCMD;
    }

    /* Return error, since the command preparation failed */
    if (ret != MLAN_STATUS_SUCCESS) {
        PRINTM(MERROR, "PREP_CMD: Command 0x%x preparation failed\n", cmd_no);
        pcmd_node->pioctl_buf = MNULL;
        wlan_insert_cmd_to_free_q(pmadapter, pcmd_node);
        if (pioctl_req)
            pioctl_req->status_code = MLAN_ERROR_CMD_DNLD_FAIL;
        ret = MLAN_STATUS_FAILURE;
        goto done;
    }

    /* Send command */
#ifdef STA_SUPPORT
    if (cmd_no == HostCmd_CMD_802_11_SCAN
        || cmd_no == HostCmd_CMD_802_11_SCAN_EXT)
        wlan_queue_scan_cmd(pmpriv, pcmd_node);
    else {
#endif
        if ((cmd_no == HostCmd_CMD_802_11_HS_CFG_ENH) &&
            (cmd_action == HostCmd_ACT_GEN_SET) &&
            (pmadapter->hs_cfg.conditions == HOST_SLEEP_CFG_CANCEL))
            wlan_insert_cmd_to_pending_q(pmadapter, pcmd_node, MFALSE);
        else
            wlan_insert_cmd_to_pending_q(pmadapter, pcmd_node, MTRUE);
#ifdef STA_SUPPORT
    }
#endif
  done:
    LEAVE();
    return ret;
}

/**
 *  @brief This function inserts command node to cmd_free_q
 *              after cleaning it.
 *
 *  @param pmadapter    A pointer to mlan_adapter structure
 *  @param pcmd_node    A pointer to cmd_ctrl_node structure
 *
 *  @return             N/A
 */
t_void
wlan_insert_cmd_to_free_q(IN mlan_adapter * pmadapter,
                          IN cmd_ctrl_node * pcmd_node)
{
    mlan_callbacks *pcb = (mlan_callbacks *) & pmadapter->callbacks;
    mlan_ioctl_req *pioctl_req = MNULL;
    ENTER();

    if (pcmd_node == MNULL)
        goto done;
    if (pcmd_node->pioctl_buf) {
        pioctl_req = (mlan_ioctl_req *) pcmd_node->pioctl_buf;
        if (pioctl_req->status_code != MLAN_ERROR_NO_ERROR)
            pcb->moal_ioctl_complete(pmadapter->pmoal_handle,
                                     pioctl_req, MLAN_STATUS_FAILURE);
        else
            pcb->moal_ioctl_complete(pmadapter->pmoal_handle,
                                     pioctl_req, MLAN_STATUS_SUCCESS);
    }
    /* Clean the node */
    wlan_clean_cmd_node(pmadapter, pcmd_node);

    /* Insert node into cmd_free_q */
    util_enqueue_list_tail(pmadapter->pmoal_handle, &pmadapter->cmd_free_q,
                           (pmlan_linked_list) pcmd_node,
                           pmadapter->callbacks.moal_spin_lock,
                           pmadapter->callbacks.moal_spin_unlock);
  done:
    LEAVE();
}

/**
 *  @brief This function queues the command to cmd list.
 *
 *  @param pmadapter    A pointer to mlan_adapter structure
 *  @param pcmd_node    A pointer to cmd_ctrl_node structure
 *  @param add_tail      Specify if the cmd needs to be queued in the header or tail
 *
 *  @return             N/A
 */
t_void
wlan_insert_cmd_to_pending_q(IN mlan_adapter * pmadapter,
                             IN cmd_ctrl_node * pcmd_node, IN t_u32 add_tail)
{
    HostCmd_DS_COMMAND *pcmd = MNULL;
    t_u16 command;

    ENTER();

    if (pcmd_node == MNULL) {
        PRINTM(MERROR, "QUEUE_CMD: pcmd_node is MNULL\n");
        goto done;
    }

    pcmd =
        (HostCmd_DS_COMMAND *) (pcmd_node->cmdbuf->pbuf +
                                pcmd_node->cmdbuf->data_offset);
    if (pcmd == MNULL) {
        PRINTM(MERROR, "QUEUE_CMD: pcmd is MNULL\n");
        goto done;
    }

    command = wlan_le16_to_cpu(pcmd->command);

    /* Exit_PS command needs to be queued in the header always. */
    if (command == HostCmd_CMD_802_11_PS_MODE_ENH) {
        HostCmd_DS_802_11_PS_MODE_ENH *pm = &pcmd->params.psmode_enh;
        if (wlan_le16_to_cpu(pm->action) == DIS_AUTO_PS) {
            if (pmadapter->ps_state != PS_STATE_AWAKE)
                add_tail = MFALSE;
        }
    }

    if (add_tail) {
        util_enqueue_list_tail(pmadapter->pmoal_handle,
                               &pmadapter->cmd_pending_q,
                               (pmlan_linked_list) pcmd_node,
                               pmadapter->callbacks.moal_spin_lock,
                               pmadapter->callbacks.moal_spin_unlock);
    } else {
        util_enqueue_list_head(pmadapter->pmoal_handle,
                               &pmadapter->cmd_pending_q,
                               (pmlan_linked_list) pcmd_node,
                               pmadapter->callbacks.moal_spin_lock,
                               pmadapter->callbacks.moal_spin_unlock);
    }

    PRINTM_NETINTF(MCMND, pcmd_node->priv);
    PRINTM(MCMND, "QUEUE_CMD: cmd=0x%x is queued\n", command);

  done:
    LEAVE();
    return;
}

/**
 *  @brief This function executes next command in command
 *      pending queue. It will put firmware back to PS mode
 *      if applicable.
 *
 *  @param pmadapter     A pointer to mlan_adapter structure
 *
 *  @return             MLAN_STATUS_SUCCESS or MLAN_STATUS_FAILURE
 */
mlan_status
wlan_exec_next_cmd(mlan_adapter * pmadapter)
{
    mlan_private *priv = MNULL;
    cmd_ctrl_node *pcmd_node = MNULL;
    mlan_status ret = MLAN_STATUS_SUCCESS;
    HostCmd_DS_COMMAND *pcmd;

    ENTER();

    /* Sanity test */
    if (pmadapter == MNULL) {
        PRINTM(MERROR, "EXEC_NEXT_CMD: pmadapter is MNULL\n");
        ret = MLAN_STATUS_FAILURE;
        goto done;
    }
    /* Check if already in processing */
    if (pmadapter->curr_cmd) {
        PRINTM(MERROR, "EXEC_NEXT_CMD: there is command in processing!\n");
        ret = MLAN_STATUS_FAILURE;
        goto done;
    }

    wlan_request_cmd_lock(pmadapter);
    /* Check if any command is pending */
    pcmd_node =
        (cmd_ctrl_node *) util_peek_list(pmadapter->pmoal_handle,
                                         &pmadapter->cmd_pending_q,
                                         pmadapter->callbacks.moal_spin_lock,
                                         pmadapter->callbacks.moal_spin_unlock);

    if (pcmd_node) {
        pcmd =
            (HostCmd_DS_COMMAND *) (pcmd_node->cmdbuf->pbuf +
                                    pcmd_node->cmdbuf->data_offset);
        priv = pcmd_node->priv;

        if (pmadapter->ps_state != PS_STATE_AWAKE) {
            PRINTM(MERROR,
                   "Cannot send command in sleep state, this should not happen\n");
            wlan_release_cmd_lock(pmadapter);
            goto done;
        }

        util_unlink_list(pmadapter->pmoal_handle,
                         &pmadapter->cmd_pending_q,
                         (pmlan_linked_list) pcmd_node,
                         pmadapter->callbacks.moal_spin_lock,
                         pmadapter->callbacks.moal_spin_unlock);
        wlan_release_cmd_lock(pmadapter);
        ret = wlan_dnld_cmd_to_fw(priv, pcmd_node);
        priv = wlan_get_priv(pmadapter, MLAN_BSS_ROLE_ANY);
        /* Any command sent to the firmware when host is in sleep mode, should
           de-configure host sleep */
        /* We should skip the host sleep configuration command itself though */
        if (priv &&
            (pcmd->command !=
             wlan_cpu_to_le16(HostCmd_CMD_802_11_HS_CFG_ENH))) {
            if (pmadapter->hs_activated == MTRUE) {
                pmadapter->is_hs_configured = MFALSE;
                wlan_host_sleep_activated_event(priv, MFALSE);
            }
        }
        goto done;
    } else {
        wlan_release_cmd_lock(pmadapter);
    }
    ret = MLAN_STATUS_SUCCESS;
  done:
    LEAVE();
    return ret;
}

/**
 *  @brief This function handles the command response
 *
 *  @param pmadapter    A pointer to mlan_adapter structure
 *
 *  @return             MLAN_STATUS_SUCCESS or MLAN_STATUS_FAILURE
 */
mlan_status
wlan_process_cmdresp(mlan_adapter * pmadapter)
{
    HostCmd_DS_COMMAND *resp = MNULL;
    mlan_private *pmpriv = wlan_get_priv(pmadapter, MLAN_BSS_ROLE_ANY);
    mlan_private *pmpriv_next = MNULL;
    mlan_status ret = MLAN_STATUS_SUCCESS;
    t_u16 orig_cmdresp_no;
    t_u16 cmdresp_no;
    t_u16 cmdresp_result;
    mlan_ioctl_req *pioctl_buf = MNULL;
    mlan_callbacks *pcb = (mlan_callbacks *) & pmadapter->callbacks;
    t_u32 sec, usec, i;

    ENTER();

    /* Now we got response from FW, cancel the command timer */
    if (pmadapter->cmd_timer_is_set) {
        /* Cancel command timeout timer */
        pcb->moal_stop_timer(pmadapter->pmoal_handle,
                             pmadapter->pmlan_cmd_timer);
        /* Cancel command timeout timer */
        pmadapter->cmd_timer_is_set = MFALSE;
    }

    if (pmadapter->curr_cmd)
        if (pmadapter->curr_cmd->pioctl_buf != MNULL) {
            pioctl_buf = (mlan_ioctl_req *) pmadapter->curr_cmd->pioctl_buf;
        }

    if (!pmadapter->curr_cmd || !pmadapter->curr_cmd->respbuf) {
        resp = (HostCmd_DS_COMMAND *) pmadapter->upld_buf;
        resp->command = wlan_le16_to_cpu(resp->command);
        PRINTM(MERROR, "CMD_RESP: No curr_cmd, 0x%x\n", resp->command);
        if (pioctl_buf)
            pioctl_buf->status_code = MLAN_ERROR_CMD_RESP_FAIL;
        ret = MLAN_STATUS_FAILURE;
        goto done;
    }

    pmadapter->num_cmd_timeout = 0;

    DBG_HEXDUMP(MCMD_D, "CMD_RESP",
                pmadapter->curr_cmd->respbuf->pbuf +
                pmadapter->curr_cmd->respbuf->data_offset,
                pmadapter->curr_cmd->respbuf->data_len);

    resp =
        (HostCmd_DS_COMMAND *) (pmadapter->curr_cmd->respbuf->pbuf +
                                pmadapter->curr_cmd->respbuf->data_offset);
    wlan_request_cmd_lock(pmadapter);
    if (pmadapter->curr_cmd->cmd_flag & CMD_F_CANCELED) {
        cmd_ctrl_node *free_cmd = pmadapter->curr_cmd;
        pmadapter->curr_cmd = MNULL;
        wlan_release_cmd_lock(pmadapter);
        PRINTM(MERROR, "CMD_RESP: 0x%x been canceled!\n",
               wlan_le16_to_cpu(resp->command));
        wlan_insert_cmd_to_free_q(pmadapter, free_cmd);
        if (pioctl_buf)
            pioctl_buf->status_code = MLAN_ERROR_CMD_CANCEL;
        ret = MLAN_STATUS_FAILURE;
        goto done;
    } else {
        wlan_release_cmd_lock(pmadapter);
    }
    if (pmadapter->curr_cmd->cmd_flag & CMD_F_HOSTCMD) {
        /* Copy original response back to response buffer */
        wlan_ret_host_cmd(pmpriv, resp, pioctl_buf);
    }
    orig_cmdresp_no = wlan_le16_to_cpu(resp->command);
    resp->size = wlan_le16_to_cpu(resp->size);
    resp->seq_num = wlan_le16_to_cpu(resp->seq_num);
    resp->result = wlan_le16_to_cpu(resp->result);

    /* Get BSS number and corresponding priv */
    pmpriv =
        wlan_get_priv_by_id(pmadapter, HostCmd_GET_BSS_NO(resp->seq_num),
                            HostCmd_GET_BSS_TYPE(resp->seq_num));
    if (!pmpriv)
        pmpriv = wlan_get_priv(pmadapter, MLAN_BSS_ROLE_ANY);
    /* Clear RET_BIT from HostCmd */
    resp->command = (orig_cmdresp_no & HostCmd_CMD_ID_MASK);
    cmdresp_no = resp->command;

    cmdresp_result = resp->result;

    /* Save the last command response to debug log */
    pmadapter->dbg.last_cmd_resp_index =
        (pmadapter->dbg.last_cmd_resp_index + 1) % DBG_CMD_NUM;
    pmadapter->dbg.last_cmd_resp_id[pmadapter->dbg.last_cmd_resp_index] =
        orig_cmdresp_no;

    pmadapter->callbacks.moal_get_system_time(pmadapter->pmoal_handle, &sec,
                                              &usec);
    PRINTM_NETINTF(MCMND, pmadapter->curr_cmd->priv);
    PRINTM(MCMND, "CMD_RESP (%lu.%06lu): 0x%x, result %d, len %d, seqno 0x%x\n",
           sec, usec, orig_cmdresp_no, cmdresp_result, resp->size,
           resp->seq_num);

    if (!(orig_cmdresp_no & HostCmd_RET_BIT)) {
        PRINTM(MERROR, "CMD_RESP: Invalid response to command!\n");
        if (pioctl_buf) {
            pioctl_buf->status_code = MLAN_ERROR_FW_CMDRESP;
        }
        wlan_insert_cmd_to_free_q(pmadapter, pmadapter->curr_cmd);
        wlan_request_cmd_lock(pmadapter);
        pmadapter->curr_cmd = MNULL;
        wlan_release_cmd_lock(pmadapter);
        ret = MLAN_STATUS_FAILURE;
        goto done;
    }

    if (pmadapter->curr_cmd->cmd_flag & CMD_F_HOSTCMD) {
        pmadapter->curr_cmd->cmd_flag &= ~CMD_F_HOSTCMD;
        if ((cmdresp_result == HostCmd_RESULT_OK)
            && (cmdresp_no == HostCmd_CMD_802_11_HS_CFG_ENH))
            ret = wlan_ret_802_11_hs_cfg(pmpriv, resp, pioctl_buf);
    } else {
        /* handle response */
        ret = pmpriv->ops.process_cmdresp(pmpriv, cmdresp_no, resp, pioctl_buf);
    }

    /* Check init command response */
    if (pmadapter->hw_status == WlanHardwareStatusInitializing) {
        if (ret == MLAN_STATUS_FAILURE) {
#if defined(STA_SUPPORT)
            if (pmadapter->pwarm_reset_ioctl_req) {
                /* warm reset failure */
                pmadapter->pwarm_reset_ioctl_req->status_code =
                    MLAN_ERROR_CMD_RESP_FAIL;
                pcb->moal_ioctl_complete(pmadapter->pmoal_handle,
                                         pmadapter->pwarm_reset_ioctl_req,
                                         MLAN_STATUS_FAILURE);
                pmadapter->pwarm_reset_ioctl_req = MNULL;
                goto done;
            }
#endif
            PRINTM(MERROR, "cmd 0x%02x failed during initialization\n",
                   cmdresp_no);
            wlan_init_fw_complete(pmadapter);
            goto done;
        }
    }

    wlan_request_cmd_lock(pmadapter);
    if (pmadapter->curr_cmd) {
        cmd_ctrl_node *free_cmd = pmadapter->curr_cmd;
        pioctl_buf = (mlan_ioctl_req *) pmadapter->curr_cmd->pioctl_buf;
        pmadapter->curr_cmd = MNULL;
        wlan_release_cmd_lock(pmadapter);

        if (pioctl_buf && (ret == MLAN_STATUS_SUCCESS))
            pioctl_buf->status_code = MLAN_ERROR_NO_ERROR;
        else if (pioctl_buf && (ret == MLAN_STATUS_FAILURE))
            pioctl_buf->status_code = MLAN_ERROR_CMD_RESP_FAIL;

        /* Clean up and put current command back to cmd_free_q */
        wlan_insert_cmd_to_free_q(pmadapter, free_cmd);
    } else {
        wlan_release_cmd_lock(pmadapter);
    }

    if ((pmadapter->hw_status == WlanHardwareStatusInitializing) &&
        (pmadapter->last_init_cmd == cmdresp_no)) {
        i = pmpriv->bss_index + 1;
        while (!(pmpriv_next = pmadapter->priv[i]) && i < pmadapter->priv_num)
            i++;
        if (!pmpriv_next || i >= pmadapter->priv_num) {
#if defined(STA_SUPPORT)
            if (pmadapter->pwarm_reset_ioctl_req) {
                /* warm reset complete */
                pmadapter->hw_status = WlanHardwareStatusReady;
                pcb->moal_ioctl_complete(pmadapter->pmoal_handle,
                                         pmadapter->pwarm_reset_ioctl_req,
                                         MLAN_STATUS_SUCCESS);
                pmadapter->pwarm_reset_ioctl_req = MNULL;
                goto done;
            }
#endif
            pmadapter->hw_status = WlanHardwareStatusInitdone;
        } else {
            /* Issue init commands for the next interface */
            ret = pmpriv_next->ops.init_cmd(pmpriv_next, MFALSE);
        }
    }

  done:
    LEAVE();
    return ret;
}

/**
 *  @brief This function handles the timeout of command sending.
 *  It will re-send the same command again.
 *
 *  @param function_context   A pointer to function_context
 *  @return 	   N/A
 */
t_void
wlan_cmd_timeout_func(t_void * function_context)
{
    mlan_adapter *pmadapter = (mlan_adapter *) function_context;
    cmd_ctrl_node *pcmd_node = MNULL;
    mlan_ioctl_req *pioctl_buf = MNULL;
    t_u32 sec, usec;
    t_u8 i;
    mlan_private *pmpriv = MNULL;

    ENTER();

    pmadapter->cmd_timer_is_set = MFALSE;
    pmadapter->num_cmd_timeout++;
    pmadapter->dbg.num_cmd_timeout++;
    if (!pmadapter->curr_cmd) {
        PRINTM(MWARN, "CurCmd Empty\n");
        goto exit;
    }
    pcmd_node = pmadapter->curr_cmd;
    if (pcmd_node->pioctl_buf != MNULL) {
        pioctl_buf = (mlan_ioctl_req *) pcmd_node->pioctl_buf;
        pioctl_buf->status_code = MLAN_ERROR_CMD_TIMEOUT;
    }

    if (pcmd_node) {
        pmadapter->dbg.timeout_cmd_id =
            pmadapter->dbg.last_cmd_id[pmadapter->dbg.last_cmd_index];
        pmadapter->dbg.timeout_cmd_act =
            pmadapter->dbg.last_cmd_act[pmadapter->dbg.last_cmd_index];
        pmadapter->callbacks.moal_get_system_time(pmadapter->pmoal_handle, &sec,
                                                  &usec);
        PRINTM(MERROR, "Timeout cmd id (%lu.%06lu) = 0x%x, act = 0x%x \n", sec,
               usec, pmadapter->dbg.timeout_cmd_id,
               pmadapter->dbg.timeout_cmd_act);
        if (pcmd_node->cmdbuf) {
            t_u8 *pcmd_buf;
            pcmd_buf =
                pcmd_node->cmdbuf->pbuf + pcmd_node->cmdbuf->data_offset +
                INTF_HEADER_LEN;
            for (i = 0; i < 16; i++) {
                PRINTM(MERROR, "%02x ", *pcmd_buf++);
            }
            PRINTM(MERROR, "\n");
        }

        pmpriv = pcmd_node->priv;
        if (pmpriv) {
            PRINTM(MERROR, "BSS type = %d BSS role= %d\n", pmpriv->bss_type,
                   pmpriv->bss_role);
        }

        PRINTM(MERROR, "num_cmd_timeout = %d\n",
               pmadapter->dbg.num_cmd_timeout);
        PRINTM(MERROR, "last_cmd_index = %d\n", pmadapter->dbg.last_cmd_index);
        PRINTM(MERROR, "last_cmd_id = ");
        for (i = 0; i < DBG_CMD_NUM; i++) {
            PRINTM(MERROR, "0x%x ", pmadapter->dbg.last_cmd_id[i]);
        }
        PRINTM(MERROR, "\n");
        PRINTM(MERROR, "last_cmd_act = ");
        for (i = 0; i < DBG_CMD_NUM; i++) {
            PRINTM(MERROR, "0x%x ", pmadapter->dbg.last_cmd_act[i]);
        }
        PRINTM(MERROR, "\n");
        PRINTM(MERROR, "last_cmd_resp_index = %d\n",
               pmadapter->dbg.last_cmd_resp_index);
        PRINTM(MERROR, "last_cmd_resp_id = ");
        for (i = 0; i < DBG_CMD_NUM; i++) {
            PRINTM(MERROR, "0x%x ", pmadapter->dbg.last_cmd_resp_id[i]);
        }
        PRINTM(MERROR, "\n");
        PRINTM(MERROR, "last_event_index = %d\n",
               pmadapter->dbg.last_event_index);
        PRINTM(MERROR, "last_event = ");
        for (i = 0; i < DBG_CMD_NUM; i++) {
            PRINTM(MERROR, "0x%x ", pmadapter->dbg.last_event[i]);
        }
        PRINTM(MERROR, "\n");

        PRINTM(MERROR, "num_data_h2c_failure = %d\n",
               pmadapter->dbg.num_tx_host_to_card_failure);
        PRINTM(MERROR, "num_cmd_h2c_failure = %d\n",
               pmadapter->dbg.num_cmd_host_to_card_failure);
        PRINTM(MERROR, "num_data_c2h_failure = %d\n",
               pmadapter->dbg.num_rx_card_to_host_failure);
        PRINTM(MERROR, "num_cmdevt_c2h_failure = %d\n",
               pmadapter->dbg.num_cmdevt_card_to_host_failure);
        PRINTM(MERROR, "num_int_read_failure = %d\n",
               pmadapter->dbg.num_int_read_failure);
        PRINTM(MERROR, "last_int_status = %d\n",
               pmadapter->dbg.last_int_status);

        PRINTM(MERROR, "num_event_deauth = %d\n",
               pmadapter->dbg.num_event_deauth);
        PRINTM(MERROR, "num_event_disassoc = %d\n",
               pmadapter->dbg.num_event_disassoc);
        PRINTM(MERROR, "num_event_link_lost = %d\n",
               pmadapter->dbg.num_event_link_lost);
        PRINTM(MERROR, "num_cmd_deauth = %d\n", pmadapter->dbg.num_cmd_deauth);
        PRINTM(MERROR, "num_cmd_assoc_success = %d\n",
               pmadapter->dbg.num_cmd_assoc_success);
        PRINTM(MERROR, "num_cmd_assoc_failure = %d\n",
               pmadapter->dbg.num_cmd_assoc_failure);
        PRINTM(MERROR, "cmd_resp_received=%d\n", pmadapter->cmd_resp_received);
        PRINTM(MERROR, "event_received=%d\n", pmadapter->event_received);

        PRINTM(MERROR, "max_tx_buf_size=%d\n", pmadapter->max_tx_buf_size);
        PRINTM(MERROR, "tx_buf_size=%d\n", pmadapter->tx_buf_size);
        PRINTM(MERROR, "curr_tx_buf_size=%d\n", pmadapter->curr_tx_buf_size);

        PRINTM(MERROR, "data_sent=%d cmd_sent=%d\n", pmadapter->data_sent,
               pmadapter->cmd_sent);

        PRINTM(MERROR, "ps_mode=%d ps_state=%d\n", pmadapter->ps_mode,
               pmadapter->ps_state);
        PRINTM(MERROR, "wakeup_dev_req=%d wakeup_tries=%d\n",
               pmadapter->pm_wakeup_card_req, pmadapter->pm_wakeup_fw_try);
        PRINTM(MERROR, "hs_configured=%d hs_activated=%d\n",
               pmadapter->is_hs_configured, pmadapter->hs_activated);
        PRINTM(MERROR, "pps_uapsd_mode=%d sleep_pd=%d\n",
               pmadapter->pps_uapsd_mode, pmadapter->sleep_period.period);
        PRINTM(MERROR, "tx_lock_flag = %d\n", pmadapter->tx_lock_flag);
        PRINTM(MERROR, "scan_processing = %d\n", pmadapter->scan_processing);

        PRINTM(MERROR, "mp_rd_bitmap=0x%x curr_rd_port=0x%x\n",
               pmadapter->mp_rd_bitmap, pmadapter->curr_rd_port);
        PRINTM(MERROR, "mp_wr_bitmap=0x%x curr_wr_port=0x%x\n",
               pmadapter->mp_wr_bitmap, pmadapter->curr_wr_port);
    }
    if (pmadapter->hw_status == WlanHardwareStatusInitializing)
        wlan_init_fw_complete(pmadapter);
    else
        /* Signal MOAL to perform extra handling for debugging */
    if (pmpriv) {
        wlan_recv_event(pmpriv, MLAN_EVENT_ID_DRV_DBG_DUMP, MNULL);
    } else {
        wlan_recv_event(wlan_get_priv(pmadapter, MLAN_BSS_ROLE_ANY),
                        MLAN_EVENT_ID_DRV_DBG_DUMP, MNULL);
    }

  exit:
    LEAVE();
    return;
}

#ifdef STA_SUPPORT
/**
 *  @brief Internal function used to flush the scan pending queue
 *
 *  @param pmadapter    A pointer to mlan_adapter structure
 *
 *  @return             N/A
 */
t_void
wlan_flush_scan_queue(IN pmlan_adapter pmadapter)
{
    mlan_callbacks *pcb = (pmlan_callbacks) & pmadapter->callbacks;
    cmd_ctrl_node *pcmd_node = MNULL;

    ENTER();

    while ((pcmd_node =
            (cmd_ctrl_node *) util_peek_list(pmadapter->pmoal_handle,
                                             &pmadapter->scan_pending_q,
                                             pcb->moal_spin_lock,
                                             pcb->moal_spin_unlock))) {
        util_unlink_list(pmadapter->pmoal_handle, &pmadapter->scan_pending_q,
                         (pmlan_linked_list) pcmd_node, pcb->moal_spin_lock,
                         pcb->moal_spin_unlock);
        pcmd_node->pioctl_buf = MNULL;
        wlan_insert_cmd_to_free_q(pmadapter, pcmd_node);
    }
    wlan_request_cmd_lock(pmadapter);
    pmadapter->scan_processing = MFALSE;
    wlan_release_cmd_lock(pmadapter);

    LEAVE();
}
#endif

/**
 *  @brief Cancel all pending cmd.
 *
 *  @param pmadapter	A pointer to mlan_adapter
 *
 *  @return		N/A
 */
t_void
wlan_cancel_all_pending_cmd(pmlan_adapter pmadapter)
{
    cmd_ctrl_node *pcmd_node = MNULL;
    pmlan_callbacks pcb = &pmadapter->callbacks;
    mlan_ioctl_req *pioctl_buf = MNULL;

    ENTER();
    /* Cancel current cmd */
    wlan_request_cmd_lock(pmadapter);
    if ((pmadapter->curr_cmd) && (pmadapter->curr_cmd->pioctl_buf)) {
        pioctl_buf = (mlan_ioctl_req *) pmadapter->curr_cmd->pioctl_buf;
        pmadapter->curr_cmd->pioctl_buf = MNULL;
        wlan_release_cmd_lock(pmadapter);
        pioctl_buf->status_code = MLAN_ERROR_CMD_CANCEL;
        pcb->moal_ioctl_complete(pmadapter->pmoal_handle, pioctl_buf,
                                 MLAN_STATUS_FAILURE);
    } else {
        wlan_release_cmd_lock(pmadapter);
    }
    /* Cancel all pending command */
    while ((pcmd_node =
            (cmd_ctrl_node *) util_peek_list(pmadapter->pmoal_handle,
                                             &pmadapter->cmd_pending_q,
                                             pcb->moal_spin_lock,
                                             pcb->moal_spin_unlock))) {
        util_unlink_list(pmadapter->pmoal_handle, &pmadapter->cmd_pending_q,
                         (pmlan_linked_list) pcmd_node, pcb->moal_spin_lock,
                         pcb->moal_spin_unlock);
        if (pcmd_node->pioctl_buf) {
            pioctl_buf = (mlan_ioctl_req *) pcmd_node->pioctl_buf;
            pioctl_buf->status_code = MLAN_ERROR_CMD_CANCEL;
            pcb->moal_ioctl_complete(pmadapter->pmoal_handle, pioctl_buf,
                                     MLAN_STATUS_FAILURE);
            pcmd_node->pioctl_buf = MNULL;
        }
        wlan_insert_cmd_to_free_q(pmadapter, pcmd_node);
    }
#ifdef STA_SUPPORT
    /* Cancel all pending scan command */
    wlan_flush_scan_queue(pmadapter);
#endif
    LEAVE();
}

/**
 *  @brief Cancel pending ioctl cmd.
 *
 *  @param pmadapter	A pointer to mlan_adapter
 *  @param pioctl_req	A pointer to mlan_ioctl_req buf
 *
 *  @return		N/A
 */
t_void
wlan_cancel_pending_ioctl(pmlan_adapter pmadapter, pmlan_ioctl_req pioctl_req)
{
    pmlan_callbacks pcb = &pmadapter->callbacks;
    cmd_ctrl_node *pcmd_node = MNULL;

    ENTER();

    PRINTM(MIOCTL, "MOAL Cancel IOCTL: 0x%x sub_id=0x%x action=%d\n",
           pioctl_req->req_id, *((t_u32 *) pioctl_req->pbuf),
           (int) pioctl_req->action);

    wlan_request_cmd_lock(pmadapter);
    if ((pmadapter->curr_cmd) &&
        (pmadapter->curr_cmd->pioctl_buf == pioctl_req)) {
        pcmd_node = pmadapter->curr_cmd;
        pcmd_node->pioctl_buf = MNULL;
        pcmd_node->cmd_flag |= CMD_F_CANCELED;
    }

    while ((pcmd_node =
            wlan_get_pending_ioctl_cmd(pmadapter, pioctl_req)) != MNULL) {
        util_unlink_list(pmadapter->pmoal_handle, &pmadapter->cmd_pending_q,
                         (pmlan_linked_list) pcmd_node,
                         pmadapter->callbacks.moal_spin_lock,
                         pmadapter->callbacks.moal_spin_unlock);
        pcmd_node->pioctl_buf = MNULL;
        wlan_release_cmd_lock(pmadapter);
        wlan_insert_cmd_to_free_q(pmadapter, pcmd_node);
        wlan_request_cmd_lock(pmadapter);
    }
    wlan_release_cmd_lock(pmadapter);
#ifdef STA_SUPPORT
    if (pioctl_req->req_id == MLAN_IOCTL_SCAN) {
        /* IOCTL will be completed, avoid calling IOCTL complete again from
           EVENT */
        if (pmadapter->pext_scan_ioctl_req == pioctl_req)
            pmadapter->pext_scan_ioctl_req = MNULL;
        /* Cancel all pending scan command */
        wlan_flush_scan_queue(pmadapter);
    }
#endif
    pioctl_req->status_code = MLAN_ERROR_CMD_CANCEL;
    pcb->moal_ioctl_complete(pmadapter->pmoal_handle, pioctl_req,
                             MLAN_STATUS_FAILURE);

    LEAVE();
    return;
}

/**
 *  @brief Handle the version_ext resp
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param resp         A pointer to HostCmd_DS_COMMAND
 *  @param pioctl_buf   A pointer to mlan_ioctl_req structure
 *
 *  @return             MLAN_STATUS_SUCCESS
 */
mlan_status
wlan_ret_ver_ext(IN pmlan_private pmpriv,
                 IN HostCmd_DS_COMMAND * resp, IN mlan_ioctl_req * pioctl_buf)
{
    HostCmd_DS_VERSION_EXT *ver_ext = &resp->params.verext;
    mlan_ds_get_info *info;
    ENTER();
    if (pioctl_buf) {
        info = (mlan_ds_get_info *) pioctl_buf->pbuf;
        info->param.ver_ext.version_str_sel = ver_ext->version_str_sel;
        memcpy(pmpriv->adapter, info->param.ver_ext.version_str,
               ver_ext->version_str, sizeof(char) * 128);
    }
    LEAVE();
    return MLAN_STATUS_SUCCESS;
}

/**
 *  @brief Handle the rx mgmt forward registration resp
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param resp         A pointer to HostCmd_DS_COMMAND
 *  @param pioctl_buf   A pointer to mlan_ioctl_req structure
 *
 *  @return             MLAN_STATUS_SUCCESS
 */
mlan_status
wlan_ret_rx_mgmt_ind(IN pmlan_private pmpriv,
                     IN HostCmd_DS_COMMAND * resp,
                     IN mlan_ioctl_req * pioctl_buf)
{
    mlan_ds_misc_cfg *misc = MNULL;
    ENTER();

    if (pioctl_buf) {
        misc = (mlan_ds_misc_cfg *) pioctl_buf->pbuf;
        misc->param.mgmt_subtype_mask =
            wlan_le32_to_cpu(resp->params.rx_mgmt_ind.mgmt_subtype_mask);
    }

    LEAVE();
    return MLAN_STATUS_SUCCESS;
}

/**
 *  @brief This function checks conditions and prepares to
 *              send sleep confirm command to firmware if OK.
 *
 *  @param pmadapter    A pointer to mlan_adapter structure
 *
 *  @return             N/A
 */
t_void
wlan_check_ps_cond(mlan_adapter * pmadapter)
{
    ENTER();

    if (!pmadapter->cmd_sent &&
        !pmadapter->curr_cmd && !IS_CARD_RX_RCVD(pmadapter)) {
        wlan_dnld_sleep_confirm_cmd(pmadapter);
    } else {
        PRINTM(MCMND, "Delay Sleep Confirm (%s%s%s)\n",
               (pmadapter->cmd_sent) ? "D" : "",
               (pmadapter->curr_cmd) ? "C" : "",
               (IS_CARD_RX_RCVD(pmadapter)) ? "R" : "");
    }

    LEAVE();
}

/**
 *  @brief This function sends the HS_ACTIVATED event to the application
 *
 *  @param priv         A pointer to mlan_private structure
 *  @param activated    MTRUE if activated, MFALSE if de-activated
 *
 *  @return             N/A
 */
t_void
wlan_host_sleep_activated_event(pmlan_private priv, t_u8 activated)
{
    ENTER();

    if (activated) {
        if (priv->adapter->is_hs_configured) {
            priv->adapter->hs_activated = MTRUE;
            PRINTM(MEVENT, "hs_activated\n");
            wlan_recv_event(priv, MLAN_EVENT_ID_DRV_HS_ACTIVATED, MNULL);
        } else
            PRINTM(MWARN, "hs_activated: HS not configured !!!\n");
    } else {
        PRINTM(MEVENT, "hs_deactived\n");
        priv->adapter->hs_activated = MFALSE;
        wlan_recv_event(priv, MLAN_EVENT_ID_DRV_HS_DEACTIVATED, MNULL);
    }

    LEAVE();
}

/**
 *  @brief This function sends the HS_WAKEUP event to the application
 *
 *  @param priv         A pointer to mlan_private structure
 *
 *  @return             N/A
 */
t_void
wlan_host_sleep_wakeup_event(pmlan_private priv)
{
    ENTER();

    if (priv->adapter->is_hs_configured) {
        wlan_recv_event(priv, MLAN_EVENT_ID_FW_HS_WAKEUP, MNULL);
    } else {
        PRINTM(MWARN, "hs_wakeup: Host Sleep not configured !!!\n");
    }

    LEAVE();
}

/**
 *  @brief This function handles the command response of hs_cfg
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param resp         A pointer to HostCmd_DS_COMMAND
 *  @param pioctl_buf   A pointer to mlan_ioctl_req structure
 *
 *  @return             MLAN_STATUS_SUCCESS
 */
mlan_status
wlan_ret_802_11_hs_cfg(IN pmlan_private pmpriv,
                       IN HostCmd_DS_COMMAND * resp,
                       IN mlan_ioctl_req * pioctl_buf)
{
    pmlan_adapter pmadapter = pmpriv->adapter;
    HostCmd_DS_802_11_HS_CFG_ENH *phs_cfg = &resp->params.opt_hs_cfg;

    ENTER();

    if (wlan_le16_to_cpu(phs_cfg->action) == HS_ACTIVATE) {
        /* clean up curr_cmd to allow suspend */
        if (pioctl_buf)
            pioctl_buf->status_code = MLAN_ERROR_NO_ERROR;
        /* Clean up and put current command back to cmd_free_q */
        wlan_insert_cmd_to_free_q(pmadapter, pmadapter->curr_cmd);
        wlan_request_cmd_lock(pmadapter);
        pmadapter->curr_cmd = MNULL;
        wlan_release_cmd_lock(pmadapter);
        wlan_host_sleep_activated_event(pmpriv, MTRUE);
        goto done;
    } else {
        phs_cfg->params.hs_config.conditions =
            wlan_le32_to_cpu(phs_cfg->params.hs_config.conditions);
        PRINTM(MCMND,
               "CMD_RESP: HS_CFG cmd reply result=%#x,"
               " conditions=0x%x gpio=0x%x gap=0x%x\n", resp->result,
               phs_cfg->params.hs_config.conditions,
               phs_cfg->params.hs_config.gpio, phs_cfg->params.hs_config.gap);
    }
    if (phs_cfg->params.hs_config.conditions != HOST_SLEEP_CFG_CANCEL) {
        pmadapter->is_hs_configured = MTRUE;
    } else {
        pmadapter->is_hs_configured = MFALSE;
        if (pmadapter->hs_activated)
            wlan_host_sleep_activated_event(pmpriv, MFALSE);
    }

  done:
    LEAVE();
    return MLAN_STATUS_SUCCESS;
}

/**
 *  @brief Perform hs related activities on receiving the power up interrupt
 *
 *  @param pmadapter  A pointer to the adapter structure
 *  @return           N/A
 */
t_void
wlan_process_hs_config(pmlan_adapter pmadapter)
{
    ENTER();
    PRINTM(MINFO, "Recevie interrupt/data in HS mode\n");
    if (pmadapter->hs_cfg.gap == HOST_SLEEP_CFG_GAP_FF)
        wlan_pm_wakeup_card(pmadapter);
    LEAVE();
    return;
}

/**
 *  @brief Check sleep confirm command response and set the state to ASLEEP
 *
 *  @param pmadapter  A pointer to the adapter structure
 *  @param pbuf       A pointer to the command response buffer
 *  @param upld_len   Command response buffer length
 *  @return           N/A
 */
void
wlan_process_sleep_confirm_resp(pmlan_adapter pmadapter, t_u8 * pbuf,
                                t_u32 upld_len)
{
    HostCmd_DS_COMMAND *cmd;

    ENTER();

    if (!upld_len) {
        PRINTM(MERROR, "Command size is 0\n");
        LEAVE();
        return;
    }
    cmd = (HostCmd_DS_COMMAND *) pbuf;
    cmd->result = wlan_le16_to_cpu(cmd->result);
    cmd->command = wlan_le16_to_cpu(cmd->command);
    cmd->seq_num = wlan_le16_to_cpu(cmd->seq_num);

    /* Update sequence number */
    cmd->seq_num = HostCmd_GET_SEQ_NO(cmd->seq_num);
    /* Clear RET_BIT from HostCmd */
    cmd->command &= HostCmd_CMD_ID_MASK;

    if (cmd->command != HostCmd_CMD_802_11_PS_MODE_ENH) {
        PRINTM(MERROR,
               "Received unexpected response for command %x, result = %x\n",
               cmd->command, cmd->result);
        LEAVE();
        return;
    }
    PRINTM(MEVENT, "#\n");
    if (cmd->result != MLAN_STATUS_SUCCESS) {
        PRINTM(MERROR, "Sleep confirm command failed\n");
        pmadapter->pm_wakeup_card_req = MFALSE;
        pmadapter->ps_state = PS_STATE_AWAKE;
        LEAVE();
        return;
    }
    pmadapter->pm_wakeup_card_req = MTRUE;

    if (pmadapter->is_hs_configured) {
        wlan_host_sleep_activated_event(wlan_get_priv
                                        (pmadapter, MLAN_BSS_ROLE_ANY), MTRUE);
    }
    pmadapter->ps_state = PS_STATE_SLEEP;
    LEAVE();
}

/**
 *  @brief This function prepares command of power mode
 *
 *  @param pmpriv		A pointer to mlan_private structure
 *  @param cmd	   		A pointer to HostCmd_DS_COMMAND structure
 *  @param cmd_action   the action: GET or SET
 *  @param ps_bitmap    PS bitmap
 *  @param pdata_buf    A pointer to data buffer
 *  @return         MLAN_STATUS_SUCCESS
 */
mlan_status
wlan_cmd_enh_power_mode(pmlan_private pmpriv,
                        IN HostCmd_DS_COMMAND * cmd,
                        IN t_u16 cmd_action,
                        IN t_u16 ps_bitmap, IN t_void * pdata_buf)
{
    HostCmd_DS_802_11_PS_MODE_ENH *psmode_enh = &cmd->params.psmode_enh;
    t_u8 *tlv = MNULL;
    t_u16 cmd_size = 0;

    ENTER();

    PRINTM(MCMND, "PS Command: action = 0x%x, bitmap = 0x%x\n", cmd_action,
           ps_bitmap);

    cmd->command = wlan_cpu_to_le16(HostCmd_CMD_802_11_PS_MODE_ENH);
    if (cmd_action == DIS_AUTO_PS) {
        psmode_enh->action = wlan_cpu_to_le16(DIS_AUTO_PS);
        psmode_enh->params.ps_bitmap = wlan_cpu_to_le16(ps_bitmap);
        cmd->size = wlan_cpu_to_le16(S_DS_GEN + AUTO_PS_FIX_SIZE);
    } else if (cmd_action == GET_PS) {
        psmode_enh->action = wlan_cpu_to_le16(GET_PS);
        psmode_enh->params.ps_bitmap = wlan_cpu_to_le16(ps_bitmap);
        cmd->size = wlan_cpu_to_le16(S_DS_GEN + AUTO_PS_FIX_SIZE);
    } else if (cmd_action == EN_AUTO_PS) {
        psmode_enh->action = wlan_cpu_to_le16(EN_AUTO_PS);
        psmode_enh->params.auto_ps.ps_bitmap = wlan_cpu_to_le16(ps_bitmap);
        cmd_size = S_DS_GEN + AUTO_PS_FIX_SIZE;
        tlv = (t_u8 *) cmd + cmd_size;
        if (ps_bitmap & BITMAP_STA_PS) {
            pmlan_adapter pmadapter = pmpriv->adapter;
            MrvlIEtypes_ps_param_t *ps_tlv = (MrvlIEtypes_ps_param_t *) tlv;
            ps_param *ps_mode = &ps_tlv->param;
            ps_tlv->header.type = wlan_cpu_to_le16(TLV_TYPE_PS_PARAM);
            ps_tlv->header.len =
                wlan_cpu_to_le16(sizeof(MrvlIEtypes_ps_param_t) -
                                 sizeof(MrvlIEtypesHeader_t));
            cmd_size += sizeof(MrvlIEtypes_ps_param_t);
            tlv += sizeof(MrvlIEtypes_ps_param_t);
            ps_mode->null_pkt_interval =
                wlan_cpu_to_le16(pmadapter->null_pkt_interval);
            ps_mode->multiple_dtims =
                wlan_cpu_to_le16(pmadapter->multiple_dtim);
            ps_mode->bcn_miss_timeout =
                wlan_cpu_to_le16(pmadapter->bcn_miss_time_out);
            ps_mode->local_listen_interval =
                wlan_cpu_to_le16(pmadapter->local_listen_interval);
            ps_mode->adhoc_wake_period =
                wlan_cpu_to_le16(pmadapter->adhoc_awake_period);
            ps_mode->delay_to_ps = wlan_cpu_to_le16(pmadapter->delay_to_ps);
            ps_mode->mode = wlan_cpu_to_le16(pmadapter->enhanced_ps_mode);
        }
        if (ps_bitmap & BITMAP_AUTO_DS) {
            MrvlIEtypes_auto_ds_param_t *auto_ps_tlv =
                (MrvlIEtypes_auto_ds_param_t *) tlv;
            auto_ds_param *auto_ds = &auto_ps_tlv->param;
            t_u16 idletime = 0;
            auto_ps_tlv->header.type = wlan_cpu_to_le16(TLV_TYPE_AUTO_DS_PARAM);
            auto_ps_tlv->header.len =
                wlan_cpu_to_le16(sizeof(MrvlIEtypes_auto_ds_param_t) -
                                 sizeof(MrvlIEtypesHeader_t));
            cmd_size += sizeof(MrvlIEtypes_auto_ds_param_t);
            tlv += sizeof(MrvlIEtypes_auto_ds_param_t);
            if (pdata_buf)
                idletime = ((mlan_ds_auto_ds *) pdata_buf)->idletime;
            auto_ds->deep_sleep_timeout = wlan_cpu_to_le16(idletime);
        }
#if defined(UAP_SUPPORT)
        if (pdata_buf &&
            (ps_bitmap & (BITMAP_UAP_INACT_PS | BITMAP_UAP_DTIM_PS))) {
            mlan_ds_ps_mgmt *ps_mgmt = (mlan_ds_ps_mgmt *) pdata_buf;
            MrvlIEtypes_sleep_param_t *sleep_tlv = MNULL;
            MrvlIEtypes_inact_sleep_param_t *inact_tlv = MNULL;
            if (ps_mgmt->flags & PS_FLAG_SLEEP_PARAM) {
                sleep_tlv = (MrvlIEtypes_sleep_param_t *) tlv;
                sleep_tlv->header.type =
                    wlan_cpu_to_le16(TLV_TYPE_AP_SLEEP_PARAM);
                sleep_tlv->header.len =
                    wlan_cpu_to_le16(sizeof(MrvlIEtypes_sleep_param_t) -
                                     sizeof(MrvlIEtypesHeader_t));
                sleep_tlv->ctrl_bitmap =
                    wlan_cpu_to_le32(ps_mgmt->sleep_param.ctrl_bitmap);
                sleep_tlv->min_sleep =
                    wlan_cpu_to_le32(ps_mgmt->sleep_param.min_sleep);
                sleep_tlv->max_sleep =
                    wlan_cpu_to_le32(ps_mgmt->sleep_param.max_sleep);
                cmd_size += sizeof(MrvlIEtypes_sleep_param_t);
                tlv += sizeof(MrvlIEtypes_sleep_param_t);
            }
            if (ps_mgmt->flags & PS_FLAG_INACT_SLEEP_PARAM) {
                inact_tlv = (MrvlIEtypes_inact_sleep_param_t *) tlv;
                inact_tlv->header.type =
                    wlan_cpu_to_le16(TLV_TYPE_AP_INACT_SLEEP_PARAM);
                inact_tlv->header.len =
                    wlan_cpu_to_le16(sizeof(MrvlIEtypes_inact_sleep_param_t)
                                     - sizeof(MrvlIEtypesHeader_t));
                inact_tlv->inactivity_to =
                    wlan_cpu_to_le32(ps_mgmt->inact_param.inactivity_to);
                inact_tlv->min_awake =
                    wlan_cpu_to_le32(ps_mgmt->inact_param.min_awake);
                inact_tlv->max_awake =
                    wlan_cpu_to_le32(ps_mgmt->inact_param.max_awake);
                cmd_size += sizeof(MrvlIEtypes_inact_sleep_param_t);
                tlv += sizeof(MrvlIEtypes_inact_sleep_param_t);
            }
        }
#endif
        cmd->size = wlan_cpu_to_le16(cmd_size);
    }

    LEAVE();
    return MLAN_STATUS_SUCCESS;
}

/**
 *  @brief This function handles the command response of ps_mode_enh
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param resp         A pointer to HostCmd_DS_COMMAND
 *  @param pioctl_buf   A pointer to mlan_ioctl_req structure
 *
 *  @return             MLAN_STATUS_SUCCESS
 */
mlan_status
wlan_ret_enh_power_mode(IN pmlan_private pmpriv,
                        IN HostCmd_DS_COMMAND * resp,
                        IN mlan_ioctl_req * pioctl_buf)
{
    pmlan_adapter pmadapter = pmpriv->adapter;
    MrvlIEtypesHeader_t *mrvl_tlv = MNULL;
    MrvlIEtypes_auto_ds_param_t *auto_ds_tlv = MNULL;
    HostCmd_DS_802_11_PS_MODE_ENH *ps_mode = &resp->params.psmode_enh;

    ENTER();

    ps_mode->action = wlan_le16_to_cpu(ps_mode->action);
    PRINTM(MINFO, "CMD_RESP: PS_MODE cmd reply result=%#x action=0x%X\n",
           resp->result, ps_mode->action);
    if (ps_mode->action == EN_AUTO_PS) {
        ps_mode->params.auto_ps.ps_bitmap =
            wlan_le16_to_cpu(ps_mode->params.auto_ps.ps_bitmap);
        if (ps_mode->params.auto_ps.ps_bitmap & BITMAP_AUTO_DS) {
            PRINTM(MCMND, "Enabled auto deep sleep\n");
            pmpriv->adapter->is_deep_sleep = MTRUE;
            mrvl_tlv =
                (MrvlIEtypesHeader_t *) ((t_u8 *) ps_mode + AUTO_PS_FIX_SIZE);
            while (wlan_le16_to_cpu(mrvl_tlv->type) != TLV_TYPE_AUTO_DS_PARAM) {
                mrvl_tlv =
                    (MrvlIEtypesHeader_t *) ((t_u8 *) mrvl_tlv +
                                             wlan_le16_to_cpu(mrvl_tlv->len)
                                             + sizeof(MrvlIEtypesHeader_t));
            }
            auto_ds_tlv = (MrvlIEtypes_auto_ds_param_t *) mrvl_tlv;
            pmpriv->adapter->idle_time =
                wlan_le16_to_cpu(auto_ds_tlv->param.deep_sleep_timeout);
        }
        if (ps_mode->params.auto_ps.ps_bitmap & BITMAP_STA_PS) {
            PRINTM(MCMND, "Enabled STA power save\n");
            if (pmadapter->sleep_period.period) {
                PRINTM(MCMND, "Setting uapsd/pps mode to TRUE\n");
            }
        }
#if defined(UAP_SUPPORT)
        if (ps_mode->params.auto_ps.
            ps_bitmap & (BITMAP_UAP_INACT_PS | BITMAP_UAP_DTIM_PS)) {
            pmadapter->ps_mode = Wlan802_11PowerModePSP;
            PRINTM(MCMND, "Enabled uAP power save\n");
        }
#endif
    } else if (ps_mode->action == DIS_AUTO_PS) {
        ps_mode->params.ps_bitmap = wlan_cpu_to_le16(ps_mode->params.ps_bitmap);
        if (ps_mode->params.ps_bitmap & BITMAP_AUTO_DS) {
            pmpriv->adapter->is_deep_sleep = MFALSE;
            PRINTM(MCMND, "Disabled auto deep sleep\n");
        }
        if (ps_mode->params.ps_bitmap & BITMAP_STA_PS) {
            PRINTM(MCMND, "Disabled STA power save\n");
            if (pmadapter->sleep_period.period) {
                pmadapter->delay_null_pkt = MFALSE;
                pmadapter->tx_lock_flag = MFALSE;
                pmadapter->pps_uapsd_mode = MFALSE;
            }
        }
#if defined(UAP_SUPPORT)
        if (ps_mode->params.
            ps_bitmap & (BITMAP_UAP_INACT_PS | BITMAP_UAP_DTIM_PS)) {
            pmadapter->ps_mode = Wlan802_11PowerModeCAM;
            PRINTM(MCMND, "Disabled uAP power save\n");
        }
#endif
    } else if (ps_mode->action == GET_PS) {
        ps_mode->params.ps_bitmap = wlan_le16_to_cpu(ps_mode->params.ps_bitmap);
        if (ps_mode->params.auto_ps.
            ps_bitmap & (BITMAP_STA_PS | BITMAP_UAP_INACT_PS |
                         BITMAP_UAP_DTIM_PS))
            pmadapter->ps_mode = Wlan802_11PowerModePSP;
        else
            pmadapter->ps_mode = Wlan802_11PowerModeCAM;
        PRINTM(MCMND, "ps_bitmap=0x%x\n", ps_mode->params.ps_bitmap);
        if (pioctl_buf) {
            mlan_ds_pm_cfg *pm_cfg = (mlan_ds_pm_cfg *) pioctl_buf->pbuf;
            if (pm_cfg->sub_command == MLAN_OID_PM_CFG_IEEE_PS) {
                if (ps_mode->params.auto_ps.ps_bitmap & BITMAP_STA_PS)
                    pm_cfg->param.ps_mode = 1;
                else
                    pm_cfg->param.ps_mode = 0;
            }
#if defined(UAP_SUPPORT)
            if (pm_cfg->sub_command == MLAN_OID_PM_CFG_PS_MODE) {
                MrvlIEtypes_sleep_param_t *sleep_tlv = MNULL;
                MrvlIEtypes_inact_sleep_param_t *inact_tlv = MNULL;
                MrvlIEtypesHeader_t *tlv = MNULL;
                t_u16 tlv_type = 0;
                t_u16 tlv_len = 0;
                t_u16 tlv_buf_left = 0;
                pm_cfg->param.ps_mgmt.flags = PS_FLAG_PS_MODE;
                if (ps_mode->params.ps_bitmap & BITMAP_UAP_INACT_PS)
                    pm_cfg->param.ps_mgmt.ps_mode = PS_MODE_INACTIVITY;
                else if (ps_mode->params.ps_bitmap & BITMAP_UAP_DTIM_PS)
                    pm_cfg->param.ps_mgmt.ps_mode = PS_MODE_PERIODIC_DTIM;
                else
                    pm_cfg->param.ps_mgmt.ps_mode = PS_MODE_DISABLE;
                tlv_buf_left = resp->size - (S_DS_GEN + AUTO_PS_FIX_SIZE);
                tlv =
                    (MrvlIEtypesHeader_t *) ((t_u8 *) ps_mode +
                                             AUTO_PS_FIX_SIZE);
                while (tlv_buf_left >= sizeof(MrvlIEtypesHeader_t)) {
                    tlv_type = wlan_le16_to_cpu(tlv->type);
                    tlv_len = wlan_le16_to_cpu(tlv->len);
                    switch (tlv_type) {
                    case TLV_TYPE_AP_SLEEP_PARAM:
                        sleep_tlv = (MrvlIEtypes_sleep_param_t *) tlv;
                        pm_cfg->param.ps_mgmt.flags |= PS_FLAG_SLEEP_PARAM;
                        pm_cfg->param.ps_mgmt.sleep_param.ctrl_bitmap =
                            wlan_le32_to_cpu(sleep_tlv->ctrl_bitmap);
                        pm_cfg->param.ps_mgmt.sleep_param.min_sleep =
                            wlan_le32_to_cpu(sleep_tlv->min_sleep);
                        pm_cfg->param.ps_mgmt.sleep_param.max_sleep =
                            wlan_le32_to_cpu(sleep_tlv->max_sleep);
                        break;
                    case TLV_TYPE_AP_INACT_SLEEP_PARAM:
                        inact_tlv = (MrvlIEtypes_inact_sleep_param_t *) tlv;
                        pm_cfg->param.ps_mgmt.flags |=
                            PS_FLAG_INACT_SLEEP_PARAM;
                        pm_cfg->param.ps_mgmt.inact_param.inactivity_to =
                            wlan_le32_to_cpu(inact_tlv->inactivity_to);
                        pm_cfg->param.ps_mgmt.inact_param.min_awake =
                            wlan_le32_to_cpu(inact_tlv->min_awake);
                        pm_cfg->param.ps_mgmt.inact_param.max_awake =
                            wlan_le32_to_cpu(inact_tlv->max_awake);
                        break;
                    }
                    tlv_buf_left -= tlv_len + sizeof(MrvlIEtypesHeader_t);
                    tlv =
                        (MrvlIEtypesHeader_t *) ((t_u8 *) tlv + tlv_len +
                                                 sizeof(MrvlIEtypesHeader_t));
                }
            }
#endif
        }
    }

    LEAVE();
    return MLAN_STATUS_SUCCESS;
}

/**
 *  @brief This function handles the command response of tx rate query
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param resp         A pointer to HostCmd_DS_COMMAND
 *  @param pioctl_buf   A pointer to mlan_ioctl_req structure
 *
 *  @return             MLAN_STATUS_SUCCESS
 */
mlan_status
wlan_ret_802_11_tx_rate_query(IN pmlan_private pmpriv,
                              IN HostCmd_DS_COMMAND * resp,
                              IN mlan_ioctl_req * pioctl_buf)
{
    mlan_adapter *pmadapter = pmpriv->adapter;
    mlan_ds_rate *rate = MNULL;
    ENTER();

    pmpriv->tx_rate = resp->params.tx_rate.tx_rate;
    pmpriv->tx_htinfo = resp->params.tx_rate.ht_info;
    if (!pmpriv->is_data_rate_auto) {
        pmpriv->data_rate = wlan_index_to_data_rate(pmadapter,
                                                    pmpriv->tx_rate,
                                                    pmpriv->tx_htinfo);
    }

    if (pioctl_buf) {
        rate = (mlan_ds_rate *) pioctl_buf->pbuf;
        if (rate->sub_command == MLAN_OID_RATE_CFG) {
            if (rate->param.rate_cfg.rate_type == MLAN_RATE_INDEX) {
                if (pmpriv->tx_htinfo & MBIT(0))
                    rate->param.rate_cfg.rate =
                        pmpriv->tx_rate + MLAN_RATE_INDEX_MCS0;
                else
                    /* For HostCmd_CMD_802_11_TX_RATE_QUERY, there is a hole in
                       rate table between HR/DSSS and OFDM rates, so minus 1
                       for OFDM rate index */
                    rate->param.rate_cfg.rate =
                        (pmpriv->tx_rate >
                         MLAN_RATE_INDEX_OFDM0) ? pmpriv->tx_rate -
                        1 : pmpriv->tx_rate;
            } else {
                rate->param.rate_cfg.rate =
                    wlan_index_to_data_rate(pmadapter, pmpriv->tx_rate,
                                            pmpriv->tx_htinfo);
            }
        } else if (rate->sub_command == MLAN_OID_GET_DATA_RATE) {
            if (pmpriv->tx_htinfo & MBIT(0)) {
                rate->param.data_rate.tx_data_rate =
                    pmpriv->tx_rate + MLAN_RATE_INDEX_MCS0;
                if (pmpriv->tx_htinfo & MBIT(1))
                    rate->param.data_rate.tx_ht_bw = MLAN_HT_BW40;
                else
                    rate->param.data_rate.tx_ht_bw = MLAN_HT_BW20;
                if (pmpriv->tx_htinfo & MBIT(2))
                    rate->param.data_rate.tx_ht_gi = MLAN_HT_SGI;
                else
                    rate->param.data_rate.tx_ht_gi = MLAN_HT_LGI;
            } else
                /* For HostCmd_CMD_802_11_TX_RATE_QUERY, there is a hole in
                   rate table between HR/DSSS and OFDM rates, so minus 1 for
                   OFDM rate index */
                rate->param.data_rate.tx_data_rate =
                    (pmpriv->tx_rate >
                     MLAN_RATE_INDEX_OFDM0) ? pmpriv->tx_rate -
                    1 : pmpriv->tx_rate;
            if (pmpriv->rxpd_htinfo & MBIT(0)) {
                rate->param.data_rate.rx_data_rate =
                    pmpriv->rxpd_rate + MLAN_RATE_INDEX_MCS0;
                if (pmpriv->rxpd_htinfo & MBIT(1))
                    rate->param.data_rate.rx_ht_bw = MLAN_HT_BW40;
                else
                    rate->param.data_rate.rx_ht_bw = MLAN_HT_BW20;
                if (pmpriv->tx_htinfo & MBIT(2))
                    rate->param.data_rate.rx_ht_gi = MLAN_HT_SGI;
                else
                    rate->param.data_rate.rx_ht_gi = MLAN_HT_LGI;
            } else
                /* For rate index in RxPD, there is a hole in rate table
                   between HR/DSSS and OFDM rates, so minus 1 for OFDM rate
                   index */
                rate->param.data_rate.rx_data_rate =
                    (pmpriv->rxpd_rate >
                     MLAN_RATE_INDEX_OFDM0) ? pmpriv->rxpd_rate -
                    1 : pmpriv->rxpd_rate;
        }
        pioctl_buf->data_read_written =
            sizeof(mlan_multicast_list) + MLAN_SUB_COMMAND_SIZE;
    }
    LEAVE();
    return MLAN_STATUS_SUCCESS;
}

/**
 *  @brief This function prepares command of tx_rate_cfg.
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param cmd          A pointer to HostCmd_DS_COMMAND structure
 *  @param cmd_action   The action: GET or SET
 *  @param pdata_buf    A pointer to data buffer
 *
 *  @return             MLAN_STATUS_SUCCESS
 */
mlan_status
wlan_cmd_tx_rate_cfg(IN pmlan_private pmpriv,
                     IN HostCmd_DS_COMMAND * cmd,
                     IN t_u16 cmd_action, IN t_void * pdata_buf)
{
    HostCmd_DS_TX_RATE_CFG *rate_cfg = &cmd->params.tx_rate_cfg;
    MrvlRateScope_t *rate_scope;
    MrvlRateDropPattern_t *rate_drop;
    t_u16 *pbitmap_rates = (t_u16 *) pdata_buf;

    t_u32 i;

    ENTER();

    cmd->command = wlan_cpu_to_le16(HostCmd_CMD_TX_RATE_CFG);

    rate_cfg->action = wlan_cpu_to_le16(cmd_action);
    rate_cfg->cfg_index = 0;

    rate_scope = (MrvlRateScope_t *) ((t_u8 *) rate_cfg +
                                      sizeof(HostCmd_DS_TX_RATE_CFG));
    rate_scope->type = wlan_cpu_to_le16(TLV_TYPE_RATE_SCOPE);
    rate_scope->length = wlan_cpu_to_le16(sizeof(MrvlRateScope_t) -
                                          sizeof(MrvlIEtypesHeader_t));
    if (pbitmap_rates != MNULL) {
        rate_scope->hr_dsss_rate_bitmap = wlan_cpu_to_le16(pbitmap_rates[0]);
        rate_scope->ofdm_rate_bitmap = wlan_cpu_to_le16(pbitmap_rates[1]);
        for (i = 0; i < sizeof(rate_scope->ht_mcs_rate_bitmap) / sizeof(t_u16);
             i++)
            rate_scope->ht_mcs_rate_bitmap[i] =
                wlan_cpu_to_le16(pbitmap_rates[2 + i]);
    } else {
        rate_scope->hr_dsss_rate_bitmap =
            wlan_cpu_to_le16(pmpriv->bitmap_rates[0]);
        rate_scope->ofdm_rate_bitmap =
            wlan_cpu_to_le16(pmpriv->bitmap_rates[1]);
        for (i = 0; i < sizeof(rate_scope->ht_mcs_rate_bitmap) / sizeof(t_u16);
             i++)
            rate_scope->ht_mcs_rate_bitmap[i] =
                wlan_cpu_to_le16(pmpriv->bitmap_rates[2 + i]);
    }

    rate_drop = (MrvlRateDropPattern_t *) ((t_u8 *) rate_scope +
                                           sizeof(MrvlRateScope_t));
    rate_drop->type = wlan_cpu_to_le16(TLV_TYPE_RATE_DROP_PATTERN);
    rate_drop->length = wlan_cpu_to_le16(sizeof(rate_drop->rate_drop_mode));
    rate_drop->rate_drop_mode = 0;

    cmd->size = wlan_cpu_to_le16(S_DS_GEN + sizeof(HostCmd_DS_TX_RATE_CFG) +
                                 sizeof(MrvlRateScope_t) +
                                 sizeof(MrvlRateDropPattern_t));

    LEAVE();
    return MLAN_STATUS_SUCCESS;
}

/**
 *  @brief This function handles the command response of tx_rate_cfg
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param resp         A pointer to HostCmd_DS_COMMAND
 *  @param pioctl_buf   A pointer to mlan_ioctl_req structure
 *
 *  @return             MLAN_STATUS_SUCCESS or MLAN_STATUS_FAILURE
 */
mlan_status
wlan_ret_tx_rate_cfg(IN pmlan_private pmpriv,
                     IN HostCmd_DS_COMMAND * resp,
                     IN mlan_ioctl_req * pioctl_buf)
{
    mlan_adapter *pmadapter = pmpriv->adapter;
    mlan_ds_rate *ds_rate = MNULL;
    HostCmd_DS_TX_RATE_CFG *prate_cfg = MNULL;
    MrvlRateScope_t *prate_scope;
    MrvlIEtypesHeader_t *head = MNULL;
    t_u16 tlv, tlv_buf_len;
    t_u8 *tlv_buf;
    t_u32 i;
    t_s32 index;
    mlan_status ret = MLAN_STATUS_SUCCESS;

    ENTER();

    if (resp == MNULL) {
        LEAVE();
        return MLAN_STATUS_FAILURE;
    }
    prate_cfg = &resp->params.tx_rate_cfg;

    tlv_buf = (t_u8 *) ((t_u8 *) prate_cfg) + sizeof(HostCmd_DS_TX_RATE_CFG);
    tlv_buf_len = *(t_u16 *) (tlv_buf + sizeof(t_u16));
    tlv_buf_len = wlan_le16_to_cpu(tlv_buf_len);

    while (tlv_buf && tlv_buf_len > 0) {
        tlv = (*tlv_buf);
        tlv = tlv | (*(tlv_buf + 1) << 8);

        switch (tlv) {
        case TLV_TYPE_RATE_SCOPE:
            prate_scope = (MrvlRateScope_t *) tlv_buf;
            pmpriv->bitmap_rates[0] =
                wlan_le16_to_cpu(prate_scope->hr_dsss_rate_bitmap);
            pmpriv->bitmap_rates[1] =
                wlan_le16_to_cpu(prate_scope->ofdm_rate_bitmap);
            for (i = 0;
                 i < sizeof(prate_scope->ht_mcs_rate_bitmap) / sizeof(t_u16);
                 i++)
                pmpriv->bitmap_rates[2 + i] =
                    wlan_le16_to_cpu(prate_scope->ht_mcs_rate_bitmap[i]);
            break;
            /* Add RATE_DROP tlv here */
        }

        head = (MrvlIEtypesHeader_t *) tlv_buf;
        head->len = wlan_le16_to_cpu(head->len);
        tlv_buf += head->len + sizeof(MrvlIEtypesHeader_t);
        tlv_buf_len -= head->len;
    }

    pmpriv->is_data_rate_auto = wlan_is_rate_auto(pmpriv);

    if (pmpriv->is_data_rate_auto) {
        pmpriv->data_rate = 0;
    } else {
        ret = wlan_prepare_cmd(pmpriv,
                               HostCmd_CMD_802_11_TX_RATE_QUERY,
                               HostCmd_ACT_GEN_GET, 0, MNULL, MNULL);

    }

    if (pioctl_buf) {
        ds_rate = (mlan_ds_rate *) pioctl_buf->pbuf;
        if (ds_rate == MNULL) {
            PRINTM(MERROR, "Request buffer not found!\n");
            LEAVE();
            return MLAN_STATUS_FAILURE;
        }
        if (pmpriv->is_data_rate_auto) {
            ds_rate->param.rate_cfg.is_rate_auto = MTRUE;
        } else {
            ds_rate->param.rate_cfg.is_rate_auto = MFALSE;
            /* check the LG rate */
            index = wlan_get_rate_index(pmadapter, &pmpriv->bitmap_rates[0], 4);
            if (index != -1) {
                if ((index >= MLAN_RATE_BITMAP_OFDM0) &&
                    (index <= MLAN_RATE_BITMAP_OFDM7))
                    index -= (MLAN_RATE_BITMAP_OFDM0 - MLAN_RATE_INDEX_OFDM0);
                ds_rate->param.rate_cfg.rate = index;
            }
            /* check the HT rate */
            index = wlan_get_rate_index(pmadapter,
                                        &pmpriv->bitmap_rates[2], 16);
            if (index != -1) {
                ds_rate->param.rate_cfg.rate = index + MLAN_RATE_INDEX_MCS0;
            }
            PRINTM(MINFO, "Rate index is %d\n", ds_rate->param.rate_cfg.rate);
        }
        for (i = 0; i < MAX_BITMAP_RATES_SIZE; i++) {
            ds_rate->param.rate_cfg.bitmap_rates[i] = pmpriv->bitmap_rates[i];
        }

    }

    LEAVE();
    return ret;
}

/**
 *  @brief  This function issues adapter specific commands
 *  		to initialize firmware
 *
 *  @param pmadapter    A pointer to mlan_adapter structure
 *
 *  @return             MLAN_STATUS_PENDING or MLAN_STATUS_FAILURE
 */
mlan_status
wlan_adapter_init_cmd(IN pmlan_adapter pmadapter)
{
    mlan_status ret = MLAN_STATUS_SUCCESS;
    pmlan_private pmpriv = MNULL;
#ifdef STA_SUPPORT
    pmlan_private pmpriv_sta = MNULL;
#endif

    ENTER();

    pmpriv = wlan_get_priv(pmadapter, MLAN_BSS_ROLE_ANY);
#ifdef STA_SUPPORT
    pmpriv_sta = wlan_get_priv(pmadapter, MLAN_BSS_ROLE_STA);
#endif

    /*
     * This should be issued in the very first to config
     *   SDIO_GPIO interrupt mode.
     */
    if (wlan_set_sdio_gpio_int(pmpriv) != MLAN_STATUS_SUCCESS) {
        ret = MLAN_STATUS_FAILURE;
        goto done;
    }

    ret = wlan_prepare_cmd(pmpriv, HostCmd_CMD_FUNC_INIT,
                           HostCmd_ACT_GEN_SET, 0, MNULL, MNULL);
    if (ret) {
        ret = MLAN_STATUS_FAILURE;
        goto done;
    }

    /** Cal data dnld cmd prepare */
    if ((pmadapter->pcal_data) && (pmadapter->cal_data_len > 0)) {
        ret =
            wlan_prepare_cmd(pmpriv, HostCmd_CMD_CFG_DATA, HostCmd_ACT_GEN_SET,
                             0, MNULL, MNULL);
        if (ret) {
            ret = MLAN_STATUS_FAILURE;
            goto done;
        }
        pmadapter->pcal_data = MNULL;
        pmadapter->cal_data_len = 0;
    }

    /*
     * Get HW spec
     */
    ret = wlan_prepare_cmd(pmpriv, HostCmd_CMD_GET_HW_SPEC,
                           HostCmd_ACT_GEN_GET, 0, MNULL, MNULL);
    if (ret) {
        ret = MLAN_STATUS_FAILURE;
        goto done;
    }

    /* Reconfigure tx buf size */
    ret = wlan_prepare_cmd(pmpriv, HostCmd_CMD_RECONFIGURE_TX_BUFF,
                           HostCmd_ACT_GEN_SET, 0, MNULL,
                           &pmadapter->max_tx_buf_size);
    if (ret) {
        ret = MLAN_STATUS_FAILURE;
        goto done;
    }
#if defined(STA_SUPPORT)
    if (pmpriv_sta && (pmpriv_sta->state_11d.user_enable_11d == ENABLE_11D)) {
        /* Send command to FW to enable 11d */
        ret = wlan_prepare_cmd(pmpriv_sta,
                               HostCmd_CMD_802_11_SNMP_MIB,
                               HostCmd_ACT_GEN_SET,
                               Dot11D_i,
                               MNULL, &pmpriv_sta->state_11d.user_enable_11d);
        if (ret) {
            ret = MLAN_STATUS_FAILURE;
            goto done;
        }
    }
#endif

#if defined(STA_SUPPORT)
    if (pmpriv_sta && (pmadapter->ps_mode == Wlan802_11PowerModePSP)) {
        ret = wlan_prepare_cmd(pmpriv_sta, HostCmd_CMD_802_11_PS_MODE_ENH,
                               EN_AUTO_PS, BITMAP_STA_PS, MNULL, MNULL);
        if (ret) {
            ret = MLAN_STATUS_FAILURE;
            goto done;
        }
    }
#endif

    if (pmadapter->init_auto_ds) {
        mlan_ds_auto_ds auto_ds;
        /* Enable auto deep sleep */
        auto_ds.idletime = pmadapter->idle_time;
        ret = wlan_prepare_cmd(pmpriv, HostCmd_CMD_802_11_PS_MODE_ENH,
                               EN_AUTO_PS, BITMAP_AUTO_DS, MNULL, &auto_ds);
        if (ret) {
            ret = MLAN_STATUS_FAILURE;
            goto done;
        }
    }

    ret = MLAN_STATUS_PENDING;
  done:
    LEAVE();
    return ret;
}

/**
 *  @brief This function prepares command of get_hw_spec.
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param pcmd         A pointer to HostCmd_DS_COMMAND structure
 *
 *  @return             MLAN_STATUS_SUCCESS
 */
mlan_status
wlan_cmd_get_hw_spec(IN pmlan_private pmpriv, IN HostCmd_DS_COMMAND * pcmd)
{
    HostCmd_DS_GET_HW_SPEC *hw_spec = &pcmd->params.hw_spec;

    ENTER();

    pcmd->command = wlan_cpu_to_le16(HostCmd_CMD_GET_HW_SPEC);
    pcmd->size = wlan_cpu_to_le16(sizeof(HostCmd_DS_GET_HW_SPEC) + S_DS_GEN);
    memcpy(pmpriv->adapter, hw_spec->permanent_addr, pmpriv->curr_addr,
           MLAN_MAC_ADDR_LENGTH);

    LEAVE();
    return MLAN_STATUS_SUCCESS;
}

/**
 *  @brief This function prepares command of set_cfg_data.
 *
 *  @param pmpriv       A pointer to mlan_private strcture
 *  @param pcmd         A pointer to HostCmd_DS_COMMAND structure
 *  @param cmd_action   Command action: GET or SET
 *  @param pdata_buf    A pointer to cal_data buf
 *
 *  @return             MLAN_STATUS_SUCCESS
 */
mlan_status
wlan_cmd_cfg_data(IN pmlan_private pmpriv,
                  IN HostCmd_DS_COMMAND * pcmd,
                  IN t_u16 cmd_action, IN t_void * pdata_buf)
{
    mlan_status ret = MLAN_STATUS_SUCCESS;
    HostCmd_DS_802_11_CFG_DATA *pcfg_data = &(pcmd->params.cfg_data);
    pmlan_adapter pmadapter = pmpriv->adapter;
    t_u32 len;
    t_u32 cal_data_offset;
    t_u8 *temp_pcmd = (t_u8 *) pcmd;

    ENTER();

    cal_data_offset = S_DS_GEN + sizeof(HostCmd_DS_802_11_CFG_DATA);
    if ((pmadapter->pcal_data) && (pmadapter->cal_data_len > 0)) {
        len = wlan_parse_cal_cfg((t_u8 *) pmadapter->pcal_data,
                                 pmadapter->cal_data_len,
                                 (t_u8 *) (temp_pcmd + cal_data_offset));
    } else {
        ret = MLAN_STATUS_FAILURE;
        goto done;
    }

    pcfg_data->action = cmd_action;
    pcfg_data->type = 2;        /* cal data type */
    pcfg_data->data_len = len;

    pcmd->command = HostCmd_CMD_CFG_DATA;
    pcmd->size = pcfg_data->data_len + cal_data_offset;

    pcmd->command = wlan_cpu_to_le16(pcmd->command);
    pcmd->size = wlan_cpu_to_le16(pcmd->size);

    pcfg_data->action = wlan_cpu_to_le16(pcfg_data->action);
    pcfg_data->type = wlan_cpu_to_le16(pcfg_data->type);
    pcfg_data->data_len = wlan_cpu_to_le16(pcfg_data->data_len);

  done:
    LEAVE();
    return ret;
}

/**
 *  @brief This function handles the command response of set_cfg_data
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param resp         A pointer to HostCmd_DS_COMMAND
 *  @param pioctl_buf   A pointer to A pointer to mlan_ioctl_req
 *
 *  @return             MLAN_STATUS_SUCCESS or MLAN_STATUS_FAILURE
 */
mlan_status
wlan_ret_cfg_data(IN pmlan_private pmpriv,
                  IN HostCmd_DS_COMMAND * resp, IN t_void * pioctl_buf)
{
    mlan_status ret = MLAN_STATUS_SUCCESS;

    ENTER();

    if (resp->result != HostCmd_RESULT_OK) {
        PRINTM(MERROR, "Cal data cmd resp failed\n");
        ret = MLAN_STATUS_FAILURE;
    }
    LEAVE();
    return ret;
}

/**
 *  @brief This function handles the command response of get_hw_spec
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param resp         A pointer to HostCmd_DS_COMMAND
 *  @param pioctl_buf   A pointer to command buffer
 *
 *  @return             MLAN_STATUS_SUCCESS or MLAN_STATUS_FAILURE
 */
mlan_status
wlan_ret_get_hw_spec(IN pmlan_private pmpriv,
                     IN HostCmd_DS_COMMAND * resp, IN t_void * pioctl_buf)
{
    HostCmd_DS_GET_HW_SPEC *hw_spec = &resp->params.hw_spec;
    mlan_adapter *pmadapter = pmpriv->adapter;
    mlan_status ret = MLAN_STATUS_SUCCESS;
    t_u32 i;
    pmlan_ioctl_req pioctl_req = (mlan_ioctl_req *) pioctl_buf;

    ENTER();

    pmadapter->fw_cap_info = wlan_le32_to_cpu(hw_spec->fw_cap_info);
#ifdef STA_SUPPORT
    if (IS_SUPPORT_MULTI_BANDS(pmadapter)) {
        pmadapter->fw_bands = (t_u8) GET_FW_DEFAULT_BANDS(pmadapter);
    } else {
        pmadapter->fw_bands = BAND_B;
    }

    pmadapter->config_bands = pmadapter->fw_bands;
    for (i = 0; i < pmadapter->priv_num; i++) {
        if (pmadapter->priv[i])
            pmadapter->priv[i]->config_bands = pmadapter->fw_bands;
    }

    if (pmadapter->fw_bands & BAND_A) {
        if (pmadapter->fw_bands & BAND_GN) {
            pmadapter->config_bands |= BAND_AN;
            for (i = 0; i < pmadapter->priv_num; i++) {
                if (pmadapter->priv[i])
                    pmadapter->priv[i]->config_bands |= BAND_AN;
            }

            pmadapter->fw_bands |= BAND_AN;
        }
        if ((pmadapter->fw_bands & BAND_AN)
            ) {
            pmadapter->adhoc_start_band = BAND_A | BAND_AN;
            pmadapter->adhoc_11n_enabled = MTRUE;
        } else
            pmadapter->adhoc_start_band = BAND_A;
        pmpriv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL_A;
    } else if ((pmadapter->fw_bands & BAND_GN)
        ) {
        pmadapter->adhoc_start_band = BAND_G | BAND_B | BAND_GN;
        pmpriv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
        pmadapter->adhoc_11n_enabled = MTRUE;
    } else if (pmadapter->fw_bands & BAND_G) {
        pmadapter->adhoc_start_band = BAND_G | BAND_B;
        pmpriv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
    } else if (pmadapter->fw_bands & BAND_B) {
        pmadapter->adhoc_start_band = BAND_B;
        pmpriv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
    }
#endif /* STA_SUPPORT */

    pmadapter->fw_release_number = hw_spec->fw_release_number;
    pmadapter->number_of_antenna = wlan_le16_to_cpu(hw_spec->number_of_antenna);

    PRINTM(MINFO, "GET_HW_SPEC: fw_release_number- 0x%X\n",
           wlan_le32_to_cpu(pmadapter->fw_release_number));
    PRINTM(MINFO, "GET_HW_SPEC: Permanent addr- %2x:%2x:%2x:%2x:%2x:%2x\n",
           hw_spec->permanent_addr[0], hw_spec->permanent_addr[1],
           hw_spec->permanent_addr[2], hw_spec->permanent_addr[3],
           hw_spec->permanent_addr[4], hw_spec->permanent_addr[5]);
    PRINTM(MINFO, "GET_HW_SPEC: hw_if_version=0x%X  version=0x%X\n",
           wlan_le16_to_cpu(hw_spec->hw_if_version),
           wlan_le16_to_cpu(hw_spec->version));

    if (pmpriv->curr_addr[0] == 0xff)
        memmove(pmadapter, pmpriv->curr_addr, hw_spec->permanent_addr,
                MLAN_MAC_ADDR_LENGTH);

    pmadapter->hw_dot_11n_dev_cap = wlan_le32_to_cpu(hw_spec->dot_11n_dev_cap);
    pmadapter->usr_dot_11n_dev_cap_bg = pmadapter->hw_dot_11n_dev_cap &
        DEFAULT_11N_CAP_MASK_BG;
    pmadapter->usr_dot_11n_dev_cap_a = pmadapter->hw_dot_11n_dev_cap &
        DEFAULT_11N_CAP_MASK_A;
    pmadapter->usr_dev_mcs_support = pmadapter->hw_dev_mcs_support =
        hw_spec->dev_mcs_support;
    wlan_show_dot11ndevcap(pmadapter, pmadapter->hw_dot_11n_dev_cap);
    wlan_show_devmcssupport(pmadapter, pmadapter->hw_dev_mcs_support);
    pmadapter->mp_end_port = wlan_le16_to_cpu(hw_spec->mp_end_port);

    for (i = 1; i <= (unsigned) (MAX_PORT - pmadapter->mp_end_port); i++) {
        pmadapter->mp_data_port_mask &= ~(1 << (MAX_PORT - i));
    }

    pmadapter->max_mgmt_ie_index = wlan_le16_to_cpu(hw_spec->mgmt_buf_count);
    PRINTM(MINFO, "GET_HW_SPEC: mgmt IE count=%d\n",
           pmadapter->max_mgmt_ie_index);
    if (!pmadapter->max_mgmt_ie_index)
        pmadapter->max_mgmt_ie_index = MAX_MGMT_IE_INDEX;

    pmadapter->region_code = wlan_le16_to_cpu(hw_spec->region_code);
    for (i = 0; i < MRVDRV_MAX_REGION_CODE; i++) {
        /* Use the region code to search for the index */
        if (pmadapter->region_code == region_code_index[i])
            break;
    }
    /* If it's unidentified region code, use the default */
    if (i >= MRVDRV_MAX_REGION_CODE) {
        pmadapter->region_code = MRVDRV_DEFAULT_REGION_CODE;
        PRINTM(MWARN, "unidentified region code, use the default (0x%02x)\n",
               MRVDRV_DEFAULT_REGION_CODE);
    }

    if (wlan_set_regiontable(pmpriv, (t_u8) pmadapter->region_code,
                             pmadapter->fw_bands)) {
        if (pioctl_req)
            pioctl_req->status_code = MLAN_ERROR_CMD_SCAN_FAIL;
        ret = MLAN_STATUS_FAILURE;
        goto done;
    }
#ifdef STA_SUPPORT
    if (wlan_11d_set_universaltable(pmpriv, pmadapter->fw_bands)) {
        if (pioctl_req)
            pioctl_req->status_code = MLAN_ERROR_CMD_SCAN_FAIL;
        ret = MLAN_STATUS_FAILURE;
        goto done;
    }
#endif /* STA_SUPPORT */

  done:
    LEAVE();
    return ret;
}

/**
 *  @brief This function prepares command of radio_control.
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param cmd          A pointer to HostCmd_DS_COMMAND structure
 *  @param cmd_action   The action: GET or SET
 *  @param pdata_buf    A pointer to data buffer
 *
 *  @return             MLAN_STATUS_SUCCESS
 */
mlan_status
wlan_cmd_802_11_radio_control(IN pmlan_private pmpriv,
                              IN HostCmd_DS_COMMAND * cmd,
                              IN t_u16 cmd_action, IN t_void * pdata_buf)
{
    HostCmd_DS_802_11_RADIO_CONTROL *pradio_control = &cmd->params.radio;
    t_u32 radio_ctl;
    ENTER();
    cmd->size = wlan_cpu_to_le16((sizeof(HostCmd_DS_802_11_RADIO_CONTROL))
                                 + S_DS_GEN);
    cmd->command = wlan_cpu_to_le16(HostCmd_CMD_802_11_RADIO_CONTROL);
    pradio_control->action = wlan_cpu_to_le16(cmd_action);
    memcpy(pmpriv->adapter, &radio_ctl, pdata_buf, sizeof(t_u32));
    pradio_control->control = wlan_cpu_to_le16((t_u16) radio_ctl);
    LEAVE();
    return MLAN_STATUS_SUCCESS;
}

/**
 *  @brief This function handles the command response of radio_control
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param resp         A pointer to HostCmd_DS_COMMAND
 *  @param pioctl_buf   A pointer to mlan_ioctl_req structure
 *
 *  @return             MLAN_STATUS_SUCCESS
 */
mlan_status
wlan_ret_802_11_radio_control(IN pmlan_private pmpriv,
                              IN HostCmd_DS_COMMAND * resp,
                              IN mlan_ioctl_req * pioctl_buf)
{
    HostCmd_DS_802_11_RADIO_CONTROL *pradio_ctrl =
        (HostCmd_DS_802_11_RADIO_CONTROL *) & resp->params.radio;
    mlan_ds_radio_cfg *radio_cfg = MNULL;
    mlan_adapter *pmadapter = pmpriv->adapter;

    ENTER();
    pmadapter->radio_on = wlan_le16_to_cpu(pradio_ctrl->control);
    if (pioctl_buf) {
        radio_cfg = (mlan_ds_radio_cfg *) pioctl_buf->pbuf;
        radio_cfg->param.radio_on_off = (t_u32) pmadapter->radio_on;
        pioctl_buf->data_read_written = sizeof(mlan_ds_radio_cfg);
    }
    LEAVE();
    return MLAN_STATUS_SUCCESS;
}

#ifdef WIFI_DIRECT_SUPPORT
/**
 *  @brief This function prepares command of remain_on_channel.
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param cmd          A pointer to HostCmd_DS_COMMAND structure
 *  @param cmd_action   The action: GET or SET
 *  @param pdata_buf    A pointer to data buffer
 *
 *  @return             MLAN_STATUS_SUCCESS
 */
mlan_status
wlan_cmd_remain_on_channel(IN pmlan_private pmpriv,
                           IN HostCmd_DS_COMMAND * cmd,
                           IN t_u16 cmd_action, IN t_void * pdata_buf)
{
    HostCmd_DS_REMAIN_ON_CHANNEL *remain_channel = &cmd->params.remain_on_chan;
    mlan_ds_remain_chan *cfg = (mlan_ds_remain_chan *) pdata_buf;
    ENTER();
    cmd->size = wlan_cpu_to_le16((sizeof(HostCmd_DS_REMAIN_ON_CHANNEL))
                                 + S_DS_GEN);
    cmd->command = wlan_cpu_to_le16(HostCmd_CMD_802_11_REMAIN_ON_CHANNEL);
    remain_channel->action = cmd_action;
    if (cmd_action == HostCmd_ACT_GEN_SET) {
        if (cfg->remove) {
            remain_channel->action = HostCmd_ACT_GEN_REMOVE;
        } else {
            remain_channel->bandcfg = cfg->bandcfg;
            remain_channel->channel = cfg->channel;
            remain_channel->remain_period =
                wlan_cpu_to_le32(cfg->remain_period);
        }
    }
    remain_channel->action = wlan_cpu_to_le16(remain_channel->action);

    LEAVE();
    return MLAN_STATUS_SUCCESS;
}

/**
 *  @brief This function handles the command response of remain_on_channel
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param resp         A pointer to HostCmd_DS_COMMAND
 *  @param pioctl_buf   A pointer to mlan_ioctl_req structure
 *
 *  @return             MLAN_STATUS_SUCCESS
 */
mlan_status
wlan_ret_remain_on_channel(IN pmlan_private pmpriv,
                           IN HostCmd_DS_COMMAND * resp,
                           IN mlan_ioctl_req * pioctl_buf)
{
    HostCmd_DS_REMAIN_ON_CHANNEL *remain_channel = &resp->params.remain_on_chan;
    mlan_ds_radio_cfg *radio_cfg = MNULL;

    ENTER();
    if (pioctl_buf) {
        radio_cfg = (mlan_ds_radio_cfg *) pioctl_buf->pbuf;
        radio_cfg->param.remain_chan.status = remain_channel->status;
        radio_cfg->param.remain_chan.bandcfg = remain_channel->bandcfg;
        radio_cfg->param.remain_chan.channel = remain_channel->channel;
        radio_cfg->param.remain_chan.remain_period =
            wlan_le32_to_cpu(remain_channel->remain_period);
        pioctl_buf->data_read_written = sizeof(mlan_ds_radio_cfg);
    }
    LEAVE();
    return MLAN_STATUS_SUCCESS;
}

/**
 *  @brief This function prepares command of wifi direct mode.
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param cmd          A pointer to HostCmd_DS_COMMAND structure
 *  @param cmd_action   The action: GET or SET
 *  @param pdata_buf    A pointer to data buffer
 *
 *  @return             MLAN_STATUS_SUCCESS
 */
mlan_status
wlan_cmd_wifi_direct_mode(IN pmlan_private pmpriv,
                          IN HostCmd_DS_COMMAND * cmd,
                          IN t_u16 cmd_action, IN t_void * pdata_buf)
{
    HostCmd_DS_WIFI_DIRECT_MODE *wfd_mode = &cmd->params.wifi_direct_mode;
    t_u16 mode = *((t_u16 *) pdata_buf);
    ENTER();
    cmd->size = wlan_cpu_to_le16((sizeof(HostCmd_DS_WIFI_DIRECT_MODE))
                                 + S_DS_GEN);
    cmd->command = wlan_cpu_to_le16(HOST_CMD_WIFI_DIRECT_MODE_CONFIG);
    wfd_mode->action = wlan_cpu_to_le16(cmd_action);
    if (cmd_action == HostCmd_ACT_GEN_SET)
        wfd_mode->mode = wlan_cpu_to_le16(mode);

    LEAVE();
    return MLAN_STATUS_SUCCESS;
}

/**
 *  @brief This function handles the command response of wifi direct mode
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param resp         A pointer to HostCmd_DS_COMMAND
 *  @param pioctl_buf   A pointer to mlan_ioctl_req structure
 *
 *  @return             MLAN_STATUS_SUCCESS
 */
mlan_status
wlan_ret_wifi_direct_mode(IN pmlan_private pmpriv,
                          IN HostCmd_DS_COMMAND * resp,
                          IN mlan_ioctl_req * pioctl_buf)
{
    HostCmd_DS_WIFI_DIRECT_MODE *wfd_mode = &resp->params.wifi_direct_mode;
    mlan_ds_bss *bss = MNULL;

    ENTER();
    if (pioctl_buf) {
        bss = (mlan_ds_bss *) pioctl_buf->pbuf;
        bss->param.wfd_mode = wlan_le16_to_cpu(wfd_mode->mode);
        pioctl_buf->data_read_written = sizeof(mlan_ds_bss);
    }
    LEAVE();
    return MLAN_STATUS_SUCCESS;
}
#endif