summaryrefslogtreecommitdiff
path: root/network/wlan/WDI/PLATFORM/NDIS6/Ndis6Common.c
blob: a8eb835d3df2274de406ec0c522c439e0ffa75b0 (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
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
////////////////////////////////////////////////////////////////////////////////
//
//	File name:		Ndis5Common.c
//	Description:	NDIS5.x common helper functions.
//
//	Author:			rcnjko
//
////////////////////////////////////////////////////////////////////////////////
#include "Mp_Precomp.h"


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

#include "CustomOid.h"

WDI_TXRX_STATISTICS		GLWdiTxRxStatistics;
u4Byte					GLWdiTestType=0;
WDI_CMD_STATISTICS		GLWdiCmdStatistics;

typedef struct _OID
{ 
	NDIS_OID				Oid;
	NDIS_REQUEST_TYPE	Type;
} OID;

static OID NdisInitPendingOid[] = {
	{OID_DOT11_SCAN_REQUEST, NdisRequestMethod},
	{OID_DOT11_SCAN_REQUEST, NdisRequestSetInformation},
	{OID_DOT11_NIC_POWER_STATE, NdisRequestMethod},
	{OID_DOT11_NIC_POWER_STATE, NdisRequestSetInformation},
	{OID_DOT11_NIC_POWER_STATE, NdisRequestQueryInformation},
	{OID_DOT11_START_AP_REQUEST, NdisRequestMethod},
	{OID_DOT11_START_AP_REQUEST, NdisRequestSetInformation}};

//
// define local function prototype.
//
static	VOID
InitializeAdapterCommon(
	IN 	PADAPTER 	pAdapter
	);

VOID
N6CopyPacket(
	IN	PNET_BUFFER				pNetBuffer,
	OUT	PRT_TX_LOCAL_BUFFER		pLocalBuffer,
	OUT pu2Byte					pPktLength
	)
{                             
	PMDL			pCurrMdl, pNextMdl;                     
	PUCHAR			ptr;
	UINT			BufLen;
	UINT			RemainMdlLength;

	RemainMdlLength = NET_BUFFER_DATA_LENGTH(pNetBuffer);

	// Initialize packet lenth coalesced.
	*pPktLength = 0;

	// Enumerate the list of MDL.
	for (pCurrMdl = NET_BUFFER_CURRENT_MDL(pNetBuffer);
		pCurrMdl != NULL;
		pCurrMdl = pNextMdl)
	{
		pNextMdl = NDIS_MDL_LINKAGE(pCurrMdl);
		
		// Prefast warning C30030: Warning: Allocating executable memory via specifying a MM_PAGE_PRIORITY type without a bitwise OR with MdlMappingNoExecute
		// false positive and currently it is safe here, so disable the warning
#pragma warning ( disable:30030 ) 
		ptr = (pCurrMdl == NET_BUFFER_CURRENT_MDL(pNetBuffer)) ?
			((PUCHAR)MmGetSystemAddressForMdlSafe(pCurrMdl, NormalPagePriority))+NET_BUFFER_CURRENT_MDL_OFFSET(pNetBuffer) :
			MmGetSystemAddressForMdlSafe(pCurrMdl, NormalPagePriority);
		BufLen = (pCurrMdl == NET_BUFFER_CURRENT_MDL(pNetBuffer)) ?
				MmGetMdlByteCount(pCurrMdl)-NET_BUFFER_CURRENT_MDL_OFFSET(pNetBuffer) :
				MmGetMdlByteCount(pCurrMdl);

		if(RemainMdlLength < BufLen)
		{
			BufLen = RemainMdlLength;
			RemainMdlLength = 0;
		}
		else
		{
			RemainMdlLength -= BufLen;
		}

		// Copy the data.
		if(BufLen > 0)
		{
			PlatformMoveMemory( 
				(PVOID)( pLocalBuffer->Buffer.VirtualAddress + *pPktLength ), 
				ptr, 
				BufLen);
			*pPktLength += (u2Byte)BufLen;
		}
	}
}



//
//	Description:
//		Parse the file content into the lines of buffer, pBufOfLines,
//		which is a 2-dimention array, for example,
//		u1Byte pBufOfLines[nMaxNumLine][nMaxByteCntLine].
//
VOID
ParseFileBufToLines(
	IN 		PVOID		pAdapter,
	IN		pu1Byte		pucFilePtr,
	IN		u4Byte		nFileLen,
	IN OUT	pu1Byte		pBufOfLines,
	IN		s4Byte		nMaxNumLine,
	IN		s4Byte		nMaxByteCntLine,
	OUT		ps4Byte		pnNumLinesRead
	)
{
	int		nNumLinesRead;
	int		nIndex;
	pu1Byte	pucBuffer;

	BOOLEAN	bExceedBytesOfLine;
	u1Byte	szEnd[4] = {'0', 'x', 'f', 'f'};
	u1Byte	szEnd_88e[5] = {'0', 'x', 'f', 'f', 'f'};

	pu1Byte	pFileEnd = pucFilePtr + nFileLen;
	BOOLEAN bReachFileEnd = FALSE;

	RT_TRACE(COMP_INIT, DBG_TRACE, ("----> ParseFileBufToLines()\n"));

	*pnNumLinesRead = nNumLinesRead = 0;

	while((*pucFilePtr != '\0') && (pucFilePtr != pFileEnd))
	{
		// Read a line from file and copy into buffer.
		pucBuffer = (pBufOfLines + (nMaxByteCntLine * nNumLinesRead));
		nIndex = 0;
		bExceedBytesOfLine = FALSE;
		while((*pucFilePtr != '\0') && (*pucFilePtr != '\r') && (*pucFilePtr != '\n')) 
		{
			if(nIndex < (nMaxByteCntLine-1)) // 1 is reserved for EOS.
			{
				pucBuffer[nIndex] = *pucFilePtr;
				pucFilePtr++;
				nIndex++;
			}
			else
			{
				// Skip the remainding bytes if this line exceeds max byte count of a line, nMaxByteCntLine.
				pucFilePtr++;
				bExceedBytesOfLine = TRUE;
			}

			if(pucFilePtr == pFileEnd)
			{
				bReachFileEnd = TRUE;
				break;
			}
		}

		if(nIndex > 0)
		{
			// Append EOS to the line read and enumerate to next line.
			pucBuffer[nIndex] = '\0';
			nNumLinesRead++;
			RT_TRACE(COMP_INIT, DBG_TRACE, ("%04d: \"%s\"\n", nNumLinesRead, pucBuffer));
		
			// Message indication: Exceed Max Byte count of line.
			if(bExceedBytesOfLine)
			{
				RT_TRACE(COMP_INIT, DBG_WARNING, ("ParseFileBufToLines(): Exceed nMaxByteCntLine! nNumLinesRead:%d pucBuffer: \"%s\"\n", nNumLinesRead, pucBuffer));
			}

			// Reach file end. End of reading/parsing process.
			if(bReachFileEnd)
			{
				RT_TRACE(COMP_INIT, DBG_WARNING, ("ParseFileBufToLines(): Reach File End!!\n"));
				break;
			}

			{
				// According to the format of configuration file, Offset 0xff is EndOfLine
				if(!PlatformCompareMemory(pucBuffer, szEnd_88e, sizeof(szEnd_88e)))
				{
					RT_TRACE(COMP_INIT, DBG_TRACE, ("ParseFileBufToLines():End Of configuration file, break\n"));
					break;
				}			
			}
		}
	
		// Goto next line.
		pucFilePtr++;
	}

	*pnNumLinesRead = nNumLinesRead;

	RT_TRACE(COMP_INIT, DBG_TRACE, ("<---- ParseFileBufToLines()\n"));
}



//
//	Description:
//		Open the file specifed and read it into pBufOfLines, 
//		which is a 2-dimention array, for example,
//		u1Byte pBufOfLines[nMaxNumLine][nMaxByteCntLine].
//
RT_STATUS
PlatformReadFile(
	IN		PVOID				Adapter,
	IN		UNICODE_STRING*		fileName,
	IN OUT	pu1Byte				pBufOfLines,
	IN		s4Byte				nMaxNumLine,
	IN		s4Byte				nMaxByteCntLine,
	OUT		ps4Byte				pnNumLinesRead
	)
{
	RT_STATUS			rtStatus = RT_STATUS_FAILURE;
	OBJECT_ATTRIBUTES	objectAttributes;
	HANDLE				fileHandle;
	IO_STATUS_BLOCK		iostatBlock;
	NTSTATUS			ntStatus;

	// Check Input parameters.
	if(pBufOfLines == NULL)
	{
		RT_TRACE(COMP_INIT, DBG_WARNING, ("PlatformReadFile(): pBufOfLines should not be NULL!\n"));
		return rtStatus;
	}

	// Initialize the object attributes of the file we want to open
	InitializeObjectAttributes(
		&objectAttributes,
		fileName,
		OBJ_KERNEL_HANDLE | OBJ_CASE_INSENSITIVE,
		NULL,	// RootDirectory 
		NULL	// Default Security
	);	

	// Open the file using the object attributes to get a Handle
	ntStatus = ZwOpenFile(
		&fileHandle,
		GENERIC_READ, 
		&objectAttributes,
		&iostatBlock,
		0,	// Do not share the file
		FILE_NON_DIRECTORY_FILE
	);

	if (NT_SUCCESS(ntStatus)) {
		FILE_STANDARD_INFORMATION fileInformation = { 0 };

		// Get the file information to know the size we need to alloc
		ntStatus = ZwQueryInformationFile(
			fileHandle,
			&iostatBlock,
			&fileInformation,
			sizeof(fileInformation),
			FileStandardInformation
		);

		if (NT_SUCCESS(ntStatus)) {
			u1Byte* buffer = NULL;
			s4Byte bufferLength = (s4Byte)fileInformation.EndOfFile.QuadPart;

			// Try to allocate space to read the file
			buffer = ExAllocatePool2(POOL_FLAG_NON_PAGED, bufferLength, 'frP');
			if (buffer) {

				// If alloc was successful, then read the file
				ntStatus = ZwReadFile(
					fileHandle,
					NULL,
					NULL, 
					NULL,
					&iostatBlock,
					buffer,
					bufferLength,
					0,	// No offset
					NULL
				);

				if (NT_SUCCESS(ntStatus)) {
					// Read the buffer into pBufOfLines.
					ParseFileBufToLines(Adapter, buffer, bufferLength, pBufOfLines, nMaxNumLine, nMaxByteCntLine, pnNumLinesRead);
					rtStatus = RT_STATUS_SUCCESS;
				}
				else {
					RT_TRACE(COMP_INIT, DBG_SERIOUS, ("PlatformReadFile(): failed to read file, ntStatus: %#X\n", ntStatus));
				}

				ExFreePool(buffer);

			}
			else {
				RT_TRACE(COMP_INIT, DBG_SERIOUS, ("PlatformReadFile(): failed to allocate space for file!\n"));
			}
		}
		else {
			RT_TRACE(COMP_INIT, DBG_SERIOUS, ("PlatformReadFile(): failed to read file attributes!, ntStatus: %#X\n", ntStatus));
		}

		ZwClose(fileHandle);
	}
	else {
		RT_TRACE(COMP_INIT, DBG_SERIOUS, ("PlatformReadFile(): failed to open the file!, ntStatus: %#X\n", ntStatus));
	}

	return rtStatus;
}

RT_STATUS
PlatformOpenFile(
	IN		UNICODE_STRING*		fileName,
	IN OUT	HANDLE*				fileHandle
	)
{
	RT_STATUS			rtStatus = RT_STATUS_FAILURE;
	OBJECT_ATTRIBUTES	objectAttributes;
	IO_STATUS_BLOCK		iostatBlock;
	NTSTATUS			ntStatus;

	// Initialize the object attributes of the file we want to open
	InitializeObjectAttributes(
		&objectAttributes,
		fileName,
		OBJ_KERNEL_HANDLE | OBJ_CASE_INSENSITIVE,
		NULL,	// RootDirectory 
		NULL	// Default Security
	);

	// Open the file using the object attributes to get a Handle
	ntStatus = ZwOpenFile(
		fileHandle,
		GENERIC_READ,
		&objectAttributes,
		&iostatBlock,
		0,	// Do not share the file
		FILE_NON_DIRECTORY_FILE
	);

	if (NT_SUCCESS(ntStatus)) {
		rtStatus = RT_STATUS_SUCCESS;
	}
	else {
		RT_TRACE(COMP_INIT, DBG_SERIOUS, ("PlatformReadFile(): failed to open the file!, ntStatus: %#X, fileName: %wZ\n", ntStatus, fileName));
	}

	return rtStatus;
}

RT_STATUS
PlatformMapFile(
	IN OUT	HANDLE*	fileHandle,
	OUT		u1Byte* contentBuffer,
	OUT		s4Byte* contentBufferLength
	)
{

	RT_STATUS			rtStatus = RT_STATUS_FAILURE;
	OBJECT_ATTRIBUTES	objectAttributes;
	IO_STATUS_BLOCK		iostatBlock;
	NTSTATUS			ntStatus;


	FILE_STANDARD_INFORMATION fileInformation = { 0 };

	// Get the file information to know the size we need to alloc
	ntStatus = ZwQueryInformationFile(
		*fileHandle,
		&iostatBlock,
		&fileInformation,
		sizeof(fileInformation),
		FileStandardInformation
	);

	if (NT_SUCCESS(ntStatus)) {
		u1Byte* buffer = NULL;
		s4Byte bufferLength = (s4Byte)fileInformation.EndOfFile.QuadPart;

		// Try to allocate space to read the file
		buffer = ExAllocatePool2(POOL_FLAG_NON_PAGED, bufferLength, 'frP');
		if (buffer) {

			// If alloc was successful, then read the file
			ntStatus = ZwReadFile(
				fileHandle,
				NULL,
				NULL,
				NULL,
				&iostatBlock,
				buffer,
				bufferLength,
				0,	// No offset
				NULL
			);

			if (NT_SUCCESS(ntStatus)) {
				contentBuffer = buffer;
				*contentBufferLength = bufferLength;
				rtStatus = RT_STATUS_SUCCESS;
			}
			else {
				ExFreePool(buffer);
				RT_TRACE(COMP_INIT, DBG_SERIOUS, ("PlatformReadFile(): failed to read file, ntStatus: %#X\n", ntStatus));
			}
		}
		else {
			RT_TRACE(COMP_INIT, DBG_SERIOUS, ("PlatformReadFile(): failed to allocate space for file!\n"));
		}
	}
	else {
		RT_TRACE(COMP_INIT, DBG_SERIOUS, ("PlatformReadFile(): failed to read file attributes!, ntStatus: %#X\n", ntStatus));
	}

	return rtStatus;
}

VOID
PlatformUnMapFile(
	IN OUT	u1Byte* contentBuffer
	)
{
	// Relase the memory for mapping the file.
	ExFreePool(contentBuffer);
}


VOID
PlatformCloseFile(
	IN OUT	HANDLE* fileHandle
	)
{
	// Relase the memory for mapping the file.
	ZwClose(fileHandle);
}

//
//	Description:
//		Open the file specifed and copy the file into an byte buffer, 
//
RT_STATUS
PlatformReadAndMapFile(
	IN		UNICODE_STRING*		fileName,
	OUT		u1Byte* outFileBuffer,
	OUT		u4Byte* outFileBufferLength
)
{
	RT_STATUS			rtStatus = RT_STATUS_FAILURE;
	HANDLE				fileHandle;
	RT_STATUS			callStatus;

	callStatus = PlatformOpenFile(fileName, &fileHandle);

	if (callStatus == RT_STATUS_SUCCESS) {
		u1Byte* fileBuffer = NULL;
		u4Byte* fileBufferLength = NULL;

		callStatus = PlatformMapFile(&fileHandle, fileBuffer, fileBufferLength);

		if (callStatus == RT_STATUS_SUCCESS) {
			outFileBuffer = fileBuffer;
			outFileBufferLength = fileBufferLength;
			rtStatus = RT_STATUS_SUCCESS;
		}
		else {
			RT_TRACE(COMP_INIT, DBG_SERIOUS, ("PlatformReadAndMapFile(): failed to map file!, rtStatus: %#X\n", callStatus));
		}

		PlatformCloseFile(fileHandle);
	}
	else {
		RT_TRACE(COMP_INIT, DBG_SERIOUS, ("PlatformReadAndMapFile(): failed to open the file!, rtStatus: %#X\n", callStatus));
	}

	return rtStatus;
}


//
//	Description:
//		Indication for PHY power state changed.
//	061013, by rcnjko.
//
VOID
N6IndicateCurrentPhyPowerState(
	IN	PADAPTER		pAdapter,
	IN	ULONG			phyId
    )
{
	RT_RF_POWER_STATE rfState;
	DOT11_PHY_STATE_PARAMETERS phyStateParams;
	
	PADAPTER			pDefaultAdapter = GetDefaultAdapter(pAdapter);
	PMGNT_INFO			pDefaultMgnt = &pDefaultAdapter->MgntInfo;

	RT_TRACE(COMP_MLME, DBG_LOUD, ("==> N6IndicateCurrentPhyPowerState()\n"));	

	//Add for win 7, to avoid get the wrong Hw reg. By Maddest, 05112009
	pDefaultAdapter->HalFunc.GetHwRegHandler(pDefaultAdapter, HW_VAR_RF_STATE, (pu1Byte)(&rfState));

	if(pDefaultMgnt->RegRfOff)
	{
		RT_TRACE(COMP_MLME,DBG_LOUD,("N6IndicateCurrentPhyPowerState: RegRFOFF CASE \n"));
		rfState = eRfOff;
		pAdapter->MgntInfo.RfOffReason |= RF_CHANGE_BY_SW;
	}
	
	if(rfState  == eRfOff)
	{
		if (pAdapter->MgntInfo.RfOffReason >= RF_CHANGE_BY_HW)
		{
			rfState  = eRfOff;
		}
		else
		{
			rfState  = eRfOn;
		}
	}
	PlatformZeroMemory(&phyStateParams, sizeof(DOT11_PHY_STATE_PARAMETERS));

	N6_ASSIGN_OBJECT_HEADER(
		phyStateParams.Header,
		NDIS_OBJECT_TYPE_DEFAULT,
		DOT11_PHY_STATE_PARAMETERS_REVISION_1,
		sizeof(DOT11_PHY_STATE_PARAMETERS));
	
	phyStateParams.uPhyId = DOT11_PHY_ID_ANY;
	//
	// <Roger_Notes> We set following parameters to deal with mobility center 
	// which RF on/off option NEVER disabled issue.
	// 2008.03.06.
	//
	if( rfState == eRfOff )
	{	
		phyStateParams.bHardwarePhyState =(pAdapter->MgntInfo.RfOffReason & RF_CHANGE_BY_HW) ? FALSE: TRUE; 
		phyStateParams.bSoftwarePhyState = (pAdapter->MgntInfo.RfOffReason & RF_CHANGE_BY_SW) ? FALSE: TRUE;
	}	
	else
	{// We set HW/SW options all available.
		phyStateParams.bHardwarePhyState = TRUE;
		phyStateParams.bSoftwarePhyState = TRUE;
	}

	RT_TRACE(COMP_RF, DBG_LOUD, ("Indicate radio sts HW/SW=%d/%d\n", phyStateParams.bHardwarePhyState, phyStateParams.bSoftwarePhyState));

	if( !OS_SUPPORT_WDI(pAdapter) )
	{
		N6IndicateStatus(
			pAdapter, 
			NDIS_STATUS_DOT11_PHY_STATE_CHANGED,
			&phyStateParams,
			sizeof(phyStateParams));
	}
}

//
//	Description:
//		Write "RFOff" registry value
//	2006.11.13, by shien chang.
//
NDIS_STATUS
N6WriteRegRfPowerState(
	IN	PADAPTER		Adapter,
	IN	BOOLEAN			bRfOn)
{
	NDIS_HANDLE			ConfigurationHandle;
	NDIS_STATUS			Status;
	NDIS_CONFIGURATION_PARAMETER	ConfigParam;
	NDIS_STRING			KeyName = NDIS_STRING_CONST("RFOff");
	
	// Open the registry for this adapter.
	Status = N6OpenConfigurationHandle(Adapter, &ConfigurationHandle);
	if(Status != NDIS_STATUS_SUCCESS)
	{
		RT_TRACE(COMP_INIT,DBG_TRACE, ("N6WriteRegRfPowerState(): Status=%x\n", Status));
		return Status;
	}
	
	ConfigParam.ParameterType = NdisParameterInteger;
	ConfigParam.ParameterData.IntegerData = (bRfOn ? 0 : 1);
	
	NdisWriteConfiguration(
		&Status,
		ConfigurationHandle,
		&KeyName,
		&ConfigParam);

	// Close the registry
	N6CloseConfigurationHandle(ConfigurationHandle);
	return Status;
}


//
//	Description:
//		Write "Adhoc link state" registry value
//	2009.02.24, by MH.
//
NDIS_STATUS
N6WriteAdhocLinkState(
	IN	PADAPTER		Adapter,
	IN	BOOLEAN			AdhocLinkState)
{
	NDIS_HANDLE			ConfigurationHandle;
	NDIS_STATUS			Status;
	NDIS_CONFIGURATION_PARAMETER	ConfigParam;
	NDIS_STRING			KeyName = NDIS_STRING_CONST("AdhocLinkState");
	
	// Open the registry for this adapter.
	Status = N6OpenConfigurationHandle(Adapter, &ConfigurationHandle);
	if(Status != NDIS_STATUS_SUCCESS)
	{
		RT_TRACE(COMP_INIT,DBG_LOUD, ("N6WriteAdhocLinkState(): Status=%x\n", Status));
		return Status;
	}
	
	ConfigParam.ParameterType = NdisParameterInteger;
	ConfigParam.ParameterData.IntegerData = ((AdhocLinkState==TRUE) ? TRUE : FALSE);
	
	NdisWriteConfiguration(
		&Status,
		ConfigurationHandle,
		&KeyName,
		&ConfigParam);

	// Close the registry
	N6CloseConfigurationHandle(ConfigurationHandle);
	return Status;
}


//
//	Description:
//		Write "U2/U3 mode state" registry value
//	2012.12.12, by MH.
//
NDIS_STATUS
N6WriteUsbCurrentMode(
	IN	PADAPTER		Adapter,
	IN	u1Byte			UsbMode)
{
	NDIS_HANDLE			ConfigurationHandle;
	NDIS_STATUS			Status;
	NDIS_CONFIGURATION_PARAMETER	ConfigParam;
	NDIS_STRING			KeyName = NDIS_STRING_CONST("UsbCurMode");
	
	// Open the registry for this adapter.
	Status = N6OpenConfigurationHandle(Adapter, &ConfigurationHandle);
	if(Status != NDIS_STATUS_SUCCESS)
	{
		RT_DISP(FUSWITCH, FUSWITCH_ERROR, ("%s: UsbMode=%x\n", __FUNCTION__, UsbMode));
		return Status;
	}
	
	ConfigParam.ParameterType = NdisParameterInteger;
	ConfigParam.ParameterData.IntegerData = UsbMode;
	
	NdisWriteConfiguration(
		&Status,
		ConfigurationHandle,
		&KeyName,
		&ConfigParam);

	// Close the registry
	N6CloseConfigurationHandle(ConfigurationHandle);
	return Status;
}


//
//	Description:
//		Write "U2/U3 mode switch counter" registry value
//	2012.12.13, by MH.
//
NDIS_STATUS
N6WriteUsbMode3To2Counter(
	IN	PADAPTER		Adapter)
{
	NDIS_HANDLE			ConfigurationHandle;
	NDIS_STATUS			Status;
	NDIS_CONFIGURATION_PARAMETER	ConfigParam;
	NDIS_STRING			KeyName = NDIS_STRING_CONST("UsbMode3To2Cnt");
	PRT_NDIS6_COMMON	pNdisCommon = Adapter->pNdisCommon;
	
	// Open the registry for this adapter.
	Status = N6OpenConfigurationHandle(Adapter, &ConfigurationHandle);
	if(Status != NDIS_STATUS_SUCCESS)
	{
		RT_DISP(FUSWITCH, FUSWITCH_ERROR, ("%s:\n", __FUNCTION__));
		return Status;
	}

	pNdisCommon->RegUsbMode3To2Cnt++;
	
	ConfigParam.ParameterType = NdisParameterInteger;
	ConfigParam.ParameterData.IntegerData = pNdisCommon->RegUsbMode3To2Cnt;
	
	NdisWriteConfiguration(
		&Status,
		ConfigurationHandle,
		&KeyName,
		&ConfigParam);

	// Close the registry
	N6CloseConfigurationHandle(ConfigurationHandle);
	return Status;
}


//
//	Description:
//		Write "U2/U3 mode switch counter" registry value
//	2012.12.14, by MH.
//
NDIS_STATUS
N6WriteUsbMode2To3Counter(
	IN	PADAPTER		Adapter)
{
	NDIS_HANDLE			ConfigurationHandle;
	NDIS_STATUS			Status;
	NDIS_CONFIGURATION_PARAMETER	ConfigParam;
	NDIS_STRING			KeyName = NDIS_STRING_CONST("UsbMode2To3Cnt");
	PRT_NDIS6_COMMON	pNdisCommon = Adapter->pNdisCommon;
	
	// Open the registry for this adapter.
	Status = N6OpenConfigurationHandle(Adapter, &ConfigurationHandle);
	if(Status != NDIS_STATUS_SUCCESS)
	{
		RT_DISP(FUSWITCH, FUSWITCH_ERROR, ("%s():\n", __FUNCTION__));
		return Status;
	}

	pNdisCommon->RegUsbMode2To3Cnt++;
	pNdisCommon->RegUsbMode2To3CntPrev = pNdisCommon->RegUsbMode2To3Cnt;	

	ConfigParam.ParameterType = NdisParameterInteger;
	ConfigParam.ParameterData.IntegerData = pNdisCommon->RegUsbMode2To3Cnt;
	
	NdisWriteConfiguration(
		&Status,
		ConfigurationHandle,
		&KeyName,
		&ConfigParam);

	// Close the registry
	N6CloseConfigurationHandle(ConfigurationHandle);
	return Status;
}


//
//	Description:
//		Write "USB mode switch counter" registry value
//	2013.05.23, by MH.
//
NDIS_STATUS
N6WriteUsbModeSwitchFlag(
	IN	PADAPTER		Adapter,
	IN	u1Byte			UsbSwitch)
{
	NDIS_HANDLE			ConfigurationHandle;
	NDIS_STATUS			Status;
	NDIS_CONFIGURATION_PARAMETER	ConfigParam;
	NDIS_STRING			KeyName = NDIS_STRING_CONST("UsbSp");
	PRT_NDIS6_COMMON	pNdisCommon = Adapter->pNdisCommon;
	
	// Open the registry for this adapter.
	Status = N6OpenConfigurationHandle(Adapter, &ConfigurationHandle);
	if(Status != NDIS_STATUS_SUCCESS)
	{
		RT_DISP(FUSWITCH, FUSWITCH_ERROR, ("%s:\n", __FUNCTION__));
		return Status;
	}

	ConfigParam.ParameterType = NdisParameterInteger;
	ConfigParam.ParameterData.IntegerData = UsbSwitch;
	
	NdisWriteConfiguration(
		&Status,
		ConfigurationHandle,
		&KeyName,
		&ConfigParam);

	// Close the registry
	N6CloseConfigurationHandle(ConfigurationHandle);
	return Status;
}


//
//	Description:
//		Write "USB mode switch Channel" registry value
//	2013.05.23, by MH.
//
NDIS_STATUS
N6WriteUsbModeSwitchChnl(
	IN	PADAPTER		Adapter,
	IN	u2Byte			Channel)
{
	NDIS_HANDLE			ConfigurationHandle;
	NDIS_STATUS			Status;
	NDIS_CONFIGURATION_PARAMETER	ConfigParam;
	NDIS_STRING			KeyName = NDIS_STRING_CONST("UsbChnl");
	PRT_NDIS6_COMMON	pNdisCommon = Adapter->pNdisCommon;
	
	// Open the registry for this adapter.
	Status = N6OpenConfigurationHandle(Adapter, &ConfigurationHandle);
	if(Status != NDIS_STATUS_SUCCESS)
	{
		RT_DISP(FUSWITCH, FUSWITCH_ERROR, ("%s:\n", __FUNCTION__));
		return Status;
	}

	ConfigParam.ParameterType = NdisParameterInteger;
	ConfigParam.ParameterData.IntegerData = Channel;
	
	NdisWriteConfiguration(
		&Status,
		ConfigurationHandle,
		&KeyName,
		&ConfigParam);

	// Close the registry
	N6CloseConfigurationHandle(ConfigurationHandle);
	return Status;
}


//
//	Description:
//		Write "USB mode switch Channel" registry value
//	2013.06.06, by MH.
// Switch by 0 = from service 1= by driver itself.
//
NDIS_STATUS
N6WriteUsbModeSwitchBy(
	IN	PADAPTER		Adapter,
	IN	u1Byte			SwitchBy)
{
	NDIS_HANDLE			ConfigurationHandle;
	NDIS_STATUS			Status;
	NDIS_CONFIGURATION_PARAMETER	ConfigParam;
	NDIS_STRING			KeyName = NDIS_STRING_CONST("UsbSwBy");
	PRT_NDIS6_COMMON	pNdisCommon = Adapter->pNdisCommon;
	
	// Open the registry for this adapter.
	Status = N6OpenConfigurationHandle(Adapter, &ConfigurationHandle);
	if(Status != NDIS_STATUS_SUCCESS)
	{
		RT_DISP(FUSWITCH, FUSWITCH_ERROR, ("%s:\n", __FUNCTION__));
		return Status;
	}

	ConfigParam.ParameterType = NdisParameterInteger;
	ConfigParam.ParameterData.IntegerData = SwitchBy;
	
	NdisWriteConfiguration(
		&Status,
		ConfigurationHandle,
		&KeyName,
		&ConfigParam);

	// Close the registry
	N6CloseConfigurationHandle(ConfigurationHandle);
	return Status;
}




//
//	Description:
//		Write "U2/U3 mode switch counter" registry value
//	2012.12.14, by MH.
//
NDIS_STATUS
N6WriteUsbMode2To3CounterFail(
	IN	PADAPTER		Adapter)
{
	NDIS_HANDLE			ConfigurationHandle;
	NDIS_STATUS			Status;
	NDIS_CONFIGURATION_PARAMETER	ConfigParam;
	NDIS_STRING			KeyName = NDIS_STRING_CONST("UsbMode2To3CntFail");
	PRT_NDIS6_COMMON	pNdisCommon = Adapter->pNdisCommon;
	
	// Open the registry for this adapter.
	Status = N6OpenConfigurationHandle(Adapter, &ConfigurationHandle);
	if(Status != NDIS_STATUS_SUCCESS)
	{
		RT_DISP(FUSWITCH, FUSWITCH_ERROR, ("%s:\n", __FUNCTION__));
		return Status;
	}

	pNdisCommon->RegUsbMode2To3CntFail++;
	
	ConfigParam.ParameterType = NdisParameterInteger;
	ConfigParam.ParameterData.IntegerData = pNdisCommon->RegUsbMode2To3CntFail;
	
	NdisWriteConfiguration(
		&Status,
		ConfigurationHandle,
		&KeyName,
		&ConfigParam);

	// Close the registry
	N6CloseConfigurationHandle(ConfigurationHandle);
	return Status;
}


//
//	Description:
//		Indicate scan complete.
//	2006.11.14, by shien chang.
//
VOID
N6IndicateScanComplete(
	IN	PADAPTER		Adapter,
	IN	RT_STATUS		status
	)
{
	NDIS_STATUS		ndisStatus;
	PMGNT_INFO	pMgntInfo = &(Adapter->MgntInfo);
	
	if (status == RT_STATUS_SUCCESS)
	{
		ndisStatus = NDIS_STATUS_SUCCESS;
	}
	else
	{
		ndisStatus = NDIS_STATUS_FAILURE;
	}
	
	N6IndicateStatus(
		Adapter,
		NDIS_STATUS_DOT11_SCAN_CONFIRM,
		&ndisStatus,
		sizeof(NDIS_STATUS));	

}

//
//	Description:
//		Indicate connection started.
//	2006.10.17, by shien chang.
//
VOID
N6IndicateConnectionStart(
	IN	PADAPTER		Adapter
	)
{
	PMGNT_INFO	pMgntInfo = &(Adapter->MgntInfo);
	DOT11_CONNECTION_START_PARAMETERS ConnectStartParam;
	
	Adapter->LastConnectStartIndicationTime = PlatformGetCurrentTime();
	
	PlatformZeroMemory(&ConnectStartParam, sizeof(DOT11_CONNECTION_START_PARAMETERS));

	N6_ASSIGN_OBJECT_HEADER(
		ConnectStartParam.Header,
		NDIS_OBJECT_TYPE_DEFAULT,
		DOT11_CONNECTION_START_PARAMETERS_REVISION_1,
		sizeof(DOT11_CONNECTION_START_PARAMETERS));

	if (pMgntInfo->OpMode == RT_OP_MODE_INFRASTRUCTURE)
	{
		ConnectStartParam.BSSType = dot11_BSS_type_infrastructure;
	}
	else if (pMgntInfo->OpMode == RT_OP_MODE_IBSS)
	{
		ConnectStartParam.BSSType = dot11_BSS_type_independent ;

		//
		// 061227, rcnjko: 
		// For fake AP mode, we tell upper layer take our MAC address as the BSSID 
		// to make the frames sent from and indicate to upper layer having correct 
		// BSSID when we are configured to be AP mode later.
		//
		if(MgntActQuery_ApType(Adapter) == RT_AP_TYPE_IBSS_EMULATED)
		{
			PlatformMoveMemory(
				ConnectStartParam.AdhocBSSID,
				Adapter->CurrentAddress, 
				sizeof(DOT11_MAC_ADDRESS));
		}
		else
		{
			PlatformMoveMemory(
				ConnectStartParam.AdhocBSSID,
				 pMgntInfo->Bssid, 
				sizeof(DOT11_MAC_ADDRESS));
		}

		ConnectStartParam.AdhocSSID.uSSIDLength = pMgntInfo->Ssid.Length;
		PlatformMoveMemory(
			ConnectStartParam.AdhocSSID.ucSSID,
			pMgntInfo->Ssid.Octet,
			pMgntInfo->Ssid.Length);
	}

	N6IndicateStatus(
		Adapter,
		NDIS_STATUS_DOT11_CONNECTION_START,
		&ConnectStartParam,
		sizeof(DOT11_CONNECTION_START_PARAMETERS));

}

VOID
N6IndicateConnectionComplete(
	IN	PADAPTER		Adapter,
	IN	RT_STATUS		status
	)
{
	PMGNT_INFO		pMgntInfo = &(Adapter->MgntInfo);
	DOT11_CONNECTION_COMPLETION_PARAMETERS	ConnectCompleteParam;
	
	N6_ASSIGN_OBJECT_HEADER(
		ConnectCompleteParam.Header,
		NDIS_OBJECT_TYPE_DEFAULT,
		DOT11_CONNECTION_COMPLETION_PARAMETERS_REVISION_1,
		sizeof(DOT11_CONNECTION_COMPLETION_PARAMETERS));

	if (status == RT_STATUS_SUCCESS)
	{
		ConnectCompleteParam.uStatus = DOT11_ASSOC_STATUS_SUCCESS;
	}
	else 
	{
		ConnectCompleteParam.uStatus = DOT11_ASSOC_STATUS_FAILURE;
	}

	N6IndicateStatus(
		Adapter,
		NDIS_STATUS_DOT11_CONNECTION_COMPLETION,
		&ConnectCompleteParam,
		sizeof(DOT11_CONNECTION_COMPLETION_PARAMETERS));

}

VOID
N6IndicateAssociationStart(
	IN	PADAPTER		Adapter
	)
{
	PMGNT_INFO	pMgntInfo = &(Adapter->MgntInfo);
	PASOC_INFO	pAsocInfo = &(pMgntInfo->AsocInfo);
	DOT11_ASSOCIATION_START_PARAMETERS	AssoStartParam;

	PlatformZeroMemory(&AssoStartParam, sizeof(DOT11_ASSOCIATION_START_PARAMETERS));
	
	N6_ASSIGN_OBJECT_HEADER(
		AssoStartParam.Header,
		NDIS_OBJECT_TYPE_DEFAULT,
		DOT11_ASSOCIATION_START_PARAMETERS_REVISION_1,
		sizeof(DOT11_ASSOCIATION_START_PARAMETERS));
	
	PlatformMoveMemory(
		AssoStartParam.MacAddr,
		pAsocInfo->PeerAddr,
		sizeof(DOT11_MAC_ADDRESS));
	AssoStartParam.SSID.uSSIDLength = pMgntInfo->Ssid.Length;
	PlatformMoveMemory(
		AssoStartParam.SSID.ucSSID,
		pMgntInfo->Ssid.Octet,
		pMgntInfo->Ssid.Length);

	RT_PRINT_ADDR(COMP_MLME, DBG_LOUD, "Peer:", pAsocInfo->PeerAddr);
	RT_PRINT_STR(COMP_MLME, DBG_LOUD, "SSID", pMgntInfo->Ssid.Octet, pMgntInfo->Ssid.Length);

	AssoStartParam.uIHVDataOffset = 0;
	AssoStartParam.uIHVDataSize = 0;

	N6IndicateStatus(
		Adapter,
		NDIS_STATUS_DOT11_ASSOCIATION_START,
		&AssoStartParam,
		sizeof(DOT11_ASSOCIATION_START_PARAMETERS));

}

VOID
N6IndicateAssociationComplete(
	IN	PADAPTER		Adapter,
	IN	RT_STATUS		status
	)
{
	PRT_NDIS6_COMMON	pNdisCommon = Adapter->pNdisCommon;
	PMGNT_INFO		pMgntInfo = &(Adapter->MgntInfo);
	PRT_SECURITY_T	pSecInfo = &(Adapter->MgntInfo.SecurityInfo);
	RT_AUTH_MODE	authmode;
	PDOT11_ASSOCIATION_COMPLETION_PARAMETERS	pAssoCompleteParam;
	RT_STATUS		rtStatus;
	ULONG			AllocSize;

	pu1Byte pInfoStart = NULL;
	int nInfoOffset = 0;

	// <SC_TODO: what's the matter when auth failed>

	AllocSize = sizeof(DOT11_ASSOCIATION_COMPLETION_PARAMETERS) +
				sizeof(u4Byte); // PHYID
	if(status == RT_STATUS_SUCCESS)
	{
		if(pMgntInfo->OpMode == RT_OP_MODE_INFRASTRUCTURE)
		{
			AllocSize += MMPDU_BODY_LEN(pMgntInfo->AsocInfo.AsocReqLength) +
						MMPDU_BODY_LEN(pMgntInfo->AsocInfo.AsocRespLength) +
						MMPDU_BODY_LEN(pMgntInfo->AsocInfo.BeaconLength);
		}
		else if (pMgntInfo->OpMode == RT_OP_MODE_IBSS)
		{
			AllocSize += MMPDU_BODY_LEN(pMgntInfo->AsocInfo.BeaconLength);
		}
		else if (pMgntInfo->OpMode == RT_OP_MODE_AP)
		{
			ConstructBeaconFrame(Adapter);
			MgntUpdateAsocInfo(Adapter, UpdateAsocBeacon, pMgntInfo->beaconframe.Octet, pMgntInfo->beaconframe.Length);
			AllocSize += MMPDU_BODY_LEN(pMgntInfo->AsocInfo.BeaconLength);
		}
		else
		{
			RT_TRACE(COMP_MLME, DBG_LOUD, ("DrvIFIndicateAssociationComplete(): OpMode: %d\n", pMgntInfo->OpMode));
		}
	}
	rtStatus = PlatformAllocateMemory(
				Adapter,
				&pAssoCompleteParam,
				AllocSize);
	if (rtStatus != RT_STATUS_SUCCESS)
	{
		RT_TRACE(COMP_MLME, DBG_LOUD, 
			("DrvIFIndicateAssociationComplete(): failed to allocate memory for indication.\n"));
			return;
	}
	PlatformZeroMemory(pAssoCompleteParam, AllocSize);
	
	N6_ASSIGN_OBJECT_HEADER(
		pAssoCompleteParam->Header,
		NDIS_OBJECT_TYPE_DEFAULT,
		DOT11_ASSOCIATION_COMPLETION_PARAMETERS_REVISION_2,
		sizeof(DOT11_ASSOCIATION_COMPLETION_PARAMETERS));

	PlatformMoveMemory(
		pAssoCompleteParam->MacAddr,
		pMgntInfo->AsocInfo.PeerAddr,
		sizeof(DOT11_MAC_ADDRESS));
	RT_PRINT_ADDR(COMP_MLME, DBG_LOUD, "Peer:", pAssoCompleteParam->MacAddr);

	
	if (status == RT_STATUS_SUCCESS)
	{
		pAssoCompleteParam->uStatus = DOT11_ASSOC_STATUS_SUCCESS;
	}
	else
	{
		pAssoCompleteParam->uStatus = DOT11_ASSOC_STATUS_FAILURE;
	}

	if (pMgntInfo->OpMode == RT_OP_MODE_INFRASTRUCTURE)
	{
		pAssoCompleteParam->bReAssocReq = pMgntInfo->AsocInfo.FlagReAsocReq;
		pAssoCompleteParam->bReAssocResp = pMgntInfo->AsocInfo.FlagReAsocResp;
	}
	else
	{
		pAssoCompleteParam->bReAssocReq = FALSE;
		pAssoCompleteParam->bReAssocResp = FALSE;
	}

	pAssoCompleteParam->uAssocReqOffset = 0;
	pAssoCompleteParam->uAssocReqSize = 0;
	pAssoCompleteParam->uAssocRespOffset = 0;
	pAssoCompleteParam->uAssocRespSize = 0;

	pAssoCompleteParam->uIHVDataOffset = 0;
	pAssoCompleteParam->uIHVDataSize = 0;

	// DOT11_AUTH_ALGORITHM  AuthAlgo;
	MgntActQuery_802_11_AUTHENTICATION_MODE( Adapter, &authmode );
	pAssoCompleteParam->AuthAlgo = N6CAuthModeToDot11( &authmode );

	// DOT11_CIPHER_ALGORITHM  UnicastCipher;
	pAssoCompleteParam->UnicastCipher = N6CEncAlgorithmToDot11( &(pSecInfo->PairwiseEncAlgorithm) );

	// DOT11_CIPHER_ALGORITHM  MulticastCipher;
	pAssoCompleteParam->MulticastCipher = N6CEncAlgorithmToDot11( &(pSecInfo->GroupEncAlgorithm) );

	pAssoCompleteParam->bFourAddressSupported = FALSE;
	pAssoCompleteParam->bPortAuthorized = FALSE;
	pAssoCompleteParam->DSInfo = DOT11_DS_UNKNOWN;
	pAssoCompleteParam->uEncapTableOffset = 0;
	pAssoCompleteParam->uEncapTableSize = 0;

	//
	// Set Roaming Info in CCKM Fast Roaming
	//
	if(pSecInfo->AuthMode == DOT11_AUTH_ALGO_CCKM){
		if(pAssoCompleteParam->bReAssocReq){
			pAssoCompleteParam->bPortAuthorized = TRUE;
		}
	}

	//
	// Set up Extra information.
	//
	pInfoStart = (pu1Byte)pAssoCompleteParam;
	nInfoOffset = sizeof(DOT11_ASSOCIATION_COMPLETION_PARAMETERS);

	// PHY ID.
	if (status == RT_STATUS_SUCCESS)
	{
		pNdisCommon->ActivePhyId = N6CQuery_DOT11_OPERATING_PHYID(Adapter);
		
	pAssoCompleteParam->uActivePhyListSize = sizeof(u4Byte);
	pAssoCompleteParam->uActivePhyListOffset = nInfoOffset;
	{
		PULONG	pLong = (PULONG)((PUCHAR)pAssoCompleteParam + sizeof(DOT11_ASSOCIATION_COMPLETION_PARAMETERS));
			*((ULONG UNALIGNED *)pLong) = pNdisCommon->ActivePhyId;
		}				
	}
	else
	{
		//set the uActivePhyListOffset and uActivePhyListSize members to zero 
		//if uStatus is not set to NDIS_STATUS_DOT11_ASSOCIATION_COMPLETION.
		pAssoCompleteParam->uActivePhyListSize = 0;
		pAssoCompleteParam->uActivePhyListOffset = 0;
	}				
	
	nInfoOffset += pAssoCompleteParam->uActivePhyListSize;

	if(status == RT_STATUS_SUCCESS)
	{
		if(pMgntInfo->OpMode == RT_OP_MODE_INFRASTRUCTURE)
		{
			// Beacon
			pAssoCompleteParam->uBeaconOffset = nInfoOffset;
			pAssoCompleteParam->uBeaconSize = MMPDU_BODY_LEN(pMgntInfo->AsocInfo.BeaconLength);
			PlatformMoveMemory((pInfoStart+nInfoOffset), 
								MMPDU_BODY(pMgntInfo->AsocInfo.Beacon), 
								pAssoCompleteParam->uBeaconSize);
			nInfoOffset += pAssoCompleteParam->uBeaconSize;
	
			// Association Request.
			pAssoCompleteParam->uAssocReqOffset = nInfoOffset;
			pAssoCompleteParam->uAssocReqSize = MMPDU_BODY_LEN(pMgntInfo->AsocInfo.AsocReqLength); 
			PlatformMoveMemory((pInfoStart+nInfoOffset), 
							MMPDU_BODY(pMgntInfo->AsocInfo.AsocReq), 
							pAssoCompleteParam->uAssocReqSize);
			nInfoOffset += pAssoCompleteParam->uAssocReqSize;

			// Association Response.
			pAssoCompleteParam->uAssocRespOffset = nInfoOffset;
			pAssoCompleteParam->uAssocRespSize = MMPDU_BODY_LEN(pMgntInfo->AsocInfo.AsocRespLength);
			PlatformMoveMemory((pInfoStart+nInfoOffset), 
							MMPDU_BODY(pMgntInfo->AsocInfo.AsocResp), 
							pAssoCompleteParam->uAssocRespSize);
			nInfoOffset += pAssoCompleteParam->uAssocRespSize;
		
		}
		else if(pMgntInfo->OpMode == RT_OP_MODE_IBSS)
		{
			// Beacon
			pAssoCompleteParam->uBeaconOffset = nInfoOffset;
			pAssoCompleteParam->uBeaconSize = MMPDU_BODY_LEN(pMgntInfo->AsocInfo.BeaconLength);
			PlatformMoveMemory((pInfoStart+nInfoOffset), 
								MMPDU_BODY(pMgntInfo->AsocInfo.Beacon), 
								pAssoCompleteParam->uBeaconSize);
			nInfoOffset += pAssoCompleteParam->uBeaconSize;
		}
		else if(pMgntInfo->OpMode == RT_OP_MODE_AP)
		{

			RT_PRINT_DATA(COMP_AP, DBG_LOUD, "INDIC ASSOC COMP BEACON2", pMgntInfo->AsocInfo.Beacon, pMgntInfo->AsocInfo.BeaconLength);
			// Beacon
			pAssoCompleteParam->uBeaconOffset = nInfoOffset;
			pAssoCompleteParam->uBeaconSize = MMPDU_BODY_LEN(pMgntInfo->AsocInfo.BeaconLength);
			PlatformMoveMemory((pInfoStart+nInfoOffset), 
								MMPDU_BODY(pMgntInfo->AsocInfo.Beacon), 
								pAssoCompleteParam->uBeaconSize);
			nInfoOffset += pAssoCompleteParam->uBeaconSize;
		}
	}

	RT_TRACE(COMP_SEC, DBG_LOUD, ("===> pMgntInfo->bInBIPMFPMode = %d\n", pMgntInfo->bInBIPMFPMode));

	
	if( pMgntInfo->bInBIPMFPMode && TEST_FLAG(pMgntInfo->targetAKMSuite, AKM_RSNA_1X_SHA256 | AKM_RSNA_PSK_SHA256))
		pAssoCompleteParam->MulticastMgmtCipher  = DOT11_CIPHER_ALGO_BIP;
	else
		pAssoCompleteParam->MulticastMgmtCipher  = DOT11_CIPHER_ALGO_NONE;

	// If reasoncode == 30
	// TIE 
	// ID | LEN | Timeout Interval Type | Timeout Interval Value
	// 56 | 5    | 3 = Association ComeBack time
	// Set up uAssocComebackTime
	if( pMgntInfo->RspStatusCode == 30 && pMgntInfo->TIE.Length != 0)
	{
		if(pMgntInfo->TIE.Octet[0] == 3 )
		{
			pAssoCompleteParam->uAssocComebackTime = (u4Byte)(pMgntInfo->TIE.Octet[1] & 0xff);
			pAssoCompleteParam->uAssocComebackTime += (u4Byte)(pMgntInfo->TIE.Octet[2] << 8) & 0xff;
			pAssoCompleteParam->uAssocComebackTime += (u4Byte)(pMgntInfo->TIE.Octet[3] << 16) & 0xff;
			pAssoCompleteParam->uAssocComebackTime += (u4Byte)(pMgntInfo->TIE.Octet[4] << 24) & 0xff;
		}
		else
			pAssoCompleteParam->uAssocComebackTime = 0;
	}
	else 
		pAssoCompleteParam->uAssocComebackTime = 0;

	N6IndicateStatus(
		Adapter,
		NDIS_STATUS_DOT11_ASSOCIATION_COMPLETION,
		pAssoCompleteParam,
		AllocSize);

	
	PlatformFreeMemory(pAssoCompleteParam, AllocSize);

}

//
// Note:
//	After indicating Disassociation event to Ndis, the OS waits 10 secs and the miniport driver can then
//	indicate Roaming Start event if need. After 10 secs, Ndis reset the miniport driver by setting the OID
//	OID_DOT11_RESET_REUEST. By Bruce, 2009-02-10.
//
VOID
N6IndicateDisassociation(
	IN	PADAPTER		Adapter,
	IN	u2Byte			reason)
{
	DOT11_DISASSOCIATION_PARAMETERS DisAssoParam;
	PMGNT_INFO	pMgntInfo = &(Adapter->MgntInfo);
	PASOC_INFO	pAsocInfo = &(pMgntInfo->AsocInfo);

	PlatformZeroMemory(&DisAssoParam, sizeof(DOT11_DISASSOCIATION_PARAMETERS));
	
	N6_ASSIGN_OBJECT_HEADER(
		DisAssoParam.Header,
		NDIS_OBJECT_TYPE_DEFAULT,
		DOT11_DISASSOCIATION_PARAMETERS_REVISION_1,
		sizeof(DOT11_DISASSOCIATION_PARAMETERS));

	if (pMgntInfo->OpMode == RT_OP_MODE_INFRASTRUCTURE)
	{
		pu1Byte		pBSSID;

		//
		// If roaming is failed and disassociation event should be indicated, 
		// we should indicate the previous BSSID before roaming. 
		// By Bruce, 2008-05-08.
		//
		if(pMgntInfo->RoamingType == RT_ROAMING_NORMAL)
		{
			pBSSID = pMgntInfo->APaddrbeforeRoaming;
		}
		else
		{
			pBSSID = pMgntInfo->Bssid;
		}
		PlatformMoveMemory(
			DisAssoParam.MacAddr,
			pBSSID,
			sizeof(DOT11_MAC_ADDRESS));
	}
	else
	{
		if(pAsocInfo->bDeauthAddrValid)
		{
			PlatformMoveMemory(
				DisAssoParam.MacAddr,
				pAsocInfo->DeauthAddr,
				sizeof(DOT11_MAC_ADDRESS));
			pAsocInfo->bDeauthAddrValid = FALSE;
		}
		else
		{
			N6_MAKE_WILDCARD_MAC_ADDRESS(DisAssoParam.MacAddr);
		}
	}

	if(reason == disas_lv_ss ||reason == inactivity)
		DisAssoParam.uReason = DOT11_ASSOC_STATUS_PEER_DISASSOCIATED | reason;
	else if (reason == deauth_lv_ss)
	{
		DisAssoParam.uReason = DOT11_ASSOC_STATUS_PEER_DEAUTHENTICATED| reason;
	}
	else if (reason == dest_unreachable){
		DisAssoParam.uReason = DOT11_ASSOC_STATUS_UNREACHABLE;//| reason;
	}
	else
		DisAssoParam.uReason = reason;

	DisAssoParam.uIHVDataOffset = 0;
	DisAssoParam.uIHVDataSize = 0;

	N6IndicateStatus(
		Adapter,
		NDIS_STATUS_DOT11_DISASSOCIATION,
		&DisAssoParam,
		sizeof(DOT11_DISASSOCIATION_PARAMETERS));

}

VOID
N6IndicateRoamingStart(
	IN	PADAPTER		Adapter
	)
{
	DOT11_ROAMING_START_PARAMETERS RoamingStartParam;
	PMGNT_INFO pMgntInfo = &(Adapter->MgntInfo);

	Adapter->LastRoamingStartIndicationTime = PlatformGetCurrentTime();

	PlatformZeroMemory(&RoamingStartParam, sizeof(DOT11_ROAMING_START_PARAMETERS));

	N6_ASSIGN_OBJECT_HEADER(
		RoamingStartParam.Header,
		NDIS_OBJECT_TYPE_DEFAULT,
		DOT11_ROAMING_START_PARAMETERS_REVISION_1,
		sizeof(DOT11_ROAMING_START_PARAMETERS));

	if (pMgntInfo->OpMode == RT_OP_MODE_IBSS)
	{
		PlatformMoveMemory(
			RoamingStartParam.AdhocBSSID,
			pMgntInfo->Bssid,
			sizeof(DOT11_MAC_ADDRESS));

		PlatformMoveMemory(
			RoamingStartParam.AdhocSSID.ucSSID,
			pMgntInfo->Ssid.Octet,
			pMgntInfo->Ssid.Length);
		RoamingStartParam.AdhocSSID.uSSIDLength = pMgntInfo->Ssid.Length;

		RoamingStartParam.uRoamingReason = DOT11_ASSOC_STATUS_ROAMING_ADHOC;
	}
	else
	{
		RoamingStartParam.uRoamingReason = DOT11_ASSOC_STATUS_ROAMING_ASSOCIATION_LOST;
	}
		
	N6IndicateStatus(
		Adapter,
		NDIS_STATUS_DOT11_ROAMING_START,
		&RoamingStartParam,
		sizeof(DOT11_ROAMING_START_PARAMETERS));

}

VOID
N6IndicateRoamingComplete(
	IN	PADAPTER		Adapter,
	IN	RT_STATUS		status
	)
{
	DOT11_ROAMING_COMPLETION_PARAMETERS RoamingCompleteParam;
	PMGNT_INFO pMgntInfo = &(Adapter->MgntInfo);

	PlatformZeroMemory(&RoamingCompleteParam, sizeof(DOT11_ROAMING_COMPLETION_PARAMETERS));

	N6_ASSIGN_OBJECT_HEADER(
		RoamingCompleteParam.Header,
		NDIS_OBJECT_TYPE_DEFAULT,
		DOT11_ROAMING_COMPLETION_PARAMETERS_REVISION_1,
		sizeof(DOT11_ROAMING_COMPLETION_PARAMETERS));

	if (status == RT_STATUS_SUCCESS)
	{
		RoamingCompleteParam.uStatus = DOT11_ASSOC_STATUS_SUCCESS;
	}
	else
	{
		RoamingCompleteParam.uStatus = DOT11_ASSOC_STATUS_FAILURE;
	}

	N6IndicateStatus(
		Adapter,
		NDIS_STATUS_DOT11_ROAMING_COMPLETION,
		&RoamingCompleteParam,
		sizeof(DOT11_ROAMING_COMPLETION_PARAMETERS));

}


//
//	Description:
//		Percedure for periodically reporting link quality.
//	2006.11.29, by shien chang.
//
VOID
LinkQualityReportCallback(
	IN	PADAPTER		Adapter
	)
{
	PMGNT_INFO	pMgntInfo = &(Adapter->MgntInfo);
	PRT_NDIS6_COMMON	pNdisCommon = Adapter->pNdisCommon;
	PDOT11_LINK_QUALITY_PARAMETERS pLinkQualityParam;
	PDOT11_LINK_QUALITY_ENTRY		pLinkQuality;
	u4Byte							AllocSize;
	RT_STATUS						rtStatus;
	/*static*/ u1Byte				LastLinkQuality /*= 0xFF*/; // init to an impossible value.

	FunctionIn(COMP_MLME);

	// Because Ndistest will recognize link quality status indication as nic power status
	// indication in the NicPowerState item, we don't indicate link quality status without
	// associated. 2006.12.04, by shien chang.
	if ( (!pMgntInfo->mAssoc && !pMgntInfo->mIbss) ||
		(pMgntInfo->bMediaConnect == FALSE) ||
		MgntScanInProgress(pMgntInfo))
	{
		RT_TRACE_F(COMP_INDIC, DBG_TRACE, 
			("LinkQualityReportCallback(): Do not indicate because pMgntInfo->mAssoc(%d), pMgntInfo->mIbss (%d), pMgntInfo->bMediaConnect (%d), MgntScanInProgress(pMgntInfo) = %d\n",
			pMgntInfo->mAssoc, pMgntInfo->mIbss, pMgntInfo->bMediaConnect, MgntScanInProgress(pMgntInfo)));
		LastLinkQuality = 0xFF; // reset the value to an impossible value.
		return;
	}

	N6IndicateLinkSpeed(Adapter);
	
	AllocSize = sizeof(DOT11_LINK_QUALITY_PARAMETERS) + 
			sizeof(DOT11_LINK_QUALITY_ENTRY);

	rtStatus = PlatformAllocateMemory(Adapter, &pLinkQualityParam, AllocSize);
	if (rtStatus != RT_STATUS_SUCCESS)
	{
		RT_TRACE(COMP_MLME, DBG_LOUD, 
			("LinkQualityReportCallback(): failed to allocate resources for link quality reporting.\n"));
		return;
	}

	{// refer to driver spec 1.2.9, 2009.01.07, haich.
		static u1Byte	LinkQualityDiffThreshold = 1;
		u1Byte		CurLinkQuality = GetSignalQuality(Adapter);
		u1Byte		LinkQualityDiff /*= (CurLinkQuality > LastLinkQuality) ? (CurLinkQuality - LastLinkQuality) : (LastLinkQuality - CurLinkQuality)*/;
		BOOLEAN 	bProceed = TRUE;

		LastLinkQuality = Adapter->RxStats.LastLinkQuality;
		LinkQualityDiff = (CurLinkQuality > LastLinkQuality) ? (CurLinkQuality - LastLinkQuality) : (LastLinkQuality - CurLinkQuality);

		// 
		// Note that we have to indicate the first notification after associated.
		//

		RT_TRACE(COMP_INDIC, DBG_LOUD, 
				("LinkQualityReportCallback(): CurLinkQuality (%u), LastLinkQuality (%u)\n",
				CurLinkQuality, LastLinkQuality));

		if(0xFF == LastLinkQuality)
		{
			bProceed = TRUE;
		}
		if(LinkQualityDiff < LinkQualityDiffThreshold)
		{// diff lower than the threshold
			bProceed = FALSE;
		}
		else if(CurLinkQuality/20 == LastLinkQuality/20) // The 5 groups: [0, 19], [20, 39], [40, 59], [60, 79], [80, 100].
		{// same group
			bProceed = FALSE;
		}

		if(pMgntInfo->RegFakeRoamSignal[0])
			bProceed = TRUE;
		
		if(!bProceed)
		{
			RT_TRACE(COMP_INDIC, DBG_LOUD, ("LinkQualityReportCallback(): not to indicate\n"));

			// Free corrsponding memory to handle exceptions.
			PlatformFreeMemory(pLinkQualityParam, AllocSize);
			return;
		}
	//	LastLinkQuality = CurLinkQuality;
		Adapter->RxStats.LastLinkQuality = CurLinkQuality;
	}

	PlatformZeroMemory(pLinkQualityParam, AllocSize);

	N6_ASSIGN_OBJECT_HEADER(
		pLinkQualityParam->Header, 
		NDIS_OBJECT_TYPE_DEFAULT, 
		DOT11_LINK_QUALITY_PARAMETERS_REVISION_1, 
		sizeof(DOT11_LINK_QUALITY_PARAMETERS));

	pLinkQualityParam->uLinkQualityListSize = 1;
	pLinkQualityParam->uLinkQualityListOffset = sizeof(DOT11_LINK_QUALITY_PARAMETERS);

	pLinkQuality = (PDOT11_LINK_QUALITY_ENTRY)( 
		((pu1Byte)pLinkQualityParam) + sizeof(DOT11_LINK_QUALITY_PARAMETERS) );
	PlatformMoveMemory(
		pLinkQuality->PeerMacAddr,
		pMgntInfo->Bssid,
		6);

	//
	// SQ indication presented on Vista and later OS:
	// 0: 0 bar in WiFi status
	// 1-19: 1 bar in WiFi status
	// 20-39: 2 bar in WiFi status
	// 40-59: 3 bar in WiFi status
	// 60-79: 4 bar in WiFi status
	// Above 80: 5 bar in WiFi status
	//
	if (pMgntInfo->mAssoc || pMgntInfo->mIbss)
	{
		pLinkQuality->ucLinkQuality = GetSignalQuality(Adapter);
	}
	else
	{
		pLinkQuality->ucLinkQuality = 0;
	}

	if(OS_SUPPORT_WDI(Adapter))
	{
	WDI_IndicateLinkStateChanged(Adapter, FALSE, 0);
	}
	else
	{
		N6IndicateStatus(
			Adapter, 
			NDIS_STATUS_DOT11_LINK_QUALITY, 
			pLinkQualityParam,
			AllocSize);
	}

	PlatformFreeMemory(pLinkQualityParam, AllocSize);
}


//
//	Description:
//		An new state machine for ndis6 mlme status indication.
//	2006.11.29, by shien chang.
//
VOID
N6InitializeIndicateStateMachine(
	IN	PADAPTER		Adapter
	)
{
	PRT_NDIS6_COMMON	pNdisCommon = Adapter->pNdisCommon;
	PN6_INDICATE_STATE_MACHINE	pEngine = &(pNdisCommon->IndicationEngine);
	
	pEngine->CurrOpMode = SM_mNone;
	pEngine->Flag = SM_fNone;
	pEngine->CurrentState = N6_STATE_INITIAL;
	pEngine->NextState = N6_STATE_CONNECT_START;
}


VOID
N6PushIndicateStateMachineAp(
	IN	PADAPTER						Adapter,
	IN	RT_STA_INDICATE_STATE			StateToEnter,
	IN	u2Byte							StatusOrReasonCode
	)
{

	PRT_STA_INDICATE_STATE_MACHINE pIndicateEngine;

	//RT_ASSERT((Adapter->MgntInfo.pCurrentSta && (StateToEnter != N6_STATE_DISASOC)), 
	//		("N6PushIndicateStateMachineAp(): pCurrentSta == NULL and StateToEnter != N6_STATE_DISASOC.\n"));

	if(MgntActQuery_ApType(Adapter) != RT_AP_TYPE_VWIFI_AP)
	{
		return;
	}

	if(Adapter->MgntInfo.pCurrentSta)
	{
		pIndicateEngine = &(Adapter->MgntInfo.pCurrentSta->IndicationEngine);
	}
	else
	{
		RT_TRACE(COMP_INDIC, DBG_LOUD, ("N6PushIndicateStateMachineAp(): pCurrentSta is NULL.\n"));
		return;
	}
	
	if (pIndicateEngine->NextState & StateToEnter)
	{
		RT_STA_INDICATE_STATE OriginalState = pIndicateEngine->CurrentState;
		RT_TRACE(COMP_INDIC, DBG_LOUD,("Transit from 0x%X to 0x%X.\n", OriginalState, StateToEnter));
		RT_PRINT_ADDR(COMP_INDIC, DBG_LOUD, "STA:", Adapter->MgntInfo.pCurrentSta->MacAddr);

		pIndicateEngine->CurrentState = StateToEnter;
		switch (StateToEnter)
		{
		case RT_STA_STATE_INITIAL:
			RT_TRACE(COMP_INDIC, DBG_LOUD,("N6_STATE_INITIAL\n"));
			//pIndicateEngine->Flag = SM_fNone;
			pIndicateEngine->NextState = RT_STA_STATE_AP_INCOMING_ASOC_STARTED;
			break;
			
		case RT_STA_STATE_AP_INCOMING_ASOC_STARTED:
			RT_TRACE(COMP_INDIC, DBG_LOUD,("N6_STATE_AP_INCOMING_ASOC_STARTED\n"));
			
			//pIndicateEngine->Flag = SM_fNone;
			pIndicateEngine->NextState = 0;

			pIndicateEngine->NextState |= RT_STA_STATE_AP_INCOMING_ASOC_COMPLETE;
			pIndicateEngine->NextState |= RT_STA_STATE_AP_INCOMING_ASOC_REQ_RECVD;

			N62CAPIndicateIncomAssocStart(Adapter);
			break;

		case RT_STA_STATE_AP_INCOMING_ASOC_REQ_RECVD:
			RT_TRACE(COMP_INDIC, DBG_LOUD,("N6_STATE_AP_INCOMING_ASOC_REQ_RECVD\n"));
			pIndicateEngine->NextState = RT_STA_STATE_AP_INCOMING_ASOC_COMPLETE;
			N62CAPIndicateIncomAssocReqRecv(Adapter);
			break;

		case RT_STA_STATE_AP_INCOMING_ASOC_COMPLETE:
			RT_TRACE(COMP_INDIC, DBG_LOUD,("N6_STATE_AP_INCOMING_ASOC_COMPLETE\n"));
			pIndicateEngine->NextState = 0;
			
			//if(!IS_STATUS_CODE_SUCCESS(StatusOrReasonCode))
			//{
			//	pIndicateEngine->Flag = SM_fConnected;
			//}

			pIndicateEngine->NextState |= N6_STATE_DISASOC;
			//pIndicateEngine->NextState |= N6_STATE_AP_INCOMING_ASOC_STARTED;
			
			N62CAPIndicateIncomAssocComplete(Adapter, StatusOrReasonCode);
			break;

		case RT_STA_STATE_DISASOC:
			RT_TRACE(COMP_INDIC, DBG_LOUD,("N6_STATE_DISASOC\n"));
			//pIndicateEngine->Flag = SM_fNone;
			pIndicateEngine->NextState = RT_STA_STATE_AP_INCOMING_ASOC_STARTED;
			N62CAPIndicateDisassociation(Adapter, StatusOrReasonCode);
			break;

		default:
			break;
		}
	}
}


VOID
N6PushIndicateStateMachineInfra(
	IN	PADAPTER	Adapter,
	IN	N6_INDICATE_STATE	StateToEnter,
	IN	u2Byte		StatusOrReasonCode
	)
{
	PN6_INDICATE_STATE_MACHINE	pIndicateEngine = 
				&(Adapter->pNdisCommon->IndicationEngine);

	if (pIndicateEngine->NextState & StateToEnter)
	{
		RT_TRACE(COMP_INDIC, DBG_LOUD, ("NextState 0x%x, StateToEnter 0x%x", pIndicateEngine->NextState, StateToEnter));	
		pIndicateEngine->CurrentState = StateToEnter;
		switch (StateToEnter)
		{
		case N6_STATE_INITIAL:
			RT_TRACE(COMP_INDIC, DBG_LOUD,("N6_STATE_INITIAL\n"));

			pIndicateEngine->Flag = SM_fNone;
			pIndicateEngine->NextState = N6_STATE_CONNECT_START;
			break;

		case N6_STATE_CONNECT_START:
			RT_TRACE(COMP_INDIC, DBG_LOUD,("N6_STATE_CONNECT_START\n"));

			pIndicateEngine->Flag = SM_fNone;
			pIndicateEngine->NextState = N6_STATE_ASOC_START | N6_STATE_CONNECT_COMPLETE;
			N6IndicateConnectionStart(Adapter);
			break;

		case N6_STATE_ASOC_START:
			RT_TRACE(COMP_INDIC, DBG_LOUD,("N6_STATE_ASOC_START\n"));
			
			pIndicateEngine->NextState = N6_STATE_ASOC_COMPLETE;
			N6IndicateAssociationStart(Adapter);
			break;

		case N6_STATE_ASOC_COMPLETE:
			RT_TRACE(COMP_INDIC, DBG_LOUD,("N6_STATE_ASOC_COMPLETE\n"));

			pIndicateEngine->NextState = 0;
			if ( !IS_STATUS_CODE_SUCCESS(StatusOrReasonCode) )
			{
				pIndicateEngine->NextState |= N6_STATE_ASOC_START;
			}
			
			if ( pIndicateEngine->Flag == SM_fRoaming )
			{
				pIndicateEngine->NextState |= N6_STATE_ROAM_COMPLETE;
			}
			else
			{
				pIndicateEngine->NextState |= N6_STATE_CONNECT_COMPLETE;
			}
			N6IndicateAssociationComplete(Adapter, StatusOrReasonCode);
			break;

		case N6_STATE_CONNECT_COMPLETE:
			RT_TRACE(COMP_INDIC, DBG_LOUD,("N6_STATE_CONNECT_COMPLETE\n"));

			pIndicateEngine->Flag = SM_fConnected;
			pIndicateEngine->NextState = N6_STATE_DISASOC | N6_STATE_ROAM_START;
			N6IndicateConnectionComplete(Adapter, StatusOrReasonCode);
			break;

		case N6_STATE_DISASOC:
			RT_TRACE(COMP_INDIC, DBG_LOUD,("N6_STATE_DISASOC\n"));

			pIndicateEngine->NextState = N6_STATE_ROAM_START;
			{
				N6IndicateDisassociation(Adapter, StatusOrReasonCode);
				RT_TRACE(COMP_INDIC, DBG_LOUD,("N6_STATE_DISASOC DO IT  !!\n"));
			}
			break;

		case N6_STATE_ROAM_START:
			RT_TRACE(COMP_INDIC, DBG_LOUD,("N6_STATE_ROAM_START\n"));

			pIndicateEngine->Flag = SM_fRoaming;
			pIndicateEngine->NextState = N6_STATE_ASOC_START | N6_STATE_ROAM_COMPLETE;
			N6IndicateRoamingStart(Adapter);
			break;

		case N6_STATE_ROAM_COMPLETE:
			RT_TRACE(COMP_INDIC, DBG_LOUD,("N6_STATE_ROAM_COMPLETE\n"));

			pIndicateEngine->Flag = SM_fConnected;
			pIndicateEngine->NextState = N6_STATE_DISASOC | N6_STATE_ROAM_START;
			N6IndicateRoamingComplete(Adapter, StatusOrReasonCode);
			break;

		default:
			break;
		}
	}
}

VOID
N6PushIndicateStateMachineIBSS(
	IN	PADAPTER	Adapter,
	IN	N6_INDICATE_STATE	StateToEnter,
	IN	u2Byte		StatusOrReasonCode
	)
{
	PN6_INDICATE_STATE_MACHINE	pIndicateEngine = 
				&(Adapter->pNdisCommon->IndicationEngine);

	RT_TRACE(COMP_INDIC, DBG_LOUD,("==>N6PushIndicateStateMachineIBSS() pIndicateEngine->Flag  %d\n",pIndicateEngine->Flag));
	RT_TRACE(COMP_INDIC, DBG_LOUD, ("NextState 0x%x, StateToEnter 0x%x", pIndicateEngine->NextState, StateToEnter));	
	
	if (pIndicateEngine->NextState & StateToEnter)
	{
		RT_TRACE(COMP_INDIC, DBG_TRACE, ("NextState 0x%x, StateToEnter 0x%x", pIndicateEngine->NextState, StateToEnter));	
	
		pIndicateEngine->CurrentState = StateToEnter;
		switch (StateToEnter)
		{
		case N6_STATE_INITIAL:
			RT_TRACE(COMP_INDIC, DBG_LOUD,("N6_STATE_INITIAL\n"));

			pIndicateEngine->Flag = SM_fNone;
			pIndicateEngine->NextState = N6_STATE_CONNECT_START;
			break;

		case N6_STATE_CONNECT_START:
			RT_TRACE(COMP_INDIC, DBG_LOUD,("N6_STATE_CONNECT_START\n"));

			pIndicateEngine->Flag = SM_fNone;
			pIndicateEngine->NextState = N6_STATE_ASOC_START | N6_STATE_CONNECT_COMPLETE;
			N6IndicateConnectionStart(Adapter);
			break;

		case N6_STATE_ASOC_START:
			RT_TRACE(COMP_INDIC, DBG_LOUD,("N6_STATE_ASOC_START\n"));

			pIndicateEngine->NextState = N6_STATE_ASOC_COMPLETE;
			N6IndicateAssociationStart(Adapter);
			break;

		case N6_STATE_ASOC_COMPLETE:
			RT_TRACE(COMP_INDIC, DBG_LOUD,("N6_STATE_ASOC_COMPLETE\n"));

			if ( (pIndicateEngine->Flag == SM_fConnected) ||(pIndicateEngine->Flag == SM_fRoaming) )
			{
				RT_TRACE(COMP_INDIC, DBG_LOUD,("==>N6PushIndicateStateMachineIBSS() N6_STATE_ASOC_COMPLETE  SM_fConnected\n"));
				pIndicateEngine->NextState = N6_STATE_ASOC_START | N6_STATE_DISASOC|N6_STATE_ROAM_COMPLETE;
				if(pIndicateEngine->Flag == SM_fConnected)	//Association Operation Guidelines for Independent BSS Networks diagram 1	
					pIndicateEngine->NextState |= N6_STATE_ROAM_START;					
			}
			else
			{
				RT_TRACE(COMP_INDIC, DBG_LOUD,("==>N6PushIndicateStateMachineIBSS() N6_STATE_ASOC_COMPLETE not SM_fConnected\n"));
				pIndicateEngine->NextState = N6_STATE_ASOC_START | N6_STATE_CONNECT_COMPLETE;
			}
			N6IndicateAssociationComplete(Adapter, StatusOrReasonCode);
			break;

		case N6_STATE_CONNECT_COMPLETE:
			RT_TRACE(COMP_INDIC, DBG_LOUD,("N6_STATE_CONNECT_COMPLETE\n"));

			pIndicateEngine->Flag = SM_fConnected;
			pIndicateEngine->NextState = N6_STATE_ASOC_START | N6_STATE_DISASOC| N6_STATE_ROAM_START;
			N6IndicateConnectionComplete(Adapter, StatusOrReasonCode);
			break;

		case N6_STATE_DISASOC:
			// David says: We should resume from disassociate state by indicate an
			// NDIS_STATUS_DOT11_ASSOCIATION_START or the starting of IBSS
			// will have problem.
			RT_TRACE(COMP_INDIC, DBG_LOUD,("N6_STATE_DISASOC\n"));

			pIndicateEngine->NextState = N6_STATE_ASOC_START | N6_STATE_ROAM_START | N6_STATE_DISASOC;
			N6IndicateDisassociation(Adapter, StatusOrReasonCode);
			break;

		//
		//Porting from 8xb to avoid IBSS can not relink after s3/s4 on Vista, by Maddest
		//
		case N6_STATE_ROAM_START :
			RT_TRACE(COMP_INDIC, DBG_LOUD,("N6_STATE_ROAM_START\n"));

			pIndicateEngine->Flag = SM_fRoaming;
			pIndicateEngine->NextState = N6_STATE_ASOC_START | N6_STATE_ROAM_COMPLETE;
			N6IndicateRoamingStart(Adapter);
			break;
		//
		//Porting from 8xb to avoid IBSS can not relink after s3/s4 on Vista, by Maddest
		//			
		case N6_STATE_ROAM_COMPLETE:
			RT_TRACE(COMP_INDIC, DBG_LOUD,("N6_STATE_ROAM_COMPLETE\n"));

			pIndicateEngine->Flag = SM_fConnected;
			pIndicateEngine->NextState = N6_STATE_DISASOC | N6_STATE_ROAM_START|N6_STATE_ASOC_START;
			N6IndicateRoamingComplete(Adapter, StatusOrReasonCode);
			break;
			
		default:
			break;
		}

	}
	RT_TRACE(COMP_INDIC, DBG_LOUD,("<==N6PushIndicateStateMachineIBSS() \n"));
}

//
//	Description:
//		Push forward the state machine of ndis6 status indication.
//	2006.11.29, by shien chang.
//
VOID
N6PushIndicateStateMachine(
	IN	PADAPTER		Adapter,
	IN	N6_INDICATE_STATE	StateToEnter,
	IN	u2Byte			StatusOrReasonCode
	)
{
	PMGNT_INFO	pMgntInfo = &(Adapter->MgntInfo);
	PN6_INDICATE_STATE_MACHINE	pIndicateEngine = 
				&(Adapter->pNdisCommon->IndicationEngine);
	
	PlatformAcquireSpinLock(Adapter, RT_INDIC_SPINLOCK);
	
	if(MgntActQuery_ApType(Adapter) == RT_AP_TYPE_VWIFI_AP)
	{
		N6PushIndicateStateMachineAp(Adapter, StateToEnter, StatusOrReasonCode);
		PlatformReleaseSpinLock(Adapter, RT_INDIC_SPINLOCK);
		return;
	}

	if (pIndicateEngine->CurrentState == N6_STATE_INITIAL)
	{
		switch (pMgntInfo->OpMode)
		{
		case RT_OP_MODE_INFRASTRUCTURE:
			pIndicateEngine->CurrOpMode = SM_mInfra;
			break;

		case RT_OP_MODE_IBSS:
			pIndicateEngine->CurrOpMode = SM_mAdhoc;
			break;

		case RT_OP_MODE_AP:
			if(MgntActQuery_ApType(Adapter) == RT_AP_TYPE_IBSS_EMULATED)
			{
				//
				// 061227, rcnjko:
				// Because our UI will set a fake IBSS profile before setting 
				// AP profile CurrOpMode will be set to SM_mAdhoc for AP mode case.
				//
				RT_TRACE(COMP_MLME, DBG_WARNING, 
					("N6PushIndicateStateMachine(): N6_STATE_INITIAL with RT_OP_MODE_AP is unexpected!!!\n"));
				pIndicateEngine->CurrOpMode = SM_mAdhoc;

			}
			RT_TRACE(COMP_MLME, DBG_WARNING, 
				("N6PushIndicateStateMachine(): TODO!\n"));
			PlatformReleaseSpinLock(Adapter, RT_INDIC_SPINLOCK);
			return;
			break;

		case RT_OP_MODE_NO_LINK:
			RT_TRACE(COMP_INDIC,DBG_LOUD, ("RT_OP_MODE_NO_LINK\n"));
			PlatformReleaseSpinLock(Adapter, RT_INDIC_SPINLOCK);
			return;
			break;
		}
	}
	
	if (pIndicateEngine->CurrOpMode == SM_mInfra)
	{
		N6PushIndicateStateMachineInfra(Adapter, StateToEnter, StatusOrReasonCode);
	}
	else if (pIndicateEngine->CurrOpMode == SM_mAdhoc)
	{
		N6PushIndicateStateMachineIBSS(Adapter, StateToEnter, StatusOrReasonCode);
	}
	
	PlatformReleaseSpinLock(Adapter, RT_INDIC_SPINLOCK);
}

//
//	Description:
//		Map status or reason code to DOT11 status code.
//	2006.12.01, by shien chang.
//
DOT11_ASSOC_STATUS
N6TranslateToDot11AssocStatus(
	IN	N6_INDICATE_STATE	CurrentState,
	IN	u2Byte			StatusOrReasonCode
	)
{
	switch (CurrentState)
	{
	case N6_STATE_ASOC_COMPLETE:
	case N6_STATE_CONNECT_COMPLETE:
	case N6_STATE_ROAM_COMPLETE:
		// Status code.
		
		break;
		
	case N6_STATE_DISASOC:
		// Reason code.
		
		break;
		
	default:
		break;
	}

	// not implement.
	return DOT11_ASSOC_STATUS_SUCCESS;
}

int
GetSignedInteger(
	u4Byte	usDW,
	u1Byte	BitNumber
	)
{
	u4Byte	u4bValue, SignBit;
	int		iValue, idx;
	u4Byte	Mask = 0x00000001;
	u4Byte	u4bTmp = 0x00000001;

	if( BitNumber >= 32 )
	{
		return	0;
	}
	
	SignBit = 0x00000001 << (BitNumber-1);
	for( idx=1; idx<(BitNumber-1); idx++ )
	{
		Mask |= ( u4bTmp << idx );
	}
	
	u4bValue = (usDW & Mask);

	if( usDW & SignBit )
	{ // negative
		u4bValue = ~(u4bValue) + 1;
		u4bValue &= Mask;
		iValue = 0 - (int)u4bValue;
	}
	else
	{ // positive
		iValue = (int)u4bValue;
	}

	return	iValue;
}

BOOLEAN
N6Dot11AddrIsBcast(
	IN	DOT11_MAC_ADDRESS	dot11Addr)
{
	pu1Byte	pAddr = (pu1Byte)dot11Addr;

	return (
		( (pAddr[0] == 0xFF) && (pAddr[1] == 0xFF) && (pAddr[2] == 0xFF) &&
		  (pAddr[3] == 0xFF) && (pAddr[4] == 0xFF) && (pAddr[5] == 0xFF) ) ? 
		TRUE : FALSE);
}


//DTM will set this wrong addr, by Maddest 070823
BOOLEAN
N6Dot11AddrIsfe(
	IN	DOT11_MAC_ADDRESS	dot11Addr)
{
	pu1Byte	pAddr = (pu1Byte)dot11Addr;

	return (
		( (pAddr[0] == 0xFe) && (pAddr[1] == 0xFF) && (pAddr[2] == 0xFF) &&
		  (pAddr[3] == 0xFF) && (pAddr[4] == 0xFF) && (pAddr[5] == 0xFF) ) ? 
		TRUE : FALSE);
}

//
//	Description:
//		Callback function of SetRFPowerStateWorkItem, 
//		which is used by OID to change RF power state.
//
VOID
SetRFPowerStateWorkItemCallback(
	IN PVOID			pContext
	)
{
	PADAPTER 			Adapter = (PADAPTER)pContext;
	PADAPTER			pAdapter = GetDefaultAdapter(Adapter);	
	PMGNT_INFO			pMgntInfo = &(pAdapter->MgntInfo);
	HAL_DATA_TYPE		*pHalData	= GET_HAL_DATA(Adapter);
	PRT_NDIS6_COMMON 	pNdisCommon = pAdapter->pNdisCommon;
	BOOLEAN				bValue;
	NDIS_STATUS			ndisStatus;
	PRT_POWER_SAVE_CONTROL	pPSC = GET_POWER_SAVE_CONTROL(pMgntInfo);

	pMgntInfo->eSwRfPowerState = pNdisCommon->eRfPowerStateToSet;

	// Write registry for persistent.
	bValue = (pMgntInfo->eSwRfPowerState == eRfOn) ? TRUE : FALSE;

	//
	//It is a temp soluation, when DTM 1.0c Test with Nicpowerstate, after reinitialize the regoff value will
	//wrong, by Maddest 070823
	//
	pNdisCommon->RegRfOff=(bValue ==TRUE) ? FALSE : TRUE;
	pMgntInfo->RegRfOff = (bValue ==TRUE) ? FALSE : TRUE;

	ndisStatus = N6WriteRegRfPowerState(pAdapter, bValue);
	if (ndisStatus != NDIS_STATUS_SUCCESS)
	{
		RT_TRACE(COMP_POWER, DBG_LOUD, ("N6CSet_DOT11_NIC_POWER_STATE(): failed to write registry.\n"));
	}

	if(pNdisCommon->eRfPowerStateToSet == eRfOn)
	{
		if(pPSC->RegRfPsLevel & RT_RF_OFF_LEVL_ASPM)
		{
			RT_DISABLE_ASPM(pAdapter);
			RT_CLEAR_PS_LEVEL(pAdapter, RT_RF_OFF_LEVL_ASPM);
		}
		else if(pPSC->RegRfPsLevel & RT_RF_OFF_LEVL_PCI_D3)
		{
			RT_LEAVE_D3(pAdapter, FALSE);
			RT_CLEAR_PS_LEVEL(pAdapter, RT_RF_OFF_LEVL_PCI_D3);
		}

	}
	// Change RF power state.
	MgntActSet_RF_State(pAdapter, pNdisCommon->eRfPowerStateToSet, RF_CHANGE_BY_SW,FALSE);
	// Report the new power state to the OS.

	DrvIFIndicateCurrentPhyStatus(pAdapter);

	if(pNdisCommon->eRfPowerStateToSet == eRfOff)
	{
//		PlatformEFIOWrite1Byte(pAdapter, 0x03, 0x31);
		if(pPSC->RegRfPsLevel & RT_RF_OFF_LEVL_ASPM)
		{
			RT_ENABLE_ASPM(pAdapter);
			RT_SET_PS_LEVEL(pAdapter, RT_RF_OFF_LEVL_ASPM);
		}
		else if(pPSC->RegRfPsLevel & RT_RF_OFF_LEVL_PCI_D3)
		{
			RT_ENTER_D3(pAdapter, FALSE);
			RT_SET_PS_LEVEL(pAdapter, RT_RF_OFF_LEVL_PCI_D3);
		}

		// Turn off LED if RF is not ON.
		pAdapter->HalFunc.LedControlHandler(pAdapter, LED_CTL_POWER_OFF); 
	}

	//
	// SW radio off ext port must report the same pwr status as default port
	//
//	N62ExtPortIndicatePwrStatus(pAdapter);

	if( !OS_SUPPORT_WDI(pAdapter) )
	{
		// Complete pending OID.
		N6CompletePendedOID(
				pAdapter,
				RT_PENDED_OID_RF_STATE,
				NDIS_STATUS_SUCCESS);
	}
}


//
//	Description:
//		Callback function of SetAdhocLinkStateWorkItem, 
//		which is used by OID to change adhoc link state.
//		2009/02/25 MH Write Adhoc Link State
//
VOID
SetAdhocLinkStateWorkItemCallback(
	IN PVOID			pContext
	)
{
	PADAPTER pAdapter = (PADAPTER)pContext;
	PMGNT_INFO	pMgntInfo = &(pAdapter->MgntInfo);
	PRT_NDIS6_COMMON pNdisCommon = pAdapter->pNdisCommon;
	BOOLEAN				bValue;
	NDIS_STATUS			ndisStatus;
	HAL_DATA_TYPE 		*pHalData = GET_HAL_DATA(pAdapter);	

	// Write registry for persistent.
	bValue = (pMgntInfo->eSwRfPowerState == eRfOn) ? TRUE : FALSE;	

	ndisStatus = N6WriteAdhocLinkState(pAdapter, pHalData->AdhocLinkState);
		
	if (ndisStatus != NDIS_STATUS_SUCCESS)
	{
		RT_TRACE(COMP_MLME, DBG_LOUD, ("SetAdhocLinkStateWorkItemCallback(): failed to write registry.\n"));
	}

}


//
//	Description:
//		Initialize Rx return packet waiting mechanism.
//
//	Assumption:
//		1. AllPacketReturnedEvent is initialized.
//		2. RT_RX_SPINLOCK is NOT acquired.
//
VOID
N6CStartWaitReturnPacketMechanism(
	IN	PADAPTER		Adapter
	)
{
	//PlatformAcquireSpinLock(Adapter, RT_RX_SPINLOCK);

	if( !OS_SUPPORT_WDI(Adapter) )
	{
		PlatformAcquireSpinLock(Adapter, RT_RX_REF_CNT_SPINLOCK);
		RT_ASSERT((RT_GET_RCV_REF(Adapter) == 0), 
			("N6CStartWaitReturnPacketMechanism(): RefCnt(%d) != 0 !!!\n", 
			 RT_GET_RCV_REF(Adapter)));
		PlatformReleaseSpinLock(Adapter, RT_RX_REF_CNT_SPINLOCK);
	}
	//RT_INC_RCV_REF(Adapter);

	//PlatformReleaseSpinLock(Adapter, RT_RX_SPINLOCK);
}

//
//	Description:
//		Wait event for returned packet. 2007.01.19, by shien chang.
//
//	Assumption:
//		1. AllPacketReturnedEvent is initialized.
//		2. RT_RX_SPINLOCK is NOT acquired.
//		3. N6XXX_CANNOT_RX() is TRUE.
//
VOID
N6CWaitForReturnPacket(
	IN	PADAPTER		Adapter
	)
{
	PRT_NDIS6_COMMON	pNdisCommon = Adapter->pNdisCommon;
	
	PlatformAcquireSpinLock(Adapter, RT_RX_REF_CNT_SPINLOCK);
	if (RT_GET_RCV_REF(Adapter) > 0)
	{
		PlatformReleaseSpinLock(Adapter, RT_RX_REF_CNT_SPINLOCK);
		RT_TRACE(COMP_INIT, DBG_LOUD, ("N6CWaitForReturnPacket(): wait for un-returned packet (%d)\n", RT_GET_RCV_REF(Adapter)));

		//PlatformReleaseSpinLock(Adapter, RT_RX_SPINLOCK);

		while (TRUE)
		{
			RT_TRACE(COMP_INIT, DBG_LOUD, ("N6CWaitForReturnPacket(): wait for AllPacketReturnedEvent\n"));
			// Prefast warning C28121: The function 'NdisWaitEvent' is not permitted to be called at the current IRQ level.
			// Prefast warning C28156: The actual IRQL 2 is inconsistent with the required IRQL 0
			// False positive, irql should be restored by PlatformReleaseSpinLock.
#pragma warning( disable:28121 )
#pragma warning( disable:28156 )
			if ( NdisWaitEvent(&pNdisCommon->AllPacketReturnedEvent, 2000) )
			{
				NdisResetEvent(&pNdisCommon->AllPacketReturnedEvent);
				break;
			}
		}

		RT_TRACE(COMP_INIT, DBG_LOUD, ("N6CWaitForReturnPacket(): end of waiting returned packet\n"));
	}
	else	{
		PlatformReleaseSpinLock(Adapter, RT_RX_REF_CNT_SPINLOCK);		
	}
}


NDIS_STATUS
N6AllocateNative80211MIBs(
	IN	PADAPTER		Adapter
	)
{
	PRT_NDIS6_COMMON	pNdisCommon = Adapter->pNdisCommon;
	NDIS_STATUS		ndisStatus = NDIS_STATUS_SUCCESS;
	RT_STATUS		rtStatus;
	ULONG			AllocSize;

	do {
		// pDot11SupportedPhyTypes.
		AllocSize = sizeof(DOT11_SUPPORTED_PHY_TYPES) + (NATIVE_802_11_MAX_NUM_PHY_TYPES-1) * sizeof(DOT11_PHY_TYPE);
		rtStatus = PlatformAllocateMemory(
			Adapter,
			&(pNdisCommon->pDot11SupportedPhyTypes),
			AllocSize
			);
		if (rtStatus != RT_STATUS_SUCCESS)
		{
			RT_TRACE(COMP_INIT, DBG_SERIOUS, ("N6AllocateNative80211MIBs(): failed to allocate pDot11SupportedPhyTypes\n"));
			ndisStatus = NDIS_STATUS_FAILURE;
			break;
		}
		PlatformZeroMemory(pNdisCommon->pDot11SupportedPhyTypes, AllocSize);

		// pDot11PhyMIBs.
		AllocSize = sizeof(NIC_PHY_MIB) * NATIVE_802_11_MAX_NUM_PHY_TYPES;
		rtStatus = PlatformAllocateMemory(
			Adapter,
			&(pNdisCommon->pDot11PhyMIBs),
			AllocSize
			);
		if (rtStatus != RT_STATUS_SUCCESS)
		{
			RT_TRACE(COMP_INIT, DBG_SERIOUS, ("N6AllocateNative80211MIBs(): failed to allocate pDot11PhyMIBs\n"));
			ndisStatus = NDIS_STATUS_FAILURE;
			break;
		}

	}while (FALSE);

	//
	// If allocation failed, reclaim memory resources.
	//
	if (ndisStatus != NDIS_STATUS_SUCCESS)
	{
		if (pNdisCommon->pDot11SupportedPhyTypes)
		{
			PlatformFreeMemory(
				pNdisCommon->pDot11SupportedPhyTypes,
				sizeof(DOT11_SUPPORTED_PHY_TYPES) + (NATIVE_802_11_MAX_NUM_PHY_TYPES-1) * sizeof(DOT11_PHY_TYPE)
				);
			pNdisCommon->pDot11SupportedPhyTypes = NULL;
		}

		if (pNdisCommon->pDot11PhyMIBs)
		{
			PlatformFreeMemory(
				pNdisCommon->pDot11PhyMIBs,
				sizeof(NIC_PHY_MIB) * NATIVE_802_11_MAX_NUM_PHY_TYPES
				);
			pNdisCommon->pDot11PhyMIBs = NULL;
		}
	}
	
	return ndisStatus;
}


VOID
N6InitializeNative80211MIBs(
	IN 	PADAPTER 	Adapter
	)
{
	PRT_NDIS6_COMMON	pNdisCommon = Adapter->pNdisCommon;
	u1Byte	RateSetBuf[16]={0};
	u2Byte	RateSetLen=0;
	u4Byte	index=0, opRateIdx=0, BasicRateIdx=0;
	u4Byte 	curPhyId=0;
	u4Byte	WirelessModeCapa=0;
	HAL_DATA_TYPE *pHalData = GET_HAL_DATA(Adapter);
	PMGNT_INFO	pMgntInfo=&Adapter->MgntInfo;
	PRT_NLO_INFO	pNLOInfo = &(pMgntInfo->NLOInfo);

	// Set operation mode.
	pNdisCommon->dot11CurrentOperationMode.uCurrentOpMode = DOT11_OPERATION_MODE_EXTENSIBLE_STATION;
	pNdisCommon->dot11CurrentOperationMode.uReserved = 0;

	// dot11OperationModeCapability.
	pNdisCommon->dot11OperationModeCapability.uOpModeCapability = 
			DOT11_OPERATION_MODE_EXTENSIBLE_STATION |
			DOT11_OPERATION_MODE_NETWORK_MONITOR;

#if (NDIS_SUPPORT_NDIS620)
	if(pNdisCommon->NdisVersion > NDIS_VERSION_BASE_6_1)
	{
		Adapter->bStartVwifi = TRUE;

		if(!Adapter->pNdisCommon->bRegVWifiSupport)
		{
			Adapter->bStartVwifi = FALSE;
		}
		
		if(Adapter->bStartVwifi == TRUE)
		{
			pNdisCommon->dot11OperationModeCapability.uOpModeCapability |= 
				DOT11_OPERATION_MODE_EXTENSIBLE_AP;
		}
	}
#endif	

#if (NDIS_SUPPORT_NDIS630) 
		pNdisCommon->dot11OperationModeCapability.uOpModeCapability |= 
			DOT11_OPERATION_MODE_WFD_DEVICE| DOT11_OPERATION_MODE_WFD_GROUP_OWNER|DOT11_OPERATION_MODE_WFD_CLIENT;
#endif

#if FINE_TUNE_TH_8814A == 1
	pNdisCommon->dot11OperationModeCapability.uNumOfTXBuffers = 1024; // We can buffer as many packets as OS sends.
#else
	pNdisCommon->dot11OperationModeCapability.uNumOfTXBuffers = 0; // We can buffer as many packets as OS sends.
#endif
	pNdisCommon->dot11OperationModeCapability.uNumOfRXBuffers = Adapter->NumRxDesc[RX_MPDU_QUEUE];
	pNdisCommon->dot11OperationModeCapability.uMajorVersion = NATIVE_802_11_OPMODE_CAPABILITY_MAJOR_VERSION;
	pNdisCommon->dot11OperationModeCapability.uMinorVersion = NATIVE_802_11_OPMODE_CAPABILITY_MINOR_VERSION;
	pNdisCommon->dot11OperationModeCapability.uReserved = 0;

	//set  dot11MultiDomainCapabilityImplemented
	pNdisCommon->dot11MultiDomainCapabilityImplemented = MgntActQuery_MultiDomainImp(Adapter);

	// set support phy type
	pNdisCommon->pDot11SupportedPhyTypes->uTotalNumOfEntries = NATIVE_802_11_MAX_NUM_PHY_TYPES;
	pNdisCommon->pDot11SupportedPhyTypes->uNumOfEntries = 0; 


	WirelessModeCapa = HalGetSupportedWirelessMode(Adapter);
	
	if(WirelessModeCapa & WIRELESS_MODE_N_24G || (WirelessModeCapa & WIRELESS_MODE_AC_24G))
	{	
		//4 N mode		
		pNdisCommon->pDot11SupportedPhyTypes->uNumOfEntries ++; 
		curPhyId = pNdisCommon->pDot11SupportedPhyTypes->uNumOfEntries -1;
		pNdisCommon->pDot11SupportedPhyTypes->dot11PHYType[curPhyId] = dot11_phy_type_ht;
		// pDot11PhyMIBs.
		pNdisCommon->pDot11PhyMIBs[curPhyId].PhyID = curPhyId;
		pNdisCommon->pDot11PhyMIBs[curPhyId].PhyType = dot11_phy_type_ht; //6

		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.uRateSetLength = 16;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[0] = 30;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[1] = 60;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[2] = 90;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[3] = 120;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[4] = 180;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[5] = 240;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[6] = (UCHAR)270;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[7] = (UCHAR)300;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[8] = (UCHAR)60;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[9] = (UCHAR)120;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[10] = (UCHAR)180;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[11] = (UCHAR)240;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[12] = (UCHAR)360;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[13] = (UCHAR)480;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[14] = (UCHAR)540;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[15] = (UCHAR)600;

		pNdisCommon->pDot11PhyMIBs[curPhyId].ActiveRateSet.uRateSetLength = 0;
		pNdisCommon->pDot11PhyMIBs[curPhyId].Channel = MgntActQuery_802_11_CHANNEL_NUMBER(Adapter);

		PlatformZeroMemory(
			&(pNdisCommon->pDot11PhyMIBs[curPhyId].SupportedDataRatesValue),
			sizeof(DOT11_SUPPORTED_DATA_RATES_VALUE_V2));
		PlatformMoveMemory(
			pNdisCommon->pDot11PhyMIBs[curPhyId].SupportedDataRatesValue.ucSupportedTxDataRatesValue,
			pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet,
			pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.uRateSetLength);
		PlatformMoveMemory(
			pNdisCommon->pDot11PhyMIBs[curPhyId].SupportedDataRatesValue.ucSupportedRxDataRatesValue,
			pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet,
			pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.uRateSetLength);	//4 
	}

	if(WirelessModeCapa & WIRELESS_MODE_G)
	{
		//G mode		
		pNdisCommon->pDot11SupportedPhyTypes->uNumOfEntries ++; 
		curPhyId = pNdisCommon->pDot11SupportedPhyTypes->uNumOfEntries -1;
		pNdisCommon->pDot11SupportedPhyTypes->dot11PHYType[curPhyId] = dot11_phy_type_erp;
		// pDot11PhyMIBs.
		pNdisCommon->pDot11PhyMIBs[curPhyId].PhyID = curPhyId;
		pNdisCommon->pDot11PhyMIBs[curPhyId].PhyType = dot11_phy_type_erp; //6

		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.uRateSetLength = 12;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[0] = 2;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[1] = 4;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[2] = 11;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[3] = 22;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[4] = 12;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[5] = 18;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[6] = 24;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[7] = 36;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[8] = 48;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[9] = 72;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[10] = 96;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[11] = 108;

		pNdisCommon->pDot11PhyMIBs[curPhyId].ActiveRateSet.uRateSetLength = 0;
		pNdisCommon->pDot11PhyMIBs[curPhyId].Channel = 
			MgntActQuery_802_11_CHANNEL_NUMBER(Adapter);

	// <SC_TODO: basic rate and active rate ??>
		PlatformZeroMemory(
			&(pNdisCommon->pDot11PhyMIBs[curPhyId].SupportedDataRatesValue),
			sizeof(DOT11_SUPPORTED_DATA_RATES_VALUE_V2));
		PlatformMoveMemory(
			pNdisCommon->pDot11PhyMIBs[curPhyId].SupportedDataRatesValue.ucSupportedTxDataRatesValue,
			pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet,
			pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.uRateSetLength);
		PlatformMoveMemory(
			pNdisCommon->pDot11PhyMIBs[curPhyId].SupportedDataRatesValue.ucSupportedRxDataRatesValue,
			pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet,
			pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.uRateSetLength);
		
	}
	
	if(WirelessModeCapa & WIRELESS_MODE_B) 
	{//Bmode
		pNdisCommon->pDot11SupportedPhyTypes->uNumOfEntries++; 
		curPhyId = pNdisCommon->pDot11SupportedPhyTypes->uNumOfEntries -1;
		pNdisCommon->pDot11SupportedPhyTypes->dot11PHYType[curPhyId] = 
													dot11_phy_type_hrdsss;
		// pDot11PhyMIBs.
		pNdisCommon->pDot11PhyMIBs[curPhyId].PhyID = curPhyId;
		pNdisCommon->pDot11PhyMIBs[curPhyId].PhyType = dot11_phy_type_hrdsss; //5

		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.uRateSetLength = 4;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[0] = 2;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[1] = 4;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[2] = 11;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[3] = 22;

		pNdisCommon->pDot11PhyMIBs[curPhyId].BasicRateSet.uRateSetLength = 4;
		pNdisCommon->pDot11PhyMIBs[curPhyId].BasicRateSet.ucRateSet[0] = 2;
		pNdisCommon->pDot11PhyMIBs[curPhyId].BasicRateSet.ucRateSet[1] = 4;
		pNdisCommon->pDot11PhyMIBs[curPhyId].BasicRateSet.ucRateSet[2] = 11;
		pNdisCommon->pDot11PhyMIBs[curPhyId].BasicRateSet.ucRateSet[3] = 22;
		
		pNdisCommon->pDot11PhyMIBs[curPhyId].ActiveRateSet.uRateSetLength = 0;
		pNdisCommon->pDot11PhyMIBs[curPhyId].Channel = 
			MgntActQuery_802_11_CHANNEL_NUMBER(Adapter);
		
		PlatformZeroMemory(
			&(pNdisCommon->pDot11PhyMIBs[curPhyId].SupportedDataRatesValue),
			sizeof(DOT11_SUPPORTED_DATA_RATES_VALUE_V2));
		PlatformMoveMemory(
			pNdisCommon->pDot11PhyMIBs[curPhyId].SupportedDataRatesValue.ucSupportedTxDataRatesValue,
			pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet,
			pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.uRateSetLength);
		PlatformMoveMemory(
			pNdisCommon->pDot11PhyMIBs[curPhyId].SupportedDataRatesValue.ucSupportedRxDataRatesValue,
			pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet,
			pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.uRateSetLength);
	
	}
	
	RT_TRACE_F(COMP_INIT, DBG_LOUD, ("WirelessModeCapa 0x%x pNdisCommon->NdisVersion 0x%x\n", WirelessModeCapa, pNdisCommon->NdisVersion));

	if((WirelessModeCapa & WIRELESS_MODE_AC_5G) &&
		pNdisCommon->NdisVersion>=NDIS_VERSION_BASE_6_40)
	{	
		pNdisCommon->pDot11SupportedPhyTypes->uNumOfEntries ++; 
		curPhyId = pNdisCommon->pDot11SupportedPhyTypes->uNumOfEntries - 1;
		pNdisCommon->pDot11SupportedPhyTypes->dot11PHYType[curPhyId] = dot11_phy_type_vht;
	
		pNdisCommon->pDot11PhyMIBs[curPhyId].PhyID = curPhyId;
		pNdisCommon->pDot11PhyMIBs[curPhyId].PhyType = dot11_phy_type_vht;
	
	
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.uRateSetLength = 16;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[0] = 30;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[1] = 60;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[2] = 90;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[3] = 120;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[4] = 180;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[5] = 240;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[6] = (UCHAR)270;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[7] = (UCHAR)300;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[8] = (UCHAR)60;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[9] = (UCHAR)120;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[10] = (UCHAR)180;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[11] = (UCHAR)240;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[12] = (UCHAR)360;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[13] = (UCHAR)480;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[14] = (UCHAR)540;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[15] = (UCHAR)600;

		pNdisCommon->pDot11PhyMIBs[curPhyId].ActiveRateSet.uRateSetLength = 0;
		pNdisCommon->pDot11PhyMIBs[curPhyId].Channel = MgntActQuery_802_11_CHANNEL_NUMBER(Adapter);
	
		PlatformZeroMemory(
			&(pNdisCommon->pDot11PhyMIBs[curPhyId].SupportedDataRatesValue),
			sizeof(DOT11_SUPPORTED_DATA_RATES_VALUE_V2));
		PlatformMoveMemory(
			pNdisCommon->pDot11PhyMIBs[curPhyId].SupportedDataRatesValue.ucSupportedTxDataRatesValue,
			pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet,
			pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.uRateSetLength);
		PlatformMoveMemory(
			pNdisCommon->pDot11PhyMIBs[curPhyId].SupportedDataRatesValue.ucSupportedRxDataRatesValue,
			pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet,
			pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.uRateSetLength);	//4 
	}

	if((WirelessModeCapa & WIRELESS_MODE_N_5G) || (WirelessModeCapa & WIRELESS_MODE_AC_5G))
	{	
		//4 N 5g mode		
		pNdisCommon->pDot11SupportedPhyTypes->uNumOfEntries ++; 
		curPhyId = pNdisCommon->pDot11SupportedPhyTypes->uNumOfEntries -1;
		pNdisCommon->pDot11SupportedPhyTypes->dot11PHYType[curPhyId] = dot11_phy_type_ht;
	// pDot11PhyMIBs.
		pNdisCommon->pDot11PhyMIBs[curPhyId].PhyID = curPhyId;
		pNdisCommon->pDot11PhyMIBs[curPhyId].PhyType = dot11_phy_type_ht; //6


		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.uRateSetLength = 16;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[0] = 30;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[1] = 60;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[2] = 90;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[3] = 120;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[4] = 180;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[5] = 240;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[6] = (UCHAR)270;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[7] = (UCHAR)300;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[8] = (UCHAR)60;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[9] = (UCHAR)120;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[10] = (UCHAR)180;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[11] = (UCHAR)240;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[12] = (UCHAR)360;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[13] = (UCHAR)480;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[14] = (UCHAR)540;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[15] = (UCHAR)600;

		pNdisCommon->pDot11PhyMIBs[curPhyId].ActiveRateSet.uRateSetLength = 0;
		pNdisCommon->pDot11PhyMIBs[curPhyId].Channel = MgntActQuery_802_11_CHANNEL_NUMBER(Adapter);

		PlatformZeroMemory(
			&(pNdisCommon->pDot11PhyMIBs[curPhyId].SupportedDataRatesValue),
			sizeof(DOT11_SUPPORTED_DATA_RATES_VALUE_V2));
		PlatformMoveMemory(
			pNdisCommon->pDot11PhyMIBs[curPhyId].SupportedDataRatesValue.ucSupportedTxDataRatesValue,
			pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet,
			pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.uRateSetLength);
		PlatformMoveMemory(
			pNdisCommon->pDot11PhyMIBs[curPhyId].SupportedDataRatesValue.ucSupportedRxDataRatesValue,
			pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet,
			pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.uRateSetLength);	//4 
	}

	if(WirelessModeCapa& WIRELESS_MODE_A)
	{
		//A mode		
		pNdisCommon->pDot11SupportedPhyTypes->uNumOfEntries ++; 
		curPhyId = pNdisCommon->pDot11SupportedPhyTypes->uNumOfEntries -1;
		pNdisCommon->pDot11SupportedPhyTypes->dot11PHYType[curPhyId] = dot11_phy_type_ofdm;
		// pDot11PhyMIBs.
		pNdisCommon->pDot11PhyMIBs[curPhyId].PhyID = curPhyId;
		pNdisCommon->pDot11PhyMIBs[curPhyId].PhyType = dot11_phy_type_ofdm; //4// 4


		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.uRateSetLength = 8;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[0] = 12;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[1] = 18;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[2] = 24;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[3] = 36;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[4] = 48;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[5] = 72;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[6] = 96;
		pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet[7] = 108;

		pNdisCommon->pDot11PhyMIBs[curPhyId].ActiveRateSet.uRateSetLength = 0;
		pNdisCommon->pDot11PhyMIBs[curPhyId].Channel = 
			MgntActQuery_802_11_CHANNEL_NUMBER(Adapter);

		PlatformZeroMemory(
			&(pNdisCommon->pDot11PhyMIBs[curPhyId].SupportedDataRatesValue),
			sizeof(DOT11_SUPPORTED_DATA_RATES_VALUE_V2));
		PlatformMoveMemory(
			pNdisCommon->pDot11PhyMIBs[curPhyId].SupportedDataRatesValue.ucSupportedTxDataRatesValue,
			pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet,
			pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.uRateSetLength);
		PlatformMoveMemory(
			pNdisCommon->pDot11PhyMIBs[curPhyId].SupportedDataRatesValue.ucSupportedRxDataRatesValue,
			pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.ucRateSet,
			pNdisCommon->pDot11PhyMIBs[curPhyId].OperationalRateSet.uRateSetLength);
	}
	
	
	pNdisCommon->dot11SelectedPhyId = 0;
	pNdisCommon->pDot11SelectedPhyMIB = pNdisCommon->pDot11PhyMIBs;

	// dot11SupportedPowerLevels.
	pNdisCommon->dot11SupportedPowerLevels.uTxPowerLevelValues[0] = 10;	//?? NEO tEST 123
	pNdisCommon->dot11SupportedPowerLevels.uTxPowerLevelValues[1] = 20;
	pNdisCommon->dot11SupportedPowerLevels.uTxPowerLevelValues[2] = 30;
	pNdisCommon->dot11SupportedPowerLevels.uTxPowerLevelValues[3] = 50;
	pNdisCommon->dot11SupportedPowerLevels.uNumOfSupportedPowerLevels = 4;

	// dot11CurrentTxPowerLevel.
	pNdisCommon->dot11CurrentTxPowerLevel = 1;

	//
	// Reset desired PHY ID list
	//
	pNdisCommon->staDesiredPhyCount = 1;
	pNdisCommon->staDesiredPhyList[0] = DOT11_PHY_ID_ANY;

	//
	// PCF and service class.
	//
	pNdisCommon->RegOptionalCapability.uReserved = 0;
	if(MgntActQuery_CfPollable(Adapter))
	{
		pNdisCommon->RegOptionalCapability.bDot11PCF = FALSE;
		pNdisCommon->RegOptionalCapability.bDot11PCFMPDUTransferToPC = FALSE;
	}
	else
	{
		pNdisCommon->RegOptionalCapability.bDot11PCF = FALSE;
		pNdisCommon->RegOptionalCapability.bDot11PCFMPDUTransferToPC = FALSE;
	}
	pNdisCommon->RegOptionalCapability.bStrictlyOrderedServiceClass = MgntActQuery_StrictlyOrderedImp(Adapter);

	PlatformMoveMemory(
		&(pNdisCommon->dot11CurrOptionalCapability), 
		&(pNdisCommon->RegOptionalCapability), 
		sizeof(DOT11_OPTIONAL_CAPABILITY));

	// bDot11MediaStreamingEnabled.
	pNdisCommon->bDot11MediaStreamingEnabled = FALSE;

	// dot11DesiredSSIDList.
	N6_ASSIGN_OBJECT_HEADER(
		pNdisCommon->dot11DesiredSSIDList.Header,
		NDIS_OBJECT_TYPE_DEFAULT,
		DOT11_SSID_LIST_REVISION_1,
		sizeof(DOT11_SSID_LIST));
	pNdisCommon->dot11DesiredSSIDList.uNumOfEntries = 0;
	pNdisCommon->dot11DesiredSSIDList.uTotalNumOfEntries = 0;
	pNdisCommon->dot11DesiredSSIDListIndex = 0;

	// dot11DesiredSSIDList.
	N6_ASSIGN_OBJECT_HEADER(
		pNdisCommon->dot11DesiredSSIDListCopy.Header,
		NDIS_OBJECT_TYPE_DEFAULT,
		DOT11_SSID_LIST_REVISION_1,
		sizeof(DOT11_SSID_LIST));
	pNdisCommon->dot11DesiredSSIDListCopy.uNumOfEntries = 0;
	pNdisCommon->dot11DesiredSSIDListCopy.uTotalNumOfEntries = 0;	

	//Ndis6 IBSS Parameters. For DTM Vista 1.0c, by Bruce, 2007-08-13.
	pNdisCommon->dot11IbssParams.bDot11IbssJoinOnly = FALSE;
	if(pNdisCommon->dot11IbssParams.AdditionalIEData)
	{
		PlatformFreeMemory(pNdisCommon->dot11IbssParams.AdditionalIEData, pNdisCommon->dot11IbssParams.AdditionalIESize);
	}
	pNdisCommon->dot11IbssParams.AdditionalIEData = NULL;
	pNdisCommon->dot11IbssParams.AdditionalIESize = 0;

	
	// dot11AutoConfigEnabled.
	pNdisCommon->dot11AutoConfigEnabled = 
		DOT11_PHY_AUTO_CONFIG_ENABLED_FLAG |
		DOT11_MAC_AUTO_CONFIG_ENABLED_FLAG;

	// dot11DesiredBSSIDList and NumDot11DesiredBSSIDList.
	N6_MAKE_WILDCARD_MAC_ADDRESS(pNdisCommon->dot11DesiredBSSIDList);
	pNdisCommon->NumDot11DesiredBSSIDList = 1;

	// dot11UnreachableDetectionThreshold.
	// <SC_TODO: revisit it>
	pNdisCommon->dot11UnreachableDetectionThreshold = 10;

	// dot11UnicastUseGroupEnabled.
	pNdisCommon->dot11UnicastUseGroupEnabled = TRUE;

	// dot11HiddenNetworkEnabled.
	pNdisCommon->dot11HiddenNetworkEnabled = FALSE;

	// dot11NetworkListOffload
	pNLOInfo->NumDot11OffloadNetwork = 0;
	pNdisCommon->Oidcounter = 0;

	// New indication engine. 2006.11.29, by shien chang.
	N6InitializeIndicateStateMachine(Adapter);

	// bWakeupAutoLinkInProgressing
	pNdisCommon->bWakeupAutoLinkInProgressing = FALSE;

	// PrivacyExemptionList
	Adapter->pNdisCommon->PrivacyExemptionEntrieNum = 0;	
}


//
//	Description:
//		Add NBL list into NBL wait queue.
//		
//	Assumption:
//		RT_BUFFER_SPINLOCK is acquired.
//
VOID
N6CAddNblWaitQueue(
	IN PRT_SINGLE_LIST_HEAD		pNBLWaitQueue,
	IN PNET_BUFFER_LIST		pNetBufferLists,
	IN BOOLEAN				bToHead
	)
{
	PRT_SINGLE_LIST_HEAD pList = pNBLWaitQueue;
	PRT_SINGLE_LIST_ENTRY pFirstEntry = (PRT_SINGLE_LIST_ENTRY)RT_GET_NBL_LINK(pNetBufferLists);
	PRT_SINGLE_LIST_ENTRY pLastEntry;

	RT_ASSERT(pFirstEntry != NULL, 
		("N6CAddNblWaitQueue(): pFirstEntry should not be NULL, pNetBufferLists(%p)\n !!!", pNetBufferLists));

	for(pLastEntry = pFirstEntry; pLastEntry->Next != NULL; pLastEntry = pLastEntry->Next)
		;

	if(bToHead)
	{
		if(!RTIsSListEmpty(pList))
		{
			pLastEntry->Next = pList->First;
			pList->First = pFirstEntry;
		}
		else
		{
			pLastEntry->Next = pList->First;
			pList->First = pFirstEntry;
			pList->Last = pLastEntry;
		}
	}
	else
	{
		if(!RTIsSListEmpty(pList))
		{
			pList->Last->Next = pFirstEntry; 
			pList->Last = pLastEntry;
		}
		else
		{
			pList->Last = pLastEntry;
			pList->First = pFirstEntry;
		}
	}
}

VOID
N6CConcatenateTwoList(
	IN PRT_SINGLE_LIST_ENTRY		pDest,
	IN PRT_SINGLE_LIST_ENTRY		pSource
	)
{
	PRT_SINGLE_LIST_ENTRY pDstLastEntry = NULL;
	PRT_SINGLE_LIST_ENTRY pSrcLastEntry = NULL;

	if( pDest == NULL )
		return;

	if( pSource == NULL )
		return;
	
	for(pDstLastEntry = pDest; pDstLastEntry->Next != NULL; pDstLastEntry = pDstLastEntry->Next)
		;
	
	pDstLastEntry->Next = pSource; 
}

//
//	Description:
//		Remove one NBL from head of list.
//		
//	Assumption:
//		RT_BUFFER_SPINLOCK is acquired.
//
PNET_BUFFER_LIST
N6CRemoveNblWaitQueue(
	IN PRT_SINGLE_LIST_HEAD		pNBLWaitQueue,
	IN BOOLEAN					bFromHead	
	)
{
	PRT_SINGLE_LIST_HEAD pList = pNBLWaitQueue;
	PRT_SINGLE_LIST_ENTRY pFirstEntry = NULL,	pLastEntry = NULL, pTempEntry = NULL;

	if( RTIsSListEmpty(pList) )
	{
		RT_ASSERT(FALSE, ("N6CRemvoeNblWaitQueue(): pList(%p) is empty!!!\n", pList));
		return NULL;
	}

	if(bFromHead)
	{
		PRT_SINGLE_LIST_ENTRY pEntry;

		pEntry = RTRemoveHeadSList(pList);
		return RT_GET_NBL_FROM_QUEUE_LINK(pEntry);
	}
	else
	{
		pFirstEntry = pList->First;

		for(pLastEntry = pFirstEntry; pLastEntry->Next != NULL; pLastEntry = pLastEntry->Next)
		{
			pTempEntry = pLastEntry->Next;
			if( pTempEntry->Next != NULL )
			{
				continue;
			}
			else
			{
				pLastEntry->Next = NULL;
				return RT_GET_NBL_FROM_QUEUE_LINK(pTempEntry);
			}
		}

		RTInitializeSListHead(pList);
		
		return RT_GET_NBL_FROM_QUEUE_LINK(pFirstEntry);
	}
}


//
//	Description:
//		This routine is called if miniport need to complete the NetBufferLists 
//		before associated with a TCB.
//
//	Assumption:
//		1. RT_TX_SPINLOCK is NOT acquired.
//
VOID
N6CompleteNetBufferLists(
	IN	PADAPTER		Adapter,
	IN	PNET_BUFFER_LIST	pNetBufferLists,
	IN	NDIS_STATUS			ndisStatus,
	IN	BOOLEAN				bDispatchLevel
	)
{
	PNDIS_ADAPTER_TYPE		pDevice = GET_NDIS_ADAPTER(Adapter);
	PNET_BUFFER_LIST		pCurrNetBufferList, pNextNetBufferList;

	for (pCurrNetBufferList = pNetBufferLists;
		pCurrNetBufferList != NULL;
		pCurrNetBufferList = pNextNetBufferList)
	{
		pNextNetBufferList = NET_BUFFER_LIST_NEXT_NBL(pCurrNetBufferList);
		NET_BUFFER_LIST_STATUS(pCurrNetBufferList) = ndisStatus;
	}


	NdisMSendNetBufferListsComplete(
		pDevice->hNdisAdapter, 
		pNetBufferLists, 
		(bDispatchLevel ? NDIS_SEND_COMPLETE_FLAGS_DISPATCH_LEVEL : 0));
	
}


//
// Description:
// 	Indicate the current link (Tx/Rx) speed to Ndis6.
// Note:
//	Only call this function when the link status is associated.
// By Bruce, 2007-08-30.
//
VOID
N6IndicateLinkSpeed(
	PVOID			Adapter
	)
{
	PADAPTER			pAdapter = (PADAPTER)Adapter;
	PMGNT_INFO			pMgntInfo = &(pAdapter->MgntInfo);
	NDIS_LINK_STATE		LinkState;

	N6_ASSIGN_OBJECT_HEADER(
		LinkState.Header,
		NDIS_OBJECT_TYPE_DEFAULT,
		NDIS_LINK_STATE_REVISION_1,
		sizeof(NDIS_LINK_STATE));
	
	LinkState.MediaConnectState = MediaConnectStateConnected;

/*
MediaDuplexStateHalf 
	The miniport adapter can transmit or receive but not both simultaneously. 
MediaDuplexStateFull 
	The miniport adapter can transmit and receive simultaneously. 
MediaDuplexStateUnknown 
	The duplex state of the miniport adapter is unknown. 
*/	
	LinkState.MediaDuplexState = MediaDuplexStateHalf;

	LinkState.RcvLinkSpeed = 300000000;
	LinkState.XmitLinkSpeed = (MgntActQuery_RT_11N_USER_SHOW_RATES(pAdapter , pMgntInfo->bForcedShowRxRate, FALSE)/2)*1000000;

	if(P2P_ADAPTER_OS_SUPPORT_P2P(pAdapter) && LinkState.XmitLinkSpeed < INTEL_HIGH_RESOLUTION_MIN_XMIT_SPEED)
	{
		// Note:
		//	Changing the transmit speed to upscale the Intel Miracast Graphics resolution 1920x1080.
		//	This is not a good solution and we will check this with Intel.
		//	Windows would not show the rate for P2P port on Windows UI.
		//	By Bruce, 2014-09-11.
		LinkState.XmitLinkSpeed = INTEL_HIGH_RESOLUTION_MIN_XMIT_SPEED;
	}
	RT_TRACE_F(COMP_INDIC, DBG_LOUD, ("XmitSpeed = %d RcvLinkSpeed = %d for adapter port = %d\n", (u4Byte)LinkState.XmitLinkSpeed, (u4Byte)LinkState.RcvLinkSpeed, GET_PORT_NUMBER(pAdapter)));
	LinkState.PauseFunctions = NdisPauseFunctionsUnsupported;
	LinkState.AutoNegotiationFlags = NDIS_LINK_STATE_DUPLEX_AUTO_NEGOTIATED;
	
	N6IndicateStatus(
					pAdapter,
					NDIS_STATUS_LINK_STATE,
					&LinkState,
					sizeof(NDIS_LINK_STATE));
	
}


//
// Description:
//	Decide whether the BSSID of AP or peer address is agreed by desired list.
//	2006.11.14, by shien chang.
// 	Because it is Platform based function, I move it from "IsInDesiredBSSIDList in N6C_OidSet.c" to here.
// Argument:
//	Adapter -
//		NIC adapter context pointer.
//	MacAddr -
//		The mac address to be verified if it is in the desired list.
// Rewritten by Bruce, 2008-05-27.
//
BOOLEAN
PlatformIsInDesiredBSSIDList(
	IN	PADAPTER		Adapter,
	IN	pu1Byte 		MacAddr
	)
{
	PRT_NDIS6_COMMON	pNdisCommon = Adapter->pNdisCommon;
	u4Byte				index;

	if (pNdisCommon->NumDot11DesiredBSSIDList == 0)
	{
		RT_TRACE(COMP_MLME, DBG_LOUD, ("IsInDesiredBSSIDList(): NumDot11DesiredBSSIDList is zero\n"));
		return FALSE;
	}

	for (index=0; index<pNdisCommon->NumDot11DesiredBSSIDList; index++)
	{
		if ( N6Dot11AddrIsBcast( pNdisCommon->dot11DesiredBSSIDList[index] ))
		{
			return TRUE;
		}

		if ( eqNByte(
			(pu1Byte)(pNdisCommon->dot11DesiredBSSIDList[index]),
			MacAddr,
			sizeof(DOT11_MAC_ADDRESS)) )
		{
			return TRUE;
		}
	}

	return FALSE;
}


BOOLEAN
PlatformGetDesiredSSIDList(
	IN	PADAPTER		Adapter
	)
{
	PRT_NDIS6_COMMON	pNdisCommon = Adapter->pNdisCommon;
	u4Byte				index = pNdisCommon->dot11DesiredSSIDListIndex;
	PDOT11_SSID_LIST	pSsidList = &(pNdisCommon->dot11DesiredSSIDList);
	PMGNT_INFO			pMgntInfo=&Adapter->MgntInfo;

	if (pSsidList->uNumOfEntries == 0 ||
		(u1Byte)pSsidList->uNumOfEntries <= pNdisCommon->dot11DesiredSSIDListIndex)
	{
		RT_TRACE(COMP_MLME, DBG_LOUD, ("PlatformCheckDesiredSSIDList(): uNumOfEntries %d dot11DesiredSSIDListIndex %d\n", pNdisCommon->dot11DesiredSSIDList.uNumOfEntries, pNdisCommon->dot11DesiredSSIDListIndex));
		return FALSE;
	}

	if(CompareSSID(pMgntInfo->Ssid.Octet, pMgntInfo->Ssid.Length, pSsidList->SSIDs[index].ucSSID, (u2Byte)pSsidList->SSIDs[index].uSSIDLength))
		pNdisCommon->dot11DesiredSSIDListIndex++;

	if((u1Byte)pSsidList->uNumOfEntries <= pNdisCommon->dot11DesiredSSIDListIndex)
		return FALSE;

	index = pNdisCommon->dot11DesiredSSIDListIndex;
	
	CopyMem(pMgntInfo->Ssid.Octet, pSsidList->SSIDs[index].ucSSID, pSsidList->SSIDs[index].uSSIDLength);
	pMgntInfo->Ssid.Length =(u2Byte) pSsidList->SSIDs[index].uSSIDLength;	

	RT_PRINT_STR(COMP_MLME, DBG_LOUD, "===> PlatformGetDesiredSSIDList(): ", pMgntInfo->Ssid.Octet, pMgntInfo->Ssid.Length);

	pNdisCommon->dot11DesiredSSIDListIndex++;

	return TRUE;
}


//
// Descriotion:
//	Determine the conditions under Vista if the packets should be filtered and not to be indicated to Ndis 6.
// Adapter -
//	NIC adapter context.
// pRfd -
//	RFD buffer.
// Return -
//	TRUE if this packet should not be filtered. Or return FALSE.
// From 818xB, by Bruce, 2008-06-20.
//
BOOLEAN
N6ReceiveIndicateFilter(
	IN	PADAPTER	Adapter,
	IN	PRT_RFD		pRfd
	)
{
	PRT_NDIS6_COMMON	pNdisCommon = Adapter->pNdisCommon;
	PMGNT_INFO			pMgntInfo = &(Adapter->MgntInfo);
	OCTET_STRING		pduOS;
	u4Byte				i;
	pu1Byte 			pRaddr;
	pu1Byte				pBssid;
	BOOLEAN				bAddrMatch = FALSE;
	BOOLEAN				bIsBroadcast = FALSE;
	BOOLEAN				bIsMulticast = FALSE;

	
	FillOctetString(pduOS, pRfd->Buffer.VirtualAddress + pRfd->FragOffset, pRfd->PacketLength);
	pRaddr = Frame_pRaddr(pduOS);
	pBssid = Frame_pBssid(pduOS);
	bAddrMatch = (PlatformCompareMemory(Adapter->CurrentAddress, pRaddr, ETHERNET_ADDRESS_LENGTH)) ? FALSE : TRUE;
	bIsBroadcast = (MacAddr_isBcst(pRaddr));
	bIsMulticast = (MacAddr_isMulticast(pRaddr));

	// Always indicate all packets in bNetMonitorMode.
	if(Adapter->MgntInfo.bNetMonitorMode)
	{
		return TRUE;
	}

	if(N6MatchPrivacyExemptList(Adapter, pRfd))
	{		
		return FALSE; 
	}

	// 
	// For DTM 1.2 Packetfilter_ext 
	// to filter data packet when CURRENT_PACKET_FILTER is set
	// 
	// (1) These standard packet filters are only applicable to 802.11 data packets
	//
	// (2) If NdisPacketFilter is not set (remains zero)
	//		 do not indicate any packets
	//
	// (3) For broadcast packet only indicate when
	//		 a) NDIS_PACKET_TYPE_BROADCAST is set in current packet filter
	//
	// (4) For multicast packet only indicate when
	//		 a) NDIS_PACKET_TYPE_ALL_MULTICAST is set in current packet filter or
	//		 b) NDIS_PACKET_TYPE_MULTICASTis set in current packet filter, and
	//			 DA belongs to one of the multicast address (set from OID_DOT11_MULTICAST_LIST)
	// 
	// Add by hpfan 2008.05.12
	//
	// For 802.11 specified filters respectively, by Bruce, 2008-06-26.
	//
	if(IsMgntFrame(pduOS.Octet))
	{
		if(bAddrMatch && (pNdisCommon->NdisPacketFilter & NDIS_PACKET_TYPE_802_11_DIRECTED_MGMT))
			return TRUE;
		if(bIsBroadcast && (pNdisCommon->NdisPacketFilter & NDIS_PACKET_TYPE_802_11_BROADCAST_MGMT))
			return TRUE;
		if(bIsMulticast && (pNdisCommon->NdisPacketFilter & (NDIS_PACKET_TYPE_802_11_MULTICAST_MGMT | 
								NDIS_PACKET_TYPE_802_11_ALL_MULTICAST_MGMT)))
			return TRUE;

		return FALSE;
	}
	else if(IsCtrlFrame(pduOS.Octet))
	{
		//
		// We do not implement yet, by Bruce, 2008-06-26.
		//
		return FALSE;
	}
	else if(IsDataFrame(pduOS.Octet))
	{
		//
		// Note:
		//	It is only valid for the miniport driver to enable the NDIS_PACKET_TYPE_PROMISCUOUS,
		//	NDIS_PACKET_TYPE_802_11_PROMISCUOUS_MGMT, or 
		//	NDIS_PACKET_TYPE_802_11_PROMISCUOUS_CTRL packet filters if the driver is 
		//	operating in Network Monitor (NetMon) mode.
		//
		if(bAddrMatch && (pNdisCommon->NdisPacketFilter & NDIS_PACKET_TYPE_DIRECTED))
			return TRUE;
		if(bIsBroadcast && (pNdisCommon->NdisPacketFilter & NDIS_PACKET_TYPE_BROADCAST))
            return TRUE;
		if(bIsMulticast && (pNdisCommon->NdisPacketFilter & NDIS_PACKET_TYPE_ALL_MULTICAST))
            return TRUE;

		// Do not filter data frame in IBSS mode.
		if(pMgntInfo->mIbss && !(PlatformCompareMemory(pMgntInfo->Bssid, pBssid, 6)))
		{
			return TRUE;	
		}
		if(bIsMulticast && (pNdisCommon->NdisPacketFilter & NDIS_PACKET_TYPE_MULTICAST))
		{
			if(Adapter->MCAddrCount == 0)
			{
				RT_PRINT_ADDR(COMP_RECV | COMP_MLME, DBG_LOUD, "N6ReceiveIndicateFilter(): filter multicast (MCAddrCount = 0) addr ", pRaddr);
				return FALSE;
			}
			for(i = 0; i < Adapter->MCAddrCount; i ++ )
			{
				if(i < MAX_MCAST_LIST_NUM)
				{
					if(!PlatformCompareMemory(Adapter->MCList[i], pRaddr, ETHERNET_ADDRESS_LENGTH) )
					{
						return TRUE;
					}
				}
			}
		}
		return FALSE;
	}
	RT_PRINT_ADDR(COMP_RECV | COMP_MLME, DBG_LOUD, "N6ReceiveIndicateFilter(): Filter this multicast not in MCL: ", pRaddr);
	return FALSE;
}

//
// Descriotion:
//	Try to Fulshing D0 Coalesing Queue !!
// Adapter -
//	NIC adapter context.
// bAcqRxLock -
//	Need to Acquire Rx SpinLock or NO
// bIndic - 
//	Drop or Indic 
//

VOID
N6FulshD0CoalescingQueue(
	IN	PADAPTER	Adapter,
	IN	BOOLEAN		bAcqRxLock,
	IN	BOOLEAN					bIndic
	)
{
	PRT_NDIS6_COMMON		pNdisCommon = Adapter->pNdisCommon;
	PRT_RFD					*RfdArray;
	u2Byte					RfdCnt = 0;
	PRT_GEN_TEMP_BUFFER 	pGenBuf;
	PRT_RFD					TempRfd;

	RT_TRACE(COMP_TEST, DBG_LOUD, ("===> N6FulshD0CoalescingQueue() \n"));

	if(bAcqRxLock)
			PlatformAcquireSpinLock(Adapter, RT_RX_SPINLOCK);

	if( 	pNdisCommon->D0FilterState == RT_D0_FILTER_NONE ||  
		pNdisCommon->D0FilterState == RT_D0_FILTER_FLUSHING)
	{
		if(bAcqRxLock)
			PlatformReleaseSpinLock( Adapter, RT_RX_SPINLOCK);
		return;
	}

	if( RTIsListEmpty(&(pNdisCommon->D0FilterPktQueue)) )
	{
		RT_TRACE(COMP_TEST, DBG_LOUD, ("<=== N6FulshD0CoalescingQueue() : Flush NULL\n"));
		if(bAcqRxLock)
			PlatformReleaseSpinLock( Adapter, RT_RX_SPINLOCK);
		return;
	}

	if( bIndic )
	{
		RT_TRACE(COMP_TEST, DBG_LOUD, ("===> N6FulshD0CoalescingQueue() IndicatePackets !!\n"));
		if( !RTIsListEmpty(&(pNdisCommon->D0FilterPktQueue)) )
		{
			pNdisCommon->D0FilterState = RT_D0_FILTER_FLUSHING;
			RfdCnt = 0;
			pGenBuf = GetGenTempBuffer (Adapter, sizeof(PRT_RFD)*REORDER_WIN_SIZE);
			RfdArray = (PRT_RFD *)pGenBuf->Buffer.Ptr;
			
			while(!RTIsListEmpty(&pNdisCommon->D0FilterPktQueue))
			{
				TempRfd = (PRT_RFD)RTRemoveHeadList(&pNdisCommon->D0FilterPktQueue);
				RfdArray[RfdCnt] = TempRfd;
				RfdCnt = RfdCnt + 1;
			}
			
			DrvIFD0RxIndicatePackets(Adapter , RfdArray , RfdCnt);		
			ReturnGenTempBuffer(Adapter, pGenBuf);
		}
	
	}
	else
	{
		RT_TRACE(COMP_TEST, DBG_LOUD, ("===> N6FulshD0CoalescingQueue() ReturnRFDList !!\n"));
		if( !RTIsListEmpty(&(pNdisCommon->D0FilterPktQueue)) )
		{
			pNdisCommon->D0FilterState = RT_D0_FILTER_FLUSHING;

			while(!RTIsListEmpty(&pNdisCommon->D0FilterPktQueue))
			{
				TempRfd = (PRT_RFD)RTRemoveHeadList(&pNdisCommon->D0FilterPktQueue);
				ReturnRFDList(Adapter , TempRfd);
			}
		}
		
	}
	RT_TRACE(COMP_TEST, DBG_LOUD, ("<=== N6FulshD0CoalescingQueue() : Flush OK\n"));
	pNdisCommon->D0FilterState = RT_D0_FILTER_INIT;
	if(bAcqRxLock)
		PlatformReleaseSpinLock( Adapter, RT_RX_SPINLOCK);
	
}

//
// Descriotion:
//	Determine the packet to queue or let it go !!
// Adapter -
//	NIC adapter context.
// pRfd -
//	RFD buffer.
// Return 
//	True : be queue 
//	False: Not match !! 

BOOLEAN
N6ReceiveCoalescingFilter(
	IN	PADAPTER	Adapter,
	IN	PRT_RFD					pRfd
	)
{
	PRT_NDIS6_COMMON			pNdisCommon = Adapter->pNdisCommon;
	BOOLEAN						bBufferPkt = FALSE;
	PMGNT_INFO 					pMgntInfo = &Adapter->MgntInfo;
	u1Byte						ColseParIndex = 0;
	u4Byte						ColseFieldIndex = 0;
	BOOLEAN						bFiledMatch = TRUE;
	PRT_DO_COALESICING_FILTER_PARAMETER		pCurrFilterPar =NULL;
	PRT_DO_COALESICING_FIELD_INFO			pCurrFieldInfo = NULL;
	PRX_FILTER_INFO					pCurrRfdFilteInfo =NULL;
	PRT_DO_COALESICING_FILTER_PARAMETER		pMatchFilterPar =NULL;
	u4Byte						msDelay = 500;

//	OCTET_STRING								frame;
//	FillOctetString(frame, pRfd->Buffer.VirtualAddress+PLATFORM_GET_FRAGOFFSET(pRfd), pRfd->PacketLength);

	

	if(pRfd->nTotalSubframe > 0 )
		return bBufferPkt;

	if( pNdisCommon->D0FilterState == RT_D0_FILTER_NONE )
		return bBufferPkt;

	if( pNdisCommon->D0FilterState == RT_D0_FILTER_FLUSHING )  
		return bBufferPkt;

	if( pRfd->D0FilterCoalPktInfo.PacketType == RXPacketTypeUndefined)
		return bBufferPkt;

	if(!pMgntInfo->mRtD0ColesFilterInfo.bEnable)
		return bBufferPkt;

	pCurrRfdFilteInfo = &(pRfd->D0FilterCoalPktInfo);
	

	for( ColseParIndex = 0 ;  ColseParIndex < 10 && bBufferPkt == FALSE; ColseParIndex++)
	{
		bBufferPkt = FALSE;
		pCurrFilterPar = &(pMgntInfo->mRtD0ColesFilterInfo.dFilterArry[ColseParIndex]);

		if(pCurrFilterPar->FilterID == 0)
			continue;
		
		bFiledMatch =TRUE;

		for(ColseFieldIndex = 0 ; ColseFieldIndex < pCurrFilterPar->NumOfElem && bFiledMatch ==TRUE;  ColseFieldIndex++)
		{
			pCurrFieldInfo = &(pCurrFilterPar->dFieldArry[ColseFieldIndex]);
			
			switch(pCurrFieldInfo->FrameHead )
			{
				case RTFrameHeaderMac:
					if(pCurrFieldInfo->FilterTest == RTReceiveFilterTestEqual)
					{
						if(pCurrFieldInfo->HeaderField.MacHeaderField == RTMacHeaderFieldPacketType )
						{
							if( pCurrFieldInfo->FieldValue.FieldByteValue  != pCurrRfdFilteInfo->PacketType )
							{
								bFiledMatch = FALSE;
							}
						}
						else if(pCurrFieldInfo->HeaderField.MacHeaderField == RTMacHeaderFieldProtocol)
						{
							if( PlatformCompareMemory(&(pCurrFieldInfo->FieldValue.FieldShortValue) , pCurrRfdFilteInfo->pEtherType , 2))
							{
								bFiledMatch = FALSE;
							}
						}
						else
						{
							RT_TRACE(COMP_POWER, DBG_LOUD, ("===> RTFrameHeaderMac (%d)\n",pCurrFieldInfo->HeaderField.MacHeaderField));
							bFiledMatch = FALSE;
						}
					}
					else if( pCurrFieldInfo->FilterTest == RTReceiveFilterTestNotEqual )
					{
						bFiledMatch = FALSE;  // Now alway Equal 
					}
					else
					{
						bFiledMatch = FALSE;
					}
				 	break;
    				case RTFrameHeaderArp:
					if(pCurrFieldInfo->FilterTest == RTReceiveFilterTestEqual)
					{
						if(pCurrFieldInfo->HeaderField.ArpHeaderField ==  RTARPHeaderFieldOperation)
						{
							if( PlatformCompareMemory(&(pCurrFieldInfo->FieldValue.FieldShortValue) , pCurrRfdFilteInfo->ARPOption , 2))
							{
								bFiledMatch = FALSE;
							}
						}
						else if(pCurrFieldInfo->HeaderField.ArpHeaderField ==  RTARPHeaderFieldSPA)
						{
							if( PlatformCompareMemory(&(pCurrFieldInfo->FieldValue.FieldLong64Value) , pCurrRfdFilteInfo->ARPSPA , 6))
							{
								bFiledMatch = FALSE;
							}
						}
						else if(pCurrFieldInfo->HeaderField.ArpHeaderField ==  RTARPHeaderFieldTPA)
						{
							if( PlatformCompareMemory(&(pCurrFieldInfo->FieldValue.FieldLong64Value) , pCurrRfdFilteInfo->ARPTPA , 6))
							{
								bFiledMatch = FALSE;
							}
						}
						else
						{
							bFiledMatch = FALSE;
						}
					}
					else if(pCurrFieldInfo->FilterTest == RTReceiveFilterTestNotEqual)
					{
						if(pCurrFieldInfo->HeaderField.ArpHeaderField ==  RTARPHeaderFieldOperation)
						{
							if( !PlatformCompareMemory(&(pCurrFieldInfo->FieldValue.FieldShortValue) , pCurrRfdFilteInfo->ARPOption , 2))
							{
								bFiledMatch = FALSE;
							}
						}
						else if(pCurrFieldInfo->HeaderField.ArpHeaderField ==  RTARPHeaderFieldSPA)
						{
							if( !PlatformCompareMemory(&(pCurrFieldInfo->FieldValue.FieldLong64Value) , pCurrRfdFilteInfo->ARPSPA , 6))
							{
								bFiledMatch = FALSE;
							}
						}
						else if(pCurrFieldInfo->HeaderField.ArpHeaderField ==  RTARPHeaderFieldTPA)
						{
							if( !PlatformCompareMemory(&(pCurrFieldInfo->FieldValue.FieldLong64Value) , pCurrRfdFilteInfo->ARPTPA , 6))
							{
								bFiledMatch = FALSE;
							}
						}
						else
						{
							bFiledMatch = FALSE;
						}
					}
					else
					{
						bFiledMatch = FALSE;
					}
					break;
				case RTFrameHeaderIPv4:
					if(pCurrFieldInfo->FilterTest == RTReceiveFilterTestEqual)
					{
						if(pCurrFieldInfo->HeaderField.IPv4HeaderField ==  RTIPv4HeaderFieldProtocol)
						{
							if( pCurrFieldInfo->FieldValue.FieldByteValue  != pCurrRfdFilteInfo->Protocol )
							{
								bFiledMatch = FALSE;
							}
						}
						else
						{
							bFiledMatch = FALSE;
						}
					}
					else if(pCurrFieldInfo->FilterTest == RTReceiveFilterTestNotEqual)
					{
						if(pCurrFieldInfo->HeaderField.IPv4HeaderField ==  RTIPv4HeaderFieldProtocol)
						{
							if( pCurrFieldInfo->FieldValue.FieldByteValue  == pCurrRfdFilteInfo->Protocol )
							{
								bFiledMatch = FALSE;
							}
						}
						else
						{
							bFiledMatch = FALSE;
						}
					}
					else
					{						
						bFiledMatch = FALSE;
					}
				 	break;
    				case RTFrameHeaderIPv6:
					if(pCurrFieldInfo->FilterTest == RTReceiveFilterTestEqual)
					{
						if(pCurrFieldInfo->HeaderField.IPv6HeaderField ==  RTIPv6HeaderFieldProtocol)
						{
							if( pCurrFieldInfo->FieldValue.FieldByteValue  != pCurrRfdFilteInfo->Protocol )
							{
								bFiledMatch = FALSE;
							}
						}
						else
						{
							bFiledMatch = FALSE;
						}
					}
					else if(pCurrFieldInfo->FilterTest == RTReceiveFilterTestNotEqual)
					{
						if(pCurrFieldInfo->HeaderField.IPv6HeaderField ==  RTIPv6HeaderFieldProtocol)
						{
							if( pCurrFieldInfo->FieldValue.FieldByteValue  == pCurrRfdFilteInfo->Protocol )
							{
								bFiledMatch = FALSE;
							}
						}
						else
						{
							bFiledMatch = FALSE;
						}
					}
					else
					{
						bFiledMatch = FALSE;
					}
					 break;
				case RTFrameHeaderUdp:
					if(pCurrFieldInfo->FilterTest == RTReceiveFilterTestEqual)
					{
						if(pCurrFieldInfo->HeaderField.UdpHeaderField ==  RTUdpHeaderFieldDestinationPort)
						{
							if( PlatformCompareMemory(&(pCurrFieldInfo->FieldValue.FieldShortValue) , pCurrRfdFilteInfo->pDestinationPort , 2))
							{
								bFiledMatch = FALSE;
							}
						}
						else
						{
							bFiledMatch = FALSE;
						}
					}
					else if(pCurrFieldInfo->FilterTest == RTReceiveFilterTestNotEqual)
					{
						if(pCurrFieldInfo->HeaderField.UdpHeaderField ==  RTUdpHeaderFieldDestinationPort)
						{
							if( !PlatformCompareMemory(&(pCurrFieldInfo->FieldValue.FieldShortValue) , pCurrRfdFilteInfo->pDestinationPort , 2))
							{
								bFiledMatch = FALSE;
							}
						}
						else
						{
							bFiledMatch = FALSE;
						}
					}
					else
					{
						bFiledMatch = FALSE;
					}
				 	break;
    				default:
					bFiledMatch = FALSE;
					break;
			}
		}

		if(bFiledMatch)
		{
			bBufferPkt = TRUE;
			pMatchFilterPar = pCurrFilterPar;
		}
		
	}

	
	if(bBufferPkt )
	{
		//RT_PRINT_DATA(COMP_TEST, DBG_LOUD, "===>bTestBufferPkt(true):\n", frame.Octet, frame.Length);
		RT_TRACE(COMP_TEST, DBG_LOUD,("===>bBufferPkt(true) !!\n"));
	}
	
	// Change state !!
	if(bBufferPkt)
	{
		RTInsertTailList(&pNdisCommon->D0FilterPktQueue, &pRfd->List);
		if(pNdisCommon->D0FilterState == RT_D0_FILTER_INIT)
		{
			if(pMatchFilterPar != NULL)
			{
				msDelay = pMatchFilterPar->Delaytime;
			}
			PlatformSetTimer( Adapter, &pNdisCommon->D0RxIndicatTimer, msDelay);
			pNdisCommon->D0FilterState =  RT_D0_FILTER_BUFFING;
		}
		else
		{
			RT_TRACE(COMP_TEST, DBG_LOUD,("bBufferPkt ==> pNdisCommon->D0FilterState(%d) !!\n",pNdisCommon->D0FilterState));
		}
	}
	else
	{
		if(pNdisCommon->D0FilterState ==  RT_D0_FILTER_BUFFING)
		{
			RT_TRACE(COMP_TEST, DBG_LOUD,("N6ReceiveCoalescingFilter(): N6FulshD0CoalescingQueue !!\n"));
			PlatformCancelTimer(Adapter , &pNdisCommon->D0RxIndicatTimer);
			//PlatformStallExecution(100);
			N6FulshD0CoalescingQueue( Adapter, FALSE, TRUE);
			pNdisCommon->D0FilterState = RT_D0_FILTER_INIT;
		}
	}

	return bBufferPkt;
}

VOID
N6Fill80211PhyAttributes(
	IN	PADAPTER		Adapter,
	IN	PNDIS_MINIPORT_ADAPTER_NATIVE_802_11_ATTRIBUTES	pDot11Attributes
	)
{
	PRT_NDIS6_COMMON	pNdisCommon = Adapter->pNdisCommon;
	PDOT11_PHY_ATTRIBUTES	pPhyAttributes;
	ULONG	i, index;
	UCHAR	rate;
	
	for (i=0, pPhyAttributes = pDot11Attributes->SupportedPhyAttributes ; 
		i<pDot11Attributes->NumSupportedPhys ;
		i++,pPhyAttributes++)
	{
		N6_ASSIGN_OBJECT_HEADER(
			pPhyAttributes->Header,
			NDIS_OBJECT_TYPE_DEFAULT,
			DOT11_PHY_ATTRIBUTES_REVISION_1,
			sizeof(DOT11_PHY_ATTRIBUTES));

		pPhyAttributes->PhyType = pNdisCommon->pDot11PhyMIBs[i].PhyType;
		pPhyAttributes->bHardwarePhyState = TRUE;
		pPhyAttributes->bSoftwarePhyState = TRUE;
		pPhyAttributes->bCFPollable = MgntActQuery_CfPollable(Adapter);
		pPhyAttributes->uMPDUMaxLength = NATIVE_802_11_MTU_SIZE;
		pPhyAttributes->TempType = dot11_temp_type_unknown;
		pPhyAttributes->DiversitySupport = dot11_diversity_support_dynamic;

		switch (pPhyAttributes->PhyType)
		{
		case dot11_phy_type_hrdsss:
			pPhyAttributes->HRDSSSAttributes.bShortPreambleOptionImplemented = FALSE;
			pPhyAttributes->HRDSSSAttributes.bPBCCOptionImplemented = FALSE;
			pPhyAttributes->HRDSSSAttributes.bChannelAgilityPresent = FALSE;
			pPhyAttributes->HRDSSSAttributes.uHRCCAModeSupported = FALSE;
			break;

		case dot11_phy_type_ofdm:
			pPhyAttributes->OFDMAttributes.uFrequencyBandsSupported = 
					DOT11_FREQUENCY_BANDS_LOWER | 
					DOT11_FREQUENCY_BANDS_MIDDLE;
			break;

		case dot11_phy_type_erp:
			pPhyAttributes->ERPAttributes.bShortPreambleOptionImplemented = TRUE;
			pPhyAttributes->ERPAttributes.bPBCCOptionImplemented = FALSE;
			pPhyAttributes->ERPAttributes.bChannelAgilityPresent = FALSE;
			pPhyAttributes->ERPAttributes.uHRCCAModeSupported = FALSE;
			pPhyAttributes->ERPAttributes.bERPPBCCOptionImplemented = FALSE;
			pPhyAttributes->ERPAttributes.bDSSSOFDMOptionImplemented = FALSE;
			pPhyAttributes->ERPAttributes.bShortSlotTimeOptionImplemented = TRUE;
			break;

		default:
			break;
		}

		// Tx Power Level.
		pPhyAttributes->uNumberSupportedPowerLevels = 
			pNdisCommon->dot11SupportedPowerLevels.uNumOfSupportedPowerLevels;
		PlatformMoveMemory(
			pPhyAttributes->TxPowerLevels,
			pNdisCommon->dot11SupportedPowerLevels.uTxPowerLevelValues,
			sizeof(pPhyAttributes->TxPowerLevels));
	
		// Data Rate.
		PlatformMoveMemory(
			&(pPhyAttributes->SupportedDataRatesValue),
			&(pNdisCommon->pDot11PhyMIBs[i].SupportedDataRatesValue),
			sizeof(DOT11_SUPPORTED_DATA_RATES_VALUE_V2));
		index = 0;
		while ( (rate = pNdisCommon->pDot11PhyMIBs[i].SupportedDataRatesValue.ucSupportedTxDataRatesValue[index]) != 0)
		{
			pPhyAttributes->DataRateMappingEntries[index].ucDataRateIndex = rate;
			pPhyAttributes->DataRateMappingEntries[index].ucDataRateFlag = 0;
			pPhyAttributes->DataRateMappingEntries[index].usDataRateValue = rate;

			index++;
		}
		pPhyAttributes->uNumDataRateMappingEntries = index;
	}
}

VOID
N6Fill80211ExtStaAttributes(
	IN	PADAPTER		Adapter,
	IN	PNDIS_MINIPORT_ADAPTER_NATIVE_802_11_ATTRIBUTES	pDot11Attributes,
	IN	PNIC_SUPPORTED_AUTH_CIPHER_PAIRS	pSupportedAuthCipherAlgs
	)
{
	PDOT11_EXTSTA_ATTRIBUTES	pExtStaAttributes = pDot11Attributes->ExtSTAAttributes;
	PRT_NDIS6_COMMON	pNdisCommon = Adapter->pNdisCommon;
	pu1Byte				Buffer = NULL;

	N6_ASSIGN_OBJECT_HEADER(
		pExtStaAttributes->Header,
		NDIS_OBJECT_TYPE_DEFAULT,
		DOT11_EXTSTA_ATTRIBUTES_REVISION_3,
		sizeof(DOT11_EXTSTA_ATTRIBUTES));

	// For FIPS support indication.
	pExtStaAttributes->bSafeModeImplemented = DOT11_EXTSTA_ATTRIBUTES_SAFEMODE_OID_SUPPORTED;


	pExtStaAttributes->uScanSSIDListSize = NATIVE_802_11_MAX_SCAN_SSID;
	pExtStaAttributes->uDesiredBSSIDListSize = NATIVE_802_11_MAX_DESIRED_BSSID;
	pExtStaAttributes->uDesiredSSIDListSize = NATIVE_802_11_MAX_DESIRED_SSID;
	pExtStaAttributes->uExcludedMacAddressListSize = NATIVE_802_11_MAX_EXCLUDED_MACADDR;
	pExtStaAttributes->uPrivacyExemptionListSize = NATIVE_802_11_MAX_PRIVACY_EXEMPTION;
	pExtStaAttributes->uKeyMappingTableSize = NATIVE_802_11_MAX_KEY_MAPPING_ENTRY;
	pExtStaAttributes->uDefaultKeyTableSize = NATIVE_802_11_MAX_DEFAULT_KEY_ENTRY;
	pExtStaAttributes->uWEPKeyValueMaxLength = NATIVE_802_11_MAX_WEP_KEY_LENGTH;
	pExtStaAttributes->uPMKIDCacheSize = NATIVE_802_11_MAX_PMKID_CACHE;
	pExtStaAttributes->uMaxNumPerSTADefaultKeyTables = NATIVE_802_11_MAX_PER_STA_DEFAULT_KEY;
	pExtStaAttributes->bStrictlyOrderedServiceClassImplemented = FALSE;
	pExtStaAttributes->ucSupportedQoSProtocolFlags = 0;
	//pExtStaAttributes->bSafeModeImplemented = FALSE;
	pExtStaAttributes->uNumSupportedCountryOrRegionStrings = 0;
	pExtStaAttributes->pSupportedCountryOrRegionStrings = NULL;

	RT_TRACE( COMP_TEST,DBG_LOUD, ("bAutoPowerSaveMode   0000000\n"));
	pExtStaAttributes->uMaxNetworkOffloadListSize  = NATIVE_802_11_MAX_NETWORKOFFLOAD_SIZE;
	pExtStaAttributes->bAutoPowerSaveMode = TRUE;
	pExtStaAttributes->bMFPCapable = TRUE;


	pExtStaAttributes->uInfraNumSupportedMcastMgmtAlgoPairs = 
		pSupportedAuthCipherAlgs->pInfraSupportedMcastMgmtAlgoList->uNumOfEntries;	
	pExtStaAttributes->pInfraSupportedMcastMgmtAlgoPairs = 
		pSupportedAuthCipherAlgs->pInfraSupportedMcastMgmtAlgoList->AuthCipherPairs;	

	//
	// Authentication and Encryption algorithm.
	//
	pExtStaAttributes->uInfraNumSupportedUcastAlgoPairs = 
		pSupportedAuthCipherAlgs->pInfraUcastAuthCipherList->uNumOfEntries;
	pExtStaAttributes->pInfraSupportedUcastAlgoPairs =
		pSupportedAuthCipherAlgs->pInfraUcastAuthCipherList->AuthCipherPairs;

	pExtStaAttributes->uInfraNumSupportedMcastAlgoPairs =
		pSupportedAuthCipherAlgs->pInfraMcastAuthCipherList->uNumOfEntries;
	pExtStaAttributes->pInfraSupportedMcastAlgoPairs =
		pSupportedAuthCipherAlgs->pInfraMcastAuthCipherList->AuthCipherPairs;

	pExtStaAttributes->uAdhocNumSupportedUcastAlgoPairs =
		pSupportedAuthCipherAlgs->pAdhocUcastAuthCipherList->uNumOfEntries;
	pExtStaAttributes->pAdhocSupportedUcastAlgoPairs =
		pSupportedAuthCipherAlgs->pAdhocUcastAuthCipherList->AuthCipherPairs;

	pExtStaAttributes->uAdhocNumSupportedMcastAlgoPairs =
		pSupportedAuthCipherAlgs->pAdhocMcastAuthCipherList->uNumOfEntries;
	pExtStaAttributes->pAdhocSupportedMcastAlgoPairs =
		pSupportedAuthCipherAlgs->pAdhocMcastAuthCipherList->AuthCipherPairs;
	
}



NDIS_STATUS
N6CSetGeneralAttributes(
	IN	PADAPTER		Adapter
	)
{
	NDIS_MINIPORT_ADAPTER_ATTRIBUTES	MiniportAttributes;
	PRT_NDIS6_COMMON					pNdisCommon = Adapter->pNdisCommon;
	PMGNT_INFO							pMgntInfo = &(Adapter->MgntInfo);
#if (NDIS_SUPPORT_NDIS620)
	NDIS_PM_CAPABILITIES				PmCapabilities;
#endif
	NDIS_PNP_CAPABILITIES				PnpCapabilities;


	PlatformZeroMemory(&MiniportAttributes, sizeof(NDIS_MINIPORT_ADAPTER_ATTRIBUTES));

	//
	// <Roger_Notes> We should fill up capability variable with zero value first to prevent 
	// unexpected NDIS parsing and IRQL_NOT_LESS_OR_EQUAL (a) bugcheck code occurs
	// after ndisMInitializeAdapter and NdisSetTimer is fired.
	// 2010.05.24.
	//

#if (NDIS_SUPPORT_NDIS620)
	N6_ASSIGN_OBJECT_HEADER(
		MiniportAttributes.GeneralAttributes.Header,
		NDIS_OBJECT_TYPE_MINIPORT_ADAPTER_GENERAL_ATTRIBUTES,
		NDIS_MINIPORT_ADAPTER_GENERAL_ATTRIBUTES_REVISION_2,
		sizeof(NDIS_MINIPORT_ADAPTER_GENERAL_ATTRIBUTES));

#elif (NDIS_SUPPORT_NDIS6)
	N6_ASSIGN_OBJECT_HEADER(
		MiniportAttributes.GeneralAttributes.Header,
		NDIS_OBJECT_TYPE_MINIPORT_ADAPTER_GENERAL_ATTRIBUTES,
		NDIS_MINIPORT_ADAPTER_GENERAL_ATTRIBUTES_REVISION_1,
		sizeof(NDIS_MINIPORT_ADAPTER_GENERAL_ATTRIBUTES));
#endif

	// By tynli for wowlan attribute "PowerManagementCapabilitiesEx" which only works on revision 2.

	MiniportAttributes.GeneralAttributes.MediaType = NATIVE_802_11_MEDIA_TYPE;
	MiniportAttributes.GeneralAttributes.PhysicalMediumType = NATIVE_802_11_PHYSICAL_MEDIA_TYPE;
	MiniportAttributes.GeneralAttributes.MtuSize = NATIVE_802_11_MTU_SIZE;
	// neo test 123 // this two should be query from HW supported limit
	MiniportAttributes.GeneralAttributes.MaxXmitLinkSpeed = NATIVE_802_11_MAX_XMIT_LINK_SPEED;
	MiniportAttributes.GeneralAttributes.MaxRcvLinkSpeed = NATIVE_802_11_MAX_RCV_LINK_SPEED;
	MiniportAttributes.GeneralAttributes.XmitLinkSpeed = NDIS_LINK_SPEED_UNKNOWN;
	MiniportAttributes.GeneralAttributes.RcvLinkSpeed = NDIS_LINK_SPEED_UNKNOWN;
	MiniportAttributes.GeneralAttributes.MediaConnectState = MediaConnectStateDisconnected;
	MiniportAttributes.GeneralAttributes.MediaDuplexState = MediaDuplexStateHalf;
	MiniportAttributes.GeneralAttributes.LookaheadSize = NIC_MAX_PACKET_SIZE - NIC_HEADER_SIZE;

#if (NDIS_SUPPORT_NDIS620)
	PlatformZeroMemory(&PmCapabilities, sizeof(NDIS_PM_CAPABILITIES));

	MiniportAttributes.GeneralAttributes.PowerManagementCapabilitiesEx = &PmCapabilities;
	FillPmCapabilities(Adapter, &PmCapabilities);

	MiniportAttributes.GeneralAttributes.PowerManagementCapabilities = NULL;
#else
	PlatformZeroMemory(&PnpCapabilities, sizeof(NDIS_PNP_CAPABILITIES));
	MiniportAttributes.GeneralAttributes.PowerManagementCapabilities = &PnpCapabilities;
	FillPnpCapabilities(Adapter, &PnpCapabilities);
#endif

	MiniportAttributes.GeneralAttributes.MacOptions = 
						NDIS_MAC_OPTION_TRANSFERS_NOT_PEND |
#if COALESCE_RECEIVED_PACKET
						NDIS_MAC_OPTION_COPY_LOOKAHEAD_DATA |
#endif
						NDIS_MAC_OPTION_NO_LOOPBACK;

	// Qos support. 2007.01.05, by shien chang.
	if (pMgntInfo->pStaQos->QosCapability > QOS_DISABLE)
	{
		MiniportAttributes.GeneralAttributes.MacOptions |= NDIS_MAC_OPTION_8021P_PRIORITY;
	}
	
	MiniportAttributes.GeneralAttributes.SupportedPacketFilters =
						NIC_SUPPORTED_FILTERS;
	
	MiniportAttributes.GeneralAttributes.MaxMulticastListSize = NATIVE_802_11_MAX_MULTICAST_LIST;
	MiniportAttributes.GeneralAttributes.MacAddressLength = ETH_LENGTH_OF_ADDRESS;

	PlatformMoveMemory(
		&(MiniportAttributes.GeneralAttributes.PermanentMacAddress),
		Adapter->PermanentAddress,
		ETH_LENGTH_OF_ADDRESS
		);

	PlatformMoveMemory(
		&(MiniportAttributes.GeneralAttributes.CurrentMacAddress),
		Adapter->CurrentAddress,
		ETH_LENGTH_OF_ADDRESS
		);

	MiniportAttributes.GeneralAttributes.RecvScaleCapabilities = NULL;
	MiniportAttributes.GeneralAttributes.AccessType = NET_IF_ACCESS_BROADCAST;
	MiniportAttributes.GeneralAttributes.DirectionType = NET_IF_DIRECTION_SENDRECEIVE;
	MiniportAttributes.GeneralAttributes.IfType = IF_TYPE_IEEE80211;
	MiniportAttributes.GeneralAttributes.IfConnectorPresent = TRUE;
	MiniportAttributes.GeneralAttributes.DataBackFillSize = NATIVE_802_11_DATA_BACK_FILL_SIZE;

	// Support OIDs.
	MiniportAttributes.GeneralAttributes.SupportedOidList = N6GetSupportedOids();
	MiniportAttributes.GeneralAttributes.SupportedOidListLength = N6GetSupportedOidsLength();
	
	return NdisMSetMiniportAttributes(Adapter->pNdisCommon->hNdisAdapter,
								&MiniportAttributes
								);
}


NDIS_STATUS
N6Set80211Attributes(
	IN	PADAPTER		Adapter
	)
{
	NDIS_MINIPORT_ADAPTER_NATIVE_802_11_ATTRIBUTES		Dot11Attributes;
	PRT_NDIS6_COMMON	pNdisCommon = Adapter->pNdisCommon;
	NDIS_STATUS		ndisStatus;
	RT_STATUS		rtStatus;
	NIC_SUPPORTED_AUTH_CIPHER_PAIRS		SupportedAuthCipherAlgs;
	u4Byte	BytesWritten, BytesNeeded;
	u1Byte	tempAuthType = 0;

	// For single .sys support: 
	//	Point to types: DOT11_VWIFI_COMBINATION_V2 and DOT11_VWIFI_COMBINATION_V3
	PVOID	pCombination = NULL;



#if NDIS_SUPPORT_NDIS620
	PDOT11_VWIFI_ATTRIBUTES 	pVWiFiAttribs = NULL;
	ULONG	vwifiAttrSize = 0;
	PRT_NDIS62_COMMON	pNdis62Common = Adapter->pNdis62Common;
#endif

#if NDIS_SUPPORT_NDIS630
	PDOT11_WFD_ATTRIBUTES   WFDAttributes;
    	BOOLEAN bAutoPowerSaveMode;
    	ULONG uMaxNetworkOffloadListSize;
#endif

	PlatformZeroMemory(&Dot11Attributes, sizeof(NDIS_MINIPORT_ADAPTER_NATIVE_802_11_ATTRIBUTES));

	if(pNdisCommon->NdisVersion <= NDIS_VERSION_BASE_6_1)
	{
		N6_ASSIGN_OBJECT_HEADER(
			Dot11Attributes.Header,
			NDIS_OBJECT_TYPE_MINIPORT_ADAPTER_NATIVE_802_11_ATTRIBUTES,
			NDIS_MINIPORT_ADAPTER_802_11_ATTRIBUTES_REVISION_1,
			NDIS_SIZEOF_MINIPORT_ADAPTER_NATIVE_802_11_ATTRIBUTES_REVISION_1);
	}
#if NDIS_SUPPORT_NDIS620
	else if(pNdisCommon->NdisVersion <= NDIS_VERSION_BASE_6_20)
	{
		N6_ASSIGN_OBJECT_HEADER(
			Dot11Attributes.Header,
			NDIS_OBJECT_TYPE_MINIPORT_ADAPTER_NATIVE_802_11_ATTRIBUTES,
			NDIS_MINIPORT_ADAPTER_802_11_ATTRIBUTES_REVISION_2,
			NDIS_SIZEOF_MINIPORT_ADAPTER_NATIVE_802_11_ATTRIBUTES_REVISION_2);
	}
#endif
#if NDIS_SUPPORT_NDIS630
	else if(pNdisCommon->NdisVersion <= NDIS_VERSION_BASE_6_40)
	{
	
		N6_ASSIGN_OBJECT_HEADER(
			Dot11Attributes.Header,
			NDIS_OBJECT_TYPE_MINIPORT_ADAPTER_NATIVE_802_11_ATTRIBUTES,
			NDIS_MINIPORT_ADAPTER_802_11_ATTRIBUTES_REVISION_3,
			NDIS_SIZEOF_MINIPORT_ADAPTER_NATIVE_802_11_ATTRIBUTES_REVISION_3);
	}
#endif

	Dot11Attributes.OpModeCapability = pNdisCommon->dot11OperationModeCapability.uOpModeCapability;
	Dot11Attributes.NumOfTXBuffers = pNdisCommon->dot11OperationModeCapability.uNumOfTXBuffers;
	Dot11Attributes.NumOfRXBuffers = pNdisCommon->dot11OperationModeCapability.uNumOfRXBuffers;
	Dot11Attributes.MultiDomainCapabilityImplemented = pNdisCommon->dot11MultiDomainCapabilityImplemented;
	Dot11Attributes.NumSupportedPhys = pNdisCommon->pDot11SupportedPhyTypes->uNumOfEntries;
	
	//
	// SupportedPhyAttributes attributes.
	//
	if (Dot11Attributes.NumSupportedPhys)
	{
		rtStatus = PlatformAllocateMemory(
			Adapter, 
			&(Dot11Attributes.SupportedPhyAttributes),
			Dot11Attributes.NumSupportedPhys * sizeof(DOT11_PHY_ATTRIBUTES)
			);
		if (rtStatus != RT_STATUS_SUCCESS)
		{
			RT_TRACE(COMP_INIT, DBG_SERIOUS, 
				("N6Set80211Attributes(): failed to allocate memory for SupportedPhyAttributes\n"));
			ndisStatus = NDIS_STATUS_FAILURE;
			goto Exit;
		}

		PlatformZeroMemory(Dot11Attributes.SupportedPhyAttributes, 
				Dot11Attributes.NumSupportedPhys * sizeof(DOT11_PHY_ATTRIBUTES));

		N6Fill80211PhyAttributes(Adapter, &Dot11Attributes);
	}

	//
	// ExtSTAAttributes attributes.
	//
	rtStatus = PlatformAllocateMemory(
		Adapter,
		&(Dot11Attributes.ExtSTAAttributes),
		sizeof(DOT11_EXTSTA_ATTRIBUTES)
		);
	if (rtStatus != RT_STATUS_SUCCESS)
	{
		RT_TRACE(COMP_INIT, DBG_SERIOUS,
			("N6Set80211Attributes(): failed to allocate memory for ExtSTAAttributes\n"));
		ndisStatus = NDIS_STATUS_FAILURE;
		goto Exit;
	}

	PlatformZeroMemory(Dot11Attributes.ExtSTAAttributes,
						sizeof(DOT11_EXTSTA_ATTRIBUTES));
	
	//
	// Get the supported authentication and cipher algorithm.
	//
	PlatformZeroMemory(&SupportedAuthCipherAlgs, sizeof(NIC_SUPPORTED_AUTH_CIPHER_PAIRS));
	
	// Store current mode and switch to infrastructure mode.
	// <SC_TODO:>
	
	tempAuthType = Adapter->MgntInfo.Regdot11networktype;
	Adapter->MgntInfo.Regdot11networktype = RT_JOIN_NETWORKTYPE_INFRA;
	
	// Unicast & Infrastructure.
	ndisStatus = N6CQuery_DOT11_SUPPORTED_UNICAST_ALGORITHM_PAIR(
				Adapter,
				SupportedAuthCipherAlgs.pInfraUcastAuthCipherList,
				0,
				&BytesWritten,
				&BytesNeeded);
	rtStatus = PlatformAllocateMemory(
				Adapter, 
				&(SupportedAuthCipherAlgs.pInfraUcastAuthCipherList), 
				BytesNeeded);
	if (rtStatus != RT_STATUS_SUCCESS)
	{
		RT_TRACE(COMP_INIT, DBG_SERIOUS, 
			("N6Set80211Attributes(): no buffer for N6CQuery_DOT11_SUPPORTED_UNICAST_ALGORITHM_PAIR under infra mode\n"));
		ndisStatus = NDIS_STATUS_FAILURE;
		goto Exit;
	}
	PlatformZeroMemory(SupportedAuthCipherAlgs.pInfraUcastAuthCipherList, BytesNeeded);

	ndisStatus = N6CQuery_DOT11_SUPPORTED_UNICAST_ALGORITHM_PAIR(
				Adapter,
				SupportedAuthCipherAlgs.pInfraUcastAuthCipherList,
				BytesNeeded,
				&BytesWritten,
				&BytesNeeded);
	if (ndisStatus != NDIS_STATUS_SUCCESS)
	{
		RT_TRACE(COMP_INIT, DBG_SERIOUS,
			("N6Set80211Attributes(): failed to query unicast auth cipher pairs under infra mode\n"));
		ndisStatus = NDIS_STATUS_FAILURE;
		goto Exit;
	}

	// Multicast & Infrastructure.
	ndisStatus = N6CQuery_DOT11_SUPPORTED_MULTICAST_ALGORITHM_PAIR(
				Adapter,
				SupportedAuthCipherAlgs.pInfraMcastAuthCipherList,
				0,
				&BytesWritten,
				&BytesNeeded);
	rtStatus = PlatformAllocateMemory(
				Adapter,
				&(SupportedAuthCipherAlgs.pInfraMcastAuthCipherList),
				BytesNeeded);
	if (rtStatus != RT_STATUS_SUCCESS)
	{
		RT_TRACE(COMP_INIT, DBG_SERIOUS,
			("N6Set80211Attributes(): no buffer for N6CQuery_DOT11_SUPPORTED_MULTICAST_ALGORITHM_PAIR under infra mode.\n"));
		ndisStatus = NDIS_STATUS_FAILURE;
		goto Exit;
	}
	PlatformZeroMemory(SupportedAuthCipherAlgs.pInfraMcastAuthCipherList, BytesNeeded);

	ndisStatus = N6CQuery_DOT11_SUPPORTED_MULTICAST_ALGORITHM_PAIR(
				Adapter,
				SupportedAuthCipherAlgs.pInfraMcastAuthCipherList,
				BytesNeeded,
				&BytesWritten,
				&BytesNeeded);
	if (ndisStatus != NDIS_STATUS_SUCCESS)
	{
		RT_TRACE(COMP_INIT, DBG_SERIOUS,
			("N6Set80211Attributes(): failed to query multicast auth cipher pairs under infra mode\n"));
		ndisStatus = NDIS_STATUS_FAILURE;
		goto Exit;
	}

	// Switch to adhoc mode.
	// <SC_TODO:>
	Adapter->MgntInfo.Regdot11networktype = RT_JOIN_NETWORKTYPE_ADHOC;
	// Unicast & Adhoc.
	ndisStatus = N6CQuery_DOT11_SUPPORTED_UNICAST_ALGORITHM_PAIR(
				Adapter,
				SupportedAuthCipherAlgs.pAdhocUcastAuthCipherList,
				0,
				&BytesWritten,
				&BytesNeeded);
	rtStatus = PlatformAllocateMemory(
				Adapter, 
				&(SupportedAuthCipherAlgs.pAdhocUcastAuthCipherList), 
				BytesNeeded);
	if (rtStatus != RT_STATUS_SUCCESS)
	{
		RT_TRACE(COMP_INIT, DBG_SERIOUS, 
			("N6Set80211Attributes(): no buffer for N6CQuery_DOT11_SUPPORTED_UNICAST_ALGORITHM_PAIR under adhoc mode\n"));
		ndisStatus = NDIS_STATUS_FAILURE;
		goto Exit;
	}
	PlatformZeroMemory(SupportedAuthCipherAlgs.pAdhocUcastAuthCipherList, BytesNeeded);

	ndisStatus = N6CQuery_DOT11_SUPPORTED_UNICAST_ALGORITHM_PAIR(
				Adapter,
				SupportedAuthCipherAlgs.pAdhocUcastAuthCipherList,
				BytesNeeded,
				&BytesWritten,
				&BytesNeeded);
	if (ndisStatus != NDIS_STATUS_SUCCESS)
	{
		RT_TRACE(COMP_INIT, DBG_SERIOUS,
			("N6Set80211Attributes(): failed to query unicast auth cipher pairs under adhoc mode\n"));
		ndisStatus = NDIS_STATUS_FAILURE;
		goto Exit;
	}

	// Multicast & Adhoc.
	ndisStatus = N6CQuery_DOT11_SUPPORTED_MULTICAST_ALGORITHM_PAIR(
				Adapter,
				SupportedAuthCipherAlgs.pAdhocMcastAuthCipherList,
				0,
				&BytesWritten,
				&BytesNeeded);
	rtStatus = PlatformAllocateMemory(
				Adapter,
				&(SupportedAuthCipherAlgs.pAdhocMcastAuthCipherList),
				BytesNeeded);
	if (rtStatus != RT_STATUS_SUCCESS)
	{
		RT_TRACE(COMP_INIT, DBG_SERIOUS,
			("N6Set80211Attributes(): no buffer for N6CQuery_DOT11_SUPPORTED_MULTICAST_ALGORITHM_PAIR under adhoc mode.\n"));
		ndisStatus = NDIS_STATUS_FAILURE;
		goto Exit;
	}
	PlatformZeroMemory(SupportedAuthCipherAlgs.pAdhocMcastAuthCipherList, BytesNeeded);

	ndisStatus = N6CQuery_DOT11_SUPPORTED_MULTICAST_ALGORITHM_PAIR(
				Adapter,
				SupportedAuthCipherAlgs.pAdhocMcastAuthCipherList,
				BytesNeeded,
				&BytesWritten,
				&BytesNeeded);
	if (ndisStatus != NDIS_STATUS_SUCCESS)
	{
		RT_TRACE(COMP_INIT, DBG_SERIOUS,
			("N6Set80211Attributes(): failed to query multicast auth cipher pairs under adhoc mode\n"));
		ndisStatus = NDIS_STATUS_FAILURE;
		goto Exit;
	}

	{
		static DOT11_AUTH_CIPHER_PAIR  McastMFPCipherList[] = 
		{
			{DOT11_AUTH_ALGO_RSNA_PSK,			DOT11_CIPHER_ALGO_BIP},	// 0
			{DOT11_AUTH_ALGO_RSNA,					DOT11_CIPHER_ALGO_BIP},	// 1
		};

		u4Byte	ulNumOfPairSupported = sizeof(McastMFPCipherList)/sizeof(DOT11_AUTH_CIPHER_PAIR);
		u4Byte	TotalLength = sizeof(McastMFPCipherList) + FIELD_OFFSET(DOT11_AUTH_CIPHER_PAIR_LIST, AuthCipherPairs);
		u4Byte	FillLength = 0;
		PDOT11_AUTH_CIPHER_PAIR_LIST pMFPAUTHList = NULL;
		u4Byte	i;

		rtStatus = PlatformAllocateMemory(
					Adapter,
					&(SupportedAuthCipherAlgs.pInfraSupportedMcastMgmtAlgoList),
					TotalLength);

		if (ndisStatus != NDIS_STATUS_SUCCESS)
		{
			RT_TRACE(COMP_INIT, DBG_SERIOUS,
				("N6Set80211Attributes(): failed to query multicast MFP cipher pairs \n"));
			ndisStatus = NDIS_STATUS_FAILURE;
			goto Exit;
		}

		// Fill DOT11_AUTH_CIPHER_PAIR_LIST !!
		pMFPAUTHList = SupportedAuthCipherAlgs.pInfraSupportedMcastMgmtAlgoList;

		PlatformZeroMemory( pMFPAUTHList, sizeof(DOT11_AUTH_CIPHER_PAIR_LIST) );

		N6_ASSIGN_OBJECT_HEADER(
				pMFPAUTHList->Header, 
				NDIS_OBJECT_TYPE_DEFAULT,
				DOT11_AUTH_CIPHER_PAIR_LIST_REVISION_1,
				sizeof(DOT11_AUTH_CIPHER_PAIR_LIST));
		FillLength += FIELD_OFFSET(DOT11_AUTH_CIPHER_PAIR_LIST, AuthCipherPairs);
		
		pMFPAUTHList->uNumOfEntries = 0;
		pMFPAUTHList->uTotalNumOfEntries = ulNumOfPairSupported;
		for (i=0; i<ulNumOfPairSupported; i++)
		{
			if (FillLength + sizeof(DOT11_AUTH_CIPHER_PAIR) > TotalLength )
			{
				RT_TRACE(COMP_INIT, DBG_WARNING, (" <- [Error]N6CQuery_DOT11_SUPPORTED_MULTICAST_ALGORITHM_PAIR(), NDIS_STATUS_BUFFER_OVERFLOW\n") );
				ndisStatus = NDIS_STATUS_FAILURE;
				goto Exit;
			}
		
			pMFPAUTHList->uNumOfEntries ++;
			FillLength += sizeof(DOT11_AUTH_CIPHER_PAIR);
		}

		CopyMem(pMFPAUTHList->AuthCipherPairs,
				McastMFPCipherList,
				sizeof(DOT11_AUTH_CIPHER_PAIR)*pMFPAUTHList->uNumOfEntries);

		RT_TRACE( COMP_TEST , DBG_LOUD , ("===> TotalLength = %d\n",TotalLength) );
		RT_TRACE( COMP_TEST , DBG_LOUD , ("===> FillLength = %d\n",FillLength) );
	}

	// Switch back to origin mode.
	// <SC_TODO:>
	Adapter->MgntInfo.Regdot11networktype = tempAuthType;

	N6Fill80211ExtStaAttributes(Adapter, &Dot11Attributes, &SupportedAuthCipherAlgs);

#if NDIS_SUPPORT_NDIS620
	if(pNdisCommon->NdisVersion > NDIS_VERSION_BASE_6_1)
	{
	//4	add vwifi attributes
#if NDIS_SUPPORT_NDIS630
		if(pNdisCommon->NdisVersion > NDIS_VERSION_BASE_6_20)
		        vwifiAttrSize = FIELD_OFFSET(DOT11_VWIFI_ATTRIBUTES, Combinations)  + 
		                    (NUM_SUPPORTED_VWIFI_COMBINATIONS) * DOT11_SIZEOF_VWIFI_COMBINATION_REVISION_3;

		else 			
#endif
		        vwifiAttrSize = sizeof(DOT11_VWIFI_ATTRIBUTES) + 
		                    (NUM_SUPPORTED_VWIFI_COMBINATIONS) * DOT11_SIZEOF_VWIFI_COMBINATION_REVISION_2;

		rtStatus = PlatformAllocateMemory(
			Adapter,
			&(Dot11Attributes.VWiFiAttributes),
			vwifiAttrSize
			);
		if (rtStatus != RT_STATUS_SUCCESS)
			
		{
			RT_TRACE(COMP_INIT, DBG_SERIOUS,
				("failed to allocate memory for VWiFiAttributes\n"));
			ndisStatus = NDIS_STATUS_FAILURE;
			goto Exit;
		}

		PlatformZeroMemory(Dot11Attributes.VWiFiAttributes, vwifiAttrSize);
		N6_ASSIGN_OBJECT_HEADER(
	            	Dot11Attributes.VWiFiAttributes->Header, 
	           	NDIS_OBJECT_TYPE_DEFAULT,
	            	DOT11_VWIFI_ATTRIBUTES_REVISION_1,
		        sizeof(DOT11_VWIFI_ATTRIBUTES));

		Dot11Attributes.VWiFiAttributes->uTotalNumOfEntries = NUM_SUPPORTED_VWIFI_COMBINATIONS;

#if 1		        
        if(pNdisCommon->NdisVersion <= NDIS_VERSION_BASE_6_20)
       	{
			pCombination = (PVOID) &(Dot11Attributes.VWiFiAttributes->Combinations[0]);
			
		        N6_ASSIGN_OBJECT_HEADER(
					((PDOT11_VWIFI_COMBINATION_V2) pCombination)->Header, 
					NDIS_OBJECT_TYPE_DEFAULT,
					DOT11_VWIFI_COMBINATION_REVISION_2,
					DOT11_SIZEOF_VWIFI_COMBINATION_REVISION_2
				);
				
			((PDOT11_VWIFI_COMBINATION_V2) pCombination)->uNumInfrastructure = 1;
			((PDOT11_VWIFI_COMBINATION_V2) pCombination)->uNumSoftAP = 1;
			if(!Adapter->bInHctTest)
				((PDOT11_VWIFI_COMBINATION_V2) pCombination)->uNumVirtualStation = 1;
		}
#endif			 
#if NDIS_SUPPORT_NDIS630
		else if(pNdisCommon->NdisVersion <= NDIS_VERSION_BASE_6_40)
		{
			pCombination = (PVOID) &Dot11Attributes.VWiFiAttributes->Combinations[0];

			N6_ASSIGN_OBJECT_HEADER(
				((PDOT11_VWIFI_COMBINATION_V3) pCombination)->Header, 
				NDIS_OBJECT_TYPE_DEFAULT,
				DOT11_VWIFI_COMBINATION_REVISION_3,
				DOT11_SIZEOF_VWIFI_COMBINATION_REVISION_3
			);
			 
			((PDOT11_VWIFI_COMBINATION_V3) pCombination)->uNumInfrastructure = 1;
			((PDOT11_VWIFI_COMBINATION_V3) pCombination)->uNumSoftAP = 1;
			((PDOT11_VWIFI_COMBINATION_V3) pCombination)->uNumWFDGroup = 2;				
			((PDOT11_VWIFI_COMBINATION_V3) pCombination)->uNumVirtualStation = 0;
		}
#endif
		//4    add extap attributes	
		if(Adapter->bStartVwifi == TRUE)
		{
		        Dot11Attributes.OpModeCapability |= DOT11_OPERATION_MODE_EXTENSIBLE_AP;

			PlatformAllocateMemory(Adapter, 
				&(Dot11Attributes.ExtAPAttributes), 
				sizeof(DOT11_EXTAP_ATTRIBUTES));

			if(Dot11Attributes.ExtAPAttributes == NULL)
			{
				RT_TRACE(COMP_INIT, DBG_LOUD, ("Allocate Ndis 6.2 ExtAPAttributes Fail\n"));
				ndisStatus = NDIS_STATUS_FAILURE;
				goto Exit;
			}

			PlatformZeroMemory(Dot11Attributes.ExtAPAttributes, sizeof(DOT11_EXTAP_ATTRIBUTES));

			N6_ASSIGN_OBJECT_HEADER(
		            Dot11Attributes.ExtAPAttributes->Header, 
		            NDIS_OBJECT_TYPE_DEFAULT,
		            DOT11_EXTAP_ATTRIBUTES_REVISION_1,
		            sizeof(DOT11_EXTAP_ATTRIBUTES));

		        Dot11Attributes.ExtAPAttributes->uScanSSIDListSize = AP_SCAN_SSID_LIST_MAX_SIZE;
		        Dot11Attributes.ExtAPAttributes->uPrivacyExemptionListSize = NATIVE_802_11_MAX_PRIVACY_EXEMPTION;
			 // TODO: Get the real Size Some day
		        Dot11Attributes.ExtAPAttributes->uDefaultKeyTableSize = DOT11_MAX_NUM_DEFAULT_KEY;//VNic11DefaultKeyTableSize(vnic);
		        Dot11Attributes.ExtAPAttributes->uWEPKeyValueMaxLength = ( 104 / 8);//VNic11WEP104Implemented(vnic) ? 
		                                                // 104 / 8 : (VNic11WEP40Implemented(vnic) ? 40 / 8 : 0);

		        Dot11Attributes.ExtAPAttributes->uDesiredSSIDListSize = AP_DESIRED_SSID_LIST_MAX_SIZE;
		        Dot11Attributes.ExtAPAttributes->bStrictlyOrderedServiceClassImplemented = AP_STRICTLY_ORDERED_SERVICE_CLASS_IMPLEMENTED;
		        Dot11Attributes.ExtAPAttributes->uAssociationTableSize = AP_DEFAULT_ALLOWED_ASSOCIATION_COUNT;

		        //
		        // 11d stuff.
		        //
		        Dot11Attributes.ExtAPAttributes->uNumSupportedCountryOrRegionStrings = 0;
		        Dot11Attributes.ExtAPAttributes->pSupportedCountryOrRegionStrings = NULL;
				
			// In Ndis 6.0 We use Ad hoc to simulize AP mode So the Ad hoc Security will be the security we support
			// TODO: Win 7 , We should support more security in here for Win 7 V3 AP Mode
			if(Adapter->bInHctTest)
			{
				// In Ndis 6.0 We use Ad hoc to simulize AP mode So the Ad hoc Security will be the security we support
				Dot11Attributes.ExtAPAttributes->uInfraNumSupportedUcastAlgoPairs = 2;
				Dot11Attributes.ExtAPAttributes->pInfraSupportedUcastAlgoPairs =
					SupportedAuthCipherAlgs.pInfraUcastAuthCipherList->AuthCipherPairs;

				Dot11Attributes.ExtAPAttributes->uInfraNumSupportedMcastAlgoPairs = 2;
				Dot11Attributes.ExtAPAttributes->pInfraSupportedMcastAlgoPairs =
					SupportedAuthCipherAlgs.pInfraMcastAuthCipherList->AuthCipherPairs;
			}
			else
			{
				// In Ndis 6.0 We use Ad hoc to simulize AP mode So the Ad hoc Security will be the security we support
				Dot11Attributes.ExtAPAttributes->uInfraNumSupportedUcastAlgoPairs = 
					SupportedAuthCipherAlgs.pInfraUcastAuthCipherList	->uNumOfEntries;
				Dot11Attributes.ExtAPAttributes->pInfraSupportedUcastAlgoPairs =
					SupportedAuthCipherAlgs.pInfraUcastAuthCipherList->AuthCipherPairs;

				Dot11Attributes.ExtAPAttributes->uInfraNumSupportedMcastAlgoPairs = 
					SupportedAuthCipherAlgs.pInfraMcastAuthCipherList->uNumOfEntries;
				Dot11Attributes.ExtAPAttributes->pInfraSupportedMcastAlgoPairs =
					SupportedAuthCipherAlgs.pInfraMcastAuthCipherList->AuthCipherPairs;
			}
			
		}		
		

	}
#endif	// NDIS_SUPPORT_NDIS620

#if NDIS_SUPPORT_NDIS630
{
	PDOT11_WFD_ATTRIBUTES	pWFDAttributes = NULL;
	DOT11_COUNTRY_OR_REGION_STRING	dcors[1] = {{0,0,0}};
		
	Dot11Attributes.OpModeCapability |= 
		DOT11_OPERATION_MODE_WFD_DEVICE | 
		DOT11_OPERATION_MODE_WFD_GROUP_OWNER | 
		DOT11_OPERATION_MODE_WFD_CLIENT;
		
	PlatformAllocateMemory(Adapter, 
		&(Dot11Attributes.WFDAttributes), 
		DOT11_SIZEOF_WFD_ATTRIBUTES_REVISION_1);

	if(Dot11Attributes.WFDAttributes == NULL)
	{
		RT_TRACE(COMP_INIT, DBG_LOUD, ("Allocate Ndis 6.30 WFDAttributes Fail\n"));
		ndisStatus = NDIS_STATUS_FAILURE;
		goto Exit;
	}
		
	pWFDAttributes = Dot11Attributes.WFDAttributes;
		
	PlatformZeroMemory(Dot11Attributes.WFDAttributes, DOT11_SIZEOF_WFD_ATTRIBUTES_REVISION_1);
	{
		N6_ASSIGN_OBJECT_HEADER(
				pWFDAttributes->Header, 
				NDIS_OBJECT_TYPE_DEFAULT,
				DOT11_WFD_ATTRIBUTES_REVISION_1,
				DOT11_SIZEOF_WFD_ATTRIBUTES_REVISION_1
			);
			
		pWFDAttributes->uNumConcurrentGORole = 1;
		pWFDAttributes->uNumConcurrentClientRole = 1;
		pWFDAttributes->WPSVersionsSupported = DOT11_WPS_VERSION_2_0;
		pWFDAttributes->uMaxSecondaryDeviceTypeListSize = 8;
		pWFDAttributes->bServiceDiscoverySupported = FALSE;
		pWFDAttributes->bClientDiscoverabilitySupported = FALSE;
		pWFDAttributes->bInfrastructureManagementSupported = FALSE;

		// Win8: Let the device port use the locally-administered MAC address ------------------------------------------------
		cpMacAddr(pWFDAttributes->DeviceAddress, Adapter->PermanentAddress);
		pWFDAttributes->DeviceAddress[0] |= BIT1;
		RT_PRINT_ADDR(COMP_INIT, DBG_LOUD, "Win8 Registered Device Address: ", pWFDAttributes->DeviceAddress);
		// ----------------------------------------------------------------------------------------------------------

		// Win8: Fill the interface addresses ------------------------------------------------------------------------------
		pWFDAttributes->uInterfaceAddressListCount = 2 ;

		PlatformAllocateMemory(
				Adapter, 
				(PVOID*) &pWFDAttributes->pInterfaceAddressList,
				sizeof(DOT11_MAC_ADDRESS) * pWFDAttributes->uInterfaceAddressListCount
			);

		if(pWFDAttributes->pInterfaceAddressList == NULL)
		{
			RT_TRACE(COMP_INIT, DBG_LOUD, ("Allocate Ndis 6.30 pInterfaceAddressList Failure! \n"));
			ndisStatus = NDIS_STATUS_FAILURE;
			goto Exit;
		}
		
		cpMacAddr(pWFDAttributes->pInterfaceAddressList[0], Adapter->CurrentAddress);
		RT_PRINT_ADDR(COMP_INIT, DBG_LOUD, "Win8 Registered Interface Address [0]: ", pWFDAttributes->pInterfaceAddressList[0]);

		cpMacAddr(pWFDAttributes->pInterfaceAddressList[1], Adapter->CurrentAddress);
		RT_PRINT_ADDR(COMP_INIT, DBG_LOUD, "Win8 Registered Interface Address [1]: ", pWFDAttributes->pInterfaceAddressList[1]);
		// -----------------------------------------------------------------------------------------------------------

		pWFDAttributes->uNumSupportedCountryOrRegionStrings = 0;
		pWFDAttributes->pSupportedCountryOrRegionStrings = &dcors[0];
		pWFDAttributes->uDiscoveryFilterListSize = AP_SCAN_SSID_LIST_MAX_SIZE;
		pWFDAttributes->uGORoleClientTableSize = P2P_MAX_P2P_CLIENT;	
	}
}
#endif

	//
	// Ok, now register the attributes.
	//
	ndisStatus = NdisMSetMiniportAttributes(Adapter->pNdisCommon->hNdisAdapter,
								(PNDIS_MINIPORT_ADAPTER_ATTRIBUTES)&Dot11Attributes
								);	

	RT_TRACE(COMP_INIT, DBG_LOUD, ("NdisMSetMiniportAttributes: ndisStatu: %d\n", ndisStatus));

Exit:
	// Memory cleanup.

	if (SupportedAuthCipherAlgs.pInfraUcastAuthCipherList != NULL)
	{
		PlatformFreeMemory(
			SupportedAuthCipherAlgs.pInfraUcastAuthCipherList,
			SupportedAuthCipherAlgs.pInfraUcastAuthCipherList->uNumOfEntries * sizeof(DOT11_AUTH_CIPHER_PAIR) + 
				FIELD_OFFSET(DOT11_AUTH_CIPHER_PAIR_LIST, AuthCipherPairs));
	}

	if (SupportedAuthCipherAlgs.pInfraMcastAuthCipherList != NULL)
	{
		PlatformFreeMemory(
			SupportedAuthCipherAlgs.pInfraMcastAuthCipherList,
			SupportedAuthCipherAlgs.pInfraMcastAuthCipherList->uNumOfEntries * sizeof(DOT11_AUTH_CIPHER_PAIR) + 
				FIELD_OFFSET(DOT11_AUTH_CIPHER_PAIR_LIST, AuthCipherPairs));
	}	

	if (SupportedAuthCipherAlgs.pAdhocUcastAuthCipherList != NULL)
	{
		PlatformFreeMemory(
			SupportedAuthCipherAlgs.pAdhocUcastAuthCipherList,
			SupportedAuthCipherAlgs.pAdhocUcastAuthCipherList->uNumOfEntries * sizeof(DOT11_AUTH_CIPHER_PAIR) + 
				FIELD_OFFSET(DOT11_AUTH_CIPHER_PAIR_LIST, AuthCipherPairs));
	}

	if (SupportedAuthCipherAlgs.pAdhocMcastAuthCipherList != NULL)
	{
		PlatformFreeMemory(
			SupportedAuthCipherAlgs.pAdhocMcastAuthCipherList,
			SupportedAuthCipherAlgs.pAdhocMcastAuthCipherList->uNumOfEntries * sizeof(DOT11_AUTH_CIPHER_PAIR) + 
				FIELD_OFFSET(DOT11_AUTH_CIPHER_PAIR_LIST, AuthCipherPairs));
	}

	if (SupportedAuthCipherAlgs.pInfraSupportedMcastMgmtAlgoList != NULL)
	{
		PlatformFreeMemory(
			SupportedAuthCipherAlgs.pInfraSupportedMcastMgmtAlgoList,
			SupportedAuthCipherAlgs.pInfraSupportedMcastMgmtAlgoList->uNumOfEntries * sizeof(DOT11_AUTH_CIPHER_PAIR) + 
				FIELD_OFFSET(DOT11_AUTH_CIPHER_PAIR_LIST, AuthCipherPairs));
	}

	if (Dot11Attributes.ExtSTAAttributes != NULL)
	{
		PlatformFreeMemory(Dot11Attributes.ExtSTAAttributes,
						sizeof(DOT11_EXTSTA_ATTRIBUTES));
	}
	
	if (Dot11Attributes.VWiFiAttributes != NULL)
	{
		PlatformFreeMemory(Dot11Attributes.VWiFiAttributes,vwifiAttrSize);
	}


	if (Dot11Attributes.ExtAPAttributes != NULL)
	{
		PlatformFreeMemory(Dot11Attributes.ExtAPAttributes,
						sizeof(DOT11_EXTAP_ATTRIBUTES));
	}

	if (Dot11Attributes.WFDAttributes != NULL)
	{
		if(Dot11Attributes.WFDAttributes->pInterfaceAddressList!=NULL)
		{
			PlatformFreeMemory(
					Dot11Attributes.WFDAttributes->pInterfaceAddressList,
					sizeof(DOT11_MAC_ADDRESS) * Dot11Attributes.WFDAttributes->uInterfaceAddressListCount
				);
		}

		PlatformFreeMemory(Dot11Attributes.WFDAttributes,sizeof(DOT11_WFD_ATTRIBUTES));
	}

	if (Dot11Attributes.NumSupportedPhys && Dot11Attributes.SupportedPhyAttributes != NULL)
	{
		PlatformFreeMemory(Dot11Attributes.SupportedPhyAttributes,
					Dot11Attributes.NumSupportedPhys * sizeof(DOT11_PHY_ATTRIBUTES));
	}

	return ndisStatus;
}

u4Byte
N6CGetTCPCheckFormNetBuffLisInfo(
	IN	PADAPTER		pAdapter,
	IN  PNET_BUFFER_LIST	pNetBufferList
)
{
	return 0;
}


VOID
N6CSetTCPCheckToNetBuffLisInfo(
	IN	PADAPTER			pAdapter,
	IN	PRT_RFD				pRfd,
	IN  	PNET_BUFFER_LIST	pNetBufferList
)
{
	return;
}

VOID
N6FreeAdapter(
	IN	PADAPTER	Adapter
	)
{

	RT_TRACE(COMP_INIT, DBG_LOUD, ("===>N6FreeAdapter()\n"));
	// free CtxWorkItem and ndis62Common.
	
	MgntFreeMemory(Adapter);
	PlatformFreeMemory(Adapter->pNdisCommon, sizeof(RT_NDIS6_COMMON));
	PlatformFreeMemory(Adapter, sizeof(ADAPTER));
	RT_TRACE(COMP_INIT, DBG_LOUD, ("<===N6FreeAdapter()\n"));
}

//
// Description:
//	Compare the packet's privacy field, the EatherType and the packet destination type with the exemption list,
//	and return TRUE if this packet should be dropped, otherwise return FALSE.
// Note:
//	This function should be called after removing 802.11 Qos and Security fileds.
//
BOOLEAN
N6MatchPrivacyExemptList(
	IN	PADAPTER	Adapter,
	IN	PRT_RFD		pRfd
	)
{
	PRT_NDIS6_COMMON	pNdisCommon = Adapter->pNdisCommon;
	OCTET_STRING		pduOS;
	u2Byte				TypeLength; // EatherType
	u1Byte				TypeLengthBuf[2];
	u4Byte				i;
	USHORT				usEtherType;
	USHORT				usExemptionPacketType;
	USHORT				usExemptionActionType;

	pduOS.Octet = pRfd->Buffer.VirtualAddress + pRfd->FragOffset;
	pduOS.Length = pRfd->PacketLength;

	//
	// Get Type Length.
	//
	if(pduOS.Length < 32) 
		return FALSE;

	//
	// Get EatherType from the IEEE 802.11packet without Qos and Security(IV) fields.
	// The Ndis sets the Exemption list with EatherTypes in "Big-Endian", thus transfer
	// the EatherType filed from "Little-Endian" to "Big-Endian".
	//
	TypeLengthBuf[0] = pduOS.Octet[30];
	TypeLengthBuf[1] = pduOS.Octet[31];
	TypeLength= (((u2Byte)EF1Byte(TypeLengthBuf[0]))<<8) + EF1Byte(TypeLengthBuf[1]); // host order.

	//
	// Check against each privacy exemption entry.
	//
	for(i = 0; i < pNdisCommon->PrivacyExemptionEntrieNum; i++)
	{
		usEtherType = pNdisCommon->PrivacyExemptionEntries[i].usEtherType;
		usExemptionActionType = pNdisCommon->PrivacyExemptionEntries[i].usExemptionActionType;
		usExemptionPacketType = pNdisCommon->PrivacyExemptionEntries[i].usExemptionPacketType;
		
		if(usEtherType != N2H2BYTE(TypeLength)) // TypeLength is in network order.
			continue; // check next
		
		if(usExemptionPacketType == DOT11_EXEMPT_MULTICAST)
		{
			if(!MacAddr_isMulticast(Frame_pDaddr(pduOS)))
				continue; // check next
		}
		else if(usExemptionPacketType == DOT11_EXEMPT_UNICAST)
		{
			if(MacAddr_isMulticast(Frame_pDaddr(pduOS)))
				continue; // check next
		}
		else if(usExemptionPacketType == DOT11_EXEMPT_BOTH)
		{
			// go down
		}
		else
		{// not recognized
			continue; // check next
		}

		if(usExemptionActionType == DOT11_EXEMPT_NO_EXEMPTION)
		{
			continue; // check next
		}
		else if(usExemptionActionType == DOT11_EXEMPT_ALWAYS)
		{
			if(Frame_WEP(pduOS))
			{ // the packet is encrypted, but the list alwyas discards the packets with privacy bit set.
				return TRUE;
			}

		}
		else if(usExemptionActionType == DOT11_EXEMPT_ON_KEY_MAPPING_KEY_UNAVAILABLE)
		{
			//
			// If we have a key mapping key corresponding to the transmitter address and 
			// the packet is in plain text then drop the packet.
			// If the key is unavailable, and the packet without privacy bit set, we should indicate this packet.
			//
			if(!Frame_WEP(pduOS)) // the packe is in plain text
			{
				PRT_WLAN_STA pEntry;
				PMGNT_INFO pMgntInfo = &Adapter->MgntInfo;

				pEntry = AsocEntry_GetEntry(pMgntInfo, Frame_pTaddr(pduOS));
				if(!pEntry)
				{
					// The key is unavailable and this packet is not encrypted.
				}
				else
				{
					static u1Byte EmptyPTK[PTK_LEN];
					PlatformZeroMemory(EmptyPTK, PTK_LEN);
			if(PlatformCompareMemory(pEntry->perSTAKeyInfo.PTK, EmptyPTK, PTK_LEN) != 0)
					{// plain text and key mapping key is unavailable, exempt this frame by returning TRUE
						return TRUE;
					}
					
				}
			}
			else
				continue; // check next
		}
		else
		{// not recognized
			continue; // check next
		}
			
	}	

	return FALSE;
	
}



VOID
InitializeAdapterTimerCallback(
	PRT_TIMER pTimer
	)
{
	PADAPTER Adapter = (PADAPTER)pTimer->Adapter;
	PRT_NDIS6_COMMON pNdisCommon = Adapter->pNdisCommon;

	PlatformScheduleWorkItem(&(pNdisCommon->InitializeAdapterWorkItem));

	return;
}

// Add by CCW. 20130416
VOID
PNPReConnentTimerCallback(
	PRT_TIMER pTimer
	)
{
	PADAPTER Adapter = (PADAPTER)pTimer->Adapter;
	PMGNT_INFO	pMgntInfo = &(Adapter->MgntInfo);
	PRT_NDIS6_COMMON pNdisCommon = Adapter->pNdisCommon;
	// Refent to  http://msdn.microsoft.com/en-us/library/windows/hardware/ff559418(v=vs.85).aspx
	// For a miniport adapter in the Pausing state, the miniport driver:
	// Can provide status indications with the NdisMIndicateStatusEx function.
#if 0
	if(N6C_GET_MP_DRIVER_STATE(Adapter) != MINIPORT_RUNNING && pNdisCommon->PNPconnentCout < MAX_PNP_RECONNENT_COUNTER )
	{
		RT_TRACE(COMP_INIT, DBG_LOUD, ("PNPReConnentTimerCallback(): Driver state(%d) != MINIPORT_RUNNING, Reset timer!\n", N6C_GET_MP_DRIVER_STATE(Adapter)));
		pNdisCommon->PNPconnentCout++;
		PlatformSetTimer( Adapter, &pNdisCommon->PNPReConnentTimer, 20);
		return;
	}

	if(pNdisCommon->PNPconnentCout == MAX_PNP_RECONNENT_COUNTER) // timeout for 500ms, the timeout value can be tuned by different platform or test cases.
	{
		DrvIFIndicateDisassociation(Adapter , unspec_reason, pMgntInfo->mAId + 1);
		return;
		// ?? try to dissaco AP !!
	}
#endif


	if(Adapter->MgntInfo.bSetPnpPwrInProgress)
	{
		// Add delay to prevent from being returned in MgntLinkRetry() by bSetPnpPwrInProgress flag.
		PlatformSetTimer( Adapter, &pNdisCommon->PNPReConnentTimer, 10);
		return;
	}

	//
	// <Roger_Notes> We indicate roaming start here while S3/S4 resume if we dissociated before sleep.
	// If we indicate roaming start before entering S4 state, 
	// which could suffers from roaming failure due to roaming timeout.
	// Added by Roger, 2007.11.07.
	//
	// Move from PNP wake flow to PNPReConnentTimerCallback(). 2013.04.
	DrvIFIndicateRoamingStart(Adapter);

	if(pNdisCommon->bDissociateBeforeSleep || !Adapter->MgntInfo.bMediaConnect)
	{
		MgntActSet_802_11_SSID(Adapter, Adapter->MgntInfo.Ssid.Octet, Adapter->MgntInfo.Ssid.Length, TRUE );
	}
	else
	{
		// If we do not disconnect to AP before PNP sleep, just call MgntLinkRetry() to roam to the original AP.
		PlatformMoveMemory(Adapter->MgntInfo.APaddrbeforeRoaming, Adapter->MgntInfo.Bssid, 6);
		Dot11d_Reset(Adapter);
		RemoveAllTS(Adapter); // Do this to reset TS data structure.
		MgntLinkRetry(Adapter, TRUE);
	}

	pNdisCommon->bWakeupAutoLinkInProgressing = FALSE;
	pNdisCommon->bDissociateBeforeSleep = FALSE;
}

VOID
D0RxIndicatTimerCallback(
	PRT_TIMER pTimer
	)
{
	PADAPTER 				Adapter = (PADAPTER)pTimer->Adapter;
	PRT_NDIS6_COMMON 		pNdisCommon = Adapter->pNdisCommon;
	PRT_RFD					*RfdArray;
	u2Byte					RfdCnt = 0;
	PRT_GEN_TEMP_BUFFER 	pGenBuf;
	PRT_RFD					TempRfd;

	RT_TRACE(COMP_TEST, DBG_LOUD, ("===> D0RxIndicatTimerCallback()\n"));

	PlatformAcquireSpinLock(Adapter, RT_RX_SPINLOCK);

	if( pNdisCommon->D0FilterState == RT_D0_FILTER_NONE ||  
		pNdisCommon->D0FilterState == RT_D0_FILTER_FLUSHING)
	{
		PlatformReleaseSpinLock( Adapter, RT_RX_SPINLOCK);
		return;
	}
	
	if( !RTIsListEmpty(&(pNdisCommon->D0FilterPktQueue)) )
	{
		pNdisCommon->D0FilterState = RT_D0_FILTER_FLUSHING;
		RfdCnt = 0;
		pGenBuf = GetGenTempBuffer (Adapter, sizeof(PRT_RFD)*REORDER_WIN_SIZE);
		RfdArray = (PRT_RFD *)pGenBuf->Buffer.Ptr;
		
		while(!RTIsListEmpty(&pNdisCommon->D0FilterPktQueue))
		{
			TempRfd = (PRT_RFD)RTRemoveHeadList(&pNdisCommon->D0FilterPktQueue);
			RfdArray[RfdCnt] = TempRfd;
			RfdCnt = RfdCnt + 1;
		}
		
		DrvIFD0RxIndicatePackets(Adapter , RfdArray , RfdCnt);
		
		ReturnGenTempBuffer(Adapter, pGenBuf);
	}
	RT_TRACE(COMP_TEST, DBG_LOUD, ("<=== D0RxIndicatTimerCallback() : Flush OK\n"));
	pNdisCommon->D0FilterState = RT_D0_FILTER_INIT;
	PlatformReleaseSpinLock( Adapter, RT_RX_SPINLOCK);
}

//
// To faster NIC init time for OS.
//
VOID
InitializeAdapterWorkItemCallback(
	IN PVOID			pContext
	)
{
	PADAPTER pAdapter = (PADAPTER)pContext;

	InitializeAdapterCommon(pAdapter);
}


VOID 
InitializeAdapterThread(
    IN PVOID                    pContext
    )
{
	InitializeAdapterCommon(((PRT_THREAD)pContext)->Adapter);
}

//
// Gather same code for initialize adapter workitem and callback API.
//
#define	MAX_INIT_RETRY_CNT	10

BOOLEAN
MatchInitPendingOid(
	IN	PNDIS_OID_REQUEST	NdisRequest
	)
{
	u1Byte index = 0;

	for(index=0; index< (sizeof(NdisInitPendingOid)/sizeof(OID)); index++)
	{

		RT_TRACE(COMP_OID_SET, DBG_TRACE, ("MatchInitPendingOid OID (0x%x) type (0x%x) \n", NdisInitPendingOid[index].Oid, NdisInitPendingOid[index].Type));
	
		if(NdisInitPendingOid[index].Type == NdisRequest->RequestType &&
			NdisInitPendingOid[index].Oid == NdisRequest->DATA.METHOD_INFORMATION.Oid)
		{
			return TRUE;
		}
	}

	return FALSE;
}

BOOLEAN
InitializePendingOIDBeforeReady(
	IN	PADAPTER				Adapter,
	IN	PNDIS_OID_REQUEST	NdisRequest,
	OUT	PNDIS_STATUS			pRetVal
	)
{
	
	if(N6_INIT_READY(Adapter))
	{
		return FALSE;
	}
	else	
	{

		RT_TRACE( COMP_INIT, DBG_LOUD, ("InitializePendingOIDBeforeReady(): QUERY_INFORMATION OID(%#x) when initializing.\n", NdisRequest->DATA.QUERY_INFORMATION.Oid));
		RT_TRACE( COMP_INIT, DBG_LOUD, ("InitializePendingOIDBeforeReady(): METHOD_INFORMATION OID(%#x) when initializing.\n", NdisRequest->DATA.METHOD_INFORMATION.Oid));

		N6C_DOT11_DUMP_OID(NdisRequest->DATA.METHOD_INFORMATION.Oid);

		if(MatchInitPendingOid(NdisRequest))
		{
			RT_TRACE(COMP_INIT, DBG_LOUD, ("InitializePendingOIDBeforeReady(): OID when initializing. return PENDING\n"));
			*pRetVal = NDIS_STATUS_PENDING;
			return TRUE;
		}

		// Reset request is skip during initialization.
		if(NdisRequest->RequestType==NdisRequestMethod|| NdisRequest->RequestType==NdisRequestSetInformation)
		{
		
			if(NdisRequest->DATA.METHOD_INFORMATION.Oid==OID_DOT11_RESET_REQUEST)
			{		
			
				//
				// <Roger_Notes> We need to check whether OutputBufferLength is valid prior to following status indication.
				// 2014.05.02.
				//
				if ( NdisRequest->DATA.METHOD_INFORMATION.OutputBufferLength >= sizeof(DOT11_STATUS_INDICATION) )
				{
				
					PDOT11_STATUS_INDICATION pDot11StatusIndication = NdisRequest->DATA.METHOD_INFORMATION.InformationBuffer;

					PlatformZeroMemory(pDot11StatusIndication, sizeof(DOT11_STATUS_INDICATION));
					pDot11StatusIndication->uStatusType = DOT11_STATUS_RESET_CONFIRM;
					pDot11StatusIndication->ndisStatus = NDIS_STATUS_SUCCESS;
					NdisRequest->DATA.METHOD_INFORMATION.BytesRead = sizeof(DOT11_RESET_REQUEST);// After reset operation was completed, return the status indication. 

					RT_TRACE( COMP_INIT, DBG_LOUD, ("InitializePendingOIDBeforeReady(): OID_DOT11_RESET_REQUEST when initializing. return SUCCESS\n"));
					
					*pRetVal = NDIS_STATUS_SUCCESS;
					return TRUE;
				}				
			}
		}

		// No specific handle in initialization.
		return FALSE;
	}
}

InitializeCompleteOID(
	IN 	PADAPTER 	pAdapter
)
{
	PRT_NDIS6_COMMON	pNdisCommon = pAdapter->pNdisCommon;

	FunctionIn(COMP_MLME);

	PlatformAcquireSpinLock(pAdapter, RT_PENDED_OID_SPINLOCK);
	if(pNdisCommon->PendedRequest!= NULL)
	{
		if(MatchInitPendingOid(pNdisCommon->PendedRequest))
		{
			 RT_TRACE(COMP_INIT, DBG_LOUD, 
					("InitializeAdapterCommon(): (1) complete PendedRequest(%p) call N6PciOidRequest()!!!++++++++\n", pNdisCommon->PendedRequest));
			
			N6C_DOT11_DUMP_OID(pNdisCommon->PendedRequest->DATA.METHOD_INFORMATION.Oid);

			RT_TRACE(COMP_INIT, DBG_LOUD, 
					("InitializeAdapterCommon(): complete PendedRequest(%p) ++++++++\n", pNdisCommon->PendedRequest));
						
			PlatformReleaseSpinLock(pAdapter, RT_PENDED_OID_SPINLOCK);

			N6CompletePendedOID(pAdapter, RT_PENDED_OID_DONT_CARE, 
			N6SdioOidRequest((NDIS_HANDLE)pAdapter, pNdisCommon->PendedRequest));
		}
		else
		{
			PlatformReleaseSpinLock(pAdapter, RT_PENDED_OID_SPINLOCK);
			RT_TRACE(COMP_INIT, DBG_LOUD, ("InitializeAdapterCommon(): No matched OID complete ++++++++\n"));
		}
	}
	else
	{
		PlatformReleaseSpinLock(pAdapter, RT_PENDED_OID_SPINLOCK);
	}
}


static	VOID
InitializeAdapterCommon(
	IN 	PADAPTER 	pAdapter
	)
{
	PMGNT_INFO 	pMgntInfo = &pAdapter->MgntInfo;
	PRT_POWER_SAVE_CONTROL	pPSC = NULL;
	RT_STATUS	RTStatus = RT_STATUS_SUCCESS;
	BOOLEAN		bInitFail = FALSE;

	RT_TRACE(COMP_INIT, DBG_LOUD, ("-->InitializeAdapterCommon\n"));

	if(pAdapter->bDriverIsGoingToUnload)
	{
		RT_TRACE(COMP_INIT, DBG_LOUD, ("bDriverIsGoingToUnload before\n"));
		goto Exit;
	}

	while(!pAdapter->initfinish)
	{
		RT_TRACE(COMP_INIT, DBG_LOUD, ("pAdapter->initfinish %d \n", pAdapter->initfinish));
		delay_ms(1);
	}
	delay_ms(1);	// this delay is use toe avoid this wokritem interrupt into n6pciinitialize!!!!
	
	if(pAdapter->bDriverIsGoingToUnload)
	{
		RT_TRACE(COMP_INIT, DBG_LOUD, ("bDriverIsGoingToUnload after\n"));
		goto Exit;		
	}
	
	pAdapter->bInitializeInProgress=TRUE;

	// 2015/02/25 MH Merge from merge temp, illegal methid to support velocity initialize.
	if(pMgntInfo->RegPreInitMem)
		RTStatus = RT_STATUS_SUCCESS;
	else
		RTStatus = NicIFAllocateMemory(pAdapter);
	if(RTStatus != RT_STATUS_SUCCESS)
	{
		bInitFail = TRUE; // Fail to perform the init process.
		N6WriteEventLog(pAdapter, RT_INIT_FAIL, 0);
		RT_TRACE(COMP_INIT, DBG_LOUD, ("NicIFAllocateMemory fail\n"));
		goto Exit;
	}

	PlatformAcquireSpinLock(pAdapter, RT_RX_SPINLOCK);
	PrepareAllRxDescBuffer(pAdapter);
	PlatformReleaseSpinLock(pAdapter, RT_RX_SPINLOCK);

	// move allocate memory here to reduce the time at the platform HP95 Toshiba.!! neo
	// need to check return value if allocate memory fail.
	ADAPTER_SET_STATUS_FLAG(pAdapter, ADAPTER_STATUS_FIRST_INIT);

	RTStatus = pAdapter->HalFunc.InitializeAdapterHandler(pAdapter, pAdapter->MgntInfo.dot11CurrentChannelNumber);

	if(RTStatus != RT_STATUS_SUCCESS)
	{
		N6WriteEventLog(pAdapter, RT_INIT_FAIL, 0);
		NicIFResetMemory(pAdapter);
		bInitFail = TRUE;
		goto Exit;
	}
	
	PlatformSetCheckForHangTimer(pAdapter);
	
	ADAPTER_CLEAR_STATUS_FLAG(pAdapter, ADAPTER_STATUS_FIRST_INIT);

	MultiPortSetAllPortsHWReadyStatus(pAdapter, TRUE);
	
	//
	// <Roger_Notes> We shall restore default ASPM settings If Non-ATI solution.
	// 2009.03.23.
	//
	pPSC = GET_POWER_SAVE_CONTROL(pMgntInfo);
	if(pPSC->RegRfPsLevel & RT_RF_PS_LEVEL_ALWAYS_ASPM)
	{
		// Always enable ASPM & Clock Req.
		RT_ENABLE_ASPM(pAdapter);
		RT_SET_PS_LEVEL(pAdapter, RT_RF_PS_LEVEL_ALWAYS_ASPM);
	}

	//DrvIFIndicateCurrentPhyStatus(pAdapter);

Exit:
	
	pAdapter->bInitializeInProgress=FALSE;

	// 
	// <Roger_Notes> If we had already returned control for the InitializeHandlerEx routine 
	// and we can removes the specified miniport driver adapter that the miniport driver 
	// has determined is unrecoverable from the system.
	// 2010.06.24.
	//
	if(pAdapter->initfinish && bInitFail)
	{
		N6CompletePendedOID(pAdapter, RT_PENDED_OID_DONT_CARE,  NDIS_STATUS_FAILURE);
		RT_TRACE(COMP_INIT, DBG_SERIOUS, ("InitializeAdapterCommon(): Fail to initialize NIC, remove it from the system!!\n"));
		if(!pAdapter->bDriverIsGoingToUnload)
		NdisMRemoveMiniport(pAdapter->pNdisCommon->hNdisAdapter);
	}
	else
	{

		// Clear interrupt before enable interrupt.
		NicIFClearInterrupt(pAdapter);
		NicIFEnableInterrupt(pAdapter);	
	
		// 2012/03/27 hpfan Add for win8 DTM DPC ISR test
		if(pAdapter->bUseThreadHandleInterrupt)
		{
			PlatformSetEventTrigerThread(
				pAdapter,
				&pAdapter->InterruptThread,
				PASSIVE_LEVEL,
				NULL);	
		}

		InitializeCompleteOID(pAdapter);
		
		pAdapter->bInitComplete = TRUE;		

		RT_SET_DRV_STATE(pAdapter, DrvStateHwInitialized);
	}

	RT_TRACE(COMP_INIT, DBG_LOUD, ("<--InitializeAdapterCommon\n"));
	
}	// InitializeAdapterCommon

PVOID 
GetTwoPortSharedResource(
	IN		PADAPTER	Adapter,
	IN		u1Byte		Type_ToGet,
	IN OUT	PVOID		pVariable_ToGet, // For more readable on caller side.
	OUT		pu1Byte		pTwoPortStatus // For convinient (pu1Byte)NULL is OK.
	)
{
	// XP/Vista not yet take into consideration, Use Platform
	PADAPTER pDefaultAdapter	= GetDefaultAdapter(Adapter);
	PADAPTER ExtAdapter	= GetFirstAPAdapter(Adapter);
	u1Byte 	 TwoPortStatus = (u1Byte)TWO_PORT_STATUS__DEFAULT_ONLY;
		
	//(1.)  ======  Decide TWO_PORT_STATUS  ======
	if(ExtAdapter == NULL || ExtAdapter == pDefaultAdapter)
	{
		if(pDefaultAdapter->MgntInfo.mAssoc==TRUE)
			TwoPortStatus = TWO_PORT_STATUS__DEFAULT_ONLY;
		else
			TwoPortStatus = TWO_PORT_STATUS__WITHOUT_ANY_ASSOCIATE;
	}
	else if( //(Adapter==ExtAdapter)&&(ExtAdapter->MgntInfo.mAssoc_start==TRUE)
		(pDefaultAdapter->MgntInfo.mAssoc==TRUE)
		&&(ExtAdapter->MgntInfo.OpMode==RT_OP_MODE_AP) // a dangerous way depend on compiler
		&&(pDefaultAdapter->MgntInfo.dot11CurrentWirelessMode&WIRELESS_MODE_G)
		&&(IS_WIRELESS_MODE_N(ExtAdapter)) )
	{
		TwoPortStatus = TWO_PORT_STATUS__DEFAULT_G_EXTENSION_N20;
	}
	else if( //(Adapter==ExtAdapter)&&(ExtAdapter->MgntInfo.mAssoc_start==TRUE)
		(pDefaultAdapter->MgntInfo.mAssoc==TRUE)
		&&(ExtAdapter->MgntInfo.OpMode==RT_OP_MODE_AP) // a dangerous way depend on compiler
		&&(pDefaultAdapter->MgntInfo.dot11CurrentWirelessMode&WIRELESS_MODE_A)
		&&(IS_WIRELESS_MODE_N(ExtAdapter)))
	{		
		TwoPortStatus = TWO_PORT_STATUS__DEFAULT_A_EXTENSION_N20;
	}
	else if(
		(pDefaultAdapter->MgntInfo.mAssoc==TRUE)
		&&(ExtAdapter->MgntInfo.OpMode==RT_OP_MODE_AP) )
	{
		TwoPortStatus = TWO_PORT_STATUS__EXTENSION_FOLLOW_DEFAULT;
	}
	else if(
		(pDefaultAdapter->MgntInfo.mAssoc==TRUE)&&(ExtAdapter->MgntInfo.OpMode!=RT_OP_MODE_AP)
		)
	{
		TwoPortStatus = TWO_PORT_STATUS__DEFAULT_ONLY;
	}
	else if(
		(pDefaultAdapter->MgntInfo.mAssoc==FALSE)&&(ExtAdapter->MgntInfo.OpMode==RT_OP_MODE_AP)
		)
	{
		TwoPortStatus = TWO_PORT_STATUS__EXTENSION_ONLY;
	}
	else if(
		(pDefaultAdapter->MgntInfo.mAssoc==FALSE)&&(pDefaultAdapter->MgntInfo.mIbss==TRUE)
	)
	{
		TwoPortStatus = TWO_PORT_STATUS__ADHOC;
	}
	else
	{
		TwoPortStatus = TWO_PORT_STATUS__WITHOUT_ANY_ASSOCIATE;
	}
	
	if(pTwoPortStatus != NULL)
		*pTwoPortStatus = (u1Byte)TwoPortStatus;
	
	//(2.)  ======  For Each Type of Share Resources Deside its Adapter Source  ======
	switch (Type_ToGet)
	{
	//===================================================================
	case TWO_PORT_SHARED_OBJECT__STATUS:
		return pTwoPortStatus;		
	break;
	//===================================================================		
	case TWO_PORT_SHARED_OBJECT__SET_OF_pStaQos_WMMParamEle:
	{		
		switch(TwoPortStatus)
		{
		case TWO_PORT_STATUS__EXTENSION_ONLY:
			return (pu1Byte)ExtAdapter->MgntInfo.pStaQos->WMMParamEle;// how to handle the pointer?
			break;
		default:			
			return (pu1Byte)pDefaultAdapter->MgntInfo.pStaQos->WMMParamEle;// how to handle the pointer?
			break;
		}
	}
	break;
	//===================================================================
	case TWO_PORT_SHARED_OBJECT__SET_OF_pStaQos:
	{
		switch(TwoPortStatus)
		{
		case TWO_PORT_STATUS__EXTENSION_ONLY:
			*(PQOS_MODE)pVariable_ToGet = ExtAdapter->MgntInfo.pStaQos->CurrentQosMode;
			break;
		default:			
			*(PQOS_MODE)pVariable_ToGet = pDefaultAdapter->MgntInfo.pStaQos->CurrentQosMode;
			break;
		}
		break;
	}
	//===================================================================
	// In this case, TwoPortStatus will be treated as BW to output.
	case TWO_PORT_SHARED_OBJECT__BW:
	{
		switch(TwoPortStatus)
		{
		case TWO_PORT_STATUS__DEFAULT_G_EXTENSION_N20:
		case TWO_PORT_STATUS__DEFAULT_A_EXTENSION_N20:
		case TWO_PORT_STATUS__ADHOC:	
			if (pTwoPortStatus != NULL)
			{
				*pTwoPortStatus = (u1Byte)CHANNEL_WIDTH_20;// cast issue
			}
			break;
			
		case TWO_PORT_STATUS__EXTENSION_ONLY:
			if (pTwoPortStatus != NULL)
			{
				*pTwoPortStatus = (u1Byte)ExtAdapter->MgntInfo.dot11CurrentChannelBandWidth;
			}
			break;
			
		case TWO_PORT_STATUS__EXTENSION_FOLLOW_DEFAULT:	
		case TWO_PORT_STATUS__DEFAULT_ONLY:
			if (pTwoPortStatus != NULL)
			{
				*pTwoPortStatus = (u1Byte)pDefaultAdapter->MgntInfo.dot11CurrentChannelBandWidth;
			}
			break;

		case TWO_PORT_STATUS__WITHOUT_ANY_ASSOCIATE:
			if (pTwoPortStatus != NULL)
			{
				if (Adapter->MgntInfo.pVHTInfo->bEnableVHT)
					*pTwoPortStatus = CHNL_GetRegBWSupport(Adapter);// cast issue
				else
					*pTwoPortStatus = CHNL_GetRegBWSupport(Adapter) ? CHANNEL_WIDTH_40 : CHANNEL_WIDTH_20;// cast issue
			}
			break;
		}
	}
	break;
	//===================================================================	
	default:
		return NULL;
	break;
	//===================================================================
		
	}
	return NULL;
}

VOID
N6WriteEventLog(

	IN PADAPTER Adapter, 
	IN RT_EVENTLOG_TYPE type,
	IN	ULONG	Num
	)
{
	PWCHAR ErrorString; 	

	// 20101025 Joseph: Merge with 91SE code base. This prevent the system unknown log shown on event view.
	// Without this log, customer may not be confused.
	return;
	
	switch(type)
	{
	case RT_FW_DOWLOAD_F:
		ErrorString = L"FW_DL_F";
		break;
	case RT_INIT_HANDLER_FAIL:
		ErrorString = L"RT_INIT_HANDLER_FAIL";
		break;
	case RT_INIT_FAIL:
		ErrorString = L"RT_INIT_FAIL";
		break;
	case RT_MUTUAL_AUTHENTICATION_FAIL:
		ErrorString = L" RT_MUTUAL_AUTHENTICATION_FAIL";
		break;
	case RT_INIT_OK:
		ErrorString = L"RT_INIT_OK";
		break;
	case RT_SCAN_START:
		ErrorString = L"OID_SCAN_REQUEST";
		break;
	case RT_SCAN_COMPLETE:
		ErrorString = L"OID_SCAN_COMPLETE";
		break;
	case RT_SCAN_NUM:
		ErrorString = L"OID_SCAN_NUM";
		break;
	case RT_TKIP_MIC_ERROR:
		ErrorString = L"TKIP_MIC_ERROR";
		break;
	case RT_HW_AIRPLANE_MODE_TRIGGERED:
		ErrorString = L"RT_HW_AIRPLANE_MODE_TRIGGERED";
		break;
	default:
		ErrorString = L"NO_EVENT_INDICATED";
		break;
	}

	NdisWriteEventLogEntry(
		GlobalRtDriverContext.NdisContext.pDriverObject,
		NDIS_STATUS_SUCCESS, 
		2, 
		1,	 
		&ErrorString, 
		sizeof(Num), 
		&Num);
}

VOID
N6CWriteEventLogEntry(
	IN  ADAPTER					*pAdapter,
	IN  NDIS_STATUS				ndisStatus,
	IN  ULONG					dataLen,
	IN  u1Byte					*data,
	IN  WCHAR					*wcFormat,
	IN  ...
	)
{
#if DBG

	va_list						argPtr;
	RT_GEN_TEMP_BUFFER			*buf = NULL;

	va_start(argPtr, wcFormat);

	do
	{
		if(NULL == (buf = GetGenTempBuffer(pAdapter, GEN_TEMP_BUFFER_SIZE)))
			break;

		if(vswprintf_s(
			(wchar_t *)buf->Buffer.Ptr, // buffer
			buf->Buffer.Length / sizeof(wchar_t), // numberOfElements
			wcFormat, // format
			argPtr // argptr
			))
		{
			NdisWriteEventLogEntry(
				GlobalRtDriverContext.NdisContext.pDriverObject,
				ndisStatus, 
				2, 
				1,	 
				&buf->Buffer.Ptr, 
				dataLen, 
				data);
		}
	}while(FALSE);

	if(buf)
		ReturnGenTempBuffer(pAdapter, buf);

	va_end(argPtr);
	
#endif

	return;
}


VOID
N6CSendSingleNetBufferListTimerCallback(
		IN	PRT_TIMER	pTimer)
{

	PADAPTER		Adapter = pTimer->Adapter;
	PRT_NDIS6_COMMON	pNdisCommon = Adapter->pNdisCommon;
	PRT_SDIO_DEVICE		pDevice = GET_RT_SDIO_DEVICE(Adapter);
	PNET_BUFFER		pCurrNetBuffer = NULL;
	PNET_BUFFER_LIST	pCurrNetBufferList = NULL;

	if(PlatformAtomicExchange(&Adapter->IntrNBLRefCount, TRUE)==TRUE)
		return;
	
	PlatformAcquireSpinLock( Adapter, RT_TX_SPINLOCK);
	if(pDevice->SendingNetBufferList == NULL)
	{
		while(TRUE)
		{
			PlatformAcquireSpinLock(Adapter, RT_BUFFER_SPINLOCK);
			if(N6CIsNblWaitQueueEmpty(pNdisCommon->TxNBLWaitQueue))
			{
				PlatformReleaseSpinLock(Adapter, RT_BUFFER_SPINLOCK);
				break;
			}

			if(!pNdisCommon->bReleaseNblWaitQueueInProgress)
			{
				pCurrNetBufferList = N6CGetHeadNblWaitQueue(pNdisCommon->TxNBLWaitQueue);
				PlatformReleaseSpinLock(Adapter, RT_BUFFER_SPINLOCK);

				if( N6C_CANNOT_TX(Adapter) )
				{
					RT_TRACE(COMP_SEND, DBG_TRACE, ("break for CANNOT_TX()\n"));
					break;
				}

				RT_TRACE(COMP_SEND, DBG_TRACE, (" handle the NBL buffered(%p)\n", pCurrNetBufferList ));

				if( !N6SdioSendSingleNetBufferList(
						Adapter, 
						pCurrNetBufferList,
						TRUE) ) // bFromQueue
				{
					RT_TRACE(COMP_SEND, DBG_TRACE, ("N6CSendSingleNetBufferList() returns FALSE\n"));
					break;
				}
			}
			else
			{
				RT_TRACE(COMP_SEND, DBG_TRACE, ("N6CSendSingleNetBufferList(): bReleaseNblWaitQueueInProgress\n"));
				PlatformReleaseSpinLock(Adapter, RT_BUFFER_SPINLOCK);
			}
		}
	}
	PlatformReleaseSpinLock( Adapter, RT_TX_SPINLOCK);

	PlatformAtomicExchange(&Adapter->IntrNBLRefCount, FALSE);
	return;
}

BOOLEAN
N6CQueryPhyIdReady(
	IN	PADAPTER			Adapter)
{

	BOOLEAN 	bResult = TRUE;
	PADAPTER 	pLoopAdapter = GetDefaultAdapter(Adapter);
	PMGNT_INFO	pMgntInfo = &(pLoopAdapter->MgntInfo);
	
	//
	// <Roger_Notes> The following configuration is for airplane mode synchronization problem on Win8 or later version
	// 2013.06.07.
	//	
	while(pLoopAdapter)
	{
		// We only take first two ports into consideration
		if((GET_PORT_NUMBER(pLoopAdapter) == 0) ||
			(GET_PORT_NUMBER(pLoopAdapter) == 1))
		{			
			if(pLoopAdapter->pNdis62Common->bDot11SetPhyIdReady == FALSE)
			{
				RT_TRACE(COMP_RF, DBG_TRACE, ("N6CQueryPhyIdReady(): pLoopAdapter(%p), PortType(%d), bDot11SetPhyIdReady(%d)\n", 
					pLoopAdapter, pLoopAdapter->pNdis62Common->PortType, pLoopAdapter->pNdis62Common->bDot11SetPhyIdReady));
				bResult = FALSE;
				break;
			}			
		}	
		pLoopAdapter = GetNextExtAdapter(pLoopAdapter);	
	}

	return bResult;
}

#if DRV_LOG_REGISTRY

//
//	Description: 
//		Save current driver state in specific registry
//
//	Assumption: 
//		1. Running at PASSIVE_LEVEL
//
//	Added by Roger, 2013.12.13.
//
NDIS_STATUS
N6WriteRegDriverState(
	IN	PADAPTER		Adapter,
	IN	ULONG			State)
{
	NDIS_HANDLE			ConfigurationHandle;
	NDIS_STATUS			Status;
	NDIS_CONFIGURATION_PARAMETER	ConfigParam;
	NDIS_STRING			KeyName = NDIS_STRING_CONST("DrvState");
	u1Byte				TimeOut = 100;


	if (KeGetCurrentIrql() > PASSIVE_LEVEL)	
	{
		RT_TRACE(COMP_INIT,DBG_WARNING, ("N6WriteRegDriverState(): Current IRQL>PASSIVE_LEVEL!!\n"));
		return NDIS_STATUS_FAILURE;
	}

	if(Adapter->bDriverStopped)
		return NDIS_STATUS_FAILURE;

	while(PlatformAtomicExchange(&Adapter->WriteRegRefCount, TRUE) == TRUE) 
	{
		RT_TRACE(COMP_INIT, DBG_WARNING, ("N6WriteRegDriverState(): Waiting for previous process...\n"));
		PlatformSleepUs(100);
		TimeOut--;
		if(TimeOut <= 0)
		{
			RT_TRACE(COMP_INIT,DBG_WARNING, ("N6WriteRegDriverState(): Wait too long!!\n"));
			return NDIS_STATUS_FAILURE;
		}
	}
	
	// Open the registry for this adapter.
	Status = N6OpenConfigurationHandle(Adapter, &ConfigurationHandle);
	if(Status != NDIS_STATUS_SUCCESS)
	{
		RT_TRACE(COMP_INIT,DBG_TRACE, ("N6WriteRegDriverState(): Status=%x\n", Status));
		return Status;
	}
	
	ConfigParam.ParameterType = NdisParameterInteger;
	ConfigParam.ParameterData.IntegerData = State;
	
	NdisWriteConfiguration(
		&Status,
		ConfigurationHandle,
		&KeyName,
		&ConfigParam);

	// Close the registry
	N6CloseConfigurationHandle(ConfigurationHandle);

	PlatformAtomicExchange(&Adapter->WriteRegRefCount, FALSE);
	
	return Status;
}

#endif

//
//	Description:
//		Write "DbgMonitor" registry value
//	2014.05.09, by Cosa.
//
NDIS_STATUS
N6WriteRegDbgMonitor(
	IN	PADAPTER		Adapter,
	IN	u4Byte			DbgMonitor
	)
{
	NDIS_HANDLE			ConfigurationHandle;
	NDIS_STATUS			Status;
	NDIS_CONFIGURATION_PARAMETER	ConfigParam;
	NDIS_STRING			KeyName = NDIS_STRING_CONST("DbgMonitor");
	
	// Open the registry for this adapter.
	Status = N6OpenConfigurationHandle(Adapter, &ConfigurationHandle);
	if(Status != NDIS_STATUS_SUCCESS)
	{
		RT_TRACE_EX(COMP_DBG_MON, DBG_LOUD, ("N6WriteRegDbgMonitor(): Fail!!!, Status=%x\n", Status));
		return Status;
	}
	
	ConfigParam.ParameterType = NdisParameterInteger;
	ConfigParam.ParameterData.IntegerData = DbgMonitor;
	
	NdisWriteConfiguration(
		&Status,
		ConfigurationHandle,
		&KeyName,
		&ConfigParam);

	// Close the registry
	N6CloseConfigurationHandle(ConfigurationHandle);
	RT_TRACE_EX(COMP_DBG_MON, DBG_LOUD, ("N6WriteRegDbgMonitor(): Success!!!, DbgMonitor=%x\n", DbgMonitor));
	return Status;
}

VOID
NdisOIDHistoryInit(
	IN PADAPTER pAdapter)
{
	PRT_NDIS_COMMON	pNdisCommon = pAdapter->pNdisCommon;
	
	RT_TRACE(COMP_OID_SET, DBG_TRACE, ("NdisOIDHistoryInit() ====> \n"));
	
	NdisAllocateSpinLock(&(pNdisCommon->OidHistorySpinLock));
	RTInitializeListHead(&pNdisCommon->OidHistoryList);
	pNdisCommon->OidHistoryCount = 0;
	
	RT_TRACE(COMP_OID_SET, DBG_TRACE, ("NdisOIDHistoryInit() <==== \n"));
}

VOID
NdisOIDHistoryDeInit(
	IN PADAPTER pAdapter)
{
	PRT_NDIS_COMMON	pNdisCommon = pAdapter->pNdisCommon;
	PRT_OID_HISTORY_ENTRY	pOidHistoryEntry = NULL;
	
	RT_TRACE(COMP_OID_SET, DBG_TRACE, ("====> NdisOIDHistoryDeInit()\n"));

	NdisAcquireSpinLock(&(pNdisCommon->OidHistorySpinLock));

	RT_TRACE(COMP_OID_SET, DBG_TRACE, ("%s: pNdisCommon->OidHistoryCount %d\n", __FUNCTION__, pNdisCommon->OidHistoryCount));
	
	if( pNdisCommon->OidHistoryCount > 0 )
	{
		do{
			pOidHistoryEntry = (PRT_OID_HISTORY_ENTRY)RTRemoveHeadListWithCnt(&pNdisCommon->OidHistoryList, &pNdisCommon->OidHistoryCount);
			RT_TRACE(COMP_OID_SET, DBG_TRACE, ("%s: pNdisCommon->OidHistoryCount %d\n", __FUNCTION__, pNdisCommon->OidHistoryCount));

			if( pOidHistoryEntry->InformationBuffer != NULL )
			{
				RT_TRACE(COMP_OID_SET, DBG_TRACE, ("%s: Free information buffer %p, length: %d\n", __FUNCTION__, pOidHistoryEntry->InformationBuffer, pOidHistoryEntry->InformationBufferLength));
				RT_PRINT_DATA(COMP_OID_SET, DBG_TRACE, ("Free information buffer:"), pOidHistoryEntry->InformationBuffer, pOidHistoryEntry->InformationBufferLength);
				PlatformFreeMemory(pOidHistoryEntry->InformationBuffer, pOidHistoryEntry->InformationBufferLength);
				pOidHistoryEntry->InformationBuffer = NULL;
				pOidHistoryEntry->InformationBufferLength = 0;
			}

			RT_TRACE(COMP_OID_SET, DBG_TRACE, ("%s: Free OID history entry.\n", __FUNCTION__));
			PlatformFreeMemory(pOidHistoryEntry, sizeof(RT_OID_HISTORY_ENTRY));
		}while( RTIsListNotEmpty(&pNdisCommon->OidHistoryList) );
	}

	NdisReleaseSpinLock(&(pNdisCommon->OidHistorySpinLock));
	NdisFreeSpinLock(&(pNdisCommon->OidHistorySpinLock));
	
	RT_TRACE(COMP_OID_SET, DBG_TRACE, ("<==== NdisOIDHistoryDeInit()\n"));
}

VOID
NdisOIDHistoryUpdate(
	IN	PADAPTER			pAdapter,
	IN	PNDIS_OID_REQUEST	pNdisRequest,
	IN	RT_OID_HISTORY_STATE		OidHistoryState)
{
	PRT_NDIS_COMMON		pNdisCommon = pAdapter->pNdisCommon;
	PRT_OID_HISTORY_ENTRY	pOidHistoryEntry = NULL;
	PWDI_MESSAGE_HEADER	pWdiHeader = (PWDI_MESSAGE_HEADER)pNdisRequest->DATA.METHOD_INFORMATION.InformationBuffer;

	RT_TRACE(COMP_OID_SET, DBG_TRACE, ("====> %s\n", __FUNCTION__));

	do
	{
		if( pAdapter == NULL )
			break;
		
		RT_TRACE(COMP_OID_SET, DBG_TRACE, ("%s: pNdisCommon->OidHistoryCount %d\n", __FUNCTION__, pNdisCommon->OidHistoryCount));
		
		if( pNdisCommon->OidHistoryCount >= OID_HISTORY_MAX_NUM )
		{
			NdisAcquireSpinLock(&(pNdisCommon->OidHistorySpinLock));
			pOidHistoryEntry = (PRT_OID_HISTORY_ENTRY)RTRemoveHeadListWithCnt(&pNdisCommon->OidHistoryList, &pNdisCommon->OidHistoryCount);
			RT_TRACE(COMP_OID_SET, DBG_TRACE, ("%s: remove entry from head, pNdisCommon->OidHistoryCount %d\n", __FUNCTION__, pNdisCommon->OidHistoryCount));
			NdisReleaseSpinLock(&(pNdisCommon->OidHistorySpinLock));

			if( pOidHistoryEntry->InformationBuffer != NULL )
			{
				PlatformFreeMemory(pOidHistoryEntry->InformationBuffer, pOidHistoryEntry->InformationBufferLength);
				pOidHistoryEntry->InformationBuffer = NULL;
				pOidHistoryEntry->InformationBufferLength = 0;
			}
			pOidHistoryEntry->CurrentState = OidHistoryState;
			pOidHistoryEntry->CurrentTime = PlatformGetCurrentTime();
			pOidHistoryEntry->Oid = pNdisRequest->DATA.METHOD_INFORMATION.Oid;
			RT_TRACE(COMP_OID_SET, DBG_TRACE, ("pOidHistoryEntry->Oid: 0x%X\r\n", pOidHistoryEntry->Oid));
			RT_TRACE(COMP_OID_SET, DBG_TRACE, ("pOidHistoryEntry->CurrentState: %d\r\n", pOidHistoryEntry->CurrentState));
			RT_TRACE(COMP_OID_SET, DBG_TRACE, ("pOidHistoryEntry->CurrentTime: %I64d\r\n", pOidHistoryEntry->CurrentTime));
			
			pOidHistoryEntry->RequestType = pNdisRequest->RequestType;
			switch (pOidHistoryEntry->RequestType)
			{
				// Query
				case NdisRequestQueryInformation:			
					pOidHistoryEntry->InformationBufferLength = pNdisRequest->DATA.QUERY_INFORMATION.InformationBufferLength;	
					break;
					
				// Set
				case NdisRequestSetInformation:
					pOidHistoryEntry->InformationBufferLength = pNdisRequest->DATA.SET_INFORMATION.InformationBufferLength;
					break;

				// Method
				case NdisRequestMethod:
					pOidHistoryEntry->InformationBufferLength = pNdisRequest->DATA.METHOD_INFORMATION.InputBufferLength;
					break;
			}
			RT_TRACE(COMP_OID_SET, DBG_TRACE, ("pOidHistoryEntry->InformationBufferLength: %#x\r\n", pOidHistoryEntry->InformationBufferLength));

			if( pOidHistoryEntry->InformationBufferLength > 0 )
			{
				if( PlatformAllocateMemory(pAdapter, &pOidHistoryEntry->InformationBuffer, pOidHistoryEntry->InformationBufferLength) == RT_STATUS_SUCCESS )
				{
					switch (pOidHistoryEntry->RequestType)
					{
						// Query
						case NdisRequestQueryInformation:				
							PlatformMoveMemory(pOidHistoryEntry->InformationBuffer, pNdisRequest->DATA.QUERY_INFORMATION.InformationBuffer, pNdisRequest->DATA.QUERY_INFORMATION.InformationBufferLength);
							break;
						// Set
						case NdisRequestSetInformation:
							PlatformMoveMemory(pOidHistoryEntry->InformationBuffer, pNdisRequest->DATA.SET_INFORMATION.InformationBuffer, pNdisRequest->DATA.SET_INFORMATION.InformationBufferLength);
							break;

						// Method
						case NdisRequestMethod:
							PlatformMoveMemory(pOidHistoryEntry->InformationBuffer, pNdisRequest->DATA.METHOD_INFORMATION.InformationBuffer, pNdisRequest->DATA.METHOD_INFORMATION.InputBufferLength);
							break;
					}
				}
				else
				{
					RT_TRACE(COMP_OID_SET, DBG_TRACE, ("[Full]Fail to allocate memory to store information buffer.\r\n"));
					pOidHistoryEntry->InformationBuffer = NULL;
					pOidHistoryEntry->InformationBufferLength = 0;
				}
			}
			if( pOidHistoryEntry->InformationBuffer != NULL )
				RT_PRINT_DATA(COMP_OID_SET, DBG_TRACE, ("pOidHistoryEntry->InformationBuffer:\n"), pOidHistoryEntry->InformationBuffer, pOidHistoryEntry->InformationBufferLength);
			
			pOidHistoryEntry->TransactionId = pWdiHeader->TransactionId;

			NdisAcquireSpinLock(&(pNdisCommon->OidHistorySpinLock));
			RTInsertTailListWithCnt(&pNdisCommon->OidHistoryList, &(pOidHistoryEntry->List), &pNdisCommon->OidHistoryCount);
			RT_TRACE(COMP_OID_SET, DBG_TRACE, ("%s: insert entry to tail, pNdisCommon->OidHistoryCount %d\n", __FUNCTION__, pNdisCommon->OidHistoryCount));
			NdisReleaseSpinLock(&(pNdisCommon->OidHistorySpinLock));
		}
		else
		{
			if( PlatformAllocateMemory(pAdapter, &pOidHistoryEntry, sizeof(RT_OID_HISTORY_ENTRY)) != RT_STATUS_SUCCESS )
			{
				RT_TRACE(COMP_OID_SET, DBG_SERIOUS, ("Allocate OID_HISTORY_ENTRY failure\r\n"));
				break;
			}
			pOidHistoryEntry->InformationBuffer = NULL;
			pOidHistoryEntry->InformationBufferLength = 0;
			pOidHistoryEntry->CurrentState = OidHistoryState;
			pOidHistoryEntry->CurrentTime = PlatformGetCurrentTime();
			pOidHistoryEntry->Oid = pNdisRequest->DATA.METHOD_INFORMATION.Oid;
			RT_TRACE(COMP_OID_SET, DBG_TRACE, ("pOidHistoryEntry->Oid: 0x%X\r\n", pOidHistoryEntry->Oid));
			RT_TRACE(COMP_OID_SET, DBG_TRACE, ("pOidHistoryEntry->CurrentState: %d\r\n", pOidHistoryEntry->CurrentState));
			RT_TRACE(COMP_OID_SET, DBG_TRACE, ("pOidHistoryEntry->CurrentTime: %I64d\r\n", pOidHistoryEntry->CurrentTime));

			pOidHistoryEntry->RequestType = pNdisRequest->RequestType;
			switch (pOidHistoryEntry->RequestType)
			{
				// Query
				case NdisRequestQueryInformation:				
					pOidHistoryEntry->InformationBufferLength = pNdisRequest->DATA.QUERY_INFORMATION.InformationBufferLength;
					break;
				// Set
				case NdisRequestSetInformation:
					pOidHistoryEntry->InformationBufferLength = pNdisRequest->DATA.SET_INFORMATION.InformationBufferLength;
					break;

				// Method
				case NdisRequestMethod:
					pOidHistoryEntry->InformationBufferLength = pNdisRequest->DATA.METHOD_INFORMATION.InputBufferLength;
					break;
			}
			RT_TRACE(COMP_OID_SET, DBG_TRACE, ("pOidHistoryEntry->InformationBufferLength: %#x\r\n", pOidHistoryEntry->InformationBufferLength));

			if( pOidHistoryEntry->InformationBufferLength > 0 )
			{
				if( PlatformAllocateMemory(pAdapter, &pOidHistoryEntry->InformationBuffer, pOidHistoryEntry->InformationBufferLength) == RT_STATUS_SUCCESS )
				{
					switch (pOidHistoryEntry->RequestType)
					{
						// Query
						case NdisRequestQueryInformation:				
							PlatformMoveMemory(pOidHistoryEntry->InformationBuffer, pNdisRequest->DATA.QUERY_INFORMATION.InformationBuffer, pNdisRequest->DATA.QUERY_INFORMATION.InformationBufferLength);
							break;
						// Set
						case NdisRequestSetInformation:
							PlatformMoveMemory(pOidHistoryEntry->InformationBuffer, pNdisRequest->DATA.SET_INFORMATION.InformationBuffer, pNdisRequest->DATA.SET_INFORMATION.InformationBufferLength);
							break;

						// Method
						case NdisRequestMethod:
							PlatformMoveMemory(pOidHistoryEntry->InformationBuffer, pNdisRequest->DATA.METHOD_INFORMATION.InformationBuffer, pNdisRequest->DATA.METHOD_INFORMATION.InputBufferLength);
							break;
					}
				}
				else
				{
					RT_TRACE(COMP_OID_SET, DBG_TRACE, ("[New]Fail to allocate memory to store information buffer.\r\n"));
					pOidHistoryEntry->InformationBuffer = NULL;
					pOidHistoryEntry->InformationBufferLength = 0;
				}
			}
			
			if( pOidHistoryEntry->InformationBuffer != NULL )
				RT_PRINT_DATA(COMP_OID_SET, DBG_TRACE, ("pOidHistoryEntry->InformationBuffer:\n"), pOidHistoryEntry->InformationBuffer, pOidHistoryEntry->InformationBufferLength);

			pOidHistoryEntry->TransactionId = pWdiHeader->TransactionId;

			NdisAcquireSpinLock(&(pNdisCommon->OidHistorySpinLock));
			RTInsertTailListWithCnt(&pNdisCommon->OidHistoryList, &(pOidHistoryEntry->List), &pNdisCommon->OidHistoryCount);
			RT_TRACE(COMP_OID_SET, DBG_TRACE, ("%s: insert entry to tail, pNdisCommon->OidHistoryCount %d\n", __FUNCTION__, pNdisCommon->OidHistoryCount));
			NdisReleaseSpinLock(&(pNdisCommon->OidHistorySpinLock));
		}
	}while(FALSE);
	
	RT_TRACE(COMP_OID_SET, DBG_TRACE, ("<==== %s\n", __FUNCTION__));
}

VOID
N6CReleaseDataFrameQueuedWorkItemCallback(
	IN PVOID		pContext
)
{
	PADAPTER				pAdapter = (PADAPTER)pContext;
	PRT_NDIS6_COMMON		pNdisCommon = pAdapter->pNdisCommon;
	PNET_BUFFER_LIST		pNetBufferList;
	PNDIS_ADAPTER_TYPE		pDevice = GET_NDIS_ADAPTER(pAdapter);
	PNET_BUFFER_LIST		pCurrNetBufferList, pNextNetBufferList;
	KIRQL			OldIrql = KeGetCurrentIrql();

	RT_TRACE(COMP_SEND, DBG_LOUD, ("N6CReleaseDataFrameQueuedWorkItemCallback()\n"));

	PlatformAcquireSpinLock(pAdapter, RT_BUFFER_SPINLOCK);
	pNdisCommon->bReleaseNblWaitQueueInProgress = TRUE;
	PlatformReleaseSpinLock(pAdapter, RT_BUFFER_SPINLOCK);
	
	while(TRUE)
	{
		
		PlatformAcquireSpinLock(pAdapter, RT_TX_SPINLOCK);
		
		//
		// Wait SendingNetBufferList completed.
		//
		if( pDevice->SendingNetBufferList != NULL )
		{
			pDevice->bWaitingSendingNBL = TRUE;
			PlatformReleaseSpinLock(pAdapter, RT_TX_SPINLOCK);
		
			RT_TRACE(COMP_SEND, DBG_LOUD, ("N6CReleaseDataFrameQueuedWorkItemCallback(): wait SendingNetBufferList...\n"));
			// Prefast warning C28121: The function 'NdisWaitEvent' is not permitted to be called at the current IRQ level.
			// Prefast warning C28156: The actual IRQL 2 is inconsistent with the required IRQL 0
			// False positive, irql should be restored by PlatformReleaseSpinLock.
#pragma warning( disable:28121 )
#pragma warning( disable:28156 )
			NdisWaitEvent(&(pDevice->evtSendingNBLCompleted), 0);
			NdisResetEvent(&(pDevice->evtSendingNBLCompleted));
		
			PlatformAcquireSpinLock(pAdapter, RT_TX_SPINLOCK);
		}
		RT_ASSERT(pDevice->SendingNetBufferList == NULL, ("N6CReleaseDataFrameQueuedWorkItemCallback(): SendingNetBufferList(%p) should be NULL!!!\n", pDevice->SendingNetBufferList));
		
		//
		// Remove pending NBLs in wait queue.
		// Note that, we must make sure: 
		// 1. SendingNetBufferList is NULL.
		// 2. No one can call N6SdioSendSingleNetBufferList() now.
		// 3. All TCB and related resource are completed.
		//
		
		PlatformReleaseSpinLock(pAdapter, RT_TX_SPINLOCK);
	
		PlatformAcquireSpinLock(pAdapter, RT_BUFFER_SPINLOCK);
		if(N6CIsNblWaitQueueEmpty(pNdisCommon->TxNBLWaitQueue))
		{
			PlatformReleaseSpinLock(pAdapter, RT_BUFFER_SPINLOCK);
			break;
		}
		pNetBufferList = N6CRemoveNblWaitQueue(&pNdisCommon->TxNBLWaitQueue, TRUE);

		if(pNetBufferList != NULL)
			RT_NBL_SET_REF_CNT(pNetBufferList, 0);   //YJ,test for MPE,120221

		for (pCurrNetBufferList = pNetBufferList;
			pCurrNetBufferList != NULL;
			pCurrNetBufferList = pNextNetBufferList)
		{
			pNextNetBufferList = NET_BUFFER_LIST_NEXT_NBL(pCurrNetBufferList);
			NET_BUFFER_LIST_STATUS(pCurrNetBufferList) = NDIS_STATUS_SUCCESS;
		}
		PlatformReleaseSpinLock(pAdapter, RT_BUFFER_SPINLOCK);

		RT_TRACE(COMP_SEND, DBG_LOUD, ("N6CReleaseDataFrameQueuedWorkItemCallback(): NdisMSendNetBufferListsComplete\n"));
		// Prefast warning C6387: 'pNetBufferList' could be '0':  this does not adhere to the specification for the function 'NdisMSendNetBufferListsComplete'.
		if (pNetBufferList != NULL)
		{
			NdisMSendNetBufferListsComplete(
				pDevice->hNdisAdapter,
				pNetBufferList,
				((NDIS_CURRENT_IRQL() == DISPATCH_LEVEL) ? NDIS_SEND_COMPLETE_FLAGS_DISPATCH_LEVEL : 0));
		}
	}

	PlatformAcquireSpinLock(pAdapter, RT_BUFFER_SPINLOCK);
	pNdisCommon->bReleaseNblWaitQueueInProgress = FALSE;
	PlatformReleaseSpinLock(pAdapter, RT_BUFFER_SPINLOCK);
}


//
//	Description: 
//		Timer resource management for SoC off on Win10 Mobile platform
//
//	Assumption:
//		- Resource allocation for the TimerLock is required before calling this function.
//		- TimerLock is not acquired before calling this function
//
//	2016.01.15, created by Roger.
//
VOID
N6CTimerResourceInit(
	IN	PADAPTER	pAdapter
)
{
	PPORT_COMMON_INFO pPortCommonInfo = pAdapter->pPortCommonInfo;

	NdisAcquireSpinLock(&(pPortCommonInfo->TimerLock));
	RTInitializeListHead(&(pPortCommonInfo->TimerResourceList));
	NdisReleaseSpinLock(&(pPortCommonInfo->TimerLock));
}


//
//	Description: 
//		Timer resource management for SoC off on Win10 Mobile platform
//
//	Assumption:
//		TimerLock is acquired before calling this function
//
//	2016.01.15, created by Roger.
//
VOID
N6CTimerResourceInsert(
	IN	PADAPTER 	pAdapter,
	IN	PRT_TIMER 	pTimer
)
{

	PPORT_COMMON_INFO pPortCommonInfo = pAdapter->pPortCommonInfo;
	
	RTInsertTailListWithCnt(&(pPortCommonInfo->TimerResourceList), &(pTimer->Handle.List), &pPortCommonInfo->uNumberOfInsertedTimerRes);

	RT_TRACE(COMP_SYSTEM, DBG_TRACE, ("N6CTimerResourceInsert(): uNumberOfInsertedTimerRes(%d), Timer: %s\n", pPortCommonInfo->uNumberOfInsertedTimerRes, pTimer->szID));
}

//
//	Description: 
//		Timer resource management for SoC off on Win10 Mobile platform
//
//	Assumption:
//		- Resource allocation for the TimerLock is required before calling this function.
//		- TimerLock is not acquired before calling this function
//
//	2016.01.15, created by Roger.
//
BOOLEAN
N6CTimerResourceRemove(
	IN	PADAPTER	pAdapter,
	IN	PRT_TIMER	pTimer
)
{

	PPORT_COMMON_INFO	pPortCommonInfo = pAdapter->pPortCommonInfo;
	PRT_LIST_ENTRY	pList = NULL;
	PRT_TIMER	pThisTimer = NULL;
	u4Byte	uNumberOfTimerHandled = 0;	
	BOOLEAN		bFound = FALSE;

	
	NdisAcquireSpinLock(&(pPortCommonInfo->TimerLock));
	
	pList = RTGetHeadList(&pPortCommonInfo->TimerResourceList);

	while(!RTIsListHead(&pPortCommonInfo->TimerResourceList, pList))
	{
		pThisTimer = (PRT_TIMER) CONTAINING_RECORD(pList, RT_TIMER, Handle.List);	

		if(pThisTimer->Handle.NdisTimerHandle == pTimer->Handle.NdisTimerHandle){
			
			RTRemoveEntryListWithCnt(pList, &pPortCommonInfo->uNumberOfInsertedTimerRes);
			bFound = TRUE;
			
			RT_TRACE(COMP_SYSTEM, DBG_TRACE, ("N6CTimerResourceRemove(): Timer Found! uNumberOfInsertedTimerRes(%d), Timer: %s\n", 
				pPortCommonInfo->uNumberOfInsertedTimerRes, pThisTimer->szID));

			break;
		}		
		pList = RTNextEntryList(&pThisTimer->Handle.List);
	}
	
	NdisReleaseSpinLock(&(pPortCommonInfo->TimerLock));

	RT_ASSERT(bFound, ("N6CTimerResourceRemove(): Fail to remove specific Timer resource(%s)\n", pTimer->szID));
	
	return bFound;
}


//
//	Description: 
//		Timer resource management for SoC off on Win10 Mobile platform, we can use this routine to confirm 
//		whether all timer resources are released when driver is going to unload or halt.
//
//	Assumption:
//		- Resource allocation for the TimerLock is required before calling this function.
//		- TimerLock is not acquired before calling this function
//
//	2016.01.15, created by Roger.
//
VOID
N6CTimerResourceDump(
	IN	PADAPTER	pAdapter
)
{

	PPORT_COMMON_INFO	pPortCommonInfo = pAdapter->pPortCommonInfo;
	PRT_LIST_ENTRY	pList = NULL;
	PRT_TIMER	pTimer = NULL;
	u4Byte	uNumberOfTimerHandled = 0;	
	
	NdisAcquireSpinLock(&(pPortCommonInfo->TimerLock));
	
	pList = RTGetHeadList(&pPortCommonInfo->TimerResourceList);

	while(!RTIsListHead(&pPortCommonInfo->TimerResourceList, pList))
	{
		pTimer = (PRT_TIMER) CONTAINING_RECORD(pList, RT_TIMER, Handle.List);
		uNumberOfTimerHandled++;
		
		RT_TRACE(COMP_SYSTEM, DBG_WARNING, ("N6CTimerResourceDump(): uNumberOfTimerHandled(%d), Timer: %s\n", uNumberOfTimerHandled, pTimer->szID));
		
		pList = RTNextEntryList(&pTimer->Handle.List);
	}

	RT_ASSERT(uNumberOfTimerHandled == pPortCommonInfo->uNumberOfInsertedTimerRes, ("N6CTimerResourceDump(): uNumberOfTimerHandled is not match!\n"));
	
	NdisReleaseSpinLock(&(pPortCommonInfo->TimerLock));

}


//
//	Description: 
//		Timer resource management for SoC off on Win10 Mobile platform
//
//	Assumption:
//		- Resource allocation for the TimerLock is required before calling this function.
//		- TimerLock is not acquired before calling this function
//
//	2016.01.15, created by Roger.
//
VOID
N6CTimerResourceAction(
	IN	PADAPTER	pAdapter,
	IN	RT_TIMER_RESOURCE_ACTION		Action
)
{

	PPORT_COMMON_INFO	pPortCommonInfo = pAdapter->pPortCommonInfo;
	PRT_LIST_ENTRY	pList = NULL;
	PRT_TIMER	pTimer = NULL;
	u4Byte	uNumberOfTimerHandled = 0;	
	
	
	NdisAcquireSpinLock(&(pPortCommonInfo->TimerLock));
	
	RT_TRACE(COMP_SYSTEM, DBG_LOUD, ("--->N6CTimerResourceAction(): Action(%d)\n", Action));
	
	pList = RTGetHeadList(&pPortCommonInfo->TimerResourceList);

	while(!RTIsListHead(&pPortCommonInfo->TimerResourceList, pList))
	{

		pTimer = (PRT_TIMER) CONTAINING_RECORD(pList, RT_TIMER, Handle.List);
		uNumberOfTimerHandled++;	

		NdisReleaseSpinLock(&(pPortCommonInfo->TimerLock));

		if(Action == RT_TIMER_RESOURCE_ACTION_SUSPEND)
			PlatformSuspendTimer(pAdapter, pTimer);
		else if(Action == RT_TIMER_RESOURCE_ACTION_RESUME)
			PlatformResumeTimer(pAdapter, pTimer);
		else{
			// TODO:
		}
		
		NdisAcquireSpinLock(&(pPortCommonInfo->TimerLock));
		
		pList = RTNextEntryList(&pTimer->Handle.List);
	}

	RT_ASSERT(uNumberOfTimerHandled == pPortCommonInfo->uNumberOfInsertedTimerRes, ("N6CTimerResourceAction(): uNumberOfTimerHandled is not match!\n"));
	RT_TRACE(COMP_SYSTEM, DBG_LOUD, ("<---N6CTimerResourceAction(): Action(%d)\n", Action));
	
	NdisReleaseSpinLock(&(pPortCommonInfo->TimerLock));	
}