summaryrefslogtreecommitdiff
path: root/network/wlan/WDI/COMMON/MgntGen.c
blob: f45a22cb774e80d6ac1d5891b98166118f437c8e (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
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
#include "Mp_Precomp.h"

#if WPP_SOFTWARE_TRACE
#include "MgntGen.tmh"
#endif

#define BEACON_FRAME_LEN	512

static VOID
TbttPollingWorkItemCallback(
	IN PVOID		pContext
	);


BOOLEAN
IsMgntNDPA(
	pu1Byte		pdu
)
{
	BOOLEAN ret = FALSE;
	if(IsMgntActionNoAck(pdu) && GET_80211_HDR_ORDER(pdu))
	{
		if(GET_HT_CTRL_NDP_ANNOUNCEMENT(pdu+sMacHdrLng) == 1)
			ret = TRUE;
	}
	return ret;
}


BOOLEAN
MgntIsRateSupport(
	u1Byte			nRate,
	OCTET_STRING	osRateSet
	)
{
	u2Byte	i;
	for(i = 0; i < osRateSet.Length; i++)
	{
		if((nRate & 0x7f) == (osRateSet.Octet[i] & 0x7f))
			return TRUE;
	}
	return FALSE;
}


//
//	Description:
//		Initialize RegSuppRateSets.
//
VOID
MgntInitRateSet(
	PADAPTER			Adapter
)
{
	PMGNT_INFO		pMgntInfo = &(Adapter->MgntInfo);
	POCTET_STRING	RegSuppRateSets = &(pMgntInfo->RegSuppRateSets[0]);
	pu1Byte			RegHTSuppRateSets = &(pMgntInfo->RegHTSuppRateSet[0]);
	pu1Byte			RegVHTSuppRateSets = &(pMgntInfo->RegVHTSuppRateSet[0]);
	u1Byte			i;

	FillOctetString( pMgntInfo->mBrates, pMgntInfo->mBratesBuf, 0 );
	FillOctetString( pMgntInfo->SupportRatesfromBCN, pMgntInfo->SupportRatesfromBCNBuf, 0 );
	FillOctetString( pMgntInfo->dot11OperationalRateSet, pMgntInfo->dot11OperationalRateBuf, 0 );
	FillOctetString( pMgntInfo->SupportedRates, pMgntInfo->SupportedRatesBuf, 0 );
	FillOctetString( pMgntInfo->Regdot11OperationalRateSet, pMgntInfo->Regdot11OperationalRateBuf, 0 );

	for(i = 0; i < MAX_WIRELESS_MODE_CNT; i++)
	{
		FillOctetString( RegSuppRateSets[i], pMgntInfo->RegSuppRateSetsBuf[i], 0 );
	}

 	// "|0x80" to tag it as basic rate.
 	
	// WIRELESS_MODE_A:
	// WIRELESS_MODE_N_5G:
	// WIRELESS_MODE_AC_5G:
	RegSuppRateSets[0].Octet[0] = MGN_6M	| 0x80;
	RegSuppRateSets[0].Octet[1] = MGN_9M;
	RegSuppRateSets[0].Octet[2] = MGN_12M	| 0x80;
	RegSuppRateSets[0].Octet[3] = MGN_18M;
	RegSuppRateSets[0].Octet[4] = MGN_24M	| 0x80;
	RegSuppRateSets[0].Octet[5] = MGN_36M;
	RegSuppRateSets[0].Octet[6] = MGN_48M;
	RegSuppRateSets[0].Octet[7] = MGN_54M;
	RegSuppRateSets[0].Length = 8;

	// WIRELESS_MODE_B:
	RegSuppRateSets[1].Octet[0] = MGN_1M 	| 0x80;
	RegSuppRateSets[1].Octet[1] = MGN_2M 	| 0x80;
	RegSuppRateSets[1].Octet[2] = MGN_5_5M	| 0x80;
	RegSuppRateSets[1].Octet[3] = MGN_11M	| 0x80;
	RegSuppRateSets[1].Length = 4;

	// WIRELESS_MODE_G:
	// WIRELESS_MODE_N_24G:
	// WIRELESS_MODE_AC_24G:
	RegSuppRateSets[2].Octet[0] = MGN_1M 	| 0x80;
	RegSuppRateSets[2].Octet[1] = MGN_2M 	| 0x80;
	RegSuppRateSets[2].Octet[2] = MGN_5_5M | 0x80;
	RegSuppRateSets[2].Octet[3] = MGN_11M	| 0x80;
	RegSuppRateSets[2].Octet[4] = MGN_6M;
	RegSuppRateSets[2].Octet[5] = MGN_9M;
	RegSuppRateSets[2].Octet[6] = MGN_12M;
	RegSuppRateSets[2].Octet[7] = MGN_18M;
	RegSuppRateSets[2].Octet[8] = MGN_24M;
	RegSuppRateSets[2].Octet[9] = MGN_36M;
	RegSuppRateSets[2].Octet[10] = MGN_48M;
	RegSuppRateSets[2].Octet[11] = MGN_54M;
	RegSuppRateSets[2].Length = 12;


	// TODO: may be different if we have differnt number of antenna
	RegHTSuppRateSets[0] = 0xFF;	//support MCS 0~7
	RegHTSuppRateSets[1] = 0xFF;	//support MCS 8~15
	RegHTSuppRateSets[2] = 0xFF;	//support MCS 16~23

	RegVHTSuppRateSets[0] = 0xff;	//Support 1SS~3SS MCS 0~9
	RegVHTSuppRateSets[1] = 0xff;	//Not support 4SS MCS 0~9
}


//
//	Description:
//		Update SupportedRates to that of current wirles mode.
//
VOID
MgntRefreshSuppRateSet(
	PADAPTER			Adapter
)
{
	PMGNT_INFO					pMgntInfo = &(Adapter->MgntInfo);
	PRT_HIGH_THROUGHPUT			pHTInfo = GET_HT_INFO(pMgntInfo);
	PRT_VERY_HIGH_THROUGHPUT	pVHTInfo = GET_VHT_INFO(pMgntInfo);

	switch(pMgntInfo->dot11CurrentWirelessMode)
	{
		case WIRELESS_MODE_A:
		case WIRELESS_MODE_N_5G:
		case WIRELESS_MODE_AC_5G:
		case WIRELESS_MODE_AC_ONLY:
			pMgntInfo->Regdot11OperationalRateSet = pMgntInfo->RegSuppRateSets[0];
		break;

		case WIRELESS_MODE_B:
			pMgntInfo->Regdot11OperationalRateSet = pMgntInfo->RegSuppRateSets[1];
		break;

		case WIRELESS_MODE_G:
		case WIRELESS_MODE_N_24G:
		case WIRELESS_MODE_AC_24G:
			pMgntInfo->Regdot11OperationalRateSet = pMgntInfo->RegSuppRateSets[2];
		break;
		
	default:
		pMgntInfo->Regdot11OperationalRateSet.Length = 0;	// No legacy rate support!!
		break;
	}	

	if(IS_WIRELESS_MODE_N(Adapter))
	{
		BOOLEAN		b1SSSupport = FALSE;
		u1Byte 		Rf_Type = RT_GetRFType(Adapter);

		PlatformMoveMemory(	pMgntInfo->Regdot11HTOperationalRateSet, 
								pMgntInfo->RegHTSuppRateSet, 
								16	);
		if(Rf_Type == RF_1T1R || Rf_Type == RF_1T2R)
			b1SSSupport = TRUE;
		else if(pHTInfo->nTxSPStream == 1 || pHTInfo->nRxSPStream == 1)
			b1SSSupport = TRUE;
		else if(Rf_Type == RF_2T2R  || Rf_Type == RF_2T3R || Rf_Type == RF_2T4R)
			pMgntInfo->Regdot11HTOperationalRateSet[2] = 0x00;

		if(b1SSSupport)
			pMgntInfo->Regdot11HTOperationalRateSet[1] = pMgntInfo->Regdot11HTOperationalRateSet[2] = 0x00;
	}	
	else
		PlatformZeroMemory(pMgntInfo->Regdot11HTOperationalRateSet, 16);

	if(IS_WIRELESS_MODE_AC(Adapter))
	{
		u1Byte 		Rf_Type = RT_GetRFType(Adapter);

		PlatformMoveMemory(	pMgntInfo->Regdot11VHTOperationalRateSet, 
								pMgntInfo->RegVHTSuppRateSet, 
								2	);
		if(Rf_Type == RF_1T1R || Rf_Type == RF_1T2R)
			pMgntInfo->Regdot11VHTOperationalRateSet[0] = 0xfe;	// Support 1SS MCS 0~9
		else if(pVHTInfo->nTxSPStream == 1 || pVHTInfo->nRxSPStream == 1)
			pMgntInfo->Regdot11VHTOperationalRateSet[0] = 0xfe;	// Support 1SS MCS 0~9
		else if(Rf_Type == RF_2T2R  || Rf_Type == RF_2T3R || Rf_Type == RF_2T4R)
			pMgntInfo->Regdot11VHTOperationalRateSet[0] = 0xfa;	//Support 1SS ~ 2SS MCS 0~9
		else if(Rf_Type == RF_3T3R)
			pMgntInfo->Regdot11VHTOperationalRateSet[0] = 0xea;	//Support 1SS ~ 3SS MCS 0~9

		if(pVHTInfo->bUpdateMcsCap)
			VHTMaskSuppDataRate(pMgntInfo->Regdot11VHTOperationalRateSet, pVHTInfo->nRxSPStream, pVHTInfo->McsCapability);

		pMgntInfo->RegVHTHighestOperaRate = VHTGetHighestMCSRate(	Adapter, 
																	pMgntInfo->Regdot11VHTOperationalRateSet);
	}	
	else
		PlatformZeroMemory(pMgntInfo->Regdot11VHTOperationalRateSet, 2);

	// Fill up SupportedRates as Regdot11OperationalRateSet.
	CopyMemOS( &pMgntInfo->SupportedRates, pMgntInfo->Regdot11OperationalRateSet, pMgntInfo->Regdot11OperationalRateSet.Length);
}

// Allocate a fixed size buffer for general purpose using.
BOOLEAN 
MgntAllocGenTempBuffer(
	IN	PADAPTER		pAdapter
	)
{
	RT_STATUS				Status;
	PRT_GEN_TEMP_BUFFER		pGenTmpBuf;
	u4Byte					i;

	pAdapter->GenTempBufferArray.Length = GEN_TEMP_BUFFER_NUM * sizeof(RT_GEN_TEMP_BUFFER);
	Status = PlatformAllocateMemory(pAdapter, &pAdapter->GenTempBufferArray.Ptr, pAdapter->GenTempBufferArray.Length);
	if(Status != RT_STATUS_SUCCESS)
		return FALSE;
	
	PlatformZeroMemory(pAdapter->GenTempBufferArray.Ptr, pAdapter->GenTempBufferArray.Length);
	RTInitializeListHead(&pAdapter->GenTempBufferQueue);
	
	pGenTmpBuf = (PRT_GEN_TEMP_BUFFER)pAdapter->GenTempBufferArray.Ptr;
	for(i=0;i<GEN_TEMP_BUFFER_NUM;i++)
	{
		Status = PlatformAllocateMemory(pAdapter, &pGenTmpBuf[i].Buffer.Ptr, GEN_TEMP_BUFFER_SIZE);
		if(Status != RT_STATUS_SUCCESS)
			break;
		pGenTmpBuf[i].Buffer.Length = GEN_TEMP_BUFFER_SIZE;
		RTInsertTailListWithCnt(&pAdapter->GenTempBufferQueue, &pGenTmpBuf[i].List, &pAdapter->NumGenTempBufferIdle);			
	}

	if (Status != RT_STATUS_SUCCESS)
		return FALSE;

	return TRUE;
}

BOOLEAN 
MgntFreeGenTempBuffer(
	IN	PADAPTER		pAdapter
	)
{
	u4Byte 					nFreed=0;
	PRT_GEN_TEMP_BUFFER		pGenTmpBuf;
	BOOLEAN					bReturn=TRUE;

	//
	// 2012/02/09 MH According to win7 driver verifier test, the setting for below items
	// A). Force pending I/O requests B). Low resource simulation C). IRP logging
	// We may init fail in allocating MGNT memory. At this time, we can not use Adpter
	// temp buffer quere to do any operation.
	//
	if(pAdapter->GenTempBufferArray.Ptr != NULL)
	{
		if(!RTIsListEmpty(&pAdapter->GenTempBufferQueue))
		{
			while(!RTIsListEmpty(&pAdapter->GenTempBufferQueue))
			{
				pGenTmpBuf = (PRT_GEN_TEMP_BUFFER)RTRemoveHeadListWithCnt(&pAdapter->GenTempBufferQueue, &pAdapter->NumGenTempBufferIdle);
		
				PlatformFreeMemory(pGenTmpBuf->Buffer.Ptr, GEN_TEMP_BUFFER_SIZE);
				nFreed++;
			}
			RT_ASSERT((nFreed==GEN_TEMP_BUFFER_NUM),("Freed General buffer(%d) less than allocated(%d)!!\n", nFreed, GEN_TEMP_BUFFER_NUM));
			bReturn = FALSE;
		}
		
		
		{
			PlatformFreeMemory(pAdapter->GenTempBufferArray.Ptr, pAdapter->GenTempBufferArray.Length);
		}
	}

	return bReturn;
}

PRT_GEN_TEMP_BUFFER
GetGenTempBuffer(
	IN PADAPTER	Adapter,
	IN	u4Byte	Length
	)
{
	PRT_GEN_TEMP_BUFFER pBuffer=NULL;
	RT_STATUS			Status;

	PlatformAcquireSpinLock(Adapter, RT_GEN_TEMP_BUF_SPINLOCK);
	if(RTIsListEmpty(&Adapter->GenTempBufferQueue) || (Length > GEN_TEMP_BUFFER_SIZE))
	{
		PlatformReleaseSpinLock(Adapter, RT_GEN_TEMP_BUF_SPINLOCK);
		/* No free General Buffer, try to allocate one dynamically now */
		Status = PlatformAllocateMemory(Adapter, (PVOID*)&pBuffer, sizeof(RT_GEN_TEMP_BUFFER)+Length);
		if(Status != RT_STATUS_SUCCESS)
		{
			RT_ASSERT(FALSE,("Get a General Buffer and Dynamical Allocate Failed!!\n"));
			return NULL;
		}
		else
		{
			pBuffer->isDynaAlloc = TRUE;
			pBuffer->Buffer.Ptr = (u1Byte *)pBuffer + sizeof(RT_GEN_TEMP_BUFFER);			
			pBuffer->Buffer.Length = Length;
			PlatformZeroMemory((u1Byte *)pBuffer->Buffer.Ptr, Length);			
		}
	}
	else
	{
		pBuffer=(PRT_GEN_TEMP_BUFFER)RTRemoveHeadListWithCnt(&Adapter->GenTempBufferQueue, &Adapter->NumGenTempBufferIdle);
		PlatformZeroMemory((u1Byte *)pBuffer->Buffer.Ptr, Length);
		pBuffer->isDynaAlloc = FALSE;
		PlatformReleaseSpinLock(Adapter, RT_GEN_TEMP_BUF_SPINLOCK);
	}

	return pBuffer;
}

VOID
ReturnGenTempBuffer(
	IN PADAPTER				Adapter,
	IN PRT_GEN_TEMP_BUFFER	pGenBuffer
	)
{
	if (pGenBuffer->isDynaAlloc)
	{
		PlatformFreeMemory((u1Byte *)pGenBuffer, sizeof(RT_GEN_TEMP_BUFFER)+pGenBuffer->Buffer.Length);
	}
	else
	{
		PlatformAcquireSpinLock(Adapter, RT_GEN_TEMP_BUF_SPINLOCK);
		RTInsertTailListWithCnt(&Adapter->GenTempBufferQueue, &pGenBuffer->List, &Adapter->NumGenTempBufferIdle);
		PlatformReleaseSpinLock(Adapter, RT_GEN_TEMP_BUF_SPINLOCK);
	}
}


//
//	Description:
//		Allocate memory of COMMON\. 
//
//	Usage:
//		Memory Allocation:
//			step 1. Add DECLARE_RT_OBJECT(OBJECT_TYPE) into the the type 
//			(OBJECT_TYPE) to allocated.
//			step 2. Use MGNT_ALLOC_RT_OBJECT(__ppObject, OBJECT_TYPE) to allocate memory 
//			for the type (OBJECT_TYPE) to specified address (__ppObject). 
//
//		Memory Release:
//			The object allocated in MgntAllocateMemory() will be release at 
//			MgntFreeMemory() automatically.
//
//	TODO: 
//		Allocate MGNT_INFO.
//
#define MGNT_ALLOC_RT_OBJECT(__pAdapter, __ObjectList, __ppObject, __OBJECT_TYPE) \
	ppObject = (__ppObject); \
	ALLOC_RT_OBJECT(__pAdapter, __ObjectList, __ppObject, __OBJECT_TYPE); \
	if(*ppObject == NULL) goto Error;

RT_STATUS
MgntAllocMemory(
	IN PADAPTER		pAdapter
	)
{
	PMGNT_INFO		pMgntInfo = &(pAdapter->MgntInfo);
	PRT_LIST_ENTRY	pObjectList = &(pMgntInfo->ObjectList);
	RT_STATUS		status = RT_STATUS_FAILURE;
	PVOID*			ppObject;

	INIT_RT_OBJECT_LIST(pObjectList);
	do
	{
		MGNT_ALLOC_RT_OBJECT(pAdapter, pObjectList, (void**)&(pMgntInfo->pStaQos), STA_QOS);		
		MGNT_ALLOC_RT_OBJECT(pAdapter, pObjectList, (void**)&(pMgntInfo->SecurityInfo.pCkipPara),CKIP_PARAMETER);
#if (WPS_SUPPORT == 1)	
		MGNT_ALLOC_RT_OBJECT(pAdapter, pObjectList, &(pMgntInfo->pSimpleConfig), SIMPLE_CONFIG_T);
#endif
		MGNT_ALLOC_RT_OBJECT(pAdapter, pObjectList, &(pMgntInfo->pDot11dInfo), RT_DOT11D_INFO);
		MGNT_ALLOC_RT_OBJECT(pAdapter, pObjectList, (void **)&(pMgntInfo->pHTInfo), RT_HIGH_THROUGHPUT);
		MGNT_ALLOC_RT_OBJECT(pAdapter, pObjectList, (void **)&(pMgntInfo->pVHTInfo), RT_VERY_HIGH_THROUGHPUT);
		MGNT_ALLOC_RT_OBJECT(pAdapter, pObjectList, (void **)&(pMgntInfo->pChannelInfo), RT_CHANNEL_INFO);

		MGNT_ALLOC_RT_OBJECT(pAdapter, pObjectList, &(pMgntInfo->pChannelList), RT_CHANNEL_LIST);

		MGNT_ALLOC_RT_OBJECT(pAdapter, pObjectList, &(pMgntInfo->pApModeInfo), RT_AP_INFO);

		if(pAdapter == GetDefaultAdapter(pAdapter) && !CustomScan_AllocInfo(pAdapter))
			break;

		if( !AllocDrvLogMemory(pAdapter) ) 
			break;

		if( !MgntAllocHashTables(pAdapter) )
			break;

		if( !MgntAllocatePacketParser(pAdapter) )
			break;

		if( !MgntAllocGenTempBuffer(pAdapter) )
			break;

		if(RT_STATUS_SUCCESS != (status = P2P_AllocP2PInfo(pAdapter)))
			break;

		if(RT_STATUS_SUCCESS != (status = NAN_Allocate(pAdapter)))
			break;

		if(RT_STATUS_SUCCESS != (status = WFD_AllocateWfdInfo(pAdapter)))
			break;

		if(RT_STATUS_SUCCESS != (status = TDLS_AllocateMemory(pAdapter)))
			break;

		status = RT_STATUS_SUCCESS;
	}while(FALSE);

Error:

	if(status != RT_STATUS_SUCCESS)
	{
		MgntFreeMemory(pAdapter);
	}

	return status;
}


//
//	Description:
//		Free memory of COMMON\. 
//
//	TODO: 
//		Allocate MGNT_INFO.
//
VOID
MgntFreeMemory(
	IN PADAPTER		pAdapter
	)
{
	PMGNT_INFO		pMgntInfo = &pAdapter->MgntInfo;
	PRT_LIST_ENTRY	pObjectList = &(pMgntInfo->ObjectList);
	PRT_WLAN_STA	pEntry = NULL;
	u4Byte			i = 0;
	
	RT_ASSERT((pObjectList->Flink != NULL && pObjectList->Blink != NULL), 
		("MgntFreeMemory(): Flink(%p) Blink(%p)\n", pObjectList->Flink, pObjectList->Blink));
	
	WFD_FreeWfdInfo(pAdapter);

	P2PFreeAllocatedMemory(pAdapter);

	P2P_FreeP2PInfo(pAdapter);
	
	NAN_Free(pAdapter); 

	TDLS_FreeMemory(pAdapter);

	MgntFreePacketParser(pAdapter);

	MgntFreeHashTables(pAdapter);

	MgntFreeGenTempBuffer (pAdapter);

	if(pMgntInfo->AdditionalBeaconIESize > 0)
	{
		PlatformFreeMemory(pMgntInfo->AdditionalBeaconIEData, pMgntInfo->AdditionalBeaconIESize);
		pMgntInfo->AdditionalBeaconIESize = 0;
	}
	
	if(pMgntInfo->AdditionalResponseIESize > 0)
	{
		PlatformFreeMemory(pMgntInfo->AdditionalResponseIEData, pMgntInfo->AdditionalResponseIESize);
		pMgntInfo->AdditionalResponseIESize = 0;
	}

	if(pMgntInfo->AdditionalAssocReqIESize > 0)
	{
		PlatformFreeMemory(pMgntInfo->AdditionalAssocReqIEData, pMgntInfo->AdditionalAssocReqIESize);
		pMgntInfo->AdditionalAssocReqIESize= 0;
	}
	
	if (pMgntInfo->AdditionalProbeReqIESize > 0)
	{
		PlatformFreeMemory(pMgntInfo->AdditionalProbeReqIEData, pMgntInfo->AdditionalProbeReqIESize);
		pMgntInfo->AdditionalProbeReqIESize = 0;
	}
	
	for(i = 0; i < ASSOCIATE_ENTRY_NUM; i++)
	{
		pEntry = &(pMgntInfo->AsocEntry[i]);
		if(pEntry)
		{
			if(pEntry->AP_RecvAsocReqLength > 0)
				PlatformFreeMemory(pEntry->AP_RecvAsocReq, 
					pEntry->AP_RecvAsocReqLength);
			
			if(pEntry->AP_SendAsocRespLength > 0)
				PlatformFreeMemory(pEntry->AP_SendAsocResp, 
					pEntry->AP_SendAsocRespLength);
		}
	}

	// Release all allocated memory in the P2P_INFO structure ----
	//P2PFreeAllocatedMemory(pP2PInfo);
	//---------------------------------------------------

	while( HAS_RT_OBJECT(pObjectList) )
	{
		FREE_RT_OBJECT(pObjectList);
	}

	FreeDrvLogMemory(pAdapter);
}


VOID
MgntInitializeAllTimer(
	PADAPTER			Adapter
)
{
	PMGNT_INFO					pMgntInfo=&Adapter->MgntInfo;
	PRT_POWER_SAVE_CONTROL	pPSC = GET_POWER_SAVE_CONTROL(pMgntInfo);	
	PSTA_QOS					pStaQos = pMgntInfo->pStaQos;
	PRT_CHANNEL_INFO			pChnlInfo = pMgntInfo->pChannelInfo;

#if (MULTICHANNEL_SUPPORT == 1)
{
	MultiChannelInitializeTimer(Adapter);
}
#endif

	PlatformInitializeTimer(Adapter, &pMgntInfo->ScanTimer, (RT_TIMER_CALL_BACK)ScanCallback, NULL, "ScanTimer");
	PlatformInitializeTimer(Adapter, &pMgntInfo->JoinTimer, (RT_TIMER_CALL_BACK)JoinTimeout, NULL, "JoinTimer");
	PlatformInitializeTimer(Adapter, &pMgntInfo->JoinConfirmTimer, (RT_TIMER_CALL_BACK)JoinConfirm, NULL, "JoinConfirm");
	PlatformInitializeTimer( Adapter, &pMgntInfo->JoinProbeReqTimer, (RT_TIMER_CALL_BACK)JoinProbeReq, NULL, "JoinProbeReq");
	PlatformInitializeTimer(Adapter, &pMgntInfo->AuthTimer, (RT_TIMER_CALL_BACK) AuthTimeout, NULL, "AuthTimer");
	PlatformInitializeTimer(Adapter, &pMgntInfo->AsocTimer, (RT_TIMER_CALL_BACK) AsocTimeout, NULL, "AsocTimer");
	PlatformInitializeTimer(Adapter, &pMgntInfo->SwBeaconTimer, (RT_TIMER_CALL_BACK)SwBeaconCallback, NULL, "SwBeaconTimer"); //use HW beacon Isaiah 
	PlatformInitializeTimer(Adapter, &pMgntInfo->globalKeyInfo.KeyMgntTimer, (RT_TIMER_CALL_BACK)KeyMgntTimeout, NULL, "globalKeyInfo.KeyMgntTimer");	// Added by Annie, 2005-06-29.
	PlatformInitializeTimer(Adapter, &pMgntInfo->AwakeTimer, (RT_TIMER_CALL_BACK)AwakeTimerCallback, NULL, "AwakeTimer");
	PlatformInitializeTimer(Adapter, &pPSC->InactivePsTimer, (RT_TIMER_CALL_BACK)InactivePsTimerCallback, NULL, "InactivePsTimer");
	PlatformInitializeTimer( Adapter, &pMgntInfo->PnpWakeUpJoinTimer, (RT_TIMER_CALL_BACK)PnPWakeUpJoinTimerCallback, NULL, "PnPWakeUpJoinTimerCallback");		
	PlatformInitializeTimer(Adapter, &pMgntInfo->LedTimer, (RT_TIMER_CALL_BACK)LedTimerCallback, NULL, "LedTimer");	
	PlatformInitializeTimer(Adapter, &(pStaQos->ACMTimer), (RT_TIMER_CALL_BACK)QosACMTimerCallback, NULL, "ACMTimer");
	PlatformInitializeTimer(Adapter, &(pStaQos->AddTsTimer), (RT_TIMER_CALL_BACK)QosAddTsTimerCallback, NULL, "AddTsTimer");	
	PlatformInitializeTimer(Adapter, &(pMgntInfo->DelaySendBeaconTimer), (RT_TIMER_CALL_BACK)DelaySendBeaconTimerCallback, NULL, "DelayStartTimer");
	PlatformInitializeTimer(Adapter, &(pMgntInfo->SecurityInfo.SAQueryTimer), (RT_TIMER_CALL_BACK)SAQueryTimerCallback, NULL, "SAQueryTimerCallback");

	DFS_TimerContrl(Adapter, DFS_TIMER_INIT);

#if (P2P_SUPPORT == 1)
{
	P2PInitializeTimer(Adapter);
}
#endif

	NAN_InitTimer(Adapter);


	PlatformInitializeTimer( Adapter, &pMgntInfo->WaitingKeyTimer, (RT_TIMER_CALL_BACK)WaitingKeyTimerCallback, NULL, "WaitingKeyTimerCallback");	

	//Move from HTInitializeHTInfo for avoiding defining duplicately. by wl 2012-01-13	
	PlatformInitializeTimer( Adapter, &(pChnlInfo->SwBwTimer), (RT_TIMER_CALL_BACK)CHNL_SetBwChnlCallback, NULL, "SwVHTBwTimer");

}


VOID
ResetPSCParameters(PADAPTER	Adapter)
{
	PMGNT_INFO				pMgntInfo=&Adapter->MgntInfo;
	PRT_POWER_SAVE_CONTROL	pPSC = GET_POWER_SAVE_CONTROL(pMgntInfo);
	//pPSC->bSwRfProcessing = FALSE ; 
	pPSC->ReturnPoint = IPS_CALLBACK_NONE;
}

VOID
InitializeWatchDogTimer(
	PADAPTER	Adapter
)
{
	PlatformInitializeTimer(Adapter, &Adapter->MgntInfo.WatchDogTimer, (RT_TIMER_CALL_BACK)WatchDogTimerCallback, NULL, "WatchDogTimer");
}

VOID
CancelWatchDogTimer(
	PADAPTER	Adapter
)
{
	PlatformCancelTimer(Adapter, &Adapter->MgntInfo.WatchDogTimer);
}

VOID
ReleaseWatchDogTimer(
	PADAPTER	Adapter
)
{
	PlatformReleaseTimer(Adapter, &Adapter->MgntInfo.WatchDogTimer);
}

	
VOID
MgntCancelAllTimer(
	PADAPTER			Adapter
	)
{
	PMGNT_INFO					pMgntInfo=&Adapter->MgntInfo;
	PRT_POWER_SAVE_CONTROL	pPSC = GET_POWER_SAVE_CONTROL(pMgntInfo);	
	PSTA_QOS					pStaQos = pMgntInfo->pStaQos;
	PRT_CHANNEL_INFO			pChannelInfo = pMgntInfo->pChannelInfo;

	FunctionIn(COMP_MLME);
	
	if(MgntScanInProgress(pMgntInfo))
		MgntResetScanProcess(Adapter);

	PlatformCancelTimer(Adapter, &pMgntInfo->ScanTimer);
	{
		PADAPTER pLoopAdapter = GetDefaultAdapter(Adapter);
	
		while(pLoopAdapter)
		{
			pLoopAdapter->MgntInfo.bScanInProgress = FALSE;
			pLoopAdapter->MgntInfo.bDualModeScanStep = 0;
			
			pLoopAdapter = GetNextExtAdapter(pLoopAdapter);
		}
	}

	PlatformCancelTimer(Adapter, &pMgntInfo->JoinTimer );
	PlatformCancelTimer(Adapter, &pMgntInfo->JoinConfirmTimer );
	PlatformCancelTimer(Adapter, &pMgntInfo->JoinProbeReqTimer );
	PlatformCancelTimer(Adapter, &pMgntInfo->AuthTimer);
	PlatformCancelTimer(Adapter, &pMgntInfo->AsocTimer);
	PlatformCancelTimer(Adapter, &pMgntInfo->SwBeaconTimer);
	PlatformCancelTimer(Adapter, &pMgntInfo->globalKeyInfo.KeyMgntTimer);	// Added by Annie, 2005-06-29.
	PlatformCancelTimer(Adapter, &pMgntInfo->AwakeTimer);	
	PlatformCancelTimer(Adapter, &pPSC->InactivePsTimer);	
	pPSC->ReturnPoint = IPS_CALLBACK_NONE;
	PlatformCancelTimer(Adapter, &pMgntInfo->PnpWakeUpJoinTimer);
	PlatformCancelTimer(Adapter, &pMgntInfo->LedTimer);	
	PlatformCancelTimer(Adapter, &(pStaQos->ACMTimer));
	PlatformCancelTimer(Adapter, &(pStaQos->AddTsTimer));
	PlatformCancelTimer(Adapter, &(pMgntInfo->DelaySendBeaconTimer));

	PlatformCancelTimer(Adapter, &(pMgntInfo->SecurityInfo.SAQueryTimer));

	DFS_TimerContrl(Adapter, DFS_TIMER_CANCEL);

#if (P2P_SUPPORT == 1)	
{
	P2PCancelTimer(Adapter);
}
#endif

	NAN_CancelTimer(Adapter);

	PlatformCancelTimer(Adapter, &pMgntInfo->WaitingKeyTimer);
	PlatformCancelTimer( Adapter, &(pChannelInfo->SwBwTimer));

	FunctionOut(COMP_MLME);
}

VOID
MgntReleaseAllTimer(
	PADAPTER			Adapter
	)
{
	PMGNT_INFO					pMgntInfo =&Adapter->MgntInfo;
	PRT_POWER_SAVE_CONTROL	pPSC = GET_POWER_SAVE_CONTROL(pMgntInfo);
#if (P2P_SUPPORT == 1)
	PP2P_INFO					pP2PInfo = (PP2P_INFO)pMgntInfo->pP2PInfo;
#endif
	PSTA_QOS					pStaQos = pMgntInfo->pStaQos;
	PRT_CHANNEL_INFO			pChannelInfo = pMgntInfo->pChannelInfo;

#if (MULTICHANNEL_SUPPORT == 1)
{
	MultiChannelReleaseTimer(Adapter);
}
#endif

	PlatformReleaseTimer(Adapter, &pMgntInfo->ScanTimer);

	PlatformReleaseTimer(Adapter, &pMgntInfo->JoinTimer );
	PlatformReleaseTimer(Adapter, &pMgntInfo->JoinConfirmTimer );
	PlatformReleaseTimer(Adapter, &pMgntInfo->JoinProbeReqTimer );
	PlatformReleaseTimer(Adapter, &pMgntInfo->AuthTimer);
	PlatformReleaseTimer(Adapter, &pMgntInfo->AsocTimer);
	PlatformReleaseTimer(Adapter, &pMgntInfo->SwBeaconTimer);
	PlatformReleaseTimer(Adapter, &pMgntInfo->globalKeyInfo.KeyMgntTimer);
	PlatformReleaseTimer(Adapter, &pMgntInfo->AwakeTimer);
	PlatformReleaseTimer(Adapter, &pPSC->InactivePsTimer);
	PlatformReleaseTimer(Adapter, &pMgntInfo->PnpWakeUpJoinTimer);	
	PlatformReleaseTimer(Adapter, &pMgntInfo->DelaySendBeaconTimer);
	PlatformReleaseTimer(Adapter, &pMgntInfo->SecurityInfo.SAQueryTimer);
	PlatformReleaseTimer(Adapter, &pMgntInfo->LedTimer);
	PlatformReleaseTimer(Adapter, &(pStaQos->ACMTimer));
	PlatformReleaseTimer(Adapter, &(pStaQos->AddTsTimer));

	DFS_TimerContrl(Adapter, DFS_TIMER_RELEASE);
	
#if (P2P_SUPPORT == 1)
{
	P2PReleaseTimer(Adapter);
}
#endif

	NAN_ReleaseTimer(Adapter);


	PlatformReleaseTimer(Adapter, &pMgntInfo->WaitingKeyTimer);
	PlatformReleaseTimer( Adapter, &(pChannelInfo->SwBwTimer));
}

//
//	Description:
//		Initialize workitems at driver initialization. 
//
VOID
MgntInitializeAllWorkItem(
	IN	PADAPTER		Adapter
	)
{
	PMGNT_INFO		pMgntInfo = &(Adapter->MgntInfo);
	
	PlatformInitializeWorkItem(
		Adapter,
		&(pMgntInfo->IbssStartRequestWorkItem), 
		IbssStartRequestCallback, 
		(PVOID)Adapter,
		"IbssStartRequestWorkItem");

	PlatformInitializeWorkItem(
		Adapter,
		&(pMgntInfo->ApStartRequestWorkItem), 
		AP_StartApRequest, 
		(PVOID)Adapter,
		"ApStartRequestWorkItem");

	PlatformInitializeWorkItem(
		Adapter,
		&(pMgntInfo->DropPacketWorkItem), 
		DropPacketWorkitemCallback, 
		(PVOID)Adapter,
		"DropPacketWorkItem");


	PlatformInitializeWorkItem(
		Adapter, 
		&(pMgntInfo->ApSendDisassocWithOldChnlWorkitem),
		Ap_SendDisassocWithOldChnlWorkitemCallback,
		(PVOID)Adapter, 
		"Ap_SendDisassocWithOldChnlWorkitem");

	PlatformInitializeWorkItem(
		Adapter,
		&(pMgntInfo->DozeWorkItem), 
		DozeWorkItemCallback, 
		(PVOID)Adapter,
		"DozeWorkItem");

	PlatformInitializeWorkItem(
		Adapter,
		&(pMgntInfo->AwakeWorkItem), 
		AwakeWorkItemCallback, 
		(PVOID)Adapter,
		"AwakeWorkItem");

	PlatformInitializeWorkItem(
		Adapter,
		&(pMgntInfo->TbttPollingWorkItem), 
		TbttPollingWorkItemCallback, 
		(PVOID)Adapter,
		"TbttPollingWorkItem");

	PlatformInitializeWorkItem(
		Adapter,
		&(pMgntInfo->PowerSaveControl.InactivePsWorkItem),
		InactivePsWorkItemCallback,
		(PVOID)Adapter,
		"InactivePsWorkItem");

	PlatformInitializeWorkItem(
		Adapter,
		&(pMgntInfo->UpdateTxPowerWorkItem), 
		UpdateTxPowerDbmWorkItemCallback, 
		(PVOID)Adapter,
		"UpdateTxPowerWorkItem");

	// 2008/05/16 MH Add to do IC verification on debug command mode.
#if DBG_CMD
	PlatformInitializeWorkItem(
		Adapter,
		&(pMgntInfo->NICVerify),
		(RT_WORKITEM_CALL_BACK)DBG_Verify_Console,
		(PVOID)Adapter,
		"NIC Verify Console");	
#endif


	DFS_WorkItemContrl(Adapter, DFS_WORKITEM_INIT);
	
#if (P2P_SUPPORT == 1)	
{
	PP2P_INFO		pP2PInfo = (PP2P_INFO)pMgntInfo->pP2PInfo;
	
	PlatformInitializeWorkItem(
		Adapter,
		&(pP2PInfo->P2PPSWorkItem), 
		(RT_WORKITEM_CALL_BACK)P2PPsWorkItemCallback,
		(PVOID)pP2PInfo,
		"P2PPsTimeoutWorkItem");
}
#endif

#if (MULTICHANNEL_SUPPORT == 1)
{
	MultiChannelInitializeWorkItem(Adapter);
}
#endif

#if(AUTO_CHNL_SEL_NHM == 1)
	PlatformInitializeWorkItem(
		Adapter,
		&(pMgntInfo->AutoChnlSel.AutoChnlSelWorkitem), 
		MgntAutoChnlSelWorkitemCallback, 
		(PVOID)Adapter,
		"MgntAutoChnlSelWorkitemCallback");
#endif

	PlatformInitializeWorkItem(
		Adapter,
		&(pMgntInfo->PowerSaveControl.FwPsClockOnWorkitem), 
		FwPsClockOnWorkitemCallback, 
		(PVOID)Adapter,
		"FwPsClockOnWorkitemCallback");

	PlatformInitializeWorkItem(
		Adapter,
		&(pMgntInfo->ChangeBwChnlFromPeerWorkitem), 
		CHNL_ChangeBwChnlFromPeerWorkitemCallBack, 
		(PVOID)Adapter,
		"CHNL_ChangeBwChnlFromPeerWorkitemCallBack");	

}


//
//	Description:
//		Free workitems at driver unload. 
//
VOID
MgntFreeAllWorkItem(
	IN	PADAPTER		Adapter
	)
{
	PMGNT_INFO		pMgntInfo = &(Adapter->MgntInfo);

	PlatformFreeWorkItem(&(pMgntInfo->IbssStartRequestWorkItem));
	PlatformFreeWorkItem(&(pMgntInfo->ApStartRequestWorkItem));
	PlatformFreeWorkItem(&(pMgntInfo->DropPacketWorkItem));
	PlatformFreeWorkItem(&(pMgntInfo->ApSendDisassocWithOldChnlWorkitem));
	PlatformFreeWorkItem(&(pMgntInfo->DozeWorkItem));
	PlatformFreeWorkItem(&(pMgntInfo->AwakeWorkItem));
	PlatformFreeWorkItem(&(pMgntInfo->TbttPollingWorkItem));
	PlatformFreeWorkItem(&(pMgntInfo->PowerSaveControl.InactivePsWorkItem));
	PlatformFreeWorkItem(&(pMgntInfo->UpdateTxPowerWorkItem));
	PlatformFreeWorkItem(&(pMgntInfo->PowerSaveControl.FwPsClockOnWorkitem));
	PlatformFreeWorkItem(&(pMgntInfo->ChangeBwChnlFromPeerWorkitem));

#if (AUTO_CHNL_SEL_NHM == 1)
	PlatformFreeWorkItem(&(pMgntInfo->AutoChnlSel.AutoChnlSelWorkitem));
#endif
	
#if DBG_CMD	
	PlatformFreeWorkItem(&(pMgntInfo->NICVerify));
#endif	
	DFS_WorkItemContrl(Adapter, DFS_WORKITEM_FREE);

	NAN_FreeAllWorkItem(Adapter);
	
#if (P2P_SUPPORT == 1)
{
	PP2P_INFO		pP2PInfo = (PP2P_INFO)pMgntInfo->pP2PInfo;
	PlatformFreeWorkItem(&(pP2PInfo->P2PPSWorkItem));
//	PlatformFreeWorkItem(&(pP2PInfo->P2POidPostProcessWorkItem));
}
#endif

#if (MULTICHANNEL_SUPPORT == 1)
{
	MultiChannelReleaseWorkItem(Adapter);
}
#endif

}

//
//	Description:
//		Reset current 802.11 management related setting.
//
VOID
ResetMgntVariables(
	PADAPTER			Adapter
	)
{
	PMGNT_INFO	pMgntInfo =&(Adapter->MgntInfo);
	u1Byte		i;
	int		nIdx;

	FunctionIn(COMP_MLME);

	PlatformCancelTimer(Adapter, &pMgntInfo->JoinTimer );
	PlatformCancelTimer(Adapter, &pMgntInfo->JoinConfirmTimer );
	PlatformCancelTimer(Adapter, &pMgntInfo->JoinProbeReqTimer );
	PlatformCancelTimer(Adapter, &pMgntInfo->AuthTimer);
	PlatformCancelTimer(Adapter, &pMgntInfo->AsocTimer);

	pMgntInfo->OpMode = RT_OP_MODE_NO_LINK;
	pMgntInfo->AuthStatus = AUTH_STATUS_IDLE;
	pMgntInfo->mDisable = TRUE;
	pMgntInfo->mAssoc = FALSE;
	pMgntInfo->mIbss = FALSE;
	pMgntInfo->ListenInterval = 2; // The same as WG111T, 2005.04.08, by rcnjko.
	pMgntInfo->AsocTimestamp = 0; // Timestamp when associated, 2006.11.23, by shien chang.
	pMgntInfo->Regdot11networktype = RT_JOIN_NETWORKTYPE_INFRA; //set by user. select join network type
	pMgntInfo->bInBIPMFPMode = FALSE;
	CLEAR_FLAGS(pMgntInfo->targetAKMSuite);
	pMgntInfo->RequestFromUplayer = FALSE;
	pMgntInfo->bDisconnectRequest = FALSE;
	pMgntInfo->bPrepareRoaming = FALSE;
	pMgntInfo->PrepareRoamState = RT_PREPARE_ROAM_NONE;
	pMgntInfo->PrepareRoamingCount = 0;
	pMgntInfo->bStartDelayActionFrame = FALSE;
	pMgntInfo->CntAfterLink = 0;

#if 1
	if(GetFirstClientPort(Adapter) || GetFirstGOPort(Adapter))
	{
		pMgntInfo->bScanOnly = TRUE;
		//Set SSID as dummy to stop the current join process.
		SET_SSID_DUMMY(pMgntInfo->Ssid.Octet, pMgntInfo->Ssid.Length);
	}
#endif

	//
	// We never stop the scan process, but the state_Synchronization_Sta should be "STATE_No_Bss" after scan completes.
	//
	if(!pMgntInfo->bScanInProgress)
		pMgntInfo->state_Synchronization_Sta = STATE_No_Bss;
	else
		pMgntInfo->state_Synchronization_Sta_BeforeScan = STATE_No_Bss;

	pMgntInfo->bJoinInProgress = FALSE;
	
	pMgntInfo->bMlmeStartReqRsn = MLMESTARTREQ_NONE;
	MgntResetRoamingState(pMgntInfo);
	MgntResetJoinCounter(pMgntInfo);

	ReleaseDataFrameQueued(Adapter);

	MgntLinkStatusResetRxBeacon(Adapter);

	for( i=0; i<RT_MAX_LD_SLOT_NUM; i++ ){
		pMgntInfo->LinkDetectInfo.RxBcnNum[i] = 0;
	}
	pMgntInfo->LinkDetectInfo.SlotNum = 2;
	pMgntInfo->LinkDetectInfo.SlotIndex = 0;
	
	MgntRefreshSuppRateSet( Adapter );

	// Intialize IBSS related setting. 2004.12.10, by rcnjko.
	pMgntInfo->dot11BeaconPeriod = pMgntInfo->Regdot11BeaconPeriod; // For 8187 WIFI, 2004.10.11, by rcnjko.
	pMgntInfo->dot11DtimPeriod = pMgntInfo->Regdot11DtimPeriod;

	// Initialize wireless mode of current BSS. 2005.02.17, by rcnjko.
	pMgntInfo->CurrentBssWirelessMode = WIRELESS_MODE_UNKNOWN;

	// Initialize SrcAddr, FragNum and LastRxUniSrcAddr of last received unicast packet. Annie, 2005-07-25.
	for( nIdx=0; nIdx<6; nIdx++ )
		pMgntInfo->LastRxUniSrcAddr[nIdx] = 0xff;

	pMgntInfo->LastRxUniFragNum = 0xff;
	pMgntInfo->LastRxUniSeqNum = 0xffff;
			

	// Reset QoS related variables. Added by Annie, 2005-11-07.
	QosInitializeSTA( Adapter );

	// Reset HT related variables.

	//[win7 Two Port BW40Mhz issue] Extention Port obey the rule of Default Port 
	//2009.05.20, by Bohn
	HTInitializeHTInfo( Adapter );


	//VHT
	VHTInitializeVHTInfo(Adapter);
	

	// Reset TS related variables.
	TSInitialize( Adapter );

	pMgntInfo->ExcludedMacAddrListLength = 0;
	pMgntInfo->bExcludeUnencrypted = TRUE;
	pMgntInfo->SafeModeEnabled = FALSE;

	//
	// Reset all association entry for AP mode and AdHoc mode.
	//
	AsocEntry_ResetAll(Adapter);

	TDLS_Reset(Adapter);

	// Get default hidden ssid setting. This value will affect the DTM test result, ex PacketFilter_ext.
	// By Bruce, 2008-09-26.
	pMgntInfo->bHiddenSSIDEnable = DEFAULT_HIDDEN_SSID;

	if(pMgntInfo->AdditionalBeaconIESize > 0)
	{
		PlatformFreeMemory(pMgntInfo->AdditionalBeaconIEData, pMgntInfo->AdditionalBeaconIESize);
		pMgntInfo->AdditionalBeaconIESize = 0;
	}
	
	if(pMgntInfo->AdditionalResponseIESize > 0)
	{
		PlatformFreeMemory(pMgntInfo->AdditionalResponseIEData, pMgntInfo->AdditionalResponseIESize);
		pMgntInfo->AdditionalResponseIESize = 0;
	}
	
	if(pMgntInfo->AdditionalAssocReqIESize > 0)
	{
		PlatformFreeMemory(pMgntInfo->AdditionalAssocReqIEData, pMgntInfo->AdditionalAssocReqIESize);
		pMgntInfo->AdditionalAssocReqIESize= 0;
	}

	//
	// Initialize SSID list to scan.
	// 
	MgntInitSsidsToScan(Adapter);

	pMgntInfo->bIndicateConnectEvent = FALSE;

	pMgntInfo->bInToSleep = FALSE;
//	pMgntInfo->bJoinInProgress = FALSE;

	if(Adapter->bSWInitReady)
	{
		ActUpdate_ProtectionMode(Adapter, FALSE);
	}

	DFS_StaMgntResetVars(Adapter);

	// Initialize TIE and 802.11 MFP 
	pMgntInfo->TIE.Octet 		 = pMgntInfo->TIEBuff;
	pMgntInfo->TIE.Length 	 = 0;
	pMgntInfo->bInBIPMFPMode  = FALSE;

	FunctionOut(COMP_MLME);

}

//
//	Description:
//		1. Initialize management related S/W resource.
//		2. Initialize default value of management related attributes, RegXXX, and	
//		reset 802.11 management related variables to initial state.
//
//	Assumption:
//		This function shall be invoke once at driver initialization.
//
VOID
InitializeMgntVariables(
	PADAPTER			Adapter
	)
{
	PMGNT_INFO	pMgntInfo=&Adapter->MgntInfo;
	int			nIdx;

	Adapter->bFWReady = FALSE;
	// 2008/02/18 MH We execute IO for 9x series after LBUS is enabled.
	pMgntInfo->LPSDelayCnt= 0;
	Adapter->bDriverIsGoingToUnload = FALSE;
	// 09/08/17 MH From Clevo 830/840 test,	we need to forbid DM when we try to enter S3/S4.
	// The function os not enabled now!!!!!
	pMgntInfo->bPwrSaveState = FALSE;
	Adapter->bDriverIsGoingToPnpSetPowerSleep = FALSE;	
	Adapter->bHWSecurityInWoL = FALSE;
	Adapter->bEnterPnpSleep = FALSE;
	Adapter->bWakeFromPnpSleep = FALSE;
	Adapter->bDriverShutdown = FALSE;
	Adapter->bInWoWLANPowerState = FALSE;
	Adapter->CPWMTimeoutCount = 0;
	
	pMgntInfo->DelayLPSLastTimeStamp = 0;
	pMgntInfo->bReceiveSystemPSOID = FALSE;
	
	// Initialize timer and workitems.
	MgntInitializeAllTimer(Adapter);
	MgntInitializeAllWorkItem(Adapter);

	// Init watchdog timer
	InitializeWatchDogTimer(Adapter);
	
	//
	// Initialize default setting, which might be overrided as user desired ones.
	// For example, registry in Windows case.. 
	//

	FillOctetString( pMgntInfo->Ssid, pMgntInfo->SsidBuf, 0 );

#if 1	
	if(GetFirstClientPort(Adapter) || GetFirstGOPort(Adapter))
	{
		//Set SSID as dummy to stop the current join process.
		SET_SSID_DUMMY(pMgntInfo->Ssid.Octet, pMgntInfo->Ssid.Length);
	}
#endif
	
	MgntInitRateSet(Adapter);

	pMgntInfo->FragThreshold=2347;
	pMgntInfo->dot11RtsThreshold = 2346;

	//
	// Max Raoming Count(Roaming Time = Roaming Count * Check_For_Hange_Period), by Bruce, 2009-02-13.
	//
	pMgntInfo->RegRoamingLimitCount = 2;

	pMgntInfo->Regdot11ChannelNumber = 1;
	pMgntInfo->Regdot11BeaconPeriod = 100; // Asked by Owen, 2004.10.11, by rcnjko.
	pMgntInfo->Regdot11DtimPeriod = 2;

	pMgntInfo->Regdot11networktype = RT_JOIN_NETWORKTYPE_INFRA; //set by user. select join network type
	pMgntInfo->RegmCap = cESS | cIBSS /*| cPrivacy | cShortPreamble*/;

	// Initialize AP mode related variables. 2005.06.21, by rcnjko.
	RTInitializeListHead( &(pMgntInfo->GroupPsQueue) );
	for(nIdx = 0; nIdx < ASSOCIATE_ENTRY_NUM; nIdx++)
	{
		RTInitializeListHead( &(pMgntInfo->AsocEntry[nIdx].PsQueue) );
		RTInitializeListHead( &(pMgntInfo->AsocEntry[nIdx].WmmPsQueue) );
	}

	// Initialzie Auto Select Channel related, 2006.1.26, by rcnjko.
	pMgntInfo->bAutoSelChnl = FALSE;
	pMgntInfo->ChnlWeightMode = 0;

	// Initialize AP mode hidden SSID and Locked STA Address List. Added by Annie, 2006-02-15.
	pMgntInfo->bHiddenSSID = FALSE;
	pMgntInfo->LockType = MAC_FILTER_DISABLE;
	pMgntInfo->LockedSTACount = 0;
	pMgntInfo->LockedSTACount_Reject= 0;
	PlatformZeroMemory( pMgntInfo->LockedSTAList, MAX_LOCKED_STA_LIST_NUM*6 );
	PlatformZeroMemory( pMgntInfo->LockedSTAList_Reject, MAX_LOCKED_STA_LIST_NUM*6 );
	pMgntInfo->bDefaultPermited = TRUE;
		// Initialize offset for querying cloud key
	pMgntInfo->cloud_key_offset = 0;
	pMgntInfo->StaCount = 0;
	
	
	// SW RF state. 2006.12.18, by shien chang.
	if(pMgntInfo->RegRfOff == TRUE)
		pMgntInfo->eSwRfPowerState = eRfOff;
	else
		pMgntInfo->eSwRfPowerState = eRfOn;

	pMgntInfo->LinkDetectInfo.NumRxUnicastTxOkThresh = 8;
	pMgntInfo->LinkDetectInfo.NumRxUnicastThresh = 2;
	pMgntInfo->LinkDetectInfo.NumRxOkThresh = 0xff;
	
	// For inactive power save mode, 2007.07.16, by shien chang.
	pMgntInfo->PowerSaveControl.bInactivePs = FALSE;
	pMgntInfo->PowerSaveControl.bIPSModeBackup = FALSE;
	// Default AC mode, wait for pnp notify to change it.
	pMgntInfo->PowerSaveControl.PowerProfile = 1; 	
	// Default No power saving level, wait for oid notify to change it.
	pMgntInfo->PowerSaveControl.PowerSaveLevel = POWER_SAVING_NO_POWER_SAVING;
	pMgntInfo->PowerSaveControl.PowerMode = POWER_SAVING_NO_POWER_SAVING;
	pMgntInfo->PowerSaveControl.PowerPolicy = POWERCFG_MAX_POWER_SAVINGS;
	pMgntInfo->keepAliveLevel = 0;
	pMgntInfo->PowerSaveControl.bDisableLPSByOID = FALSE;
	pMgntInfo->PowerSaveControl.bEnterLPSDuringSuspend = FALSE;
	pMgntInfo->PowerSaveControl.bFindWakePacket = TRUE; // default set to TRUE
	
	// For network monitor mode, 2007.08.06, by shien chang.
	pMgntInfo->bNetMonitorMode = FALSE;
	
	pMgntInfo->ClientConfigPwrInDbm = UNSPECIFIED_PWR_DBM;

	pMgntInfo->RegCapAKMSuite = DEFAULT_SUPPORT_AKM_SUITE;

	SecInit(Adapter); // 070106, by rcnjko.

	//
	// Clear the rejected association AP, 2006.12.07, by shien chang.
	//
	MgntClearRejectedAsocAP(Adapter);
	
	Dot11d_Reset(Adapter); // 070315, by rcnjko.
	
	RtActChannelList(Adapter, RT_CHNL_LIST_ACTION_INIT, NULL, NULL);

	// Initialize AP Mode Info
	AP_InitializeVariables(Adapter);

	TDLS_Init(Adapter);


	//
	// Reset current management setting.
	//
	ResetMgntVariables(Adapter);

	// IHV support type
	pMgntInfo->IhvType = IHV_SUPPORT_NONE;

	pMgntInfo->bDumpRegs = TRUE;

	pMgntInfo->ResumeBeaconTime = DELAY_START_BEACON;

	//
	// 2010/12/28 MH We need to move the init start position.
	// 
	// =====================================

	WPS_Init(Adapter);
	// 2011/01/12 MH Revice for WAPI.
	WAPI_SecFuncHandler(WAPI_ENABLEWAPISUPPORT, Adapter, WAPI_END);
	
	// 2011/12/07 hpfan Add for Tcp Reorder
	TcpReorder_Init(Adapter);
	// =====================================	

	Adapter->bReceiveCpwmInt = FALSE;
#if (USB_TX_THREAD_ENABLE)
	Adapter->bUseUsbTxThread = FALSE;
#endif	

	
	GET_POWER_SAVE_CONTROL(pMgntInfo)->bOSSupportProtocolOffload = TRUE;

	if(Adapter == GetDefaultAdapter(Adapter))
		CustomScan_Init(pMgntInfo->pCustomScanInfo, GetDefaultAdapter(Adapter), BIT45, DBG_LOUD);
}



//
//	Description:
//		DeInitialize management related S/W resource.
//
//	Assumption:
//		This function shall be invoke once at driver unload.
//
VOID
DeInitializeMgntVariables(
	PADAPTER			Adapter
	)
{
	MgntCancelAllTimer(Adapter);
	MgntReleaseAllTimer(Adapter);
	MgntFreeAllWorkItem(Adapter);
	RemoveAllTS(Adapter);
	ReleaseAllTSTimer(Adapter);
	TDLS_Release(Adapter);



	TcpReorder_Release(Adapter);

	CancelWatchDogTimer(Adapter);
	ReleaseWatchDogTimer(Adapter);

	if(Adapter->MgntInfo.pCustomScanInfo)
	{
		CustomScan_Deinit(Adapter->MgntInfo.pCustomScanInfo);
		CustomScan_FreeInfo(Adapter->MgntInfo.pCustomScanInfo);
	}
}


BOOLEAN
MgntGetFWBuffer(
	PADAPTER			Adapter,
	PRT_TCB				*ppTcb,
	PRT_TX_LOCAL_BUFFER	*ppBuf
	)
{
	*ppBuf=GetLocalFWBuffer(Adapter);
	if(*ppBuf!=NULL)
	{
		if(!RTIsListEmpty(&Adapter->TcbIdleQueue))
		{
			*ppTcb=(PRT_TCB)RTRemoveHeadListWithCnt(&Adapter->TcbIdleQueue, &Adapter->NumIdleTcb);

			// Initialize the data rate. Annie, 2005-03-31
			(*ppTcb)->DataRate = UNSPECIFIED_DATA_RATE;

			return TRUE;
		}
		else
		{
			ReturnLocalFWBuffer(Adapter, *ppBuf);
		}
	}
	return FALSE;
}


BOOLEAN
MgntGetBuffer(
	PADAPTER			pAdapter,
	PRT_TCB				*ppTcb,
	PRT_TX_LOCAL_BUFFER	*ppBuf
	)
{
	PADAPTER pDefaultAdapter =  GetDefaultAdapter(pAdapter);

	*ppBuf=GetLocalBuffer(pDefaultAdapter);
	if(*ppBuf!=NULL)
	{
		if(!RTIsListEmpty(&pDefaultAdapter->TcbIdleQueue))
		{
			*ppTcb=(PRT_TCB)RTRemoveHeadListWithCnt(&pDefaultAdapter->TcbIdleQueue, &pDefaultAdapter->NumIdleTcb);

			// Initialize the data rate. Annie, 2005-03-31
			(*ppTcb)->DataRate = UNSPECIFIED_DATA_RATE;

			return TRUE;
		}
		else
		{			
			ReturnLocalBuffer(pDefaultAdapter, *ppBuf);
		}
	}
	return FALSE;
}

VOID
MgntSendPacket(
	PADAPTER			Adapter,
	PRT_TCB				pTcb,
	PRT_TX_LOCAL_BUFFER	pBuf,
	u4Byte				Length,
	u1Byte				QueueIndex,
	u2Byte				DataRate
	)
{
	//2 Start with first buffer, because we don't need to translate header
	pTcb->BufferList[0]=pBuf->Buffer;
	pTcb->BufferList[0].Length=Length;
	pTcb->PacketLength=Length;
	pTcb->BufferCount=1;
	pTcb->Tailer.Length = 0;

	pTcb->ProtocolType=RT_PROTOCOL_802_11;
	pTcb->BufferType=RT_TCB_BUFFER_TYPE_LOCAL;
	pTcb->SpecifiedQueueID=QueueIndex;
	pTcb->DataRate=DataRate;
	pTcb->bFromUpperLayer = FALSE;
	pTcb->Reserved=pBuf;
	pTcb->FragCount=1;
	pTcb->TSID = DEFAULT_TSID;
	pTcb->pAdapter = Adapter;

	if((pTcb->SpecifiedQueueID == BE_QUEUE) ||
		(!ACTING_AS_AP(Adapter) &&
		(pTcb->SpecifiedQueueID != HIGH_QUEUE && pTcb->SpecifiedQueueID != BEACON_QUEUE)))
	pTcb->SpecifiedQueueID = NORMAL_QUEUE; // Always use normal queue to prevent corrupting BQ.

	// 20090406 Joseph: Send 6M instead of 1M when CCK is disable for IOT issue.
	if(Adapter->MgntInfo.IOTAction & HT_IOT_ACT_DISABLE_CCK_RATE)
	{
		if(pTcb->DataRate == MGN_1M)
			pTcb->DataRate = MGN_6M;
	}

	if(Adapter->MgntInfo.bDisableCck && IS_CCK_RATE(pTcb->DataRate))
	{
		pTcb->DataRate = MGN_6M;
	}

	// 5G band should not send cck rate
	if(IS_WIRELESS_MODE_5G(Adapter))
	{
		if(	pTcb->DataRate == MGN_1M ||pTcb->DataRate == MGN_2M ||
			pTcb->DataRate == MGN_5_5M ||pTcb->DataRate == MGN_11M)
			pTcb->DataRate = MGN_6M;
	}

	NicIFSendPacket(Adapter, pTcb);
}

BOOLEAN 
MgntRetryPacket(
	PADAPTER				pAdapter,
	OCTET_STRING		pduOS,
	u1Byte				packettype
	)
{
	pu1Byte				pDaddr;
	BOOLEAN				retryPkt=FALSE;
	PMGNT_INFO			pMgntInfo = &pAdapter->MgntInfo;

	u2Byte	CurrentSeqNum = (u2Byte)Frame_SeqNum(pduOS);	// 12 bits.

	pDaddr = Frame_Addr1(pduOS);
	//DbgPrint("CurrentSeqNum = %d\n", CurrentSeqNum, CurrentSeqNum);

	if(!MacAddr_isMulticast(pDaddr))
	{	
		if(Frame_Retry(pduOS))
		{
			switch(packettype)
			{
				case Type_Auth:
					if(CurrentSeqNum == pMgntInfo->last_auth_seq)
						retryPkt = TRUE;
					break;
				case Type_Asoc_Req:
					if(CurrentSeqNum == pMgntInfo->last_asoc_req_seq)
						retryPkt = TRUE;
					break;
				case Type_Asoc_Rsp:
					if(CurrentSeqNum == pMgntInfo->last_asoc_rsp_seq)
						retryPkt = TRUE;
					break;
				case Type_Disasoc:
					if(CurrentSeqNum == pMgntInfo->last_Disassoc_seq)
						retryPkt = TRUE;
					break;
				case Type_Deauth:
					if(CurrentSeqNum == pMgntInfo->last_Deauth_seq)
						retryPkt = TRUE;
					break;
				default:
					break;
			}
		}
//		else
		{
			switch(packettype)
			{
				case Type_Auth:
					pMgntInfo->last_auth_seq = CurrentSeqNum;
					break;
				case Type_Asoc_Req:
					pMgntInfo->last_asoc_req_seq = CurrentSeqNum;
					break;
				case Type_Asoc_Rsp:
					pMgntInfo->last_asoc_rsp_seq = CurrentSeqNum;
					break;
				case Type_Disasoc:
					pMgntInfo->last_Disassoc_seq = CurrentSeqNum;
					break;					
				case Type_Deauth:
					pMgntInfo->last_Deauth_seq = CurrentSeqNum;
					break;					
				default:
					break;
			}
		} 		
	}

	return retryPkt;
}

BOOLEAN
MgntDuplicatePacketDetection(
	PADAPTER			Adapter,
	PRT_RFD				pRfd
	)
{
	PMGNT_INFO		pMgntInfo = &Adapter->MgntInfo;
	OCTET_STRING	pduOS;
	pu1Byte			pTaddr,  pRaddr;
	u1Byte			CurrentFragNum;	// 4 bits.
	u2Byte			CurrentSeqNum;		// 12 bits.
	
	pduOS.Octet = pRfd->Buffer.VirtualAddress;
	pduOS.Length = pRfd->PacketLength;

	pTaddr = Frame_Addr2(pduOS);
	pRaddr = Frame_Addr1(pduOS);

	if(MacAddr_isMulticast(pRaddr))
		return TRUE;

 	// by Owen on 05/01/10 for filtering duplicated data frames and dropping these packets
	//  Only consider unicast and retry packet. 2005.06.21, by rcnjko. 
	// 	To keep the SeqNum, FragNum, SrcAddr for every packets and filter the retry packet which already received.
	//	For WMM, these information should be keep for each AC
	//	For 802.11e, these information shold be keep for each TID.	Joseph, 2005-12-16
	//	Here we just handle the duplicate retry packet which is received immediately after last packets.
	//	For the condition that receiving duplicate packets with Block Ack, we just handle it in RxReorder process. 
	//	Joseph, 2007-09-27

	#if 0  // Sequence Number & Duplicate Packet Indication Debug
	{
		DbgPrint("\n====================================================\n");
		RT_PRINT_ADDR(COMP_INIT, DBG_LOUD, "Tx Address:", pTaddr);
		RT_PRINT_ADDR(COMP_INIT, DBG_LOUD, "Rx Address:", pRaddr);
		DbgPrint("Frame_SeqNum(frame): %d\n", Frame_SeqNum(pduOS));
		DbgPrint("pRfd->Status.pRxTS: %p\n", pRfd->Status.pRxTS);
		DbgPrint("====================================================\n");
	}
	#endif

	CurrentFragNum = (u1Byte)Frame_FragNum(pduOS);
	CurrentSeqNum = (u2Byte)Frame_SeqNum(pduOS);
	
	if(ACTING_AS_AP(Adapter))
	{
		PRT_WLAN_STA pEntry = AsocEntry_GetEntry(pMgntInfo, pTaddr);
		
		if(pEntry != NULL)
		{
			if(pRfd->Status.pRxTS==NULL)
			{
				if( 	Frame_Retry(pduOS) && 
					(CurrentFragNum == pEntry->LastRxUniFragNum) && 
					(CurrentSeqNum == pEntry->LastRxUniSeqNum)	)
				{
					return FALSE;
				}
				else
				{
					pEntry->LastRxUniFragNum = CurrentFragNum;
					pEntry->LastRxUniSeqNum = CurrentSeqNum;
				} 
			}
			else
			{
				if( 	Frame_Retry(pduOS) && 
				(CurrentFragNum == pRfd->Status.pRxTS->RxLastFragNum) && 
				(CurrentSeqNum == pRfd->Status.pRxTS->RxLastSeqNum)	)
				{
					return FALSE;
				}
				else
				{
					pRfd->Status.pRxTS->RxLastFragNum = CurrentFragNum;
					pRfd->Status.pRxTS->RxLastSeqNum = CurrentSeqNum;
				}
			}
		}
		else
		{
			//not retrun false,as 92d dual mac smart concurrent need.
			//example:mac1 soft ap reset,but have no right time to disconnect all sta, so client  still connect,need to send deauth fram as below process . 
			// zhiyuan 2011/11/18
			RT_TRACE(COMP_RECV,DBG_LOUD,("pEntry is null ,don't return false as 92d easy smart concurrent need\n"));
			//	return FALSE;
		}
	}
	else
	{
		if(pRfd->Status.pRxTS==NULL)
		{
			if( 	Frame_Retry(pduOS) && 
				PlatformCompareMemory(pMgntInfo->LastRxUniSrcAddr, pTaddr, 6)== 0 &&
				(CurrentFragNum == pMgntInfo->LastRxUniFragNum) && 
				(CurrentSeqNum == pMgntInfo->LastRxUniSeqNum)	)
			{
				//RT_TRACE(COMP_RECV, DBG_LOUD, ("[SNDBG] STA MgntFilterReceivedPacket():  filtering duplicated data frames and dropping these packets\n"));
				return FALSE;
			}
			else
			{
				pMgntInfo->LastRxUniFragNum = CurrentFragNum;
				pMgntInfo->LastRxUniSeqNum = CurrentSeqNum;
				PlatformMoveMemory( pMgntInfo->LastRxUniSrcAddr, pTaddr, 6 );
				//RT_TRACE(COMP_RECV, DBG_LOUD, ("[SNDBG] STA MgntFilterReceivedPacket():  Record it.\n" ));
			} 
		}
		else
		{
			if( 	Frame_Retry(pduOS) && 
			(CurrentFragNum == pRfd->Status.pRxTS->RxLastFragNum) && 
			(CurrentSeqNum == pRfd->Status.pRxTS->RxLastSeqNum)	)
			{
				return FALSE;
			}
			else
			{
				pRfd->Status.pRxTS->RxLastFragNum = CurrentFragNum;
				pRfd->Status.pRxTS->RxLastSeqNum = CurrentSeqNum;
			}
		}
	}
	
	return TRUE;
}


VOID
MgntWMMPSPacket(
	PADAPTER			Adapter,
	OCTET_STRING		pduOS
)
{
	pu1Byte		pdu = pduOS.Octet;
	PMGNT_INFO	pMgntInfo=&Adapter->MgntInfo;

	if((pMgntInfo->dot11PowerSaveMode !=eActive) && (!ACTING_AS_AP(Adapter)))
	{
		BOOLEAN		bTriggerAC=FALSE;

		switch( GET_QOS_CTRL_WMM_UP(pdu) )
		{
			case 0:
			case 3:
				bTriggerAC=GET_BE_UAPSD(pMgntInfo->pStaQos->Curr4acUapsd);
				break;
			case 1:
			case 2:
				bTriggerAC=GET_BK_UAPSD(pMgntInfo->pStaQos->Curr4acUapsd);
				break;
			case 4:
			case 5:
				bTriggerAC=GET_VI_UAPSD(pMgntInfo->pStaQos->Curr4acUapsd);
				break;
			case 6:
			case 7:
				bTriggerAC=GET_VO_UAPSD(pMgntInfo->pStaQos->Curr4acUapsd);
				break;
		}

		if(bTriggerAC)
		{
			// Temp solution for P2P Test Plan 6.1.13 and 7.1.5. By Bruce, 2010-06-30.
			pMgntInfo->pStaQos->bWmmMoreData = Frame_MoreData(pduOS) ? TRUE : FALSE;
			
			if( GET_QOS_CTRL_WMM_EOSP(pdu) )
			{
				RT_TRACE(COMP_POWER, DBG_TRACE, ("MgntFilterReceivedPacket(): <<<<<<<<<< Leave APSD service period <<<<<<<<<<\n"));
				pMgntInfo->pStaQos->bInServicePeriod=FALSE;
			}
		}
		else
		{
			if(!GET_POWER_SAVE_CONTROL(pMgntInfo)->bFwCtrlLPS) // by tynli
			{
				if(pMgntInfo->dot11PowerSaveMode != eActive && (!ACTING_AS_AP(Adapter)))
				{
					if((Frame_MoreData(pduOS)==TRUE))
					{	
						OnMoreData(Adapter);
					}
				}
			}
		}
	}
}



BOOLEAN
MgntFilterReceivedPacket(
	PADAPTER			Adapter,
	PRT_RFD				pRfd
	)
{
	PMGNT_INFO			pMgntInfo=&Adapter->MgntInfo;
	OCTET_STRING		pduOS;
	u1Byte				WAIPkt = 0;
	AUTH_STATUS_T		state_auth; 
	ASOC_STATUS_T		state_asoc; 
	BOOLEAN				bAcceptable = TRUE;
	BOOLEAN				bValidAddr4;
	BOOLEAN				bToOtherSTA = FALSE;
	PMGNT_INFO 			pDefaultMgntInfo = &(GetDefaultAdapter(Adapter)->MgntInfo);
	pu1Byte				pdu, pDaddr, pTaddr, pRaddr;
	HAL_DATA_TYPE				*pHalData = GET_HAL_DATA(Adapter);
	PDM_ODM_T					pDM_Odm = &pHalData->DM_OutSrc;

	//1 Return FALSE if this RFD should be freed


	pduOS.Octet = pRfd->Buffer.VirtualAddress;
	pduOS.Length = pRfd->PacketLength;
	pdu = pRfd->Buffer.VirtualAddress;
	pDaddr = Frame_pDaddr(pduOS);
	pTaddr = Frame_Addr2(pduOS);
	pRaddr = Frame_Addr1(pduOS);
	bValidAddr4 = Frame_ValidAddr4(pduOS);

	//2 Filter out error packet
	if( pRfd->Status.bHwError)
	{
		return FALSE;
	}

	//
	// Filter out unicast frames to other STA.
	// Note that, it is to prevent unicast mgnt frame change our mgnt state machine, 
	// for example, deauth frame to other STA should cause us become deauthenitacted.
	// 2006.01.11, by rcnjko.
	//
	if( !MacAddr_isMulticast(pRaddr) && !eqMacAddr(pRaddr, Adapter->CurrentAddress) )
	{ // Unicast frame to other STA.
	
		if(pMgntInfo->bPSPXlinkMode == TRUE)
		{
			bToOtherSTA = TRUE;
			pRfd->Status.bForward = TRUE;
		}
		else if(pMgntInfo->bNetMonitorMode == TRUE)
		{
			{
				bToOtherSTA = TRUE;
				pRfd->Status.bForward = TRUE;
			}
		}		
		else 
			return FALSE;
	}

	// Filter out data packet that is not AMSDU but length >= 2k
	if (Frame_ContainQc(pduOS) && (GET_QOS_CTRL_HC_CFP_USRSVD(pduOS.Octet)  != 1) && (pRfd->PacketLength>= 2048))
	{
		RT_TRACE(COMP_RECV, DBG_LOUD, 
				("MgntFilterReceivedPacket(): Not AMSDU, but packet lenght > 2k, Dropped!!!===\n"));
		return FALSE;
	}

	// 8192U - if data frames don't decryption by hw then decrypt by sw
	// 8190P/8192E - If data frames is not decrypted by hw then decrypt by sw
	//RxCheckSWDecryption(Adapter, pRfd);

	//====================================
	// Filter packet
	//----------------------------------------------
	// <1>Reject frames before connection established.
	// <2>Reject Encrypted Management frame except 3rd auth frame.
	// <3>Management frame filter 
	// <4>Control frame filter 
	// <5>filter frames from different BSS
	// <6>Data frame filter 
	//-------
	// return 
	//	 FALSE to free received frame
	//	 TRUE to Defrag Packet
	//----------------------------------------------
	// ----------------------------------------------------
	// <1>Reject frames before connection established.
	// ----------------------------------------------------

	if( IsMgntFrame(pdu) )
	{
		if(MgntRetryPacket(Adapter, pduOS, PacketGetType(pduOS)))
			return FALSE;
	}

	// ----------------------------------------------------
	// <2>Reject Encrypted Management frame except 3rd auth frame.
	// ----------------------------------------------------
	if( IsMgntFrame(pdu) ){
		if( Frame_WEP(pduOS) )
		{
			if( IsMgntAuth(pdu) && Frame_AuthTransactionSeqNum(pduOS)==3 && pMgntInfo->AuthReq_auAlg==SHARED_KEY)
			{
				Mgnt_Indicate(Adapter, pRfd, noerr);
			}
			else
			{
				RT_TRACE(COMP_RECV, DBG_LOUD, ("MgntFilterReceivedPacket(): Reject Encrypted Management frame except 3rd auth frame.\n"));
			}
			return FALSE;
		}
	}

	MgntSsInquiry( Adapter, pTaddr, &state_auth, &state_asoc );

	// ----------------------------------------------------
	// <3>Management frame filter 
	// ----------------------------------------------------
	if( IsMgntFrame(pdu) ){
		if (bToOtherSTA )
			return FALSE;
		else
		{
			if( IsMgntBeacon(pdu) || IsMgntProbeReq(pdu) || IsMgntAction(pdu) || IsMgntActionNoAck(pdu)){
				Mgnt_Indicate(Adapter, pRfd, noerr);
			}
			else
			{
				if( IsMgntAuth(pdu) || IsMgntDeauth(pdu) || IsMgntAtim(pdu) || IsMgntProbeRsp(pdu) ){
					if( !MgntIsMacAddrGroup( pDaddr ) ){
						Mgnt_Indicate(Adapter, pRfd, noerr);
					}
					else{
						if( MacAddr_isBcst( pDaddr ) ){
							if( IsMgntDeauth(pdu) ){
								Mgnt_Indicate(Adapter, pRfd, noerr);
							}
						}
					}
				}
				else if( IsMgntAsocReq(pdu) || IsMgntAsocRsp(pdu) || IsMgntReAsocReq(pdu) || IsMgntReAsocRsp(pdu) || IsMgntDisasoc(pdu) ){
					if( (state_auth == opensystem_auth) || (state_auth == sharedkey_auth) || (ft_auth == state_auth)){
						Mgnt_Indicate(Adapter, pRfd, noerr);
					}
					else{
						Mgnt_Indicate(Adapter, pRfd, class2);
					}
				}
			}
			return FALSE;
		}
	}

	// ----------------------------------------------------
	// <4>Control frame filter 
	// ----------------------------------------------------
	if( IsCtrlFrame(pdu) )
	{
		if(bToOtherSTA)
			return FALSE;
		else if( IsCtrlPSpoll(pdu))
		{
			if( ACTING_AS_AP(Adapter))
			{
				if( state_asoc == assoc )
					AP_PS_OnPSPoll(Adapter, pduOS);
				else
				{
					if( state_auth == not_auth )
						Mgnt_Indicate(Adapter, pRfd, class2);					
					else
						Mgnt_Indicate(Adapter, pRfd, class3);
				}
			}
		}
		else if( IsCtrlNDPA(pdu))
		{

			RT_PRINT_DATA(COMP_RECV, DBG_TRACE, "GetNDPAFrame:\n", pduOS.Octet, pduOS.Length);
		}
		else if(IsCtrlBFReportPoll(pdu))
		{
			RT_PRINT_DATA(COMP_RECV, DBG_TRACE, "Get BFReportPoll:\n", pduOS.Octet, pduOS.Length);
		}
		else if(IsCtrlBlockAckReq(pdu))
		{
			OnBAReq(Adapter, pRfd,pduOS);
		}
		return FALSE;
	}


	// ----------------------------------------------------
	// <5>filter frames from different BSS
	// ----------------------------------------------------
	if( !bValidAddr4 && 
		!eqMacAddr(pMgntInfo->Bssid, Frame_pBssid(pduOS)) )
	{
		RT_TRACE(COMP_RECV, DBG_LOUD, ("MgntFilterReceivedPacket(): filter frames from different BSS.\n"));
		RT_TRACE(COMP_RECV, DBG_LOUD, 
		("bValidAddr4 =%d BSSID=[%02x-%02x-%02x-%02x-%02x-%02x]\n", 
		bValidAddr4, pMgntInfo->Bssid[0], pMgntInfo->Bssid[1], pMgntInfo->Bssid[2], 
		pMgntInfo->Bssid[3], pMgntInfo->Bssid[4], pMgntInfo->Bssid[5]));

		return FALSE;
	}

	//
	//  Note : 
	//		This is to fix the issue that if we failed to receive AssocRsp and 
	//		the AP starts 4-way, indicating the EAPOL-KEY may crash the supplicant.
	//		Not sure if this has any impact on BT
	//
	if( IsDataFrame(pdu))
	{
		if(!ACTING_AS_AP(Adapter))
		{
			if( !pMgntInfo->bMediaConnect )
			{
				RT_TRACE(COMP_RECV, DBG_LOUD, ("MgntFilterReceivedPacket(): Media is Disconnect.\n"));
				return FALSE;
			}
				
		}
	}

	// When WPS Enable We save the Packet then Drop the packet we won't pass to OS	
	if(GET_SIMPLE_CONFIG_ENABLED(pDefaultMgntInfo) && SecIsEAPOLPacket( Adapter, &pduOS ))
	{
		WPS_CopyRxEAPPacket(Adapter, pRfd);
		return FALSE;
	}
		
	WAPI_SecFuncHandler(WAPI_SECISWAIPACKET,Adapter,(PVOID)&pduOS,(PVOID)&WAIPkt,WAPI_END);
	if(WAIPkt != 0)//WAI Pkt 
	{
		RT_TRACE(COMP_SEC,DBG_LOUD,("MgntFilterReceivedPacket WAIPkt = %d\n",WAIPkt));
		return TRUE;
	}
			
	if( ACTING_AS_AP(Adapter) )
	{	// AP mode

		// 070209, rcnjko: Update STA information, e.g. rate and power save state.
		AP_PS_UpdateStationPSState( Adapter, &pduOS);

		//
		// AP mode: parse the EAPOL packets. Added by Annie, 2005-07-10.
		// Our authenticator don't handle EAPOL on WDS, 2006.06.14, by rcnjko.
		//
		if( !bValidAddr4 && SecIsEAPOLPacket( Adapter, &pduOS ) && 
			MgntActQuery_ApType(Adapter) != RT_AP_TYPE_VWIFI_AP 
			&& MgntActQuery_ApType(Adapter) != RT_AP_TYPE_LINUX )
		{
			AP_OnEAPOL( Adapter, pRfd );
			return FALSE;
		}
	}
	else
	{	// STA mode		

		if(pMgntInfo->PowerSaveControl.WoWLANS5Support && SecIsEAPOLPacket(Adapter, &pduOS))
		{
			SecStaGetANoseForS5(Adapter, &pduOS);
		}
		
		if(IS_TDL_EXIST(pMgntInfo))
			TDLS_PS_UpdatePeerPSState(Adapter, &pduOS, FALSE);
	}


	if( pMgntInfo->SecurityInfo.PairwiseEncAlgorithm == RT_ENC_ALG_NO_CIPHER )
	{ // station without Encryption Algrithm.
		if( Frame_WEP(pduOS) && !bValidAddr4 )
		{
			RT_TRACE(COMP_RECV, DBG_LOUD, ("MgntFilterReceivedPacket():  station without Encryption Algrithm.\n"));
			bAcceptable = FALSE;
		}
	}
	else
	{ // station with Encryption Algrithm. Only EAPOL packets allowed.
		if( Frame_WEP(pduOS) == FALSE && !bValidAddr4 )
		{
			BOOLEAN			bMoreFrag = (BOOLEAN)Frame_MoreFrag(pduOS);
			u1Byte			FragNum = (u1Byte)Frame_FragNum(pduOS); 
			BOOLEAN 		bWPSEnable = FALSE;

			bWPSEnable = Adapter->pNdis62Common->bWPSEnable;

			// EAPOL Request may be fragments. 2004.10.05, by rcnjko.
			if( (!bMoreFrag && FragNum == 0) && // Non-fragment  
				!SecIsEAPOLPacket(Adapter,&pduOS)
				)
			{
				RT_TRACE(COMP_RECV, DBG_LOUD, ("MgntFilterReceivedPacket():  station with Encryption Algrithm. Only EAPOL packets allowed.\n"));
				//for WLK1.5 softap_wps_ext
				if (pMgntInfo->bExcludeUnencrypted &&(!bWPSEnable || !Adapter->bInHctTest))					
				{
					bAcceptable = FALSE;

					CountRxExcludedUnencryptedStatistics(Adapter, pRfd);
				}
			}

			if(pMgntInfo->IOTPeer == HT_IOT_PEER_MERU)
			{
				
				// 
				// For Meru AP issue. by Neo and Emily, 20101019
				// 		Meru AP send first packet of eapol key right after association
				//		which may cause corruption of WZC stack. The root cause is
				//		driver should ignore all EAPOL packet until WZC is ready, but 
				//		here, driver just drop the first two 1st eapol packet
				if(SecIsEAPOLPacket(Adapter,&pduOS) && 
					pMgntInfo->AcceptFirstEAPOLPacket < 2 &&
					(pMgntInfo->SecurityInfo.AuthMode == RT_802_11AuthModeWPAPSK || pMgntInfo->SecurityInfo.AuthMode == RT_802_11AuthModeWPA2PSK))
				{				
					bAcceptable = FALSE;
					pMgntInfo->AcceptFirstEAPOLPacket++;
				}								
			}

		}
	}


	if( bAcceptable == FALSE ){
		return FALSE;
	}

	// 2011.09.22 - Use pRaddr to verify if this packet is unicast or broadcast since in the AP mode, the client will unicast a brocast packet to AP. 
	// Indicate Rx Traffic Stream related information.
	if(	eqMacAddr(pRaddr, Adapter->CurrentAddress) &&
		!MacAddr_isMulticast(pRaddr) &&
		pMgntInfo->pStaQos->CurrentQosMode!=QOS_DISABLE &&
		IsQoSDataFrame(pdu) )
	{
		RxTSIndicate(Adapter, pRfd);
	}
	else
	{
		pRfd->Status.pRxTS = NULL;
	}

	
#if WLAN_ETW_SUPPORT

	pRfd->RfdFrameUniqueueID = RT_INC_RX_FRAME_UNIQUE_ID(Adapter);

	//
	// <Roger_Notes> No activity needs to be associated with this event, the ActivityId is optional and can be NULL.
	// 2014.01.14.
	//
	EventWriteRxIndicationFromNIC(
			NULL, //Without associated with the event
			pRfd->RfdFrameUniqueueID, // FrameUniqueueID
			(IsQoSDataFrame(pduOS.Octet)?Frame_QoSTID(pduOS, sMacHdrLng):0), 	// TID
			0, 	// PeerID
			pRfd->Status.Seq_Num, 	//SequenceNumber
			pRfd->PacketLength, // PayloadLength in bytes
			0, 	// QueueLength
			Frame_Retry(pduOS),// Retransmit
			0,	// Status
			0,  	// CustomData1
			0, 	// CustomData2
			0);	// CustomData3
#endif
	
	// 2011.09.22 - Use pRaddr to verify if this packet is unicast or broadcast since in the AP mode, the client will unicast a brocast packet to AP. 
	if(MgntDuplicatePacketDetection(Adapter, pRfd) == FALSE)
		return FALSE;


	// ----------------------------------------------------
	// <6>Data frame filter 
	// ----------------------------------------------------
	if( IsDataFrame(pdu) )
	{
		if( IsMgntData(pdu) )
		{

			if( MgntIsMacAddrGroup( pDaddr ) && eqMacAddr(pTaddr, Adapter->CurrentAddress) )
			{
				// Filter broadcast frame from this station.
				RT_TRACE(COMP_RECV, DBG_LOUD, ("MgntFilterReceivedPacket(): Filter broadcast frame from this station.\n"));
				return FALSE;
			}
	
			// Our NIC send multicast to AP (ToDS), then AP reply to its clients (FromDS).
			if( eqMacAddr(Adapter->CurrentAddress, Frame_pSaddr(pduOS)) )
			{
				/* In network bridge case: 
				// To avoid 802.1 Bridge Spanning Tree packet infinite loop. 
				// We send Spanning Tree packet to AP, then AP forward our packet to its clients.
				// 2009-04-16, Isaiah 	
				*/	
				RT_TRACE(COMP_RECV, DBG_LOUD, ("MgntFilterReceivedPacket(): Filter Multicast from this station.\n"));
				return FALSE;
			}
	
			if(!GET_POWER_SAVE_CONTROL(pMgntInfo)->bFwCtrlLPS) // by tynli
			{
				//Isaiah for APSD TestPlan 2006-07-31
				if((pMgntInfo->dot11PowerSaveMode != eActive && !MacAddr_isMulticast(pRaddr))
					&& (!ACTING_AS_AP(Adapter)))
				{
					if(Frame_MoreData(pduOS)==TRUE)
					{
						OnMoreData(Adapter);
					}
						
				}
			}
		}//Data
		else if(IsQoSDataFrame(pdu))  //Qos Null or Qos data Isaiah 2006-07-25
			MgntWMMPSPacket(Adapter, pduOS);
		else if( IsMgntNullFrame(pdu) ){
			//if( state_auth == not_auth ){ Mgnt_Indicate(Adapter, pRfd, class2); return FALSE; }
		}
		else if( 	IsMgntDataAck(pdu) || IsMgntDataPoll(pdu) || IsMgntDataPoll_Ack(pdu) || 
				IsMgntCfack(pdu) || IsMgntCfpoll(pdu) || IsMgntCfpollAck(pdu) )
		{	//TODO:
		}

		if(!bValidAddr4)
		{ // Return FALSE if station is not associated.
			if( state_asoc != assoc )
			{
				if( state_auth == not_auth )
				{
					Mgnt_Indicate(Adapter, pRfd, class2);
				}
				else
				{
					Mgnt_Indicate(Adapter, pRfd, class3);
				}
				RT_TRACE(COMP_RECV, DBG_LOUD, ("MgntFilterReceivedPacket(): return FALSE if station is not associated.\n"));
				return FALSE;
			}
		} 
		else
		{ // Filter out invalid WDS packets here.
			if(!ACTING_AS_AP(Adapter))
			{
				return FALSE; 
			}

			if(pMgntInfo->WdsMode == WDS_DISABLED)
			{
				return FALSE; 
			}

			if(!eqMacAddr(pTaddr, pMgntInfo->WdsApAddr))
			{
				return FALSE; 
			}
		}
	} 
	
	//Here, ONLY data frame from peer station exists!!
	//====================================
	return TRUE;
}

//
//	Description:
//		Return FALSE if this packet should not be indicated(only forward).
//		If this packet have will be used in more than one purpose, 
//		we will copy it into local buffer.
//
//	<RJ_TODO_WDS>
//		Revise AP_ForwardPacketWithFromDS(), AP_FromBssToWds(), 
//		and AP_FromWdsToBss() if QAP is implemented.
//
//	070214, by rcnjko.
//
BOOLEAN
MgntCheckForwarding(
	PADAPTER			Adapter,
	PRT_RFD				pRfd
	)
{
	PMGNT_INFO		pMgntInfo = &(Adapter->MgntInfo);
	OCTET_STRING	mpdu;
	pu1Byte			pTaddr;
	pu1Byte			pDstAddr;
	BOOLEAN			bValidAddr4;
	BOOLEAN			bNeedToIndicate = TRUE;

	FillOctetString(mpdu, pRfd->Buffer.VirtualAddress, pRfd->PacketLength);
	pTaddr = Frame_pTaddr(mpdu);
	pDstAddr = Frame_pDaddr(mpdu);
	bValidAddr4 = Frame_ValidAddr4(mpdu);

	if(!bValidAddr4)
	{
		if(AsocEntry_IsStationAssociated(pMgntInfo, pTaddr))
		{ // If TA is an associated STA.
			PRT_WLAN_STA	pEntry = AsocEntry_GetEntry(pMgntInfo, pTaddr);

			// Update active time of the STA.
			AsocEntry_UpdateTimeStamp(pEntry);

			if(!eqMacAddr(pDstAddr, Adapter->CurrentAddress))
			{ // If destination address is not AP.

				if(MacAddr_isMulticast(pDstAddr))
				{ // If destination address is bcst/mcst address, 
					// Forward the packe to WDS AP if required.
					if(pMgntInfo->WdsMode != WDS_DISABLED)
					{
						AP_FromBssToWds(Adapter, pRfd, TRUE);
					}
					
				}
#if (P2P_SUPPORT == 1)	
				else if(P2P_ENABLED(GET_P2P_INFO(Adapter)) &&
					!((GET_P2P_INFO(Adapter))->GroupCapability & gcIntraBSSDistribution))
				{// intra bss (forwarding) capability is disabled
					// In this case, we only forward multicast frames. 2010.04.16, haich.
					ReturnRFDList(Adapter, pRfd);
					bNeedToIndicate = FALSE;
				}
#endif				
				else if(AsocEntry_IsStationAssociated(pMgntInfo, pDstAddr))
				{ // If destination is a STA associated.				
					// Forward the packet.
					bNeedToIndicate = TRUE;
				}
				else
				{
					// In WDS mode: unicast frame not to BSS or us will beforwad to WDS AP and don't indicate it up.
					if(pMgntInfo->WdsMode != WDS_DISABLED)
					{
						AP_FromBssToWds(Adapter, pRfd, TRUE);
					}

					// Peer is not associated -----------------------------------
					ReturnRFDList(Adapter, pRfd);
					bNeedToIndicate = FALSE;
					// -----------------------------------------------------
				}
			}
		}
	}
	else
	{ // WDS frame received: Forward it to current BSS if required.
	  // Note that, if we fall into this case, pMgntInfo->bWdsMode should not be WDS_DISABLED and 
	  // TA is a valid WDS AP, see also MgntFilterReceivedPacket().
		
		if(!eqMacAddr(pDstAddr, Adapter->CurrentAddress))
		{ // If destination address is not AP.
			if(MacAddr_isMulticast(pDstAddr))
			{ // If destination address is bcst/mcst address, we forard it to BSS and also indicate up.  
	
				// Forward the packet to BSS.
				AP_FromWdsToBss(Adapter, pRfd, TRUE);

				// We will indicate it up later.
			}
			else if(AsocEntry_IsStationAssociated(pMgntInfo, pDstAddr))
			{ // If destination is a STA associated.
	
				// Forward the packet to BSS.
				if(AP_FromWdsToBss(Adapter, pRfd, TRUE) == FALSE)
				{
					// If we failed to forward it, so just return the RFD here.
					RT_TRACE(COMP_AP, DBG_WARNING, ("MgntCheckForwarding(): Failed to forward packet from WDS to BSS!\n"));
				}
	
				// Don't indicate it up.
				ReturnRFDList(Adapter, pRfd);
				bNeedToIndicate = FALSE;
			}
			else
			{
				// Don't indicate it up.
				ReturnRFDList(Adapter, pRfd);
				bNeedToIndicate = FALSE;
			}
		}

		// If this is a unicast frame to us in AP mode, we'll indicate it up later.
	}

	return bNeedToIndicate;
}


BOOLEAN
MgntFilterTransmitPacket(
	PADAPTER			Adapter,
	PRT_TCB				pTcb
	)
{
	PMGNT_INFO pMgntInfo = &(Adapter->MgntInfo);

	if( pMgntInfo->mDisable==TRUE &&  pMgntInfo->bMediaConnect==FALSE )
	{
		//
		// Reject data frame if we are disconnected or mDisable.
		//
		if(pTcb->ProtocolType == RT_PROTOCOL_802_3)
		{
			return FALSE;
		}
		else if(pTcb->ProtocolType == RT_PROTOCOL_802_11)
		{
			if( IsFrameTypeData(pTcb->BufferList[0].VirtualAddress) )
				return FALSE;
		}
	}

	if( ACTING_AS_AP(Adapter) )
	{
		pu1Byte pRaddr = NULL;
		BOOLEAN bDataFrame = FALSE;
		BOOLEAN bWithAddr4 = FALSE;
		
		//
		// Get destination address and frame type.
		//
		if(pTcb->ProtocolType == RT_PROTOCOL_802_3)
		{
			pRaddr = pTcb->BufferList[2].VirtualAddress;
			RT_ASSERT(pRaddr!=NULL, ("MgntFilterTransmitPacket(): 802.3 pRaddr==NULL\n"));

			bDataFrame = TRUE;
		}
		else if(pTcb->ProtocolType == RT_PROTOCOL_802_11)
		{
			pu1Byte pHeader = pTcb->BufferList[0].VirtualAddress;
			RT_ASSERT(pHeader !=NULL, ("MgntFilterTransmitPacket(): 802.11 pHeader==NULL\n"));
		
			pRaddr = pHeader + 4;
			RT_ASSERT(pRaddr!=NULL, ("MgntFilterTransmitPacket(): 802.11 pRaddr==NULL\n"));
			
			if( IsFrameTypeData(pHeader) )
				bDataFrame = TRUE;

			if( IsFrameWithAddr4(pHeader) )
				bWithAddr4 = TRUE;
		}

		//
		// Reject data frame dest STA is not associated.
		//			
		if(pRaddr != NULL && !MacAddr_isMulticast(pRaddr) && bDataFrame)
		{ // Data frame with unicast destination.
			if(!bWithAddr4)
			{
				if( pMgntInfo->WdsMode == WDS_DISABLED && 
					!AsocEntry_IsStationAssociated(pMgntInfo, pRaddr) )
				{ // Destination STA is not associated.
					RT_TRACE( COMP_AP, DBG_LOUD,
						("MgntFilterTransmitPacket(): Destination STA is not associated, pRaddr: %02X-%02X-%02X-%02X-%02X-%02X!\n",
					pRaddr[0],pRaddr[1],pRaddr[2],pRaddr[3],pRaddr[4],pRaddr[5]) );
					return FALSE;
				}
			}
			else
			{
				if(pMgntInfo->WdsMode == WDS_DISABLED)
				{
					RT_TRACE( COMP_AP, DBG_LOUD, ("MgntFilterTransmitPacket(): bWithAddr4 but WdsMode is WDS_DISABLED!\n"));
					return FALSE;
				}

				if(!eqMacAddr(pRaddr, pMgntInfo->WdsApAddr))
				{
					RT_TRACE( COMP_AP, DBG_LOUD,
						("MgntFilterTransmitPacket(): unknown WDS AP, pRaddr: %02X-%02X-%02X-%02X-%02X-%02X!\n",
						pRaddr[0],pRaddr[1],pRaddr[2],pRaddr[3],pRaddr[4],pRaddr[5]) );
					return FALSE;
				}
			}
		}
	}
	
	return TRUE;
}


RT_STATUS
MgntAllocateBeaconBuf(
	PADAPTER			Adapter
	)
{
	RT_STATUS	status;
	PMGNT_INFO      pMgntInfo = &Adapter->MgntInfo;

	status = PlatformAllocateSharedMemory( 
			Adapter, 
			&pMgntInfo->BcnSharedMemory, 
			BEACON_FRAME_LEN 
			);

	if( status == RT_STATUS_SUCCESS ){
		pMgntInfo->beaconframe.Octet = pMgntInfo->BcnSharedMemory.VirtualAddress;
		pMgntInfo->beaconframe.Octet += Adapter->TXPacketShiftBytes;
		pMgntInfo->beaconframe.Length = 0;
	}

	return status;
}


VOID
MgntFreeBeaconBuf(
	PADAPTER			Adapter
	)
{
	PlatformFreeSharedMemory( Adapter, &Adapter->MgntInfo.BcnSharedMemory );
}


BOOLEAN 
MgntIsMacAddrGroup( 
	pu1Byte addr 
	)
{
	if( (addr[0]&0x01) && (MacAddr_isBcst(addr)==FALSE) )
		return TRUE;
	else
		return FALSE;
}


VOID
MgntSsInquiry( 
	PADAPTER			Adapter,
	pu1Byte				pSaddr, 
	PAUTH_STATUS_T		state_auth, 
	PASOC_STATUS_T		state_asoc 
	)
{
	PMGNT_INFO      pMgntInfo = &Adapter->MgntInfo;

	*state_auth = not_auth;
	*state_asoc = disassoc;

	if(!ACTING_AS_AP(Adapter))
	{ // STA mode.
		if( pMgntInfo->AuthStatus == AUTH_STATUS_SUCCESSFUL ){
			if( pMgntInfo->AuthReq_auAlg == OPEN_SYSTEM ){
				*state_auth = opensystem_auth;
			}
			else if(  pMgntInfo->AuthReq_auAlg == SHARED_KEY ){
				*state_auth = sharedkey_auth;
			}
			else if(AUTH_ALG_FT == pMgntInfo->AuthReq_auAlg)
			{
				*state_auth = ft_auth;
			}
		}
	
		if( pMgntInfo->mAssoc || pMgntInfo->mIbss ){
			*state_asoc = assoc;
		}
	}
	else
	{ // AP mode.
		PRT_WLAN_STA	pEntry = AsocEntry_GetEntry(pMgntInfo, pSaddr);

		if(pEntry != NULL)
		{
			if(pEntry->AuthAlg == OPEN_SYSTEM && pEntry->AuthPassSeq == 2) 
			{
				*state_auth = opensystem_auth;
			}
			else if(pEntry->AuthAlg == SHARED_KEY && pEntry->AuthPassSeq == 4) 
			{
				*state_auth = sharedkey_auth;
			}
			
			if(*state_auth != not_auth)
			{
				*state_asoc = (pEntry->bAssociated) ? assoc : disassoc; 
			}	
		}
	}
}




// return TRUE if encryption invoked.
BOOLEAN
MgntGetEncryptionInfo(
	IN	PADAPTER			Adapter,
	IN	PRT_TCB				pTcb,
	IN	PSTA_ENC_INFO_T		pEncInfo,
	IN	BOOLEAN				bIncHead
	)
{
	PMGNT_INFO	pMgntInfo = &Adapter->MgntInfo;	
	pu1Byte 		pDestaddr = NULL;
	pu1Byte		pheader = pTcb->BufferList[0].VirtualAddress;	

	if(pMgntInfo->bScanWithMagicPacket)
	{ // We don't want ot encrypt magic packet, so any frames sending in this period won't be encrypted. 2005.06.27, by rcnjok.
		return FALSE;
	}

	if( bIncHead )
		//xiong
		//pheader += USB_HWDESC_HEADER_LEN;
	{
		pheader += Adapter->TXPacketShiftBytes;
	}

	pEncInfo->bMFPPacket = FALSE;

	GetTcbDestaddr(Adapter, pTcb, &pDestaddr);

	if( pMgntInfo->SecurityInfo.PairwiseEncAlgorithm!= RT_ENC_ALG_NO_CIPHER ) 
	{
		if( !IsDataFrame(pheader) )
		{
			if( IsMgntFrame(pheader) )
			{
				OCTET_STRING osMgntFrame;

				FillOctetString(osMgntFrame, pheader, sMacHdrLng);
				// Only 3rd auth management frame had set WEP bit, see also ConstructAuthenticatePacket(), 2005.08.18, by rcnjko.
				if( IsMgntAuth(pheader) && Frame_WEP(osMgntFrame))
				{
					// Encrypt it via SW encryption. 2005.06.27, by rcnjko.
					pEncInfo->IsEncByHW = FALSE;
				}
				// MFP Case !!
				else if(Frame_WEP(osMgntFrame) )
				{
					pEncInfo->IsEncByHW = FALSE;
					pEncInfo->bMFPPacket = TRUE;
				}
				else
				{
					return FALSE;
				}
			}
			else
			{
				return FALSE;
			}
		}
		else 
		{
			OCTET_STRING pTxFrame;

			// Windows lets us do no encryption: DOT11_EXEMPT_ALWAYS
			if( pEncInfo->ExemptionActionType == 2)
			{
				pEncInfo->IsEncByHW = FALSE;
				return FALSE;
			}
			
			if( IsNoDataFrame(pheader))
			{
				// We SHOULD NOT encrypted such packet.
				pEncInfo->IsEncByHW = FALSE;
				return FALSE;
			}

			// <RJ_TODO_WDS> We had not yet implement WDS encryption/decryption. 2006.06.12, by rcnjko.
			if(IsFrameWithAddr4(pheader))
			{
				return FALSE;
			}
			
			pEncInfo->IsEncByHW = TRUE;

			FillOctetString(pTxFrame, pheader, (u2Byte)pTcb->BufferList[0].Length);
			// Only send EAP packet in open when AP do WPS Message exchange 
			
			if(GET_SIMPLE_CONFIG_ENABLED(GetDefaultMgntInfo(Adapter)) && SecIsEAPOLPacket( Adapter, &pTxFrame ))
			{
				RT_TRACE(COMP_WPS,DBG_LOUD,("In WPS mode, IsEncByHW = false!\n"));
				pEncInfo->IsEncByHW = FALSE;
				return FALSE;
			}

			if(!IsSecProtEapol(pEncInfo->SecProtInfo))
			{ // Non-EAPOL packet.
				// Set IsEncByHW to TRUE to let pMgntInfo->SecurityInfo.SWTxEncryptFlag to determine if this packet shall be encrypted by HW or SW.
				pEncInfo->IsEncByHW = TRUE;
			}
			else
			{ // EAPOL packet.
				if(IsSecProtEapolBeforeKeyInstalled(pEncInfo->SecProtInfo))
				{ // Before PTK installed.
					//RT_ASSERT(!IsSecProtEapolAfterKeyInstalled(pEncInfo->SecProtInfo), ("%s(): Invalid pEncInfo->SecProtInfo: %#X\n", __FUNCTION__, pEncInfo->SecProtInfo) );
					RT_ASSERT(!IsSecProtEapolAfterKeyInstalled(pEncInfo->SecProtInfo), ("MgntGetEncryptionInfo(): Invalid pEncInfo->SecProtInfo: %#lX\n", pEncInfo->SecProtInfo) );
					// We CANNOT encrypt the EAPOL packet before PTK installed.
					pEncInfo->IsEncByHW = FALSE;
					return FALSE;
				}
				else
				{ // After PTK installed.
					// We want to encrypt EAPOL packet after PTK installed by "SW encryption".
					pEncInfo->IsEncByHW = FALSE;
				}
			}

			//For WEP 802.1x Roam... EAPOL-Packet can't encode ...
			{
				BOOLEAN		bAPSuportCCKM = FALSE;
				BOOLEAN		bCCX8021xenable = FALSE;

				CCX_QueryCCKMSupport(Adapter, &bCCX8021xenable, &bAPSuportCCKM);
				
				if( ( pMgntInfo->SecurityInfo.PairwiseEncAlgorithm == RT_ENC_ALG_WEP40 || 
					pMgntInfo->SecurityInfo.PairwiseEncAlgorithm ==RT_ENC_ALG_WEP104 ) &&  // WEP mode 
					pMgntInfo->SecurityInfo.AuthMode == RT_802_11AuthModeOpen &&    // Open mode
					!( bCCX8021xenable && bAPSuportCCKM )      // Not in CCKM mode
					)
				{
					
					if( IsSecProtEapol(pEncInfo->SecProtInfo) )
					{			
						RT_TRACE(COMP_SEC, DBG_LOUD, ("====> WEP EAPOL Packet No Encode ....\n"));
						pEncInfo->IsEncByHW = FALSE;
						return FALSE;
					}
				}
			}

			RT_TRACE(COMP_SEC, DBG_TRACE, ("====> MgntGetEncryptionInfo() ExemptionActionType = %x \n",pEncInfo->ExemptionActionType));
		}

		// 2008.04.01
		if(Adapter->HalFunc.GetNmodeSupportBySWSecHandler(Adapter))
		{
			pEncInfo->IsEncByHW = FALSE;	
		}
		else if( MacAddr_isBcst(pDestaddr) )
		{//broadcast frame

			if( 	pMgntInfo->NdisVersion  >= RT_NDIS_VERSION_6_20 ||
				pMgntInfo->bConcurrentMode ||
				pMgntInfo->bBTMode)
				pEncInfo->IsEncByHW = FALSE;
		}
		else if( MacAddr_isMulticast(pDestaddr) )
		{//multicast frame
		
			if( pMgntInfo->NdisVersion  >= RT_NDIS_VERSION_6_20 ||
				pMgntInfo->bConcurrentMode  ||
				pMgntInfo->bBTMode)
				pEncInfo->IsEncByHW = FALSE;	
		}
		else
		{//Unicast frame
	
			//
			//  In Win7 WEP and AD mdoe always used SW encrypt !!
			//
			if( 	pMgntInfo->NdisVersion  >= RT_NDIS_VERSION_6_20 ||
				pMgntInfo->bConcurrentMode ||
				pMgntInfo->bBTMode)
			{
				
				if (	(pMgntInfo->SecurityInfo.PairwiseEncAlgorithm== RT_ENC_ALG_WEP40) ||
					(pMgntInfo->SecurityInfo.PairwiseEncAlgorithm== RT_ENC_ALG_WEP104)||
					pMgntInfo->bRSNAPSKMode )
				{
					pEncInfo->IsEncByHW = FALSE;
				}

				if( pMgntInfo->OpMode == RT_OP_MODE_IBSS )
				{
					pEncInfo->IsEncByHW = FALSE;
				}
			}
		}
		
		//just for FPGA Verification: Adhoc throughput tset
		//multicast, broadcast, unicast data frames all use HW encrypt
		if(pMgntInfo->bRegAdhocUseHWSec && pMgntInfo->OpMode == RT_OP_MODE_IBSS)
		{
			pEncInfo->IsEncByHW = TRUE;
		}
		
		pEncInfo->EncAlg = pMgntInfo->SecurityInfo.PairwiseEncAlgorithm; 
		pEncInfo->keyId = pMgntInfo->SecurityInfo.DefaultTransmitKeyIdx;
		pEncInfo->keylen = pMgntInfo->SecurityInfo.KeyLen[pEncInfo->keyId];
		pEncInfo->keybuf = pMgntInfo->SecurityInfo.KeyBuf[pEncInfo->keyId];

		return TRUE;
	}

	return FALSE;
}



u1Byte
MgntQuery_MgntFrameTxRate(
	PADAPTER		Adapter,
	pu1Byte			dstaddr
	)
{
	PMGNT_INFO	pMgntInfo = &Adapter->MgntInfo;
	u1Byte		rate;

	if(pMgntInfo->IOTAction & HT_IOT_ACT_WA_IOT_Broadcom)
	{
		rate = MgntQuery_TxRateExcludeCCKRates(pMgntInfo->mBrates);
		RT_DISP(FDM, WA_IOT, ("MgntQuery_MgntFrameTxRate(), rate = 0x%x\n", rate));
	}
	else
		rate = pMgntInfo->CurrentBasicRate & 0x7f;
	
	if(rate == 0){
		// 2005.01.26, by rcnjko.
		if(	IS_WIRELESS_MODE_5G(Adapter) ||
			(IS_WIRELESS_MODE_HT_24G(Adapter) &&!pMgntInfo->pHTInfo->bCurSuppCCK))
			rate = MGN_6M;
		else
			rate = MGN_1M;
	}

	return rate;
}

u2Byte
MgntQuery_DataFrameTxRate(
	PADAPTER		Adapter,
	pu1Byte			dstaddr
	)
{
	PMGNT_INFO	pMgntInfo = &Adapter->MgntInfo;
	u2Byte		rate = MGN_1M;
	PADAPTER	pDefAdapter = GetDefaultAdapter(Adapter);
	PMGNT_INFO	pDefMgntInfo = &pDefAdapter->MgntInfo;

	if(pDefMgntInfo->ForcedDataRate != 0)
		rate = pMgntInfo->ForcedDataRate;
	else if(MacAddr_isMulticast(dstaddr))
	{ // Bcst/Mcst.
		if(pMgntInfo->ForcedBstDataRate != 0)
			rate = pMgntInfo->ForcedBstDataRate;
		else
			rate = pMgntInfo->LowestBasicRate & 0x7f;
	}
	else if( ACTING_AS_AP(Adapter) || ACTING_AS_IBSS(Adapter))
	{
		PRT_WLAN_STA	pEntry = AsocEntry_GetEntry(pMgntInfo, dstaddr);

		if(Adapter->RASupport)
		{
			if(pEntry != NULL)
			{
				u1Byte DecisionRate = pEntry->AssociatedMacId;
				Adapter->HalFunc.GetHalDefVarHandler(Adapter, HAL_DEF_RA_DECISION_RATE, (pu1Byte)&DecisionRate);
				rate = Adapter->HalFunc.GetHwRateFromMRateHandler(DecisionRate);
			}
		}
		else
		{
			rate = CURRENT_RATE(	pMgntInfo->dot11CurrentWirelessMode, 
									pMgntInfo->CurrentOperaRate, 
									pMgntInfo->HTHighestOperaRate);

			if(pEntry != NULL)
			{
				u1Byte			PeerHighestOpRate;

				if(rate >= MGN_MCS0)
					PeerHighestOpRate = pEntry->HTInfo.HTHighestOperaRate;
				else
					PeerHighestOpRate = pEntry->HighestOperaRate;

				if( rate > PeerHighestOpRate )
				{
					rate = PeerHighestOpRate;
				}
			}
			else
			{
				if(	pMgntInfo->WdsMode != WDS_DISABLED &&
					eqMacAddr(dstaddr, pMgntInfo->WdsApAddr) )
				{
					rate = pMgntInfo->CurrentOperaRate & 0x7f;
				}
				else
				{
					RT_PRINT_ADDR(COMP_SEND, DBG_WARNING, 
						"MgntQuery_DataFrameTxRate(): nonassociated STA:\n", dstaddr);

					rate = pMgntInfo->LowestBasicRate & 0x7f;
				}
			}
		}		
	}
	else
	{//STA mode.
		if(Adapter->RASupport)
		{
			u1Byte DecisionRate = 0; //0
			Adapter->HalFunc.GetHalDefVarHandler(Adapter, HAL_DEF_RA_DECISION_RATE, (pu1Byte)&DecisionRate);
			rate = Adapter->HalFunc.GetHwRateFromMRateHandler(DecisionRate);
		}
		else
		{
			rate = CURRENT_RATE(pMgntInfo->dot11CurrentWirelessMode, 
									pMgntInfo->CurrentOperaRate, 
									pMgntInfo->HTHighestOperaRate);	

		}
	}

	if(rate == 0)
	{ 
		// 2005.01.13, by rcnjko.
		if(	IS_WIRELESS_MODE_5G(Adapter) ||
			(IS_WIRELESS_MODE_HT_24G(Adapter) && !pMgntInfo->pHTInfo->bCurSuppCCK))
			rate = MGN_6M;
		else
			rate = MGN_1M;
	}

	return rate;
}

u2Byte
MgntQuery_FrameTxRate(
	PADAPTER		Adapter,
	PRT_TCB			pTcb
	)
{
	u2Byte		rate =MGN_1M;
	pu1Byte		pRa = NULL;

	// We cannot use GeTcbDestaddr() here because it is not coalensed, 2005.07.05, by rcnjko.
	RT_ASSERT(pTcb->ProtocolType==RT_PROTOCOL_802_11, ("MgntQuery_FrameTxRate(): ProtocolType: %x\n", pTcb->ProtocolType));
	GetTcbDestaddr(Adapter, pTcb, &pRa);

	if( IsFrameTypeData( pTcb->BufferList[0].VirtualAddress ) )
	{
		if(	(Adapter->MgntInfo.NdisVersion>=RT_NDIS_VERSION_6_20) 
			&& Adapter->bInHctTest && ACTING_AS_AP(Adapter))
		{
			if(!MacAddr_isMulticast(pRa))
				pTcb->DataRate=MGN_54M;
			else
				pTcb->DataRate=MGN_1M;

			pTcb->bTxUseDriverAssingedRate=TRUE;
			rate = pTcb->DataRate;
		}
		else if(  pTcb->DataRate == UNSPECIFIED_DATA_RATE  )
			rate = MgntQuery_DataFrameTxRate( Adapter, pRa );
		else
			rate = pTcb->DataRate;
	}
	else if( IsFrameTypeMgnt( pTcb->BufferList[0].VirtualAddress ) ){

		// If data rate is already assigned, we don't have to query it again. Annie, 2005-03-31
		if(  pTcb->DataRate == UNSPECIFIED_DATA_RATE  )
			rate = MgntQuery_MgntFrameTxRate( Adapter, pRa );
		else
			rate = pTcb->DataRate;
	}
	else{
		
		if(  pTcb->DataRate == UNSPECIFIED_DATA_RATE  )
		{
			 if(	IS_WIRELESS_MODE_5G(Adapter) ||
				(IS_WIRELESS_MODE_HT_24G(Adapter)&& !Adapter->MgntInfo.pHTInfo->bCurSuppCCK))
				rate = MGN_6M;
			else
				rate = MGN_1M;
		}	
		else
			rate = pTcb->DataRate;
	}

	return rate;
}


u1Byte
MgntQuery_NssTxRate(
	IN	u2Byte			Rate
	)
{
	u1Byte	NssNum = RF_TX_NUM_NONIMPLEMENT;
	
	if ( ( Rate >= MGN_MCS8 && Rate <= MGN_MCS15 ) || 
		 ( Rate >= MGN_VHT2SS_MCS0 && Rate <= MGN_VHT2SS_MCS9 ) )
		NssNum = RF_2TX;
	else if ( ( Rate >= MGN_MCS16 && Rate <= MGN_MCS23 ) || 
		 ( Rate >= MGN_VHT3SS_MCS0 && Rate <= MGN_VHT3SS_MCS9 ) )
		NssNum = RF_3TX;
	else if ( ( Rate >= MGN_MCS24 && Rate <= MGN_MCS31 ) || 
		 ( Rate >= MGN_VHT4SS_MCS0 && Rate <= MGN_VHT4SS_MCS9 ) )
		NssNum = RF_4TX;
	else
		NssNum = RF_1TX;
		
	return NssNum;
}


u2Byte
MgntQuery_SequenceNumber(
	PADAPTER		Adapter,
	pu1Byte			pFrame,
	BOOLEAN			bBTTxPacket,
	pu1Byte			pDestAddr,
	u1Byte			TID
)
{
	u2Byte		SeqNum;
	PMGNT_INFO	pMgntInfo = &Adapter->MgntInfo;

	if(IsCtrlNDPA(pFrame))
	{
		SeqNum = pMgntInfo->SoundingSequence;
		if( pMgntInfo->SoundingSequence >= 0x3f )
			pMgntInfo->SoundingSequence = 0;
		else
			pMgntInfo->SoundingSequence++;
	}
	else if(	bBTTxPacket ||
			IsFrameTypeMgnt(pFrame)|| IsLegacyDataFrame(pFrame) || 
			MacAddr_isBcst(pDestAddr) ||MacAddr_isMulticast(pDestAddr) )
	{ // Management and Legacy Data
		SeqNum = pMgntInfo->SequenceNumber;
		if( pMgntInfo->SequenceNumber >= 0x0fff )
			pMgntInfo->SequenceNumber = 0;
		else
			pMgntInfo->SequenceNumber++;

		//RT_DISP(FQoS, QoS_INIT,  ("MGNT_LEGACY SEQ=%d \r\n", pMgntInfo->SequenceNumber));		
	}
	else 
	{ // QoS Data
		PTX_TS_RECORD pTS;
		if(GetTs(Adapter, ((PTS_COMMON_INFO *)&pTS), pDestAddr,  TID, TX_DIR, TRUE))
		{
			SeqNum = pTS->TxCurSeq;		
			//RT_DISP(FQoS, QoS_INIT,  ("pTS->TxCurSeq=%04d\r\n", pTS->TxCurSeq));	
			pTS->TxCurSeq = (SeqNum + 1) % 4096;
			RT_DISP(FQoS, QoS_INIT,  ("Qos data SEQ=%04d TID = %d\r\n", SeqNum, TID));
		}else
			SeqNum = 0;
	}
	
	return SeqNum;
}


BOOLEAN
MgntIsShortPreambleMode(
	PADAPTER		Adapter,
	PRT_TCB			pTcb
)
{
	PMGNT_INFO  pMgntInfo = &Adapter->MgntInfo;
	BOOLEAN		IsShortPreamble = FALSE;
	pu1Byte 		pDestaddr = NULL;

	GetTcbDestaddr(Adapter, pTcb, &pDestaddr);

	if( ACTING_AS_AP(Adapter))
	{
	}
	else
	{
		// 2005.01.06, by rcnjko.
		if( pMgntInfo->dot11CurrentPreambleMode == PREAMBLE_SHORT ) 
		{
			IsShortPreamble = TRUE;
		}
	}

	if(pTcb->bBTTxPacket)
	{
		IsShortPreamble = TRUE;
	}

	return IsShortPreamble;


}



BOOLEAN
MgntIsMulticastFrame(
	PADAPTER		Adapter,
	PRT_TCB			pTcb
)
{
	pu1Byte 		pDestaddr = NULL;

	GetTcbDestaddr(Adapter, pTcb, &pDestaddr);

	if( (pDestaddr[0]&0x01) && (MacAddr_isBcst(pDestaddr)==FALSE) )
		return TRUE;
	else
		return FALSE;
}





BOOLEAN
MgntIsBroadcastFrame(
	PADAPTER		Adapter,
	PRT_TCB			pTcb
)
{
	pu1Byte 		pDestaddr = NULL;

	GetTcbDestaddr(Adapter, pTcb, &pDestaddr);

	if( MacAddr_isBcst(pDestaddr) )
		return TRUE;
	else
		return FALSE;
}


//
// We do not record QosNull and Null Frame to reduce traffic,
// then it will not enter busy traffic state frequently. 2009.04.08 by tynli.
//
BOOLEAN
MgntIsDataOnlyFrame(
	PADAPTER		Adapter,
	PRT_TCB			pTcb
)
{
	pu1Byte pHeader = GET_FRAME_OF_FIRST_FRAG(Adapter, pTcb);

	if(pTcb->bInsert8BytesForEarlyMode)
		pHeader += 8;
		
	if(IsDataFrame(pHeader) && 
		(!IsMgntQosNull(pHeader)) && 
		(!IsMgntNullFrame(pHeader)))
	{
		return TRUE;
	}
	else
		return FALSE;
}


BOOLEAN
MgntIsRateValidForWirelessMode(
	u1Byte	rate,		
	u1Byte	wirelessmode
	)
{
	BOOLEAN bReturn = FALSE;

	switch(wirelessmode)
	{
	case WIRELESS_MODE_A:
		if((rate >= MGN_6M) && (rate <= MGN_54M) && (rate != MGN_11M))
			bReturn = TRUE;
		break;

	case WIRELESS_MODE_B:
		if((rate <= MGN_11M) && (rate != MGN_6M) && (rate != MGN_9M))
			bReturn = TRUE;		
		break;

	case WIRELESS_MODE_G:
		if((rate >= MGN_1M) && (rate <= MGN_54M))
			bReturn = TRUE;
		break;

	case WIRELESS_MODE_N_24G:
		if((rate >= MGN_MCS0) && (rate <= MGN_MCS31)) 
			bReturn = TRUE;
		else if((rate >= MGN_1M) && (rate <= MGN_54M))
			bReturn = TRUE;
		break;

	case WIRELESS_MODE_N_5G:
		if((rate >= MGN_MCS0) && (rate <= MGN_MCS31)) 
			bReturn = TRUE;
		else if((rate >= MGN_6M) && (rate <= MGN_54M) && (rate != MGN_11M))
			bReturn = TRUE;
		break;

	case WIRELESS_MODE_AC_5G:
		if((rate >= MGN_MCS0) && (rate <= MGN_MCS31)) 
			bReturn = TRUE;
		else if((rate >= MGN_6M) && (rate <= MGN_54M) && (rate != MGN_11M))
			bReturn = TRUE;
		else if((rate >= MGN_VHT1SS_MCS0) && (rate <= MGN_VHT4SS_MCS9))
			bReturn = TRUE;
		break;

	case WIRELESS_MODE_AC_24G:	
		if((rate >= MGN_MCS0) && (rate <= MGN_MCS31)) 
			bReturn = TRUE;
		else if((rate >= MGN_VHT1SS_MCS0) && (rate <= MGN_VHT4SS_MCS9))
			bReturn = TRUE;
		else if((rate >= MGN_1M) && (rate <= MGN_54M))
			bReturn = TRUE;
		break;

	case WIRELESS_MODE_AUTO:
		RT_ASSERT( FALSE, ("MgntIsRateValidForWirelessMode(): wirelessmode should not be WIRELESS_MODE_AUTO\n"));
		break;

	default:
		RT_ASSERT( FALSE, ("MgntIsRateValidForWirelessMode(): Unknown wirelessmode: %d\n", wirelessmode));
		break;
	}

	return bReturn;
}



u4Byte
MgntGetChannelFrequency(
	u4Byte		channel
	)
{	// Return in MHz

	// 11b channel
	if(channel <= 14)
		return DSSS_Freq_Channel[channel];

	// 11a channel
	if(channel < 184)
		return 5000+5*channel;

	// 11j channel
	return 5000-5*(200-channel);
}

VOID
MgntUpdateAsocInfo(
	PADAPTER					Adapter,
	UPDATE_ASOC_INFO_ACTION	Action,
	pu1Byte						Content,
	u4Byte						ContentLength
	)
{
	PMGNT_INFO	pMgntInfo = &(Adapter->MgntInfo);
	PASOC_INFO	pAsocInfo = &(pMgntInfo->AsocInfo);

	if (ContentLength > ASOC_INFO_IE_SIZE)
	{
		RT_TRACE(COMP_MLME, DBG_LOUD, ("MgntUpdateAsocInfo:: The Memory Length(%d) of Action(%d) is exceed %d\n", ContentLength, Action, ASOC_INFO_IE_SIZE));

		ContentLength= ASOC_INFO_IE_SIZE;
	}
	
	switch (Action)
	{
	case UpdateAuthSeq1:
		if(ContentLength <= sMacHdrLng)
		{
			RT_TRACE_F(COMP_MLME, DBG_SERIOUS, ("Incorrect ContentLength = %d by UpdateAuthSeq1\n", ContentLength));
			break;
		}
		PlatformMoveMemory(
			pAsocInfo->AuthSeq1,
			Content,
			ContentLength);
		pAsocInfo->AuthSeq1Length = ContentLength;
		break;

	case UpdateAuthSeq2:
		if(ContentLength <= sMacHdrLng)
		{
			RT_TRACE_F(COMP_MLME, DBG_SERIOUS, ("Incorrect ContentLength = %d by UpdateAuthSeq2\n", ContentLength));
			break;
		}
		
		PlatformMoveMemory(
			pAsocInfo->AuthSeq2,
			Content,
			ContentLength);
		pAsocInfo->AuthSeq2Length = ContentLength;
		break;
		
	case UpdateAsocReq:
		PlatformMoveMemory(
			pAsocInfo->AsocReq,
			Content,
			ContentLength);
		pAsocInfo->AsocReqLength = ContentLength;
		break;

	case UpdateAsocResp:
		PlatformMoveMemory(
			pAsocInfo->AsocResp,
			Content,
			ContentLength);
		pAsocInfo->AsocRespLength = ContentLength;
		break;

	case UpdateAsocBeacon:
		PlatformMoveMemory(
			pAsocInfo->Beacon,
			Content,
			ContentLength);
		pAsocInfo->BeaconLength = ContentLength;
		break;

	case UpdateAsocPeerAddr:
		PlatformMoveMemory(
			pAsocInfo->PeerAddr,
			Content,
			6);
		break;

	case UpdateFlagReAsocReq:
		pAsocInfo->FlagReAsocReq = *((PBOOLEAN)Content);
		break;

	case UpdateFlagReAsocResp:
		pAsocInfo->FlagReAsocResp = *((PBOOLEAN)Content);
		break;
		
	case UpdateDeauthAddr:
		cpMacAddr(pAsocInfo->DeauthAddr, (pu1Byte)Content); 
		pAsocInfo->bDeauthAddrValid = TRUE;
		break;
		
	default:
		break;
	}
}

BOOLEAN
MgntIsInExcludedMACList(
	PADAPTER					Adapter,
	pu1Byte						pAddr
	)
{
	PMGNT_INFO	pMgntInfo = &(Adapter->MgntInfo);
	ULONG i;

	for (i=0; i<pMgntInfo->ExcludedMacAddrListLength; i++)
	{
		if (PlatformCompareMemory(
			&pMgntInfo->ExcludedMacAddr[i],
			pAddr,
			6) == 0)
		{
			return TRUE;
		}
	}
	return FALSE;
}

VOID
MgntInitSsidsToScan(
	PADAPTER					Adapter
	)
{
	PMGNT_INFO pMgntInfo = &(Adapter->MgntInfo);
	PRT_SSIDS_TO_SCAN	pSsidsToScan = &(pMgntInfo->SsidsToScan);
	u1Byte i;

	PlatformZeroMemory(pSsidsToScan->SsidBuf, MAX_SSID_TO_SCAN*MAX_SSID_LEN);
	pSsidsToScan->NumSsid = 0;
	for (i=0; i<MAX_SSID_TO_SCAN; i++)
	{
		pSsidsToScan->Ssid[i].Octet = pSsidsToScan->SsidBuf[i];
		pSsidsToScan->Ssid[i].Length = 0;
	}
}

BOOLEAN
MgntAddSsidsToScan(
	PADAPTER					Adapter,
	OCTET_STRING				Ssid
	)
{
	PMGNT_INFO pMgntInfo = &(Adapter->MgntInfo);
	PRT_SSIDS_TO_SCAN	pSsidsToScan = &(pMgntInfo->SsidsToScan);
	u1Byte i;

	if(Ssid.Length == 0)
	{
		RT_TRACE(COMP_SCAN, DBG_LOUD, ("MgntAddSsidsToScan SSid length zero!\n"));
		return FALSE;
	}
	
	// Check if the SSID is already in.
	for (i=0; i<pSsidsToScan->NumSsid; i++)
	{
		if ( CompareSSID(pSsidsToScan->Ssid[i].Octet, pSsidsToScan->Ssid[i].Length,
						Ssid.Octet, Ssid.Length) == TRUE)
		{
			return TRUE;
		}
	}
	
	if (pSsidsToScan->NumSsid < MAX_SSID_TO_SCAN)
	{
		CopySsidOS(pSsidsToScan->Ssid[pSsidsToScan->NumSsid], Ssid);
		RT_PRINT_STR(COMP_MLME, DBG_LOUD, "===> MgntAddSsidsToScan(): dest", pSsidsToScan->Ssid[pSsidsToScan->NumSsid].Octet, pSsidsToScan->Ssid[pSsidsToScan->NumSsid].Length);
		
		pSsidsToScan->NumSsid++;
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

BOOLEAN
MgntRemoveSsidsToScan(
	PADAPTER					Adapter,
	OCTET_STRING				Ssid
	)
{
	PMGNT_INFO pMgntInfo = &(Adapter->MgntInfo);
	PRT_SSIDS_TO_SCAN	pSsidsToScan = &(pMgntInfo->SsidsToScan);
	u1Byte i, j;
	
	for (i=0; i<pSsidsToScan->NumSsid; i++)
	{
		if ( CompareSSID(pSsidsToScan->Ssid[i].Octet, pSsidsToScan->Ssid[i].Length,
						Ssid.Octet, Ssid.Length) == TRUE)
		{
			for (j=i; j<pSsidsToScan->NumSsid-1; j++)
			{
				CopySsidOS(pSsidsToScan->Ssid[j], pSsidsToScan->Ssid[j+1]);
			}
			pSsidsToScan->NumSsid--;
			return TRUE;
		}
	}	

	return FALSE;
}

VOID
MgntClearSsidsToScan(
	PADAPTER					Adapter
	)
{
	MgntInitSsidsToScan(Adapter);
}

// The timer delay is based on the windows ndis thread scheduled delay time.
// By Bruce, 2010-12-27.
#define		TBTT_POLLING_TIMER_DELAY_US	(16 * sTU)	// ms
//
//	Description:
//		Poll MAC's TSF register to simulate TBTT notification.
//
//	Assumption:
//		1. Timer may be fired a little eariler or later than expected. 
//		2. Overhead of Timer and wokritem is less than one Beacon Period.
//
//	070124, by rcnjko.
//
VOID
TbttPollingWorkItemCallback(
	IN PVOID		pContext
	)
{
	PADAPTER			pAdapter = (PADAPTER)pContext;
	PMGNT_INFO			pMgntInfo = &(pAdapter->MgntInfo);
	u8Byte				Now;
	u4Byte				TimeToSleepInUs = 0;
	u4Byte				BcnPeriodUs = (pMgntInfo->dot11BeaconPeriod * sTU);
	RT_RF_POWER_STATE 	rfState;
	u4Byte				timeSlotUs;
	u1Byte				SwBeaconType = BEACON_SEND_AUTO_SW;
	pu1Byte 			pBeaconHeader = pMgntInfo->beaconframe.Octet;
	u1Byte				BeaconQueueID = BEACON_QUEUE;
	u1Byte				nMultiplier = 0; // For IBSS and BEACON_SEND_MANUAL
	BOOLEAN				PreGroupTimSet = FALSE; // Determine if the group traffic bit is set in the TIM info of the previous beacon.

	RT_TRACE(COMP_BEACON, DBG_TRACE, ("TbttPollingWorkItemCallback()====>\n"));

	if(RT_DRIVER_STOP(pAdapter))
	{
		RT_TRACE(COMP_BEACON, DBG_WARNING, 
			("TbttPollingWorkItemCallback(): bDriverStopped = %d, bSurpriseRemoved = %d, bDriverIsGoingToUnload = %d; return this workitem!!!\n", 
			pAdapter->bDriverStopped, pAdapter->bSurpriseRemoved, pAdapter->bDriverIsGoingToUnload));
		return;	
	}
	

	pAdapter->HalFunc.GetHwRegHandler(pAdapter, HW_VAR_RF_STATE, (pu1Byte)(&rfState));
	
	if(rfState != eRfOn)
	{
		RT_TRACE(COMP_BEACON, DBG_WARNING, ("rfState = %d; return this workitem!!!\n", rfState));
		return;
	}

	if(GetDefaultAdapter(pAdapter)->MgntInfo.bStartApDueToWakeup)
	{
		RT_TRACE(COMP_BEACON, DBG_WARNING, ("Tbtt bStartApDueToWakeup ==TRUE, return\n"));
		return;
	}



	// The Beacon state has been marked  "stopped".
	if(pMgntInfo->BeaconState == BEACON_STOP)
	{
		RT_TRACE(COMP_BEACON, DBG_WARNING, ("BeaconState = BEACON_STOP; return this workitem!!!\n"));
		return;	
	}

	pAdapter->HalFunc.GetHalDefVarHandler(pAdapter, HAL_DEF_HW_BEACON_SUPPORT, (pu1Byte)&SwBeaconType);
	// Set the default queue id for beacon.
	BeaconQueueID = (SwBeaconType <= BEACON_SEND_AUTO_SW) ? BEACON_QUEUE : NORMAL_QUEUE;
	// The beacon queue may be different by the different ICs or buses.
	pAdapter->HalFunc.GetHalDefVarHandler(pAdapter, HAL_DEF_BEACON_QUEUE, (pu1Byte)&BeaconQueueID);

	if(pMgntInfo->mIbss && SwBeaconType == BEACON_SEND_MANUAL && pMgntInfo->bMediaConnect)
	{ // Randomize beacon interval if we are not the only one of the IBSS, 2005.08.23, by rcnjko.
		// To fair the Beacon distribution in IBSS, and prevent too many Beacons in the air.
		// Only apply to the manual sending beacon beacuse in other cases the HW can fair the Beacon.
		nMultiplier = (u1Byte)GetRandomNumber(0, 2);
	}
	
	//
	// Poll TSFR to determine the time to send S/W Beacon via ep2.
	//
	pAdapter->HalFunc.GetHwRegHandler(pAdapter, HW_VAR_TSF_TIMER, (pu1Byte)&Now);

	// 
	// The Beacon sending by SW timer is very complex, so we seperate the beacon intrval into
	// several parts A-B, B-C, and C-D as the following.
	//   TBTT                                                      Next TBTT
	//    || <------------------ Beacon Period --------------------- > ||
	//    A              B                              C              D
	//    || <- 16 ms -> | <- Beacon Intval - (16*2) -> | <- 16 ms ->  ||
	//    || Send Mcast  | Do Nothing                   | Update Becon ||	
	//    || --------------------------------------------------------  ||
	// AP mode
	//	a. The HW can send the Beacon in TBTT automatically (BEACON_SEND_AUTO_SW)
	//     (1) A-B: Check if it can send multicast packets now.
	//     (2) C-D: Prepare the beacon content and send it to the beacon queue.
	//   b. The SW should check the timing to send the Beacon packet by sw timer (BEACON_SEND_MANUAL)
	//     (1) A-B: Prepare the Beacon content and send it to the specificed queue.
	//     (2) A-B: After sending the Beacon, check if it can send multicasts now into the queue where the beacon sent into.
	// IBSS mode -
	//	a. The HW can send the Beacon in TBTT automatically (BEACON_SEND_AUTO_SW)
	//     (1) C-D: Prepare the beacon content and send it to the beacon queue.
	//   b. The SW should check the timing to send the Beacon packet by sw timer (BEACON_SEND_MANUAL)
	//     (1) A-B: Prepare the Beacon content and send it to the specificied queue.
	// Note:
	//	The reason why we use 16 ms is the max delay time when the workitem is woke up in Ndis.
	//	We use sTU(1024us) for each partition buffer.
	// By Bruce, 2010-12-27.
	//

	// Position where the current time is at which slot of beacon period.
	timeSlotUs = (u4Byte)PlatformModular64(Now, (u8Byte)BcnPeriodUs);

	//RT_TRACE(COMP_AP, DBG_TRACE, ("TbttPollingWorkItemCallback(): Now: %"i64fmt"d >= timeSlot: %d\n", Now, timeSlotUs));

	//3 // 1st case, A-B
	if(timeSlotUs < (TBTT_POLLING_TIMER_DELAY_US + sTU))
	{
		if(SwBeaconType == BEACON_SEND_AUTO_SW)
		{
			// AP mode
			if(ACTING_AS_AP(pAdapter))
			{ // We need to check the multicast packets.
				AP_PS_SendAllMcastPkts(pAdapter, NORMAL_QUEUE);
			}
			// Wake up at point C before TBTT to update Beacon buffer
			TimeToSleepInUs = (u4Byte)(BcnPeriodUs - TBTT_POLLING_TIMER_DELAY_US - timeSlotUs);
		}		
		else if(SwBeaconType == BEACON_SEND_MANUAL)		
		{			
			// We need to update Beacon content and send the Beacon
			ConstructBeaconFrame(pAdapter);

			
			// Set Timestamp according to current value of our TSFR.
			// Beacuse we shall send this Beacon by ourself in normal queue and the HW won't fill the 
			// timestamp for this Beacon, we need to fill the timestamp by ourself.
			SET_BEACON_PROBE_RSP_TIME_STAMP_LOW(pBeaconHeader, ((u4Byte)(Now & 0xFFFFFFFF)));
			SET_BEACON_PROBE_RSP_TIME_STAMP_HIGH(pBeaconHeader, ((u4Byte)(Now >> 32)));

			// Note: In this case, this beacon packet may not be sent by HW Beacon Queue but any other normal queue.
			SendBeaconFrame(pAdapter, BeaconQueueID);			

			if(pMgntInfo->bDisSwDmaBcn)		
			{
				pAdapter->HalFunc.SetHwRegHandler(pAdapter, HW_VAR_DIS_SW_BCN, (pu1Byte)(&pAdapter));
				pMgntInfo->bDisSwDmaBcn = FALSE;
			}
			
			if(ACTING_AS_AP(pAdapter) && (pMgntInfo->mDtimCount == 0))
			{
				// Note: The Beacon Queue ID shall not be the HW Beacon Queue, or they shall be lost.
				// Why we send the multicast packets by this queue beacause we want these packets
				// are sent after the previous Beacon. By Bruce, 2011-01-19.
				AP_PS_SendAllMcastPkts(pAdapter, BeaconQueueID);
			}
			// Wake up at point D at TBTT to update/send the next Beacon
			TimeToSleepInUs = (u4Byte)(BcnPeriodUs - timeSlotUs + (nMultiplier * BcnPeriodUs));
		}
		else
		{			
			RT_TRACE(COMP_AP, DBG_WARNING, ("TbttPollingWorkItemCallback(): Wrong SwBeaconType(%d)! Return!\n", SwBeaconType));
			return;
		}
	}
	//3 // 2nd Case C-D: Prepare beacon content and send bacon
	else if(timeSlotUs >= (BcnPeriodUs - TBTT_POLLING_TIMER_DELAY_US - sTU))
	{
		if(SwBeaconType == BEACON_SEND_AUTO_SW)		
		{
			ConstructBeaconFrame(pAdapter);
			// Note: We don't need to update timestamp because the HW will fill that.

			SendBeaconFrame(pAdapter, BeaconQueueID);			

			if(pMgntInfo->bDisSwDmaBcn)		
			{
				pAdapter->HalFunc.SetHwRegHandler(pAdapter, HW_VAR_DIS_SW_BCN, (pu1Byte)(&pAdapter));
				pMgntInfo->bDisSwDmaBcn = FALSE;
			}

			PreGroupTimSet = (BOOLEAN)pMgntInfo->TimBuf[2];
			
			if(ACTING_AS_AP(pAdapter) && PreGroupTimSet) // If the Group Tim bit in AP mode was set, we need to send the multicast packets after TBTT.
			{
				// Wake up at point D if we have group traffic to send
				TimeToSleepInUs = (u4Byte)(BcnPeriodUs - timeSlotUs);
			}
			else
			{
				// Wake up at next point C before next TBTT.
				TimeToSleepInUs = (u4Byte)((BcnPeriodUs - timeSlotUs) + (BcnPeriodUs - TBTT_POLLING_TIMER_DELAY_US));
			}
		} 
		else if(SwBeaconType == BEACON_SEND_MANUAL)
		{
			// Wake up at point D at TBTT to update/send the next Beacon
			TimeToSleepInUs = (u4Byte)(BcnPeriodUs - timeSlotUs + (nMultiplier * BcnPeriodUs));
		}
		else
		{
			RT_TRACE(COMP_AP, DBG_WARNING, ("TbttPollingWorkItemCallback(): Wrong SwBeaconType(%d)! Return!\n", SwBeaconType));
			return;
		}		
	}
	//3 // 3rd case B-C: Do Nothing and reschedule the next timer.
	else
	{
		if(SwBeaconType == BEACON_SEND_AUTO_SW)
		{
			// Wake up at point C
			TimeToSleepInUs = (u4Byte)(BcnPeriodUs - TBTT_POLLING_TIMER_DELAY_US - timeSlotUs);
		}
		else if(SwBeaconType == BEACON_SEND_MANUAL)
		{
			// Wake up at point D at TBTT to update/send the next Beacon
			TimeToSleepInUs = (u4Byte)(BcnPeriodUs - timeSlotUs + (nMultiplier * BcnPeriodUs));
		}
		else
		{
			RT_TRACE(COMP_AP, DBG_WARNING, ("TbttPollingWorkItemCallback(): Wrong SwBeaconType(%d)! Return!\n", SwBeaconType));
			return;
		}							
	}

	if(!(RT_DRIVER_HALT(pAdapter)))
	{
		PlatformSetTimer(
			pAdapter, 
			&(pMgntInfo->SwBeaconTimer), 
			(TimeToSleepInUs/1000));

		RT_TRACE(COMP_BEACON, DBG_LOUD, ("TbttPollingWorkItemCallback(): SetTimer(%d ms)! Return!\n", TimeToSleepInUs/sTU));
	}
}

//
//	Description:
//		Enable network monitor mode, all rx packets will be received.
//	2007.08.06, by shien chang.
//
VOID
MgntEnableNetMonitorMode(
	PADAPTER					Adapter,
	BOOLEAN						bInitState
	)
{
	PMGNT_INFO	pMgntInfo = &(Adapter->MgntInfo);

	pMgntInfo->bNetMonitorMode = TRUE;

	if(Adapter->bHWInitReady)
		Adapter->HalFunc.AllowAllDestAddrHandler(Adapter, TRUE, !bInitState);

	IPSDisable(Adapter,FALSE, IPS_DISABLE_PROXIMITY);

	PlatformEnableNetworkMonitorMode(Adapter);
}


//
//	Description:
//		Disable network network monitor mode, only packets destinated to
//		us will be received.
//	2007.08.06, by shien chang.
//
VOID
MgntDisableNetMonitorMode(
	PADAPTER					Adapter,
	BOOLEAN						bInitState
	)
{
	PMGNT_INFO	pMgntInfo = &(Adapter->MgntInfo);

	pMgntInfo->bNetMonitorMode = FALSE;

	if(Adapter->bHWInitReady)
		Adapter->HalFunc.AllowAllDestAddrHandler(Adapter, FALSE, !bInitState);
	
	IPSReturn(Adapter, IPS_DISABLE_PROXIMITY);
	PlatformDisableNetworkMonitorMode(Adapter);
}


//
//	Description:
//		Allocate hash tables.
//	By Bruce, 2008-02-25.
//
BOOLEAN 
MgntAllocHashTables(
	IN	PADAPTER		pAdapter
	)
{
	PMGNT_INFO				pMgntInfo = &(pAdapter->MgntInfo);	
	PSTA_QOS				pStaQos = pMgntInfo->pStaQos;
	RT_HASH_TABLE_HANDLE	hTmp;


	// Qos TStream
	hTmp = RtAllocateHashTable( 
			pAdapter, 
			MAX_AP_TS_COUNT, // Capacity
			sizeof(QOS_TSTREAM), // ValueSize
			QOS_TSTREAM_KEY_SIZE, // KeySize
			QosTsHash); // pfHash
	pStaQos->hApTsTable = hTmp; 
	if(hTmp == NULL)
		return FALSE;

	if(hTmp == NULL)
		return FALSE;
	return TRUE;
}

//
//	Description:
//		Free hash tables allocated in MgntAllocHashTables().
//
VOID 
MgntFreeHashTables(
	IN	PADAPTER		pAdapter
	)
{
	PMGNT_INFO		pMgntInfo = &(pAdapter->MgntInfo);	
	PSTA_QOS		pStaQos = pMgntInfo->pStaQos;

	if(NULL != pStaQos)
		RtFreeHashTable(pStaQos->hApTsTable);
}


u1Byte
MgntQuery_TxRateExcludeCCKRates(
	IN	OCTET_STRING	BSSBasicRateSet)
{
	u2Byte	i;
	u1Byte	QueryRate = 0;
	u1Byte	BasicRate;

	//
	// Find the lowest rate in the BSSBasicRateSet except CCK rates.
	//
	
	for( i = 0; i < BSSBasicRateSet.Length; i++)
	{
		BasicRate = BSSBasicRateSet.Octet[i] & 0x7F; 
		RT_DISP(FDM, WA_IOT, ("MgntQuery_TxRateExcludeCCKRates(), BasicRate = 0x%x\n", BasicRate));
		if(!IS_CCK_RATE(BasicRate))
		{
			if(QueryRate == 0)
			{
				QueryRate = BasicRate;
				RT_DISP(FDM, WA_IOT, ("Find first BasicRate = 0x%x\n", QueryRate));
			}
			else
			{
				if(BasicRate < QueryRate)
				{
					QueryRate = BasicRate;
					RT_DISP(FDM, WA_IOT, ("Find small BasicRate = 0x%x\n", QueryRate));
				}
			}
		}		
	}

	if(QueryRate == 0)
	{
		QueryRate = MGN_6M;	// 6M
		RT_DISP(FDM, WA_IOT, ("No BasicRate found!!\n"));
	}
	RT_DISP(FDM, WA_IOT, ("Final QueryRate = 0x%x\n", QueryRate));
	return QueryRate;
}

//
//	Description:
//		Allocate packet parser for advanced analyze.
//
BOOLEAN
MgntAllocatePacketParser(
	IN	PADAPTER					Adapter
	)
{
	BOOLEAN 	bReturn;

	bReturn = GPAllocateParser(Adapter,
							GP_VOWLAN_SIP,
							"CCX4_CAC SIP parser",
							GPAllocateParserVoWlanSIP,
							GPParserGateVoWlanSIP,
							GPParserActionVoWlanSIP,
							TRUE);
	
	return bReturn;
}

//
//	Description:
//		Free packet parser allocated at MgntAllocatePacketParser().
//
VOID
MgntFreePacketParser(
	IN	PADAPTER		Adapter
	)
{
	GPFreeParser(Adapter,
				GP_VOWLAN_SIP,
				GPFreeParserVoWlanSIP);
}

//
//	Description:
// 		Check this packet if Power Management bit is invoked.
//	By Bruce, 2007-10-26.
//
BOOLEAN
MgntGetPwrMgntInfo(
	PADAPTER			Adapter,
	PRT_TCB				pTcb,
	BOOLEAN				bIncHead
	)
{
	PMGNT_INFO		pMgntInfo = &(Adapter->MgntInfo);
	pu1Byte			pheader = GET_FRAME_OF_FIRST_FRAG(Adapter, pTcb);


	// ========================================================
	// Set PwrMgnt bit according to the following condition (arrange by priority):
	// (1) Not associate to AP: False 
	// (2) IBSS or AP mode: False (We do not enter PS in IBSS now)
	// (3) PS-Poll frame: True
	// (4) Management Frame: False
	// (5) Control frame: False
	// (6) NULL Data: According to the original packet setting
	// (7) Security Handshake process
	// (8) WMM Test Plan(dot11PowerSaveMode & APSD) Data frame: True
	// (9) TDLS PU Sleep STA (same as WMM-PS): True
	// (10) Normal data frame, don't care PS mode: False
	// By Bruce, 2007-11-09.
	// =======================================================

	// (1) Not associate to AP 
	// (2) IBSS or AP mode
	if(!pMgntInfo->mAssoc || pMgntInfo->mIbss ||  ACTING_AS_AP(Adapter))
		return FALSE;
	
	// (3) PS-Poll: True
	if(IsCtrlPSpoll(pheader))
		return TRUE;

	// (4) Management Frame: False
	// (5) Control frame: False
	if( IsMgntFrame(pheader) || IsCtrlFrame(pheader))
		return FALSE;

	// (6)NULL Data: According to the TCB setting
	if(IsMgntNullFrame(pheader) || IsMgntQosNull(pheader))
	{
		return (GET_80211_HDR_PWR_MGNT(pheader))? TRUE : FALSE;
	}
	
	// (7) Security Handshake process
	if(pMgntInfo->SecurityInfo.SecLvl > RT_SEC_LVL_NONE && !SecIsTxKeyInstalled(Adapter, pMgntInfo->Bssid))
		return FALSE;

	// FW control LPS.
	// Always return false becuase Fw will fill this bit by itself during PS mode after 8723A.
	if(IS_FW_POWER_SAVE(pMgntInfo))
		return FALSE;
	
	// (8) WMM Test Plan(dot11PowerSaveMode & APSD) Data frame: True
	if(IS_WIFI_POWER_SAVE(pMgntInfo) )
	{
		return TRUE;
	}

	// (9) TDLS PU Sleep STA
	if(TDLS_GetPwrMgntInfo(Adapter, pTcb))
	{
		pTcb->bSleepPkt = TRUE;
			return TRUE;
	}
	
	// (10) Normal data frame, don't care PS mode: False
	return FALSE;
}


VOID
LedTimerCallback(
	PRT_TIMER		pTimer
	)
{
	PADAPTER	Adapter=(PADAPTER)pTimer->Adapter;
	PMGNT_INFO  pMgntInfo = &Adapter->MgntInfo;
	u2Byte		bLedTxCntBack,bLedRxBack;

	if (pMgntInfo->mAssoc != TRUE && pMgntInfo->mIbss != TRUE)
	{
		return;
	}

	bLedTxCntBack = Adapter->LedTxCnt ;
	if (Adapter->LedTxCnt > 0)
	{
		Adapter->HalFunc.LedControlHandler( Adapter, LED_CTL_TX );
		Adapter->LedTxCnt = 0;
	}

	bLedRxBack = Adapter->LedRxCnt;
	if (Adapter->LedRxCnt > 0)
	{
		Adapter->HalFunc.LedControlHandler( Adapter, LED_CTL_RX );
		Adapter->LedRxCnt = 0;
	}

}


RT_STATUS
ChnlCommAllocateMemory(
	PADAPTER pDefaultAdapter
)
{
	RT_STATUS rtStatus = RT_STATUS_SUCCESS;

	rtStatus = PlatformAllocateMemory(pDefaultAdapter, (void **)&pDefaultAdapter->pPortCommonInfo->pChnlCommInfo, sizeof(CHANNEL_COMMON_CONTEXT));
	if(rtStatus != RT_STATUS_SUCCESS)
	{
		RT_TRACE(COMP_INIT, DBG_LOUD, ("%s: Memory Allocation Failure!\n", __FUNCTION__));

		return RT_STATUS_FAILURE;
	}

	PlatformZeroMemory(pDefaultAdapter->pPortCommonInfo->pChnlCommInfo, sizeof(CHANNEL_COMMON_CONTEXT));
	// Prefast warning C6328: Size mismatch ignore
#pragma warning (disable: 6328)
	RT_TRACE(COMP_INIT, DBG_LOUD, ("%s: Allocate BatchIndicationInfo Memory: Size: %d\n", __FUNCTION__, sizeof(CHANNEL_COMMON_CONTEXT)));

	return RT_STATUS_SUCCESS;
}


VOID
ChnlCommFreeMemory(
	PADAPTER pDefaultAdapter
)
{
	// Prefast warning C6328: Size mismatch ignore
#pragma warning (disable: 6328)
	RT_TRACE(COMP_INIT, DBG_LOUD, ("%s: Free pChnlCommInfo Memory: Size: %d\n", __FUNCTION__, sizeof(CHANNEL_COMMON_CONTEXT)));
	PlatformFreeMemory(pDefaultAdapter->pPortCommonInfo->pChnlCommInfo, sizeof(CHANNEL_COMMON_CONTEXT));
}


// Let each port (Adapter) have common context
RT_STATUS
PortCommonInfoAllocateMemoryWithCriticalInitialization(
	PADAPTER pDefaultAdapter
)
{
	RT_STATUS rtStatus = RT_STATUS_SUCCESS;

	rtStatus = PlatformAllocateMemory(pDefaultAdapter, (void **)&pDefaultAdapter->pPortCommonInfo, sizeof(PORT_COMMON_INFO));
	if(rtStatus != RT_STATUS_SUCCESS)
	{
		RT_TRACE(COMP_INIT, DBG_LOUD, ("%s: Memory Allocation Failure!\n", __FUNCTION__));

		return RT_STATUS_FAILURE;
	}
	else
	{
		PlatformZeroMemory(pDefaultAdapter->pPortCommonInfo, sizeof(PORT_COMMON_INFO));

		pDefaultAdapter->pPortCommonInfo->pDefaultAdapter = pDefaultAdapter;
		
		// Prefast warning C6328: Size mismatch ignore
#pragma warning (disable: 6328)
		RT_TRACE(COMP_INIT, DBG_LOUD, ("%s: Allocate PortCommonInfo Memory: Size: %d\n", __FUNCTION__, sizeof(PORT_COMMON_INFO)));
	}
	
	rtStatus = ChnlCommAllocateMemory(pDefaultAdapter);
	if(rtStatus != RT_STATUS_SUCCESS)
	{
		RT_TRACE(COMP_INIT, DBG_LOUD, ("%s: Memory Allocation Failure!\n", __FUNCTION__));
		PortCommonInfoFreeMemory(pDefaultAdapter);

		return RT_STATUS_FAILURE;
	}	

	// Initialize MultiPort Feature -------------
	MultiPortInitializeContext(pDefaultAdapter);
	// -----------------------------------


	return RT_STATUS_SUCCESS;	

}

VOID
PortCommonInfoFreeMemory(
	PADAPTER pDefaultAdapter
)
{
	// Prefast warning C6328: Size mismatch ignore
#pragma warning (disable: 6328)
	RT_TRACE(COMP_INIT, DBG_LOUD, ("%s: Free PortCommonInfo Memory: Size: %d\n", __FUNCTION__, sizeof(PORT_COMMON_INFO)));

	ChnlCommFreeMemory(pDefaultAdapter);
	PlatformFreeMemory(pDefaultAdapter->pPortCommonInfo, sizeof(PORT_COMMON_INFO));
}

//
// Description: Check whether WoWLAN capability is enabled.
//			The function only returns that whether we need to indicate WoWLAN capabilities to NDIS.
//
// Return value: TRUE, FALSE
//
BOOLEAN
MgntIsWoWLANCapabilityEnable(
	PADAPTER 	pAdapter
)
{	
	PMGNT_INFO		pMgntInfo = &(pAdapter->MgntInfo);
	PRT_POWER_SAVE_CONTROL	pPSC = GET_POWER_SAVE_CONTROL(pMgntInfo);
	BOOLEAN			bResult;
	
	//
	// 2009.09.02. Modified by tynli. 
	// Add condition "Adapter->bUnloadDriverwhenS3S4" and "pPSC->WoWLANMode".
	//
	if(pAdapter->bUnloadDriverwhenS3S4)
	{	// If driver is unload when S3/S4, it cannot support WoWLAN.
		bResult = FALSE;
	}
	else
	{
		if(pPSC->WoWLANMode) // Registry WoWLANMode=1~3
			bResult = TRUE;
		else //WoWLANMode=0
			bResult = FALSE;
	}
	
//	RT_TRACE(COMP_INIT, DBG_TRACE, ("MgntIsWoWLANCapabilityEnable(): bUnloadDriverwhenS3S4 = %d, WoWLANMode = %d\n",
//		pAdapter->bUnloadDriverwhenS3S4, pPSC->WoWLANMode));

	return bResult;
}

//
// Description: The function returns the result that if we allow the device to wake up host by wake event.
//			  Enable/disable WoWLAN Hw related actions will be controlled by the function
//
BOOLEAN
MgntIsSupportRemoteWakeUp(
	PADAPTER 	pAdapter
)
{
	PMGNT_INFO		pMgntInfo = &(pAdapter->MgntInfo);
	PRT_POWER_SAVE_CONTROL	pPSC = GET_POWER_SAVE_CONTROL(pMgntInfo);
	BOOLEAN			bResult;
	
	if(pPSC->bFakeWoWLAN || !(MgntIsWoWLANCapabilityEnable(pAdapter)) || !pPSC->bSupportWakeUp)
		bResult = FALSE;
	else
		bResult = TRUE;

	return bResult;
}

#if(AUTO_CHNL_SEL_NHM == 1)
//
//	Description: 
//		NHM measurement for Auto channel selection mechanism.
//
//	2014.05.15, created by Roger.
//
VOID
MgntAutoChnlSelWorkitemCallback(
	IN	PVOID	Context
	)
{

	PADAPTER		Adapter = (PADAPTER)Context;	
	PMGNT_INFO		pMgntInfo = &(GetDefaultAdapter(Adapter)->MgntInfo);
	u1Byte			AutoChnlSelOp = 1;

	if(!IS_AUTO_CHNL_SUPPORT(Adapter)){
		RT_TRACE(COMP_P2P, DBG_TRACE, ("[ACS] MgntAutoChnlSelWorkitemCallback(): Do not support ACS function, so return!!\n"));	
		return;
	}

	//
	// To check another sync control flag which has already been assigned, 2014.07.31.
	//
	while(TRUE){
		
		PlatformAcquireSpinLock(Adapter, RT_ACS_SPINLOCK);
		if(!IS_AUTO_CHNL_IN_PROGRESS(Adapter)){
			RT_TRACE(COMP_P2P, DBG_WARNING, ("[ACS] MgntAutoChnlSelWorkitemCallback(): Waiting for the control flag which has already been assigned in another thread!!\n"));
			PlatformReleaseSpinLock(Adapter, RT_ACS_SPINLOCK);
			delay_ms(1);
		}
		else{
			PlatformReleaseSpinLock(Adapter, RT_ACS_SPINLOCK);
			break;
		}
	}
	
	RT_TRACE(COMP_P2P, DBG_LOUD, ("---> [ACS] MgntAutoChnlSelWorkitemCallback(): OperationChnl(%d)\n", RT_GetChannelNumber(GetDefaultAdapter(Adapter))));	

	Adapter->HalFunc.SetHwRegHandler(Adapter, HW_VAR_AUTO_CHNL_SEL, (pu1Byte)&AutoChnlSelOp);

	SET_AUTO_CHNL_STATE(Adapter, ACS_AFTER_NHM);// Calculation is done	
	SET_AUTO_CHNL_PROGRESS(Adapter, FALSE);
	
	RT_TRACE(COMP_P2P, DBG_LOUD, ("<--- [ACS] MgntAutoChnlSelWorkitemCallback(): AutoChnlSelCalInProgress(%d)\n", pMgntInfo->AutoChnlSel.AutoChnlSelCalInProgress));
}
#endif