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
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
|
/*
*************************************************************************
* Ralink Tech Inc.
* 5F., No.36, Taiyuan St., Jhubei City,
* Hsinchu County 302,
* Taiwan, R.O.C.
*
* (c) Copyright 2002-2007, Ralink Technology, Inc.
*
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
* *
*************************************************************************
Module Name:
mlme.c
Abstract:
Revision History:
Who When What
-------- ---------- ----------------------------------------------
John Chang 2004-08-25 Modify from RT2500 code base
John Chang 2004-09-06 modified for RT2600
*/
#include "../rt_config.h"
#include <stdarg.h>
UCHAR CISCO_OUI[] = {0x00, 0x40, 0x96};
UCHAR WPA_OUI[] = {0x00, 0x50, 0xf2, 0x01};
UCHAR RSN_OUI[] = {0x00, 0x0f, 0xac};
UCHAR WAPI_OUI[] = {0x00, 0x14, 0x72};
UCHAR WME_INFO_ELEM[] = {0x00, 0x50, 0xf2, 0x02, 0x00, 0x01};
UCHAR WME_PARM_ELEM[] = {0x00, 0x50, 0xf2, 0x02, 0x01, 0x01};
UCHAR Ccx2QosInfo[] = {0x00, 0x40, 0x96, 0x04};
UCHAR RALINK_OUI[] = {0x00, 0x0c, 0x43};
UCHAR BROADCOM_OUI[] = {0x00, 0x90, 0x4c};
UCHAR WPS_OUI[] = {0x00, 0x50, 0xf2, 0x04};
UCHAR PRE_N_HT_OUI[] = {0x00, 0x90, 0x4c};
UCHAR RateSwitchTable[] = {
// Item No. Mode Curr-MCS TrainUp TrainDown // Mode- Bit0: STBC, Bit1: Short GI, Bit4,5: Mode(0:CCK, 1:OFDM, 2:HT Mix, 3:HT GF)
0x11, 0x00, 0, 0, 0, // Initial used item after association
0x00, 0x00, 0, 40, 101,
0x01, 0x00, 1, 40, 50,
0x02, 0x00, 2, 35, 45,
0x03, 0x00, 3, 20, 45,
0x04, 0x21, 0, 30, 50,
0x05, 0x21, 1, 20, 50,
0x06, 0x21, 2, 20, 50,
0x07, 0x21, 3, 15, 50,
0x08, 0x21, 4, 15, 30,
0x09, 0x21, 5, 10, 25,
0x0a, 0x21, 6, 8, 25,
0x0b, 0x21, 7, 8, 25,
0x0c, 0x20, 12, 15, 30,
0x0d, 0x20, 13, 8, 20,
0x0e, 0x20, 14, 8, 20,
0x0f, 0x20, 15, 8, 25,
0x10, 0x22, 15, 8, 25,
0x11, 0x00, 0, 0, 0,
0x12, 0x00, 0, 0, 0,
0x13, 0x00, 0, 0, 0,
0x14, 0x00, 0, 0, 0,
0x15, 0x00, 0, 0, 0,
0x16, 0x00, 0, 0, 0,
0x17, 0x00, 0, 0, 0,
0x18, 0x00, 0, 0, 0,
0x19, 0x00, 0, 0, 0,
0x1a, 0x00, 0, 0, 0,
0x1b, 0x00, 0, 0, 0,
0x1c, 0x00, 0, 0, 0,
0x1d, 0x00, 0, 0, 0,
0x1e, 0x00, 0, 0, 0,
0x1f, 0x00, 0, 0, 0,
};
UCHAR RateSwitchTable11B[] = {
// Item No. Mode Curr-MCS TrainUp TrainDown // Mode- Bit0: STBC, Bit1: Short GI, Bit4,5: Mode(0:CCK, 1:OFDM, 2:HT Mix, 3:HT GF)
0x04, 0x03, 0, 0, 0, // Initial used item after association
0x00, 0x00, 0, 40, 101,
0x01, 0x00, 1, 40, 50,
0x02, 0x00, 2, 35, 45,
0x03, 0x00, 3, 20, 45,
};
UCHAR RateSwitchTable11BG[] = {
// Item No. Mode Curr-MCS TrainUp TrainDown // Mode- Bit0: STBC, Bit1: Short GI, Bit4,5: Mode(0:CCK, 1:OFDM, 2:HT Mix, 3:HT GF)
0x0a, 0x00, 0, 0, 0, // Initial used item after association
0x00, 0x00, 0, 40, 101,
0x01, 0x00, 1, 40, 50,
0x02, 0x00, 2, 35, 45,
0x03, 0x00, 3, 20, 45,
0x04, 0x10, 2, 20, 35,
0x05, 0x10, 3, 16, 35,
0x06, 0x10, 4, 10, 25,
0x07, 0x10, 5, 16, 25,
0x08, 0x10, 6, 10, 25,
0x09, 0x10, 7, 10, 13,
};
UCHAR RateSwitchTable11G[] = {
// Item No. Mode Curr-MCS TrainUp TrainDown // Mode- Bit0: STBC, Bit1: Short GI, Bit4,5: Mode(0:CCK, 1:OFDM, 2:HT Mix, 3:HT GF)
0x08, 0x00, 0, 0, 0, // Initial used item after association
0x00, 0x10, 0, 20, 101,
0x01, 0x10, 1, 20, 35,
0x02, 0x10, 2, 20, 35,
0x03, 0x10, 3, 16, 35,
0x04, 0x10, 4, 10, 25,
0x05, 0x10, 5, 16, 25,
0x06, 0x10, 6, 10, 25,
0x07, 0x10, 7, 10, 13,
};
UCHAR RateSwitchTable11N1S[] = {
// Item No. Mode Curr-MCS TrainUp TrainDown // Mode- Bit0: STBC, Bit1: Short GI, Bit4,5: Mode(0:CCK, 1:OFDM, 2:HT Mix, 3:HT GF)
0x09, 0x00, 0, 0, 0, // Initial used item after association
0x00, 0x21, 0, 30, 101,
0x01, 0x21, 1, 20, 50,
0x02, 0x21, 2, 20, 50,
0x03, 0x21, 3, 15, 50,
0x04, 0x21, 4, 15, 30,
0x05, 0x21, 5, 10, 25,
0x06, 0x21, 6, 8, 14,
0x07, 0x21, 7, 8, 14,
0x08, 0x23, 7, 8, 14,
};
UCHAR RateSwitchTable11N2S[] = {
// Item No. Mode Curr-MCS TrainUp TrainDown // Mode- Bit0: STBC, Bit1: Short GI, Bit4,5: Mode(0:CCK, 1:OFDM, 2:HT Mix, 3:HT GF)
0x0a, 0x00, 0, 0, 0, // Initial used item after association
0x00, 0x21, 0, 30, 101,
0x01, 0x21, 1, 20, 50,
0x02, 0x21, 2, 20, 50,
0x03, 0x21, 3, 15, 50,
0x04, 0x21, 4, 15, 30,
0x05, 0x20, 12, 15, 30,
0x06, 0x20, 13, 8, 20,
0x07, 0x20, 14, 8, 20,
0x08, 0x20, 15, 8, 25,
0x09, 0x22, 15, 8, 25,
};
UCHAR RateSwitchTable11N3S[] = {
// Item No. Mode Curr-MCS TrainUp TrainDown // Mode- Bit0: STBC, Bit1: Short GI, Bit4,5: Mode(0:CCK, 1:OFDM, 2:HT Mix, 3:HT GF)
0x0a, 0x00, 0, 0, 0, // Initial used item after association
0x00, 0x21, 0, 30, 101,
0x01, 0x21, 1, 20, 50,
0x02, 0x21, 2, 20, 50,
0x03, 0x21, 3, 15, 50,
0x04, 0x21, 4, 15, 30,
0x05, 0x20, 12, 15, 30,
0x06, 0x20, 13, 8, 20,
0x07, 0x20, 14, 8, 20,
0x08, 0x20, 15, 8, 25,
0x09, 0x22, 15, 8, 25,
};
UCHAR RateSwitchTable11N2SForABand[] = {
// Item No. Mode Curr-MCS TrainUp TrainDown // Mode- Bit0: STBC, Bit1: Short GI, Bit4,5: Mode(0:CCK, 1:OFDM, 2:HT Mix, 3:HT GF)
0x0b, 0x09, 0, 0, 0, // Initial used item after association
0x00, 0x21, 0, 30, 101,
0x01, 0x21, 1, 20, 50,
0x02, 0x21, 2, 20, 50,
0x03, 0x21, 3, 15, 50,
0x04, 0x21, 4, 15, 30,
0x05, 0x21, 5, 15, 30,
0x06, 0x20, 12, 15, 30,
0x07, 0x20, 13, 8, 20,
0x08, 0x20, 14, 8, 20,
0x09, 0x20, 15, 8, 25,
0x0a, 0x22, 15, 8, 25,
};
UCHAR RateSwitchTable11N3SForABand[] = { // 3*3
// Item No. Mode Curr-MCS TrainUp TrainDown // Mode- Bit0: STBC, Bit1: Short GI, Bit4,5: Mode(0:CCK, 1:OFDM, 2:HT Mix, 3:HT GF)
0x0b, 0x09, 0, 0, 0, // Initial used item after association
0x00, 0x21, 0, 30, 101,
0x01, 0x21, 1, 20, 50,
0x02, 0x21, 2, 20, 50,
0x03, 0x21, 3, 15, 50,
0x04, 0x21, 4, 15, 30,
0x05, 0x21, 5, 15, 30,
0x06, 0x20, 12, 15, 30,
0x07, 0x20, 13, 8, 20,
0x08, 0x20, 14, 8, 20,
0x09, 0x20, 15, 8, 25,
0x0a, 0x22, 15, 8, 25,
};
UCHAR RateSwitchTable11BGN1S[] = {
// Item No. Mode Curr-MCS TrainUp TrainDown // Mode- Bit0: STBC, Bit1: Short GI, Bit4,5: Mode(0:CCK, 1:OFDM, 2:HT Mix, 3:HT GF)
0x0d, 0x00, 0, 0, 0, // Initial used item after association
0x00, 0x00, 0, 40, 101,
0x01, 0x00, 1, 40, 50,
0x02, 0x00, 2, 35, 45,
0x03, 0x00, 3, 20, 45,
0x04, 0x21, 0, 30,101, //50
0x05, 0x21, 1, 20, 50,
0x06, 0x21, 2, 20, 50,
0x07, 0x21, 3, 15, 50,
0x08, 0x21, 4, 15, 30,
0x09, 0x21, 5, 10, 25,
0x0a, 0x21, 6, 8, 14,
0x0b, 0x21, 7, 8, 14,
0x0c, 0x23, 7, 8, 14,
};
UCHAR RateSwitchTable11BGN2S[] = {
// Item No. Mode Curr-MCS TrainUp TrainDown // Mode- Bit0: STBC, Bit1: Short GI, Bit4,5: Mode(0:CCK, 1:OFDM, 2:HT Mix, 3:HT GF)
0x0a, 0x00, 0, 0, 0, // Initial used item after association
0x00, 0x21, 0, 30,101, //50
0x01, 0x21, 1, 20, 50,
0x02, 0x21, 2, 20, 50,
0x03, 0x21, 3, 15, 50,
0x04, 0x21, 4, 15, 30,
0x05, 0x20, 12, 15, 30,
0x06, 0x20, 13, 8, 20,
0x07, 0x20, 14, 8, 20,
0x08, 0x20, 15, 8, 25,
0x09, 0x22, 15, 8, 25,
};
UCHAR RateSwitchTable11BGN3S[] = { // 3*3
// Item No. Mode Curr-MCS TrainUp TrainDown // Mode- Bit0: STBC, Bit1: Short GI, Bit4,5: Mode(0:CCK, 1:OFDM, 2:HT Mix, 3:HT GF)
0x0a, 0x00, 0, 0, 0, // Initial used item after association
0x00, 0x21, 0, 30,101, //50
0x01, 0x21, 1, 20, 50,
0x02, 0x21, 2, 20, 50,
0x03, 0x21, 3, 20, 50,
0x04, 0x21, 4, 15, 50,
0x05, 0x20, 20, 15, 30,
0x06, 0x20, 21, 8, 20,
0x07, 0x20, 22, 8, 20,
0x08, 0x20, 23, 8, 25,
0x09, 0x22, 23, 8, 25,
};
UCHAR RateSwitchTable11BGN2SForABand[] = {
// Item No. Mode Curr-MCS TrainUp TrainDown // Mode- Bit0: STBC, Bit1: Short GI, Bit4,5: Mode(0:CCK, 1:OFDM, 2:HT Mix, 3:HT GF)
0x0b, 0x09, 0, 0, 0, // Initial used item after association
0x00, 0x21, 0, 30,101, //50
0x01, 0x21, 1, 20, 50,
0x02, 0x21, 2, 20, 50,
0x03, 0x21, 3, 15, 50,
0x04, 0x21, 4, 15, 30,
0x05, 0x21, 5, 15, 30,
0x06, 0x20, 12, 15, 30,
0x07, 0x20, 13, 8, 20,
0x08, 0x20, 14, 8, 20,
0x09, 0x20, 15, 8, 25,
0x0a, 0x22, 15, 8, 25,
};
UCHAR RateSwitchTable11BGN3SForABand[] = { // 3*3
// Item No. Mode Curr-MCS TrainUp TrainDown // Mode- Bit0: STBC, Bit1: Short GI, Bit4,5: Mode(0:CCK, 1:OFDM, 2:HT Mix, 3:HT GF)
0x0c, 0x09, 0, 0, 0, // Initial used item after association
0x00, 0x21, 0, 30,101, //50
0x01, 0x21, 1, 20, 50,
0x02, 0x21, 2, 20, 50,
0x03, 0x21, 3, 15, 50,
0x04, 0x21, 4, 15, 30,
0x05, 0x21, 5, 15, 30,
0x06, 0x21, 12, 15, 30,
0x07, 0x20, 20, 15, 30,
0x08, 0x20, 21, 8, 20,
0x09, 0x20, 22, 8, 20,
0x0a, 0x20, 23, 8, 25,
0x0b, 0x22, 23, 8, 25,
};
PUCHAR ReasonString[] = {
/* 0 */ "Reserved",
/* 1 */ "Unspecified Reason",
/* 2 */ "Previous Auth no longer valid",
/* 3 */ "STA is leaving / has left",
/* 4 */ "DIS-ASSOC due to inactivity",
/* 5 */ "AP unable to hanle all associations",
/* 6 */ "class 2 error",
/* 7 */ "class 3 error",
/* 8 */ "STA is leaving / has left",
/* 9 */ "require auth before assoc/re-assoc",
/* 10 */ "Reserved",
/* 11 */ "Reserved",
/* 12 */ "Reserved",
/* 13 */ "invalid IE",
/* 14 */ "MIC error",
/* 15 */ "4-way handshake timeout",
/* 16 */ "2-way (group key) handshake timeout",
/* 17 */ "4-way handshake IE diff among AssosReq/Rsp/Beacon",
/* 18 */
};
extern UCHAR OfdmRateToRxwiMCS[];
// since RT61 has better RX sensibility, we have to limit TX ACK rate not to exceed our normal data TX rate.
// otherwise the WLAN peer may not be able to receive the ACK thus downgrade its data TX rate
ULONG BasicRateMask[12] = {0xfffff001 /* 1-Mbps */, 0xfffff003 /* 2 Mbps */, 0xfffff007 /* 5.5 */, 0xfffff00f /* 11 */,
0xfffff01f /* 6 */ , 0xfffff03f /* 9 */ , 0xfffff07f /* 12 */ , 0xfffff0ff /* 18 */,
0xfffff1ff /* 24 */ , 0xfffff3ff /* 36 */ , 0xfffff7ff /* 48 */ , 0xffffffff /* 54 */};
UCHAR MULTICAST_ADDR[MAC_ADDR_LEN] = {0x1, 0x00, 0x00, 0x00, 0x00, 0x00};
UCHAR BROADCAST_ADDR[MAC_ADDR_LEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
UCHAR ZERO_MAC_ADDR[MAC_ADDR_LEN] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
// e.g. RssiSafeLevelForTxRate[RATE_36]" means if the current RSSI is greater than
// this value, then it's quaranteed capable of operating in 36 mbps TX rate in
// clean environment.
// TxRate: 1 2 5.5 11 6 9 12 18 24 36 48 54 72 100
CHAR RssiSafeLevelForTxRate[] ={ -92, -91, -90, -87, -88, -86, -85, -83, -81, -78, -72, -71, -40, -40 };
UCHAR RateIdToMbps[] = { 1, 2, 5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 72, 100};
USHORT RateIdTo500Kbps[] = { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108, 144, 200};
UCHAR SsidIe = IE_SSID;
UCHAR SupRateIe = IE_SUPP_RATES;
UCHAR ExtRateIe = IE_EXT_SUPP_RATES;
UCHAR HtCapIe = IE_HT_CAP;
UCHAR AddHtInfoIe = IE_ADD_HT;
UCHAR NewExtChanIe = IE_SECONDARY_CH_OFFSET;
UCHAR ErpIe = IE_ERP;
UCHAR DsIe = IE_DS_PARM;
UCHAR TimIe = IE_TIM;
UCHAR WpaIe = IE_WPA;
UCHAR Wpa2Ie = IE_WPA2;
UCHAR IbssIe = IE_IBSS_PARM;
UCHAR Ccx2Ie = IE_CCX_V2;
#ifdef RT2870
UCHAR WapiIe = IE_WAPI;
#endif
extern UCHAR WPA_OUI[];
UCHAR SES_OUI[] = {0x00, 0x90, 0x4c};
UCHAR ZeroSsid[32] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
// Reset the RFIC setting to new series
RTMP_RF_REGS RF2850RegTable[] = {
// ch R1 R2 R3(TX0~4=0) R4
{1, 0x98402ecc, 0x984c0786, 0x9816b455, 0x9800510b},
{2, 0x98402ecc, 0x984c0786, 0x98168a55, 0x9800519f},
{3, 0x98402ecc, 0x984c078a, 0x98168a55, 0x9800518b},
{4, 0x98402ecc, 0x984c078a, 0x98168a55, 0x9800519f},
{5, 0x98402ecc, 0x984c078e, 0x98168a55, 0x9800518b},
{6, 0x98402ecc, 0x984c078e, 0x98168a55, 0x9800519f},
{7, 0x98402ecc, 0x984c0792, 0x98168a55, 0x9800518b},
{8, 0x98402ecc, 0x984c0792, 0x98168a55, 0x9800519f},
{9, 0x98402ecc, 0x984c0796, 0x98168a55, 0x9800518b},
{10, 0x98402ecc, 0x984c0796, 0x98168a55, 0x9800519f},
{11, 0x98402ecc, 0x984c079a, 0x98168a55, 0x9800518b},
{12, 0x98402ecc, 0x984c079a, 0x98168a55, 0x9800519f},
{13, 0x98402ecc, 0x984c079e, 0x98168a55, 0x9800518b},
{14, 0x98402ecc, 0x984c07a2, 0x98168a55, 0x98005193},
// 802.11 UNI / HyperLan 2
{36, 0x98402ecc, 0x984c099a, 0x98158a55, 0x980ed1a3},
{38, 0x98402ecc, 0x984c099e, 0x98158a55, 0x980ed193},
{40, 0x98402ec8, 0x984c0682, 0x98158a55, 0x980ed183},
{44, 0x98402ec8, 0x984c0682, 0x98158a55, 0x980ed1a3},
{46, 0x98402ec8, 0x984c0686, 0x98158a55, 0x980ed18b},
{48, 0x98402ec8, 0x984c0686, 0x98158a55, 0x980ed19b},
{52, 0x98402ec8, 0x984c068a, 0x98158a55, 0x980ed193},
{54, 0x98402ec8, 0x984c068a, 0x98158a55, 0x980ed1a3},
{56, 0x98402ec8, 0x984c068e, 0x98158a55, 0x980ed18b},
{60, 0x98402ec8, 0x984c0692, 0x98158a55, 0x980ed183},
{62, 0x98402ec8, 0x984c0692, 0x98158a55, 0x980ed193},
{64, 0x98402ec8, 0x984c0692, 0x98158a55, 0x980ed1a3}, // Plugfest#4, Day4, change RFR3 left4th 9->5.
// 802.11 HyperLan 2
{100, 0x98402ec8, 0x984c06b2, 0x98178a55, 0x980ed783},
// 2008.04.30 modified
// The system team has AN to improve the EVM value
// for channel 102 to 108 for the RT2850/RT2750 dual band solution.
{102, 0x98402ec8, 0x985c06b2, 0x98578a55, 0x980ed793},
{104, 0x98402ec8, 0x985c06b2, 0x98578a55, 0x980ed1a3},
{108, 0x98402ecc, 0x985c0a32, 0x98578a55, 0x980ed193},
{110, 0x98402ecc, 0x984c0a36, 0x98178a55, 0x980ed183},
{112, 0x98402ecc, 0x984c0a36, 0x98178a55, 0x980ed19b},
{116, 0x98402ecc, 0x984c0a3a, 0x98178a55, 0x980ed1a3},
{118, 0x98402ecc, 0x984c0a3e, 0x98178a55, 0x980ed193},
{120, 0x98402ec4, 0x984c0382, 0x98178a55, 0x980ed183},
{124, 0x98402ec4, 0x984c0382, 0x98178a55, 0x980ed193},
{126, 0x98402ec4, 0x984c0382, 0x98178a55, 0x980ed15b}, // 0x980ed1bb->0x980ed15b required by Rory 20070927
{128, 0x98402ec4, 0x984c0382, 0x98178a55, 0x980ed1a3},
{132, 0x98402ec4, 0x984c0386, 0x98178a55, 0x980ed18b},
{134, 0x98402ec4, 0x984c0386, 0x98178a55, 0x980ed193},
{136, 0x98402ec4, 0x984c0386, 0x98178a55, 0x980ed19b},
{140, 0x98402ec4, 0x984c038a, 0x98178a55, 0x980ed183},
// 802.11 UNII
{149, 0x98402ec4, 0x984c038a, 0x98178a55, 0x980ed1a7},
{151, 0x98402ec4, 0x984c038e, 0x98178a55, 0x980ed187},
{153, 0x98402ec4, 0x984c038e, 0x98178a55, 0x980ed18f},
{157, 0x98402ec4, 0x984c038e, 0x98178a55, 0x980ed19f},
{159, 0x98402ec4, 0x984c038e, 0x98178a55, 0x980ed1a7},
{161, 0x98402ec4, 0x984c0392, 0x98178a55, 0x980ed187},
{165, 0x98402ec4, 0x984c0392, 0x98178a55, 0x980ed197},
// Japan
{184, 0x95002ccc, 0x9500491e, 0x9509be55, 0x950c0a0b},
{188, 0x95002ccc, 0x95004922, 0x9509be55, 0x950c0a13},
{192, 0x95002ccc, 0x95004926, 0x9509be55, 0x950c0a1b},
{196, 0x95002ccc, 0x9500492a, 0x9509be55, 0x950c0a23},
{208, 0x95002ccc, 0x9500493a, 0x9509be55, 0x950c0a13},
{212, 0x95002ccc, 0x9500493e, 0x9509be55, 0x950c0a1b},
{216, 0x95002ccc, 0x95004982, 0x9509be55, 0x950c0a23},
// still lack of MMAC(Japan) ch 34,38,42,46
};
UCHAR NUM_OF_2850_CHNL = (sizeof(RF2850RegTable) / sizeof(RTMP_RF_REGS));
FREQUENCY_ITEM FreqItems3020[] =
{
/**************************************************/
// ISM : 2.4 to 2.483 GHz //
/**************************************************/
// 11g
/**************************************************/
//-CH---N-------R---K-----------
{1, 241, 2, 2},
{2, 241, 2, 7},
{3, 242, 2, 2},
{4, 242, 2, 7},
{5, 243, 2, 2},
{6, 243, 2, 7},
{7, 244, 2, 2},
{8, 244, 2, 7},
{9, 245, 2, 2},
{10, 245, 2, 7},
{11, 246, 2, 2},
{12, 246, 2, 7},
{13, 247, 2, 2},
{14, 248, 2, 4},
};
#ifndef RT30xx
#define NUM_OF_3020_CHNL (sizeof(FreqItems3020) / sizeof(FREQUENCY_ITEM))
#endif
#ifdef RT30xx
//2008/07/10:KH Modified to share this variable
UCHAR NUM_OF_3020_CHNL=(sizeof(FreqItems3020) / sizeof(FREQUENCY_ITEM));
#endif
/*
==========================================================================
Description:
initialize the MLME task and its data structure (queue, spinlock,
timer, state machines).
IRQL = PASSIVE_LEVEL
Return:
always return NDIS_STATUS_SUCCESS
==========================================================================
*/
NDIS_STATUS MlmeInit(
IN PRTMP_ADAPTER pAd)
{
NDIS_STATUS Status = NDIS_STATUS_SUCCESS;
DBGPRINT(RT_DEBUG_TRACE, ("--> MLME Initialize\n"));
do
{
Status = MlmeQueueInit(&pAd->Mlme.Queue);
if(Status != NDIS_STATUS_SUCCESS)
break;
pAd->Mlme.bRunning = FALSE;
NdisAllocateSpinLock(&pAd->Mlme.TaskLock);
{
BssTableInit(&pAd->ScanTab);
// init STA state machines
AssocStateMachineInit(pAd, &pAd->Mlme.AssocMachine, pAd->Mlme.AssocFunc);
AuthStateMachineInit(pAd, &pAd->Mlme.AuthMachine, pAd->Mlme.AuthFunc);
AuthRspStateMachineInit(pAd, &pAd->Mlme.AuthRspMachine, pAd->Mlme.AuthRspFunc);
SyncStateMachineInit(pAd, &pAd->Mlme.SyncMachine, pAd->Mlme.SyncFunc);
WpaPskStateMachineInit(pAd, &pAd->Mlme.WpaPskMachine, pAd->Mlme.WpaPskFunc);
AironetStateMachineInit(pAd, &pAd->Mlme.AironetMachine, pAd->Mlme.AironetFunc);
// Since we are using switch/case to implement it, the init is different from the above
// state machine init
MlmeCntlInit(pAd, &pAd->Mlme.CntlMachine, NULL);
}
ActionStateMachineInit(pAd, &pAd->Mlme.ActMachine, pAd->Mlme.ActFunc);
// Init mlme periodic timer
RTMPInitTimer(pAd, &pAd->Mlme.PeriodicTimer, GET_TIMER_FUNCTION(MlmePeriodicExec), pAd, TRUE);
// Set mlme periodic timer
RTMPSetTimer(&pAd->Mlme.PeriodicTimer, MLME_TASK_EXEC_INTV);
// software-based RX Antenna diversity
RTMPInitTimer(pAd, &pAd->Mlme.RxAntEvalTimer, GET_TIMER_FUNCTION(AsicRxAntEvalTimeout), pAd, FALSE);
#ifdef RT2860
{
if (OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_ADVANCE_POWER_SAVE_PCIE_DEVICE))
{
// only PCIe cards need these two timers
RTMPInitTimer(pAd, &pAd->Mlme.PsPollTimer, GET_TIMER_FUNCTION(PsPollWakeExec), pAd, FALSE);
RTMPInitTimer(pAd, &pAd->Mlme.RadioOnOffTimer, GET_TIMER_FUNCTION(RadioOnExec), pAd, FALSE);
}
}
#endif
} while (FALSE);
DBGPRINT(RT_DEBUG_TRACE, ("<-- MLME Initialize\n"));
return Status;
}
/*
==========================================================================
Description:
main loop of the MLME
Pre:
Mlme has to be initialized, and there are something inside the queue
Note:
This function is invoked from MPSetInformation and MPReceive;
This task guarantee only one MlmeHandler will run.
IRQL = DISPATCH_LEVEL
==========================================================================
*/
VOID MlmeHandler(
IN PRTMP_ADAPTER pAd)
{
MLME_QUEUE_ELEM *Elem = NULL;
// Only accept MLME and Frame from peer side, no other (control/data) frame should
// get into this state machine
NdisAcquireSpinLock(&pAd->Mlme.TaskLock);
if(pAd->Mlme.bRunning)
{
NdisReleaseSpinLock(&pAd->Mlme.TaskLock);
return;
}
else
{
pAd->Mlme.bRunning = TRUE;
}
NdisReleaseSpinLock(&pAd->Mlme.TaskLock);
while (!MlmeQueueEmpty(&pAd->Mlme.Queue))
{
if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_MLME_RESET_IN_PROGRESS) ||
RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS) ||
RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST))
{
DBGPRINT(RT_DEBUG_TRACE, ("Device Halted or Removed or MlmeRest, exit MlmeHandler! (queue num = %ld)\n", pAd->Mlme.Queue.Num));
break;
}
//From message type, determine which state machine I should drive
if (MlmeDequeue(&pAd->Mlme.Queue, &Elem))
{
#ifdef RT2870
if (Elem->MsgType == MT2_RESET_CONF)
{
DBGPRINT_RAW(RT_DEBUG_TRACE, ("!!! reset MLME state machine !!!\n"));
MlmeRestartStateMachine(pAd);
Elem->Occupied = FALSE;
Elem->MsgLen = 0;
continue;
}
#endif // RT2870 //
// if dequeue success
switch (Elem->Machine)
{
// STA state machines
case ASSOC_STATE_MACHINE:
StateMachinePerformAction(pAd, &pAd->Mlme.AssocMachine, Elem);
break;
case AUTH_STATE_MACHINE:
StateMachinePerformAction(pAd, &pAd->Mlme.AuthMachine, Elem);
break;
case AUTH_RSP_STATE_MACHINE:
StateMachinePerformAction(pAd, &pAd->Mlme.AuthRspMachine, Elem);
break;
case SYNC_STATE_MACHINE:
StateMachinePerformAction(pAd, &pAd->Mlme.SyncMachine, Elem);
break;
case MLME_CNTL_STATE_MACHINE:
MlmeCntlMachinePerformAction(pAd, &pAd->Mlme.CntlMachine, Elem);
break;
case WPA_PSK_STATE_MACHINE:
StateMachinePerformAction(pAd, &pAd->Mlme.WpaPskMachine, Elem);
break;
case AIRONET_STATE_MACHINE:
StateMachinePerformAction(pAd, &pAd->Mlme.AironetMachine, Elem);
break;
case ACTION_STATE_MACHINE:
StateMachinePerformAction(pAd, &pAd->Mlme.ActMachine, Elem);
break;
default:
DBGPRINT(RT_DEBUG_TRACE, ("ERROR: Illegal machine %ld in MlmeHandler()\n", Elem->Machine));
break;
} // end of switch
// free MLME element
Elem->Occupied = FALSE;
Elem->MsgLen = 0;
}
else {
DBGPRINT_ERR(("MlmeHandler: MlmeQueue empty\n"));
}
}
NdisAcquireSpinLock(&pAd->Mlme.TaskLock);
pAd->Mlme.bRunning = FALSE;
NdisReleaseSpinLock(&pAd->Mlme.TaskLock);
}
/*
==========================================================================
Description:
Destructor of MLME (Destroy queue, state machine, spin lock and timer)
Parameters:
Adapter - NIC Adapter pointer
Post:
The MLME task will no longer work properly
IRQL = PASSIVE_LEVEL
==========================================================================
*/
VOID MlmeHalt(
IN PRTMP_ADAPTER pAd)
{
BOOLEAN Cancelled;
#ifdef RT3070
UINT32 TxPinCfg = 0x00050F0F;
#endif // RT3070 //
DBGPRINT(RT_DEBUG_TRACE, ("==> MlmeHalt\n"));
if (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST))
{
// disable BEACON generation and other BEACON related hardware timers
AsicDisableSync(pAd);
}
{
// Cancel pending timers
RTMPCancelTimer(&pAd->MlmeAux.AssocTimer, &Cancelled);
RTMPCancelTimer(&pAd->MlmeAux.ReassocTimer, &Cancelled);
RTMPCancelTimer(&pAd->MlmeAux.DisassocTimer, &Cancelled);
RTMPCancelTimer(&pAd->MlmeAux.AuthTimer, &Cancelled);
RTMPCancelTimer(&pAd->MlmeAux.BeaconTimer, &Cancelled);
RTMPCancelTimer(&pAd->MlmeAux.ScanTimer, &Cancelled);
#ifdef RT2860
if (OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_ADVANCE_POWER_SAVE_PCIE_DEVICE))
{
RTMPCancelTimer(&pAd->Mlme.PsPollTimer, &Cancelled);
RTMPCancelTimer(&pAd->Mlme.RadioOnOffTimer, &Cancelled);
}
#endif
}
RTMPCancelTimer(&pAd->Mlme.PeriodicTimer, &Cancelled);
RTMPCancelTimer(&pAd->Mlme.RxAntEvalTimer, &Cancelled);
if (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST))
{
// Set LED
RTMPSetLED(pAd, LED_HALT);
RTMPSetSignalLED(pAd, -100); // Force signal strength Led to be turned off, firmware is not done it.
#ifdef RT2870
{
LED_CFG_STRUC LedCfg;
RTMP_IO_READ32(pAd, LED_CFG, &LedCfg.word);
LedCfg.field.LedPolar = 0;
LedCfg.field.RLedMode = 0;
LedCfg.field.GLedMode = 0;
LedCfg.field.YLedMode = 0;
RTMP_IO_WRITE32(pAd, LED_CFG, LedCfg.word);
}
#endif // RT2870 //
#ifdef RT3070
//
// Turn off LNA_PE
//
if (IS_RT3070(pAd) || IS_RT3071(pAd))
{
TxPinCfg &= 0xFFFFF0F0;
RTUSBWriteMACRegister(pAd, TX_PIN_CFG, TxPinCfg);
}
#endif // RT3070 //
}
RTMPusecDelay(5000); // 5 msec to gurantee Ant Diversity timer canceled
MlmeQueueDestroy(&pAd->Mlme.Queue);
NdisFreeSpinLock(&pAd->Mlme.TaskLock);
DBGPRINT(RT_DEBUG_TRACE, ("<== MlmeHalt\n"));
}
VOID MlmeResetRalinkCounters(
IN PRTMP_ADAPTER pAd)
{
pAd->RalinkCounters.LastOneSecRxOkDataCnt = pAd->RalinkCounters.OneSecRxOkDataCnt;
// clear all OneSecxxx counters.
pAd->RalinkCounters.OneSecBeaconSentCnt = 0;
pAd->RalinkCounters.OneSecFalseCCACnt = 0;
pAd->RalinkCounters.OneSecRxFcsErrCnt = 0;
pAd->RalinkCounters.OneSecRxOkCnt = 0;
pAd->RalinkCounters.OneSecTxFailCount = 0;
pAd->RalinkCounters.OneSecTxNoRetryOkCount = 0;
pAd->RalinkCounters.OneSecTxRetryOkCount = 0;
pAd->RalinkCounters.OneSecRxOkDataCnt = 0;
// TODO: for debug only. to be removed
pAd->RalinkCounters.OneSecOsTxCount[QID_AC_BE] = 0;
pAd->RalinkCounters.OneSecOsTxCount[QID_AC_BK] = 0;
pAd->RalinkCounters.OneSecOsTxCount[QID_AC_VI] = 0;
pAd->RalinkCounters.OneSecOsTxCount[QID_AC_VO] = 0;
pAd->RalinkCounters.OneSecDmaDoneCount[QID_AC_BE] = 0;
pAd->RalinkCounters.OneSecDmaDoneCount[QID_AC_BK] = 0;
pAd->RalinkCounters.OneSecDmaDoneCount[QID_AC_VI] = 0;
pAd->RalinkCounters.OneSecDmaDoneCount[QID_AC_VO] = 0;
pAd->RalinkCounters.OneSecTxDoneCount = 0;
pAd->RalinkCounters.OneSecRxCount = 0;
pAd->RalinkCounters.OneSecTxAggregationCount = 0;
pAd->RalinkCounters.OneSecRxAggregationCount = 0;
return;
}
unsigned long rx_AMSDU;
unsigned long rx_Total;
/*
==========================================================================
Description:
This routine is executed periodically to -
1. Decide if it's a right time to turn on PwrMgmt bit of all
outgoiing frames
2. Calculate ChannelQuality based on statistics of the last
period, so that TX rate won't toggling very frequently between a
successful TX and a failed TX.
3. If the calculated ChannelQuality indicated current connection not
healthy, then a ROAMing attempt is tried here.
IRQL = DISPATCH_LEVEL
==========================================================================
*/
#define ADHOC_BEACON_LOST_TIME (8*OS_HZ) // 8 sec
VOID MlmePeriodicExec(
IN PVOID SystemSpecific1,
IN PVOID FunctionContext,
IN PVOID SystemSpecific2,
IN PVOID SystemSpecific3)
{
ULONG TxTotalCnt;
PRTMP_ADAPTER pAd = (RTMP_ADAPTER *)FunctionContext;
#ifdef RT2860
//Baron 2008/07/10
//printk("Baron_Test:\t%s", RTMPGetRalinkEncryModeStr(pAd->StaCfg.WepStatus));
//If the STA security setting is OPEN or WEP, pAd->StaCfg.WpaSupplicantUP = 0.
//If the STA security setting is WPAPSK or WPA2PSK, pAd->StaCfg.WpaSupplicantUP = 1.
if(pAd->StaCfg.WepStatus<2)
{
pAd->StaCfg.WpaSupplicantUP = 0;
}
else
{
pAd->StaCfg.WpaSupplicantUP = 1;
}
{
// If Hardware controlled Radio enabled, we have to check GPIO pin2 every 2 second.
// Move code to here, because following code will return when radio is off
if ((pAd->Mlme.PeriodicRound % (MLME_TASK_EXEC_MULTIPLE * 2) == 0) &&
(pAd->StaCfg.bHardwareRadio == TRUE) &&
(RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_START_UP)) &&
(!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)) &&
(!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS)))
{
UINT32 data = 0;
// Read GPIO pin2 as Hardware controlled radio state
RTMP_IO_FORCE_READ32(pAd, GPIO_CTRL_CFG, &data);
if (data & 0x04)
{
pAd->StaCfg.bHwRadio = TRUE;
}
else
{
pAd->StaCfg.bHwRadio = FALSE;
}
if (pAd->StaCfg.bRadio != (pAd->StaCfg.bHwRadio && pAd->StaCfg.bSwRadio))
{
pAd->StaCfg.bRadio = (pAd->StaCfg.bHwRadio && pAd->StaCfg.bSwRadio);
if (pAd->StaCfg.bRadio == TRUE)
{
MlmeRadioOn(pAd);
// Update extra information
pAd->ExtraInfo = EXTRA_INFO_CLEAR;
}
else
{
MlmeRadioOff(pAd);
// Update extra information
pAd->ExtraInfo = HW_RADIO_OFF;
}
}
}
}
#endif /* RT2860 */
// Do nothing if the driver is starting halt state.
// This might happen when timer already been fired before cancel timer with mlmehalt
if ((RTMP_TEST_FLAG(pAd, (fRTMP_ADAPTER_HALT_IN_PROGRESS |
fRTMP_ADAPTER_RADIO_OFF |
fRTMP_ADAPTER_RADIO_MEASUREMENT |
fRTMP_ADAPTER_RESET_IN_PROGRESS))))
return;
#ifdef RT2860
{
if ((pAd->RalinkCounters.LastReceivedByteCount == pAd->RalinkCounters.ReceivedByteCount) && (pAd->StaCfg.bRadio == TRUE))
{
// If ReceiveByteCount doesn't change, increase SameRxByteCount by 1.
pAd->SameRxByteCount++;
}
else
pAd->SameRxByteCount = 0;
// If after BBP, still not work...need to check to reset PBF&MAC.
if (pAd->SameRxByteCount == 702)
{
pAd->SameRxByteCount = 0;
AsicResetPBF(pAd);
AsicResetMAC(pAd);
}
// If SameRxByteCount keeps happens for 2 second in infra mode, or for 60 seconds in idle mode.
if (((INFRA_ON(pAd)) && (pAd->SameRxByteCount > 20)) || ((IDLE_ON(pAd)) && (pAd->SameRxByteCount > 600)))
{
if ((pAd->StaCfg.bRadio == TRUE) && (pAd->SameRxByteCount < 700))
{
DBGPRINT(RT_DEBUG_TRACE, ("---> SameRxByteCount = %lu !!!!!!!!!!!!!!! \n", pAd->SameRxByteCount));
pAd->SameRxByteCount = 700;
AsicResetBBP(pAd);
}
}
// Update lastReceiveByteCount.
pAd->RalinkCounters.LastReceivedByteCount = pAd->RalinkCounters.ReceivedByteCount;
if ((pAd->CheckDmaBusyCount > 3) && (IDLE_ON(pAd)))
{
pAd->CheckDmaBusyCount = 0;
AsicResetFromDMABusy(pAd);
}
}
#endif /* RT2860 */
RT28XX_MLME_PRE_SANITY_CHECK(pAd);
{
// Do nothing if monitor mode is on
if (MONITOR_ON(pAd))
return;
if (pAd->Mlme.PeriodicRound & 0x1)
{
// This is the fix for wifi 11n extension channel overlapping test case. for 2860D
if (((pAd->MACVersion & 0xffff) == 0x0101) &&
(STA_TGN_WIFI_ON(pAd)) &&
(pAd->CommonCfg.IOTestParm.bToggle == FALSE))
{
RTMP_IO_WRITE32(pAd, TXOP_CTRL_CFG, 0x24Bf);
pAd->CommonCfg.IOTestParm.bToggle = TRUE;
}
else if ((STA_TGN_WIFI_ON(pAd)) &&
((pAd->MACVersion & 0xffff) == 0x0101))
{
RTMP_IO_WRITE32(pAd, TXOP_CTRL_CFG, 0x243f);
pAd->CommonCfg.IOTestParm.bToggle = FALSE;
}
}
}
pAd->bUpdateBcnCntDone = FALSE;
// RECBATimerTimeout(SystemSpecific1,FunctionContext,SystemSpecific2,SystemSpecific3);
pAd->Mlme.PeriodicRound ++;
#ifdef RT3070
// execute every 100ms, update the Tx FIFO Cnt for update Tx Rate.
NICUpdateFifoStaCounters(pAd);
#endif // RT3070 //
// execute every 500ms
if ((pAd->Mlme.PeriodicRound % 5 == 0) && RTMPAutoRateSwitchCheck(pAd)/*(OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_TX_RATE_SWITCH_ENABLED))*/)
{
// perform dynamic tx rate switching based on past TX history
{
if ((OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_MEDIA_STATE_CONNECTED)
)
&& (!OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_DOZE)))
MlmeDynamicTxRateSwitching(pAd);
}
}
// Normal 1 second Mlme PeriodicExec.
if (pAd->Mlme.PeriodicRound %MLME_TASK_EXEC_MULTIPLE == 0)
{
pAd->Mlme.OneSecPeriodicRound ++;
if (rx_Total)
{
// reset counters
rx_AMSDU = 0;
rx_Total = 0;
}
// Media status changed, report to NDIS
if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_MEDIA_STATE_CHANGE))
{
RTMP_CLEAR_FLAG(pAd, fRTMP_ADAPTER_MEDIA_STATE_CHANGE);
if (OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_MEDIA_STATE_CONNECTED))
{
pAd->IndicateMediaState = NdisMediaStateConnected;
RTMP_IndicateMediaState(pAd);
}
else
{
pAd->IndicateMediaState = NdisMediaStateDisconnected;
RTMP_IndicateMediaState(pAd);
}
}
NdisGetSystemUpTime(&pAd->Mlme.Now32);
// add the most up-to-date h/w raw counters into software variable, so that
// the dynamic tuning mechanism below are based on most up-to-date information
NICUpdateRawCounters(pAd);
#ifdef RT2870
RT2870_WatchDog(pAd);
#endif // RT2870 //
// Need statistics after read counter. So put after NICUpdateRawCounters
ORIBATimerTimeout(pAd);
// The time period for checking antenna is according to traffic
if (pAd->Mlme.bEnableAutoAntennaCheck)
{
TxTotalCnt = pAd->RalinkCounters.OneSecTxNoRetryOkCount +
pAd->RalinkCounters.OneSecTxRetryOkCount +
pAd->RalinkCounters.OneSecTxFailCount;
// dynamic adjust antenna evaluation period according to the traffic
if (TxTotalCnt > 50)
{
if (pAd->Mlme.OneSecPeriodicRound % 10 == 0)
{
AsicEvaluateRxAnt(pAd);
}
}
else
{
if (pAd->Mlme.OneSecPeriodicRound % 3 == 0)
{
AsicEvaluateRxAnt(pAd);
}
}
}
STAMlmePeriodicExec(pAd);
MlmeResetRalinkCounters(pAd);
{
#ifdef RT2860
if (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST) && (pAd->bPCIclkOff == FALSE))
#endif
{
// When Adhoc beacon is enabled and RTS/CTS is enabled, there is a chance that hardware MAC FSM will run into a deadlock
// and sending CTS-to-self over and over.
// Software Patch Solution:
// 1. Polling debug state register 0x10F4 every one second.
// 2. If in 0x10F4 the ((bit29==1) && (bit7==1)) OR ((bit29==1) && (bit5==1)), it means the deadlock has occurred.
// 3. If the deadlock occurred, reset MAC/BBP by setting 0x1004 to 0x0001 for a while then setting it back to 0x000C again.
UINT32 MacReg = 0;
RTMP_IO_READ32(pAd, 0x10F4, &MacReg);
if (((MacReg & 0x20000000) && (MacReg & 0x80)) || ((MacReg & 0x20000000) && (MacReg & 0x20)))
{
RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, 0x1);
RTMPusecDelay(1);
RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, 0xC);
DBGPRINT(RT_DEBUG_WARN,("Warning, MAC specific condition occurs \n"));
}
}
}
RT28XX_MLME_HANDLER(pAd);
}
pAd->bUpdateBcnCntDone = FALSE;
}
VOID STAMlmePeriodicExec(
PRTMP_ADAPTER pAd)
{
#ifdef RT2860
ULONG TxTotalCnt;
#endif
#ifdef RT2870
ULONG TxTotalCnt;
int i;
#endif
if (pAd->StaCfg.WpaSupplicantUP == WPA_SUPPLICANT_DISABLE)
{
// WPA MIC error should block association attempt for 60 seconds
if (pAd->StaCfg.bBlockAssoc && (pAd->StaCfg.LastMicErrorTime + (60 * OS_HZ) < pAd->Mlme.Now32))
pAd->StaCfg.bBlockAssoc = FALSE;
}
#ifdef RT2860
//Baron 2008/07/10
//printk("Baron_Test:\t%s", RTMPGetRalinkEncryModeStr(pAd->StaCfg.WepStatus));
//If the STA security setting is OPEN or WEP, pAd->StaCfg.WpaSupplicantUP = 0.
//If the STA security setting is WPAPSK or WPA2PSK, pAd->StaCfg.WpaSupplicantUP = 1.
if(pAd->StaCfg.WepStatus<2)
{
pAd->StaCfg.WpaSupplicantUP = 0;
}
else
{
pAd->StaCfg.WpaSupplicantUP = 1;
}
#endif
if ((pAd->PreMediaState != pAd->IndicateMediaState) && (pAd->CommonCfg.bWirelessEvent))
{
if (pAd->IndicateMediaState == NdisMediaStateConnected)
{
RTMPSendWirelessEvent(pAd, IW_STA_LINKUP_EVENT_FLAG, pAd->MacTab.Content[BSSID_WCID].Addr, BSS0, 0);
}
pAd->PreMediaState = pAd->IndicateMediaState;
}
#ifdef RT2860
if ((pAd->OpMode == OPMODE_STA) && (IDLE_ON(pAd)) &&
(OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_ADVANCE_POWER_SAVE_PCIE_DEVICE)) &&
(pAd->Mlme.SyncMachine.CurrState == SYNC_IDLE) &&
(pAd->Mlme.CntlMachine.CurrState == CNTL_IDLE) &&
(RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_START_UP)) &&
(!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_IDLE_RADIO_OFF)))
{
RT28xxPciAsicRadioOff(pAd, GUI_IDLE_POWER_SAVE, 0);
}
#endif
AsicStaBbpTuning(pAd);
TxTotalCnt = pAd->RalinkCounters.OneSecTxNoRetryOkCount +
pAd->RalinkCounters.OneSecTxRetryOkCount +
pAd->RalinkCounters.OneSecTxFailCount;
if (OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_MEDIA_STATE_CONNECTED))
{
// update channel quality for Roaming and UI LinkQuality display
MlmeCalculateChannelQuality(pAd, pAd->Mlme.Now32);
}
// must be AFTER MlmeDynamicTxRateSwitching() because it needs to know if
// Radio is currently in noisy environment
if (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS))
AsicAdjustTxPower(pAd);
if (INFRA_ON(pAd))
{
// Is PSM bit consistent with user power management policy?
// This is the only place that will set PSM bit ON.
if (!OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_DOZE))
MlmeCheckPsmChange(pAd, pAd->Mlme.Now32);
pAd->RalinkCounters.LastOneSecTotalTxCount = TxTotalCnt;
if ((pAd->StaCfg.LastBeaconRxTime + 1*OS_HZ < pAd->Mlme.Now32) &&
(!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS)) &&
((TxTotalCnt + pAd->RalinkCounters.OneSecRxOkCnt < 600)))
{
RTMPSetAGCInitValue(pAd, BW_20);
DBGPRINT(RT_DEBUG_TRACE, ("MMCHK - No BEACON. restore R66 to the low bound(%d) \n", (0x2E + GET_LNA_GAIN(pAd))));
}
{
if (pAd->CommonCfg.bAPSDCapable && pAd->CommonCfg.APEdcaParm.bAPSDCapable)
{
// When APSD is enabled, the period changes as 20 sec
if ((pAd->Mlme.OneSecPeriodicRound % 20) == 8)
RTMPSendNullFrame(pAd, pAd->CommonCfg.TxRate, TRUE);
}
else
{
// Send out a NULL frame every 10 sec to inform AP that STA is still alive (Avoid being age out)
if ((pAd->Mlme.OneSecPeriodicRound % 10) == 8)
{
if (pAd->CommonCfg.bWmmCapable)
RTMPSendNullFrame(pAd, pAd->CommonCfg.TxRate, TRUE);
else
RTMPSendNullFrame(pAd, pAd->CommonCfg.TxRate, FALSE);
}
}
}
if (CQI_IS_DEAD(pAd->Mlme.ChannelQuality))
{
DBGPRINT(RT_DEBUG_TRACE, ("MMCHK - No BEACON. Dead CQI. Auto Recovery attempt #%ld\n", pAd->RalinkCounters.BadCQIAutoRecoveryCount));
pAd->StaCfg.CCXAdjacentAPReportFlag = TRUE;
pAd->StaCfg.CCXAdjacentAPLinkDownTime = pAd->StaCfg.LastBeaconRxTime;
// Lost AP, send disconnect & link down event
LinkDown(pAd, FALSE);
{
union iwreq_data wrqu;
memset(wrqu.ap_addr.sa_data, 0, MAC_ADDR_LEN);
wireless_send_event(pAd->net_dev, SIOCGIWAP, &wrqu, NULL);
}
MlmeAutoReconnectLastSSID(pAd);
}
else if (CQI_IS_BAD(pAd->Mlme.ChannelQuality))
{
pAd->RalinkCounters.BadCQIAutoRecoveryCount ++;
DBGPRINT(RT_DEBUG_TRACE, ("MMCHK - Bad CQI. Auto Recovery attempt #%ld\n", pAd->RalinkCounters.BadCQIAutoRecoveryCount));
MlmeAutoReconnectLastSSID(pAd);
}
// Add auto seamless roaming
if (pAd->StaCfg.bFastRoaming)
{
SHORT dBmToRoam = (SHORT)pAd->StaCfg.dBmToRoam;
DBGPRINT(RT_DEBUG_TRACE, ("Rssi=%d, dBmToRoam=%d\n", RTMPMaxRssi(pAd, pAd->StaCfg.RssiSample.LastRssi0, pAd->StaCfg.RssiSample.LastRssi1, pAd->StaCfg.RssiSample.LastRssi2), (CHAR)dBmToRoam));
if (RTMPMaxRssi(pAd, pAd->StaCfg.RssiSample.LastRssi0, pAd->StaCfg.RssiSample.LastRssi1, pAd->StaCfg.RssiSample.LastRssi2) <= (CHAR)dBmToRoam)
{
MlmeCheckForFastRoaming(pAd, pAd->Mlme.Now32);
}
}
}
else if (ADHOC_ON(pAd))
{
#ifdef RT2860
// 2003-04-17 john. this is a patch that driver forces a BEACON out if ASIC fails
// the "TX BEACON competition" for the entire past 1 sec.
// So that even when ASIC's BEACONgen engine been blocked
// by peer's BEACON due to slower system clock, this STA still can send out
// minimum BEACON to tell the peer I'm alive.
// drawback is that this BEACON won't be well aligned at TBTT boundary.
// EnqueueBeaconFrame(pAd); // software send BEACON
// if all 11b peers leave this BSS more than 5 seconds, update Tx rate,
// restore outgoing BEACON to support B/G-mixed mode
if ((pAd->CommonCfg.Channel <= 14) &&
(pAd->CommonCfg.MaxTxRate <= RATE_11) &&
(pAd->CommonCfg.MaxDesiredRate > RATE_11) &&
((pAd->StaCfg.Last11bBeaconRxTime + 5*OS_HZ) < pAd->Mlme.Now32))
{
DBGPRINT(RT_DEBUG_TRACE, ("MMCHK - last 11B peer left, update Tx rates\n"));
NdisMoveMemory(pAd->StaActive.SupRate, pAd->CommonCfg.SupRate, MAX_LEN_OF_SUPPORTED_RATES);
pAd->StaActive.SupRateLen = pAd->CommonCfg.SupRateLen;
MlmeUpdateTxRates(pAd, FALSE, 0);
MakeIbssBeacon(pAd); // re-build BEACON frame
AsicEnableIbssSync(pAd); // copy to on-chip memory
pAd->StaCfg.AdhocBOnlyJoined = FALSE;
}
if (pAd->CommonCfg.PhyMode >= PHY_11ABGN_MIXED)
{
if ((pAd->StaCfg.AdhocBGJoined) &&
((pAd->StaCfg.Last11gBeaconRxTime + 5 * OS_HZ) < pAd->Mlme.Now32))
{
DBGPRINT(RT_DEBUG_TRACE, ("MMCHK - last 11G peer left\n"));
pAd->StaCfg.AdhocBGJoined = FALSE;
}
if ((pAd->StaCfg.Adhoc20NJoined) &&
((pAd->StaCfg.Last20NBeaconRxTime + 5 * OS_HZ) < pAd->Mlme.Now32))
{
DBGPRINT(RT_DEBUG_TRACE, ("MMCHK - last 20MHz N peer left\n"));
pAd->StaCfg.Adhoc20NJoined = FALSE;
}
}
#endif /* RT2860 */
//radar detect
if ((pAd->CommonCfg.Channel > 14)
&& (pAd->CommonCfg.bIEEE80211H == 1)
&& RadarChannelCheck(pAd, pAd->CommonCfg.Channel))
{
RadarDetectPeriodic(pAd);
}
// If all peers leave, and this STA becomes the last one in this IBSS, then change MediaState
// to DISCONNECTED. But still holding this IBSS (i.e. sending BEACON) so that other STAs can
// join later.
if ((pAd->StaCfg.LastBeaconRxTime + ADHOC_BEACON_LOST_TIME < pAd->Mlme.Now32) &&
OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_MEDIA_STATE_CONNECTED))
{
MLME_START_REQ_STRUCT StartReq;
DBGPRINT(RT_DEBUG_TRACE, ("MMCHK - excessive BEACON lost, last STA in this IBSS, MediaState=Disconnected\n"));
LinkDown(pAd, FALSE);
StartParmFill(pAd, &StartReq, pAd->MlmeAux.Ssid, pAd->MlmeAux.SsidLen);
MlmeEnqueue(pAd, SYNC_STATE_MACHINE, MT2_MLME_START_REQ, sizeof(MLME_START_REQ_STRUCT), &StartReq);
pAd->Mlme.CntlMachine.CurrState = CNTL_WAIT_START;
}
#ifdef RT2870
for (i = 1; i < MAX_LEN_OF_MAC_TABLE; i++)
{
MAC_TABLE_ENTRY *pEntry = &pAd->MacTab.Content[i];
if (pEntry->ValidAsCLI == FALSE)
continue;
if (pEntry->LastBeaconRxTime + ADHOC_BEACON_LOST_TIME < pAd->Mlme.Now32)
MacTableDeleteEntry(pAd, pEntry->Aid, pEntry->Addr);
}
#endif
}
else // no INFRA nor ADHOC connection
{
if (pAd->StaCfg.bScanReqIsFromWebUI &&
((pAd->StaCfg.LastScanTime + 30 * OS_HZ) > pAd->Mlme.Now32))
goto SKIP_AUTO_SCAN_CONN;
else
pAd->StaCfg.bScanReqIsFromWebUI = FALSE;
if ((pAd->StaCfg.bAutoReconnect == TRUE)
&& RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_START_UP)
&& (MlmeValidateSSID(pAd->MlmeAux.AutoReconnectSsid, pAd->MlmeAux.AutoReconnectSsidLen) == TRUE))
{
if ((pAd->ScanTab.BssNr==0) && (pAd->Mlme.CntlMachine.CurrState == CNTL_IDLE))
{
MLME_SCAN_REQ_STRUCT ScanReq;
if ((pAd->StaCfg.LastScanTime + 10 * OS_HZ) < pAd->Mlme.Now32)
{
DBGPRINT(RT_DEBUG_TRACE, ("STAMlmePeriodicExec():CNTL - ScanTab.BssNr==0, start a new ACTIVE scan SSID[%s]\n", pAd->MlmeAux.AutoReconnectSsid));
ScanParmFill(pAd, &ScanReq, pAd->MlmeAux.AutoReconnectSsid, pAd->MlmeAux.AutoReconnectSsidLen, BSS_ANY, SCAN_ACTIVE);
MlmeEnqueue(pAd, SYNC_STATE_MACHINE, MT2_MLME_SCAN_REQ, sizeof(MLME_SCAN_REQ_STRUCT), &ScanReq);
pAd->Mlme.CntlMachine.CurrState = CNTL_WAIT_OID_LIST_SCAN;
// Reset Missed scan number
pAd->StaCfg.LastScanTime = pAd->Mlme.Now32;
}
else if (pAd->StaCfg.BssType == BSS_ADHOC) // Quit the forever scan when in a very clean room
MlmeAutoReconnectLastSSID(pAd);
}
else if (pAd->Mlme.CntlMachine.CurrState == CNTL_IDLE)
{
if ((pAd->Mlme.OneSecPeriodicRound % 7) == 0)
{
MlmeAutoScan(pAd);
pAd->StaCfg.LastScanTime = pAd->Mlme.Now32;
}
else
{
MlmeAutoReconnectLastSSID(pAd);
}
}
}
}
SKIP_AUTO_SCAN_CONN:
if ((pAd->MacTab.Content[BSSID_WCID].TXBAbitmap !=0) && (pAd->MacTab.fAnyBASession == FALSE))
{
pAd->MacTab.fAnyBASession = TRUE;
AsicUpdateProtect(pAd, HT_FORCERTSCTS, ALLN_SETPROTECT, FALSE, FALSE);
}
else if ((pAd->MacTab.Content[BSSID_WCID].TXBAbitmap ==0) && (pAd->MacTab.fAnyBASession == TRUE))
{
pAd->MacTab.fAnyBASession = FALSE;
AsicUpdateProtect(pAd, pAd->MlmeAux.AddHtInfo.AddHtInfo2.OperaionMode, ALLN_SETPROTECT, FALSE, FALSE);
}
return;
}
// Link down report
VOID LinkDownExec(
IN PVOID SystemSpecific1,
IN PVOID FunctionContext,
IN PVOID SystemSpecific2,
IN PVOID SystemSpecific3)
{
RTMP_ADAPTER *pAd = (RTMP_ADAPTER *)FunctionContext;
pAd->IndicateMediaState = NdisMediaStateDisconnected;
RTMP_IndicateMediaState(pAd);
pAd->ExtraInfo = GENERAL_LINK_DOWN;
}
// IRQL = DISPATCH_LEVEL
VOID MlmeAutoScan(
IN PRTMP_ADAPTER pAd)
{
// check CntlMachine.CurrState to avoid collision with NDIS SetOID request
if (pAd->Mlme.CntlMachine.CurrState == CNTL_IDLE)
{
DBGPRINT(RT_DEBUG_TRACE, ("MMCHK - Driver auto scan\n"));
MlmeEnqueue(pAd,
MLME_CNTL_STATE_MACHINE,
OID_802_11_BSSID_LIST_SCAN,
0,
NULL);
RT28XX_MLME_HANDLER(pAd);
}
}
// IRQL = DISPATCH_LEVEL
VOID MlmeAutoReconnectLastSSID(
IN PRTMP_ADAPTER pAd)
{
// check CntlMachine.CurrState to avoid collision with NDIS SetOID request
if ((pAd->Mlme.CntlMachine.CurrState == CNTL_IDLE) &&
(MlmeValidateSSID(pAd->MlmeAux.AutoReconnectSsid, pAd->MlmeAux.AutoReconnectSsidLen) == TRUE))
{
NDIS_802_11_SSID OidSsid;
OidSsid.SsidLength = pAd->MlmeAux.AutoReconnectSsidLen;
NdisMoveMemory(OidSsid.Ssid, pAd->MlmeAux.AutoReconnectSsid, pAd->MlmeAux.AutoReconnectSsidLen);
DBGPRINT(RT_DEBUG_TRACE, ("Driver auto reconnect to last OID_802_11_SSID setting - %s, len - %d\n", pAd->MlmeAux.AutoReconnectSsid, pAd->MlmeAux.AutoReconnectSsidLen));
MlmeEnqueue(pAd,
MLME_CNTL_STATE_MACHINE,
OID_802_11_SSID,
sizeof(NDIS_802_11_SSID),
&OidSsid);
RT28XX_MLME_HANDLER(pAd);
}
}
/*
==========================================================================
Validate SSID for connection try and rescan purpose
Valid SSID will have visible chars only.
The valid length is from 0 to 32.
IRQL = DISPATCH_LEVEL
==========================================================================
*/
BOOLEAN MlmeValidateSSID(
IN PUCHAR pSsid,
IN UCHAR SsidLen)
{
int index;
if (SsidLen > MAX_LEN_OF_SSID)
return (FALSE);
// Check each character value
for (index = 0; index < SsidLen; index++)
{
if (pSsid[index] < 0x20)
return (FALSE);
}
// All checked
return (TRUE);
}
VOID MlmeSelectTxRateTable(
IN PRTMP_ADAPTER pAd,
IN PMAC_TABLE_ENTRY pEntry,
IN PUCHAR *ppTable,
IN PUCHAR pTableSize,
IN PUCHAR pInitTxRateIdx)
{
do
{
// decide the rate table for tuning
if (pAd->CommonCfg.TxRateTableSize > 0)
{
*ppTable = RateSwitchTable;
*pTableSize = RateSwitchTable[0];
*pInitTxRateIdx = RateSwitchTable[1];
break;
}
if ((pAd->OpMode == OPMODE_STA) && ADHOC_ON(pAd))
{
if ((pAd->CommonCfg.PhyMode >= PHY_11ABGN_MIXED) &&
#ifdef RT2860
!pAd->StaCfg.AdhocBOnlyJoined &&
!pAd->StaCfg.AdhocBGJoined &&
(pAd->StaActive.SupportedPhyInfo.MCSSet[0] == 0xff) &&
((pAd->StaActive.SupportedPhyInfo.MCSSet[1] == 0x00) || (pAd->Antenna.field.TxPath == 1)))
#endif
#ifdef RT2870
(pEntry->HTCapability.MCSSet[0] == 0xff) &&
((pEntry->HTCapability.MCSSet[1] == 0x00) || (pAd->Antenna.field.TxPath == 1)))
#endif
{// 11N 1S Adhoc
*ppTable = RateSwitchTable11N1S;
*pTableSize = RateSwitchTable11N1S[0];
*pInitTxRateIdx = RateSwitchTable11N1S[1];
}
else if ((pAd->CommonCfg.PhyMode >= PHY_11ABGN_MIXED) &&
#ifdef RT2860
!pAd->StaCfg.AdhocBOnlyJoined &&
!pAd->StaCfg.AdhocBGJoined &&
(pAd->StaActive.SupportedPhyInfo.MCSSet[0] == 0xff) &&
(pAd->StaActive.SupportedPhyInfo.MCSSet[1] == 0xff) &&
#endif
#ifdef RT2870
(pEntry->HTCapability.MCSSet[0] == 0xff) &&
(pEntry->HTCapability.MCSSet[1] == 0xff) &&
#endif
(pAd->Antenna.field.TxPath == 2))
{// 11N 2S Adhoc
if (pAd->LatchRfRegs.Channel <= 14)
{
*ppTable = RateSwitchTable11N2S;
*pTableSize = RateSwitchTable11N2S[0];
*pInitTxRateIdx = RateSwitchTable11N2S[1];
}
else
{
*ppTable = RateSwitchTable11N2SForABand;
*pTableSize = RateSwitchTable11N2SForABand[0];
*pInitTxRateIdx = RateSwitchTable11N2SForABand[1];
}
}
else
#ifdef RT2860
if (pAd->CommonCfg.PhyMode == PHY_11B)
{
*ppTable = RateSwitchTable11B;
*pTableSize = RateSwitchTable11B[0];
*pInitTxRateIdx = RateSwitchTable11B[1];
}
else if((pAd->LatchRfRegs.Channel <= 14) && (pAd->StaCfg.AdhocBOnlyJoined == TRUE))
#endif
#ifdef RT2870
if ((pEntry->RateLen == 4)
&& (pEntry->HTCapability.MCSSet[0] == 0) && (pEntry->HTCapability.MCSSet[1] == 0)
)
#endif
{
// USe B Table when Only b-only Station in my IBSS .
*ppTable = RateSwitchTable11B;
*pTableSize = RateSwitchTable11B[0];
*pInitTxRateIdx = RateSwitchTable11B[1];
}
else if (pAd->LatchRfRegs.Channel <= 14)
{
*ppTable = RateSwitchTable11BG;
*pTableSize = RateSwitchTable11BG[0];
*pInitTxRateIdx = RateSwitchTable11BG[1];
}
else
{
*ppTable = RateSwitchTable11G;
*pTableSize = RateSwitchTable11G[0];
*pInitTxRateIdx = RateSwitchTable11G[1];
}
break;
}
if ((pEntry->RateLen == 12) && (pEntry->HTCapability.MCSSet[0] == 0xff) &&
((pEntry->HTCapability.MCSSet[1] == 0x00) || (pAd->CommonCfg.TxStream == 1)))
{// 11BGN 1S AP
*ppTable = RateSwitchTable11BGN1S;
*pTableSize = RateSwitchTable11BGN1S[0];
*pInitTxRateIdx = RateSwitchTable11BGN1S[1];
break;
}
if ((pEntry->RateLen == 12) && (pEntry->HTCapability.MCSSet[0] == 0xff) &&
(pEntry->HTCapability.MCSSet[1] == 0xff) && (pAd->CommonCfg.TxStream == 2))
{// 11BGN 2S AP
if (pAd->LatchRfRegs.Channel <= 14)
{
*ppTable = RateSwitchTable11BGN2S;
*pTableSize = RateSwitchTable11BGN2S[0];
*pInitTxRateIdx = RateSwitchTable11BGN2S[1];
}
else
{
*ppTable = RateSwitchTable11BGN2SForABand;
*pTableSize = RateSwitchTable11BGN2SForABand[0];
*pInitTxRateIdx = RateSwitchTable11BGN2SForABand[1];
}
break;
}
if ((pEntry->HTCapability.MCSSet[0] == 0xff) && ((pEntry->HTCapability.MCSSet[1] == 0x00) || (pAd->CommonCfg.TxStream == 1)))
{// 11N 1S AP
*ppTable = RateSwitchTable11N1S;
*pTableSize = RateSwitchTable11N1S[0];
*pInitTxRateIdx = RateSwitchTable11N1S[1];
break;
}
if ((pEntry->HTCapability.MCSSet[0] == 0xff) && (pEntry->HTCapability.MCSSet[1] == 0xff) && (pAd->CommonCfg.TxStream == 2))
{// 11N 2S AP
if (pAd->LatchRfRegs.Channel <= 14)
{
*ppTable = RateSwitchTable11N2S;
*pTableSize = RateSwitchTable11N2S[0];
*pInitTxRateIdx = RateSwitchTable11N2S[1];
}
else
{
*ppTable = RateSwitchTable11N2SForABand;
*pTableSize = RateSwitchTable11N2SForABand[0];
*pInitTxRateIdx = RateSwitchTable11N2SForABand[1];
}
break;
}
//else if ((pAd->StaActive.SupRateLen == 4) && (pAd->StaActive.ExtRateLen == 0) && (pAd->StaActive.SupportedPhyInfo.MCSSet[0] == 0) && (pAd->StaActive.SupportedPhyInfo.MCSSet[1] == 0))
if ((pEntry->RateLen == 4)
#ifndef RT30xx
//Iverson mark for Adhoc b mode,sta will use rate 54 Mbps when connect with sta b/g/n mode
&& (pEntry->HTCapability.MCSSet[0] == 0) && (pEntry->HTCapability.MCSSet[1] == 0)
#endif
)
{// B only AP
*ppTable = RateSwitchTable11B;
*pTableSize = RateSwitchTable11B[0];
*pInitTxRateIdx = RateSwitchTable11B[1];
break;
}
//else if ((pAd->StaActive.SupRateLen + pAd->StaActive.ExtRateLen > 8) && (pAd->StaActive.SupportedPhyInfo.MCSSet[0] == 0) && (pAd->StaActive.SupportedPhyInfo.MCSSet[1] == 0))
if ((pEntry->RateLen > 8)
&& (pEntry->HTCapability.MCSSet[0] == 0) && (pEntry->HTCapability.MCSSet[1] == 0)
)
{// B/G mixed AP
*ppTable = RateSwitchTable11BG;
*pTableSize = RateSwitchTable11BG[0];
*pInitTxRateIdx = RateSwitchTable11BG[1];
break;
}
//else if ((pAd->StaActive.SupRateLen + pAd->StaActive.ExtRateLen == 8) && (pAd->StaActive.SupportedPhyInfo.MCSSet[0] == 0) && (pAd->StaActive.SupportedPhyInfo.MCSSet[1] == 0))
if ((pEntry->RateLen == 8)
&& (pEntry->HTCapability.MCSSet[0] == 0) && (pEntry->HTCapability.MCSSet[1] == 0)
)
{// G only AP
*ppTable = RateSwitchTable11G;
*pTableSize = RateSwitchTable11G[0];
*pInitTxRateIdx = RateSwitchTable11G[1];
break;
}
{
//else if ((pAd->StaActive.SupportedPhyInfo.MCSSet[0] == 0) && (pAd->StaActive.SupportedPhyInfo.MCSSet[1] == 0))
if ((pEntry->HTCapability.MCSSet[0] == 0) && (pEntry->HTCapability.MCSSet[1] == 0))
{ // Legacy mode
if (pAd->CommonCfg.MaxTxRate <= RATE_11)
{
*ppTable = RateSwitchTable11B;
*pTableSize = RateSwitchTable11B[0];
*pInitTxRateIdx = RateSwitchTable11B[1];
}
else if ((pAd->CommonCfg.MaxTxRate > RATE_11) && (pAd->CommonCfg.MinTxRate > RATE_11))
{
*ppTable = RateSwitchTable11G;
*pTableSize = RateSwitchTable11G[0];
*pInitTxRateIdx = RateSwitchTable11G[1];
}
else
{
*ppTable = RateSwitchTable11BG;
*pTableSize = RateSwitchTable11BG[0];
*pInitTxRateIdx = RateSwitchTable11BG[1];
}
break;
}
if (pAd->LatchRfRegs.Channel <= 14)
{
if (pAd->CommonCfg.TxStream == 1)
{
*ppTable = RateSwitchTable11N1S;
*pTableSize = RateSwitchTable11N1S[0];
*pInitTxRateIdx = RateSwitchTable11N1S[1];
DBGPRINT_RAW(RT_DEBUG_ERROR,("DRS: unkown mode,default use 11N 1S AP \n"));
}
else
{
*ppTable = RateSwitchTable11N2S;
*pTableSize = RateSwitchTable11N2S[0];
*pInitTxRateIdx = RateSwitchTable11N2S[1];
DBGPRINT_RAW(RT_DEBUG_ERROR,("DRS: unkown mode,default use 11N 2S AP \n"));
}
}
else
{
if (pAd->CommonCfg.TxStream == 1)
{
*ppTable = RateSwitchTable11N1S;
*pTableSize = RateSwitchTable11N1S[0];
*pInitTxRateIdx = RateSwitchTable11N1S[1];
DBGPRINT_RAW(RT_DEBUG_ERROR,("DRS: unkown mode,default use 11N 1S AP \n"));
}
else
{
*ppTable = RateSwitchTable11N2SForABand;
*pTableSize = RateSwitchTable11N2SForABand[0];
*pInitTxRateIdx = RateSwitchTable11N2SForABand[1];
DBGPRINT_RAW(RT_DEBUG_ERROR,("DRS: unkown mode,default use 11N 2S AP \n"));
}
}
DBGPRINT_RAW(RT_DEBUG_ERROR,("DRS: unkown mode (SupRateLen=%d, ExtRateLen=%d, MCSSet[0]=0x%x, MCSSet[1]=0x%x)\n",
pAd->StaActive.SupRateLen, pAd->StaActive.ExtRateLen, pAd->StaActive.SupportedPhyInfo.MCSSet[0], pAd->StaActive.SupportedPhyInfo.MCSSet[1]));
}
} while(FALSE);
}
/*
==========================================================================
Description:
This routine checks if there're other APs out there capable for
roaming. Caller should call this routine only when Link up in INFRA mode
and channel quality is below CQI_GOOD_THRESHOLD.
IRQL = DISPATCH_LEVEL
Output:
==========================================================================
*/
VOID MlmeCheckForRoaming(
IN PRTMP_ADAPTER pAd,
IN ULONG Now32)
{
USHORT i;
BSS_TABLE *pRoamTab = &pAd->MlmeAux.RoamTab;
BSS_ENTRY *pBss;
DBGPRINT(RT_DEBUG_TRACE, ("==> MlmeCheckForRoaming\n"));
// put all roaming candidates into RoamTab, and sort in RSSI order
BssTableInit(pRoamTab);
for (i = 0; i < pAd->ScanTab.BssNr; i++)
{
pBss = &pAd->ScanTab.BssEntry[i];
if ((pBss->LastBeaconRxTime + BEACON_LOST_TIME) < Now32)
continue; // AP disappear
if (pBss->Rssi <= RSSI_THRESHOLD_FOR_ROAMING)
continue; // RSSI too weak. forget it.
if (MAC_ADDR_EQUAL(pBss->Bssid, pAd->CommonCfg.Bssid))
continue; // skip current AP
if (pBss->Rssi < (pAd->StaCfg.RssiSample.LastRssi0 + RSSI_DELTA))
continue; // only AP with stronger RSSI is eligible for roaming
// AP passing all above rules is put into roaming candidate table
NdisMoveMemory(&pRoamTab->BssEntry[pRoamTab->BssNr], pBss, sizeof(BSS_ENTRY));
pRoamTab->BssNr += 1;
}
if (pRoamTab->BssNr > 0)
{
// check CntlMachine.CurrState to avoid collision with NDIS SetOID request
if (pAd->Mlme.CntlMachine.CurrState == CNTL_IDLE)
{
pAd->RalinkCounters.PoorCQIRoamingCount ++;
DBGPRINT(RT_DEBUG_TRACE, ("MMCHK - Roaming attempt #%ld\n", pAd->RalinkCounters.PoorCQIRoamingCount));
MlmeEnqueue(pAd, MLME_CNTL_STATE_MACHINE, MT2_MLME_ROAMING_REQ, 0, NULL);
RT28XX_MLME_HANDLER(pAd);
}
}
DBGPRINT(RT_DEBUG_TRACE, ("<== MlmeCheckForRoaming(# of candidate= %d)\n",pRoamTab->BssNr));
}
/*
==========================================================================
Description:
This routine checks if there're other APs out there capable for
roaming. Caller should call this routine only when link up in INFRA mode
and channel quality is below CQI_GOOD_THRESHOLD.
IRQL = DISPATCH_LEVEL
Output:
==========================================================================
*/
VOID MlmeCheckForFastRoaming(
IN PRTMP_ADAPTER pAd,
IN ULONG Now)
{
USHORT i;
BSS_TABLE *pRoamTab = &pAd->MlmeAux.RoamTab;
BSS_ENTRY *pBss;
DBGPRINT(RT_DEBUG_TRACE, ("==> MlmeCheckForFastRoaming\n"));
// put all roaming candidates into RoamTab, and sort in RSSI order
BssTableInit(pRoamTab);
for (i = 0; i < pAd->ScanTab.BssNr; i++)
{
pBss = &pAd->ScanTab.BssEntry[i];
if ((pBss->Rssi <= -50) && (pBss->Channel == pAd->CommonCfg.Channel))
continue; // RSSI too weak. forget it.
if (MAC_ADDR_EQUAL(pBss->Bssid, pAd->CommonCfg.Bssid))
continue; // skip current AP
if (!SSID_EQUAL(pBss->Ssid, pBss->SsidLen, pAd->CommonCfg.Ssid, pAd->CommonCfg.SsidLen))
continue; // skip different SSID
if (pBss->Rssi < (RTMPMaxRssi(pAd, pAd->StaCfg.RssiSample.LastRssi0, pAd->StaCfg.RssiSample.LastRssi1, pAd->StaCfg.RssiSample.LastRssi2) + RSSI_DELTA))
continue; // skip AP without better RSSI
DBGPRINT(RT_DEBUG_TRACE, ("LastRssi0 = %d, pBss->Rssi = %d\n", RTMPMaxRssi(pAd, pAd->StaCfg.RssiSample.LastRssi0, pAd->StaCfg.RssiSample.LastRssi1, pAd->StaCfg.RssiSample.LastRssi2), pBss->Rssi));
// AP passing all above rules is put into roaming candidate table
NdisMoveMemory(&pRoamTab->BssEntry[pRoamTab->BssNr], pBss, sizeof(BSS_ENTRY));
pRoamTab->BssNr += 1;
}
if (pRoamTab->BssNr > 0)
{
// check CntlMachine.CurrState to avoid collision with NDIS SetOID request
if (pAd->Mlme.CntlMachine.CurrState == CNTL_IDLE)
{
pAd->RalinkCounters.PoorCQIRoamingCount ++;
DBGPRINT(RT_DEBUG_TRACE, ("MMCHK - Roaming attempt #%ld\n", pAd->RalinkCounters.PoorCQIRoamingCount));
MlmeEnqueue(pAd, MLME_CNTL_STATE_MACHINE, MT2_MLME_ROAMING_REQ, 0, NULL);
RT28XX_MLME_HANDLER(pAd);
}
}
// Maybe site survey required
else
{
if ((pAd->StaCfg.LastScanTime + 10 * 1000) < Now)
{
// check CntlMachine.CurrState to avoid collision with NDIS SetOID request
DBGPRINT(RT_DEBUG_TRACE, ("MMCHK - Roaming, No eligable entry, try new scan!\n"));
pAd->StaCfg.ScanCnt = 2;
pAd->StaCfg.LastScanTime = Now;
MlmeAutoScan(pAd);
}
}
DBGPRINT(RT_DEBUG_TRACE, ("<== MlmeCheckForFastRoaming (BssNr=%d)\n", pRoamTab->BssNr));
}
/*
==========================================================================
Description:
This routine calculates TxPER, RxPER of the past N-sec period. And
according to the calculation result, ChannelQuality is calculated here
to decide if current AP is still doing the job.
If ChannelQuality is not good, a ROAMing attempt may be tried later.
Output:
StaCfg.ChannelQuality - 0..100
IRQL = DISPATCH_LEVEL
NOTE: This routine decide channle quality based on RX CRC error ratio.
Caller should make sure a function call to NICUpdateRawCounters(pAd)
is performed right before this routine, so that this routine can decide
channel quality based on the most up-to-date information
==========================================================================
*/
VOID MlmeCalculateChannelQuality(
IN PRTMP_ADAPTER pAd,
IN ULONG Now32)
{
ULONG TxOkCnt, TxCnt, TxPER, TxPRR;
ULONG RxCnt, RxPER;
UCHAR NorRssi;
CHAR MaxRssi;
ULONG BeaconLostTime = BEACON_LOST_TIME;
MaxRssi = RTMPMaxRssi(pAd, pAd->StaCfg.RssiSample.LastRssi0, pAd->StaCfg.RssiSample.LastRssi1, pAd->StaCfg.RssiSample.LastRssi2);
//
// calculate TX packet error ratio and TX retry ratio - if too few TX samples, skip TX related statistics
//
TxOkCnt = pAd->RalinkCounters.OneSecTxNoRetryOkCount + pAd->RalinkCounters.OneSecTxRetryOkCount;
TxCnt = TxOkCnt + pAd->RalinkCounters.OneSecTxFailCount;
if (TxCnt < 5)
{
TxPER = 0;
TxPRR = 0;
}
else
{
TxPER = (pAd->RalinkCounters.OneSecTxFailCount * 100) / TxCnt;
TxPRR = ((TxCnt - pAd->RalinkCounters.OneSecTxNoRetryOkCount) * 100) / TxCnt;
}
//
// calculate RX PER - don't take RxPER into consideration if too few sample
//
RxCnt = pAd->RalinkCounters.OneSecRxOkCnt + pAd->RalinkCounters.OneSecRxFcsErrCnt;
if (RxCnt < 5)
RxPER = 0;
else
RxPER = (pAd->RalinkCounters.OneSecRxFcsErrCnt * 100) / RxCnt;
//
// decide ChannelQuality based on: 1)last BEACON received time, 2)last RSSI, 3)TxPER, and 4)RxPER
//
if (INFRA_ON(pAd) &&
(pAd->RalinkCounters.OneSecTxNoRetryOkCount < 2) && // no heavy traffic
(pAd->StaCfg.LastBeaconRxTime + BeaconLostTime < Now32))
{
DBGPRINT(RT_DEBUG_TRACE, ("BEACON lost > %ld msec with TxOkCnt=%ld -> CQI=0\n", BeaconLostTime, TxOkCnt));
pAd->Mlme.ChannelQuality = 0;
}
else
{
// Normalize Rssi
if (MaxRssi > -40)
NorRssi = 100;
else if (MaxRssi < -90)
NorRssi = 0;
else
NorRssi = (MaxRssi + 90) * 2;
// ChannelQuality = W1*RSSI + W2*TxPRR + W3*RxPER (RSSI 0..100), (TxPER 100..0), (RxPER 100..0)
pAd->Mlme.ChannelQuality = (RSSI_WEIGHTING * NorRssi +
TX_WEIGHTING * (100 - TxPRR) +
RX_WEIGHTING* (100 - RxPER)) / 100;
if (pAd->Mlme.ChannelQuality >= 100)
pAd->Mlme.ChannelQuality = 100;
}
}
VOID MlmeSetTxRate(
IN PRTMP_ADAPTER pAd,
IN PMAC_TABLE_ENTRY pEntry,
IN PRTMP_TX_RATE_SWITCH pTxRate)
{
UCHAR MaxMode = MODE_OFDM;
MaxMode = MODE_HTGREENFIELD;
if (pTxRate->STBC && (pAd->StaCfg.MaxHTPhyMode.field.STBC) && (pAd->Antenna.field.TxPath == 2))
pAd->StaCfg.HTPhyMode.field.STBC = STBC_USE;
else
pAd->StaCfg.HTPhyMode.field.STBC = STBC_NONE;
if (pTxRate->CurrMCS < MCS_AUTO)
pAd->StaCfg.HTPhyMode.field.MCS = pTxRate->CurrMCS;
if (pAd->StaCfg.HTPhyMode.field.MCS > 7)
pAd->StaCfg.HTPhyMode.field.STBC = STBC_NONE;
if (ADHOC_ON(pAd))
{
// If peer adhoc is b-only mode, we can't send 11g rate.
pAd->StaCfg.HTPhyMode.field.ShortGI = GI_800;
pEntry->HTPhyMode.field.STBC = STBC_NONE;
//
// For Adhoc MODE_CCK, driver will use AdhocBOnlyJoined flag to roll back to B only if necessary
//
pEntry->HTPhyMode.field.MODE = pTxRate->Mode;
pEntry->HTPhyMode.field.ShortGI = pAd->StaCfg.HTPhyMode.field.ShortGI;
pEntry->HTPhyMode.field.MCS = pAd->StaCfg.HTPhyMode.field.MCS;
// Patch speed error in status page
pAd->StaCfg.HTPhyMode.field.MODE = pEntry->HTPhyMode.field.MODE;
}
else
{
if (pTxRate->Mode <= MaxMode)
pAd->StaCfg.HTPhyMode.field.MODE = pTxRate->Mode;
if (pTxRate->ShortGI && (pAd->StaCfg.MaxHTPhyMode.field.ShortGI))
pAd->StaCfg.HTPhyMode.field.ShortGI = GI_400;
else
pAd->StaCfg.HTPhyMode.field.ShortGI = GI_800;
// Reexam each bandwidth's SGI support.
if (pAd->StaCfg.HTPhyMode.field.ShortGI == GI_400)
{
if ((pEntry->HTPhyMode.field.BW == BW_20) && (!CLIENT_STATUS_TEST_FLAG(pEntry, fCLIENT_STATUS_SGI20_CAPABLE)))
pAd->StaCfg.HTPhyMode.field.ShortGI = GI_800;
if ((pEntry->HTPhyMode.field.BW == BW_40) && (!CLIENT_STATUS_TEST_FLAG(pEntry, fCLIENT_STATUS_SGI40_CAPABLE)))
pAd->StaCfg.HTPhyMode.field.ShortGI = GI_800;
}
// Turn RTS/CTS rate to 6Mbps.
if ((pEntry->HTPhyMode.field.MCS == 0) && (pAd->StaCfg.HTPhyMode.field.MCS != 0))
{
pEntry->HTPhyMode.field.MCS = pAd->StaCfg.HTPhyMode.field.MCS;
if (pAd->MacTab.fAnyBASession)
{
AsicUpdateProtect(pAd, HT_FORCERTSCTS, ALLN_SETPROTECT, TRUE, (BOOLEAN)pAd->MlmeAux.AddHtInfo.AddHtInfo2.NonGfPresent);
}
else
{
AsicUpdateProtect(pAd, pAd->MlmeAux.AddHtInfo.AddHtInfo2.OperaionMode, ALLN_SETPROTECT, TRUE, (BOOLEAN)pAd->MlmeAux.AddHtInfo.AddHtInfo2.NonGfPresent);
}
}
else if ((pEntry->HTPhyMode.field.MCS == 8) && (pAd->StaCfg.HTPhyMode.field.MCS != 8))
{
pEntry->HTPhyMode.field.MCS = pAd->StaCfg.HTPhyMode.field.MCS;
if (pAd->MacTab.fAnyBASession)
{
AsicUpdateProtect(pAd, HT_FORCERTSCTS, ALLN_SETPROTECT, TRUE, (BOOLEAN)pAd->MlmeAux.AddHtInfo.AddHtInfo2.NonGfPresent);
}
else
{
AsicUpdateProtect(pAd, pAd->MlmeAux.AddHtInfo.AddHtInfo2.OperaionMode, ALLN_SETPROTECT, TRUE, (BOOLEAN)pAd->MlmeAux.AddHtInfo.AddHtInfo2.NonGfPresent);
}
}
else if ((pEntry->HTPhyMode.field.MCS != 0) && (pAd->StaCfg.HTPhyMode.field.MCS == 0))
{
AsicUpdateProtect(pAd, HT_RTSCTS_6M, ALLN_SETPROTECT, TRUE, (BOOLEAN)pAd->MlmeAux.AddHtInfo.AddHtInfo2.NonGfPresent);
}
else if ((pEntry->HTPhyMode.field.MCS != 8) && (pAd->StaCfg.HTPhyMode.field.MCS == 8))
{
AsicUpdateProtect(pAd, HT_RTSCTS_6M, ALLN_SETPROTECT, TRUE, (BOOLEAN)pAd->MlmeAux.AddHtInfo.AddHtInfo2.NonGfPresent);
}
pEntry->HTPhyMode.field.STBC = pAd->StaCfg.HTPhyMode.field.STBC;
pEntry->HTPhyMode.field.ShortGI = pAd->StaCfg.HTPhyMode.field.ShortGI;
pEntry->HTPhyMode.field.MCS = pAd->StaCfg.HTPhyMode.field.MCS;
pEntry->HTPhyMode.field.MODE = pAd->StaCfg.HTPhyMode.field.MODE;
if ((pAd->StaCfg.MaxHTPhyMode.field.MODE == MODE_HTGREENFIELD) &&
pAd->WIFItestbed.bGreenField)
pEntry->HTPhyMode.field.MODE = MODE_HTGREENFIELD;
}
pAd->LastTxRate = (USHORT)(pEntry->HTPhyMode.word);
}
/*
==========================================================================
Description:
This routine calculates the acumulated TxPER of eaxh TxRate. And
according to the calculation result, change CommonCfg.TxRate which
is the stable TX Rate we expect the Radio situation could sustained.
CommonCfg.TxRate will change dynamically within {RATE_1/RATE_6, MaxTxRate}
Output:
CommonCfg.TxRate -
IRQL = DISPATCH_LEVEL
NOTE:
call this routine every second
==========================================================================
*/
VOID MlmeDynamicTxRateSwitching(
IN PRTMP_ADAPTER pAd)
{
UCHAR UpRateIdx = 0, DownRateIdx = 0, CurrRateIdx;
ULONG i, AccuTxTotalCnt = 0, TxTotalCnt;
ULONG TxErrorRatio = 0;
BOOLEAN bTxRateChanged, bUpgradeQuality = FALSE;
PRTMP_TX_RATE_SWITCH pCurrTxRate, pNextTxRate = NULL;
PUCHAR pTable;
UCHAR TableSize = 0;
UCHAR InitTxRateIdx = 0, TrainUp, TrainDown;
CHAR Rssi, RssiOffset = 0;
TX_STA_CNT1_STRUC StaTx1;
TX_STA_CNT0_STRUC TxStaCnt0;
ULONG TxRetransmit = 0, TxSuccess = 0, TxFailCount = 0;
MAC_TABLE_ENTRY *pEntry;
//
// walk through MAC table, see if need to change AP's TX rate toward each entry
//
for (i = 1; i < MAX_LEN_OF_MAC_TABLE; i++)
{
pEntry = &pAd->MacTab.Content[i];
// check if this entry need to switch rate automatically
if (RTMPCheckEntryEnableAutoRateSwitch(pAd, pEntry) == FALSE)
continue;
if ((pAd->MacTab.Size == 1) || (pEntry->ValidAsDls))
{
#ifdef RT2860
Rssi = RTMPMaxRssi(pAd, (CHAR)pAd->StaCfg.RssiSample.AvgRssi0, (CHAR)pAd->StaCfg.RssiSample.AvgRssi1, (CHAR)pAd->StaCfg.RssiSample.AvgRssi2);
#endif
#ifdef RT2870
Rssi = RTMPMaxRssi(pAd,
pAd->StaCfg.RssiSample.AvgRssi0,
pAd->StaCfg.RssiSample.AvgRssi1,
pAd->StaCfg.RssiSample.AvgRssi2);
#endif
// Update statistic counter
RTMP_IO_READ32(pAd, TX_STA_CNT0, &TxStaCnt0.word);
RTMP_IO_READ32(pAd, TX_STA_CNT1, &StaTx1.word);
pAd->bUpdateBcnCntDone = TRUE;
TxRetransmit = StaTx1.field.TxRetransmit;
TxSuccess = StaTx1.field.TxSuccess;
TxFailCount = TxStaCnt0.field.TxFailCount;
TxTotalCnt = TxRetransmit + TxSuccess + TxFailCount;
pAd->RalinkCounters.OneSecTxRetryOkCount += StaTx1.field.TxRetransmit;
pAd->RalinkCounters.OneSecTxNoRetryOkCount += StaTx1.field.TxSuccess;
pAd->RalinkCounters.OneSecTxFailCount += TxStaCnt0.field.TxFailCount;
pAd->WlanCounters.TransmittedFragmentCount.u.LowPart += StaTx1.field.TxSuccess;
pAd->WlanCounters.RetryCount.u.LowPart += StaTx1.field.TxRetransmit;
pAd->WlanCounters.FailedCount.u.LowPart += TxStaCnt0.field.TxFailCount;
// if no traffic in the past 1-sec period, don't change TX rate,
// but clear all bad history. because the bad history may affect the next
// Chariot throughput test
AccuTxTotalCnt = pAd->RalinkCounters.OneSecTxNoRetryOkCount +
pAd->RalinkCounters.OneSecTxRetryOkCount +
pAd->RalinkCounters.OneSecTxFailCount;
if (TxTotalCnt)
TxErrorRatio = ((TxRetransmit + TxFailCount) * 100) / TxTotalCnt;
}
else
{
#ifdef RT2860
Rssi = RTMPMaxRssi(pAd, (CHAR)pEntry->RssiSample.AvgRssi0, (CHAR)pEntry->RssiSample.AvgRssi1, (CHAR)pEntry->RssiSample.AvgRssi2);
#endif
#ifdef RT2870
if (INFRA_ON(pAd) && (i == 1))
Rssi = RTMPMaxRssi(pAd,
pAd->StaCfg.RssiSample.AvgRssi0,
pAd->StaCfg.RssiSample.AvgRssi1,
pAd->StaCfg.RssiSample.AvgRssi2);
else
Rssi = RTMPMaxRssi(pAd,
pEntry->RssiSample.AvgRssi0,
pEntry->RssiSample.AvgRssi1,
pEntry->RssiSample.AvgRssi2);
#endif
TxTotalCnt = pEntry->OneSecTxNoRetryOkCount +
pEntry->OneSecTxRetryOkCount +
pEntry->OneSecTxFailCount;
if (TxTotalCnt)
TxErrorRatio = ((pEntry->OneSecTxRetryOkCount + pEntry->OneSecTxFailCount) * 100) / TxTotalCnt;
}
CurrRateIdx = pEntry->CurrTxRateIndex;
MlmeSelectTxRateTable(pAd, pEntry, &pTable, &TableSize, &InitTxRateIdx);
if (CurrRateIdx >= TableSize)
{
CurrRateIdx = TableSize - 1;
}
// When switch from Fixed rate -> auto rate, the REAL TX rate might be different from pAd->CommonCfg.TxRateIndex.
// So need to sync here.
pCurrTxRate = (PRTMP_TX_RATE_SWITCH) &pTable[(CurrRateIdx+1)*5];
if ((pEntry->HTPhyMode.field.MCS != pCurrTxRate->CurrMCS)
//&& (pAd->StaCfg.bAutoTxRateSwitch == TRUE)
)
{
// Need to sync Real Tx rate and our record.
// Then return for next DRS.
pCurrTxRate = (PRTMP_TX_RATE_SWITCH) &pTable[(InitTxRateIdx+1)*5];
pEntry->CurrTxRateIndex = InitTxRateIdx;
MlmeSetTxRate(pAd, pEntry, pCurrTxRate);
// reset all OneSecTx counters
RESET_ONE_SEC_TX_CNT(pEntry);
continue;
}
// decide the next upgrade rate and downgrade rate, if any
if ((CurrRateIdx > 0) && (CurrRateIdx < (TableSize - 1)))
{
UpRateIdx = CurrRateIdx + 1;
DownRateIdx = CurrRateIdx -1;
}
else if (CurrRateIdx == 0)
{
UpRateIdx = CurrRateIdx + 1;
DownRateIdx = CurrRateIdx;
}
else if (CurrRateIdx == (TableSize - 1))
{
UpRateIdx = CurrRateIdx;
DownRateIdx = CurrRateIdx - 1;
}
pCurrTxRate = (PRTMP_TX_RATE_SWITCH) &pTable[(CurrRateIdx+1)*5];
if ((Rssi > -65) && (pCurrTxRate->Mode >= MODE_HTMIX))
{
TrainUp = (pCurrTxRate->TrainUp + (pCurrTxRate->TrainUp >> 1));
TrainDown = (pCurrTxRate->TrainDown + (pCurrTxRate->TrainDown >> 1));
}
else
{
TrainUp = pCurrTxRate->TrainUp;
TrainDown = pCurrTxRate->TrainDown;
}
//pAd->DrsCounters.LastTimeTxRateChangeAction = pAd->DrsCounters.LastSecTxRateChangeAction;
//
// Keep the last time TxRateChangeAction status.
//
pEntry->LastTimeTxRateChangeAction = pEntry->LastSecTxRateChangeAction;
//
// CASE 1. when TX samples are fewer than 15, then decide TX rate solely on RSSI
// (criteria copied from RT2500 for Netopia case)
//
if (TxTotalCnt <= 15)
{
CHAR idx = 0;
UCHAR TxRateIdx;
//UCHAR MCS0 = 0, MCS1 = 0, MCS2 = 0, MCS3 = 0, MCS4 = 0, MCS7 = 0, MCS12 = 0, MCS13 = 0, MCS14 = 0, MCS15 = 0;
UCHAR MCS0 = 0, MCS1 = 0, MCS2 = 0, MCS3 = 0, MCS4 = 0, MCS5 =0, MCS6 = 0, MCS7 = 0;
UCHAR MCS12 = 0, MCS13 = 0, MCS14 = 0, MCS15 = 0;
UCHAR MCS20 = 0, MCS21 = 0, MCS22 = 0, MCS23 = 0; // 3*3
// check the existence and index of each needed MCS
while (idx < pTable[0])
{
pCurrTxRate = (PRTMP_TX_RATE_SWITCH) &pTable[(idx+1)*5];
if (pCurrTxRate->CurrMCS == MCS_0)
{
MCS0 = idx;
}
else if (pCurrTxRate->CurrMCS == MCS_1)
{
MCS1 = idx;
}
else if (pCurrTxRate->CurrMCS == MCS_2)
{
MCS2 = idx;
}
else if (pCurrTxRate->CurrMCS == MCS_3)
{
MCS3 = idx;
}
else if (pCurrTxRate->CurrMCS == MCS_4)
{
MCS4 = idx;
}
else if (pCurrTxRate->CurrMCS == MCS_5)
{
MCS5 = idx;
}
else if (pCurrTxRate->CurrMCS == MCS_6)
{
MCS6 = idx;
}
//else if (pCurrTxRate->CurrMCS == MCS_7)
else if ((pCurrTxRate->CurrMCS == MCS_7) && (pCurrTxRate->ShortGI == GI_800)) // prevent the highest MCS using short GI when 1T and low throughput
{
MCS7 = idx;
}
else if (pCurrTxRate->CurrMCS == MCS_12)
{
MCS12 = idx;
}
else if (pCurrTxRate->CurrMCS == MCS_13)
{
MCS13 = idx;
}
else if (pCurrTxRate->CurrMCS == MCS_14)
{
MCS14 = idx;
}
else if ((pCurrTxRate->CurrMCS == MCS_15) && (pCurrTxRate->ShortGI == GI_800)) //we hope to use ShortGI as initial rate, however Atheros's chip has bugs when short GI
{
MCS15 = idx;
}
else if (pCurrTxRate->CurrMCS == MCS_20) // 3*3
{
MCS20 = idx;
}
else if (pCurrTxRate->CurrMCS == MCS_21)
{
MCS21 = idx;
}
else if (pCurrTxRate->CurrMCS == MCS_22)
{
MCS22 = idx;
}
else if (pCurrTxRate->CurrMCS == MCS_23)
{
MCS23 = idx;
}
idx ++;
}
if (pAd->LatchRfRegs.Channel <= 14)
{
if (pAd->NicConfig2.field.ExternalLNAForG)
{
RssiOffset = 2;
}
else
{
RssiOffset = 5;
}
}
else
{
if (pAd->NicConfig2.field.ExternalLNAForA)
{
RssiOffset = 5;
}
else
{
RssiOffset = 8;
}
}
/*if (MCS15)*/
if ((pTable == RateSwitchTable11BGN3S) ||
(pTable == RateSwitchTable11N3S) ||
(pTable == RateSwitchTable))
{// N mode with 3 stream // 3*3
if (MCS23 && (Rssi >= -70))
TxRateIdx = MCS15;
else if (MCS22 && (Rssi >= -72))
TxRateIdx = MCS14;
else if (MCS21 && (Rssi >= -76))
TxRateIdx = MCS13;
else if (MCS20 && (Rssi >= -78))
TxRateIdx = MCS12;
else if (MCS4 && (Rssi >= -82))
TxRateIdx = MCS4;
else if (MCS3 && (Rssi >= -84))
TxRateIdx = MCS3;
else if (MCS2 && (Rssi >= -86))
TxRateIdx = MCS2;
else if (MCS1 && (Rssi >= -88))
TxRateIdx = MCS1;
else
TxRateIdx = MCS0;
}
else if ((pTable == RateSwitchTable11BGN2S) || (pTable == RateSwitchTable11BGN2SForABand) ||(pTable == RateSwitchTable11N2S) ||(pTable == RateSwitchTable11N2SForABand)) // 3*3
{// N mode with 2 stream
if (MCS15 && (Rssi >= (-70+RssiOffset)))
TxRateIdx = MCS15;
else if (MCS14 && (Rssi >= (-72+RssiOffset)))
TxRateIdx = MCS14;
else if (MCS13 && (Rssi >= (-76+RssiOffset)))
TxRateIdx = MCS13;
else if (MCS12 && (Rssi >= (-78+RssiOffset)))
TxRateIdx = MCS12;
else if (MCS4 && (Rssi >= (-82+RssiOffset)))
TxRateIdx = MCS4;
else if (MCS3 && (Rssi >= (-84+RssiOffset)))
TxRateIdx = MCS3;
else if (MCS2 && (Rssi >= (-86+RssiOffset)))
TxRateIdx = MCS2;
else if (MCS1 && (Rssi >= (-88+RssiOffset)))
TxRateIdx = MCS1;
else
TxRateIdx = MCS0;
}
else if ((pTable == RateSwitchTable11BGN1S) || (pTable == RateSwitchTable11N1S))
{// N mode with 1 stream
if (MCS7 && (Rssi > (-72+RssiOffset)))
TxRateIdx = MCS7;
else if (MCS6 && (Rssi > (-74+RssiOffset)))
TxRateIdx = MCS6;
else if (MCS5 && (Rssi > (-77+RssiOffset)))
TxRateIdx = MCS5;
else if (MCS4 && (Rssi > (-79+RssiOffset)))
TxRateIdx = MCS4;
else if (MCS3 && (Rssi > (-81+RssiOffset)))
TxRateIdx = MCS3;
else if (MCS2 && (Rssi > (-83+RssiOffset)))
TxRateIdx = MCS2;
else if (MCS1 && (Rssi > (-86+RssiOffset)))
TxRateIdx = MCS1;
else
TxRateIdx = MCS0;
}
else
{// Legacy mode
if (MCS7 && (Rssi > -70))
TxRateIdx = MCS7;
else if (MCS6 && (Rssi > -74))
TxRateIdx = MCS6;
else if (MCS5 && (Rssi > -78))
TxRateIdx = MCS5;
else if (MCS4 && (Rssi > -82))
TxRateIdx = MCS4;
else if (MCS4 == 0) // for B-only mode
TxRateIdx = MCS3;
else if (MCS3 && (Rssi > -85))
TxRateIdx = MCS3;
else if (MCS2 && (Rssi > -87))
TxRateIdx = MCS2;
else if (MCS1 && (Rssi > -90))
TxRateIdx = MCS1;
else
TxRateIdx = MCS0;
}
{
pEntry->CurrTxRateIndex = TxRateIdx;
pNextTxRate = (PRTMP_TX_RATE_SWITCH) &pTable[(pEntry->CurrTxRateIndex+1)*5];
MlmeSetTxRate(pAd, pEntry, pNextTxRate);
}
NdisZeroMemory(pEntry->TxQuality, sizeof(USHORT) * MAX_STEP_OF_TX_RATE_SWITCH);
NdisZeroMemory(pEntry->PER, sizeof(UCHAR) * MAX_STEP_OF_TX_RATE_SWITCH);
pEntry->fLastSecAccordingRSSI = TRUE;
// reset all OneSecTx counters
RESET_ONE_SEC_TX_CNT(pEntry);
continue;
}
if (pEntry->fLastSecAccordingRSSI == TRUE)
{
pEntry->fLastSecAccordingRSSI = FALSE;
pEntry->LastSecTxRateChangeAction = 0;
// reset all OneSecTx counters
RESET_ONE_SEC_TX_CNT(pEntry);
continue;
}
do
{
BOOLEAN bTrainUpDown = FALSE;
pEntry->CurrTxRateStableTime ++;
// downgrade TX quality if PER >= Rate-Down threshold
if (TxErrorRatio >= TrainDown)
{
bTrainUpDown = TRUE;
pEntry->TxQuality[CurrRateIdx] = DRS_TX_QUALITY_WORST_BOUND;
}
// upgrade TX quality if PER <= Rate-Up threshold
else if (TxErrorRatio <= TrainUp)
{
bTrainUpDown = TRUE;
bUpgradeQuality = TRUE;
if (pEntry->TxQuality[CurrRateIdx])
pEntry->TxQuality[CurrRateIdx] --; // quality very good in CurrRate
if (pEntry->TxRateUpPenalty)
pEntry->TxRateUpPenalty --;
else if (pEntry->TxQuality[UpRateIdx])
pEntry->TxQuality[UpRateIdx] --; // may improve next UP rate's quality
}
pEntry->PER[CurrRateIdx] = (UCHAR)TxErrorRatio;
if (bTrainUpDown)
{
// perform DRS - consider TxRate Down first, then rate up.
if ((CurrRateIdx != DownRateIdx) && (pEntry->TxQuality[CurrRateIdx] >= DRS_TX_QUALITY_WORST_BOUND))
{
pEntry->CurrTxRateIndex = DownRateIdx;
}
else if ((CurrRateIdx != UpRateIdx) && (pEntry->TxQuality[UpRateIdx] <= 0))
{
pEntry->CurrTxRateIndex = UpRateIdx;
}
}
} while (FALSE);
// if rate-up happen, clear all bad history of all TX rates
if (pEntry->CurrTxRateIndex > CurrRateIdx)
{
pEntry->CurrTxRateStableTime = 0;
pEntry->TxRateUpPenalty = 0;
pEntry->LastSecTxRateChangeAction = 1; // rate UP
NdisZeroMemory(pEntry->TxQuality, sizeof(USHORT) * MAX_STEP_OF_TX_RATE_SWITCH);
NdisZeroMemory(pEntry->PER, sizeof(UCHAR) * MAX_STEP_OF_TX_RATE_SWITCH);
//
// For TxRate fast train up
//
if (!pAd->StaCfg.StaQuickResponeForRateUpTimerRunning)
{
RTMPSetTimer(&pAd->StaCfg.StaQuickResponeForRateUpTimer, 100);
pAd->StaCfg.StaQuickResponeForRateUpTimerRunning = TRUE;
}
bTxRateChanged = TRUE;
}
// if rate-down happen, only clear DownRate's bad history
else if (pEntry->CurrTxRateIndex < CurrRateIdx)
{
pEntry->CurrTxRateStableTime = 0;
pEntry->TxRateUpPenalty = 0; // no penalty
pEntry->LastSecTxRateChangeAction = 2; // rate DOWN
pEntry->TxQuality[pEntry->CurrTxRateIndex] = 0;
pEntry->PER[pEntry->CurrTxRateIndex] = 0;
//
// For TxRate fast train down
//
if (!pAd->StaCfg.StaQuickResponeForRateUpTimerRunning)
{
RTMPSetTimer(&pAd->StaCfg.StaQuickResponeForRateUpTimer, 100);
pAd->StaCfg.StaQuickResponeForRateUpTimerRunning = TRUE;
}
bTxRateChanged = TRUE;
}
else
{
pEntry->LastSecTxRateChangeAction = 0; // rate no change
bTxRateChanged = FALSE;
}
pEntry->LastTxOkCount = TxSuccess;
// reset all OneSecTx counters
RESET_ONE_SEC_TX_CNT(pEntry);
pNextTxRate = (PRTMP_TX_RATE_SWITCH) &pTable[(pEntry->CurrTxRateIndex+1)*5];
if (bTxRateChanged && pNextTxRate)
{
MlmeSetTxRate(pAd, pEntry, pNextTxRate);
}
}
}
/*
========================================================================
Routine Description:
Station side, Auto TxRate faster train up timer call back function.
Arguments:
SystemSpecific1 - Not used.
FunctionContext - Pointer to our Adapter context.
SystemSpecific2 - Not used.
SystemSpecific3 - Not used.
Return Value:
None
========================================================================
*/
VOID StaQuickResponeForRateUpExec(
IN PVOID SystemSpecific1,
IN PVOID FunctionContext,
IN PVOID SystemSpecific2,
IN PVOID SystemSpecific3)
{
PRTMP_ADAPTER pAd = (PRTMP_ADAPTER)FunctionContext;
UCHAR UpRateIdx = 0, DownRateIdx = 0, CurrRateIdx = 0;
ULONG TxTotalCnt;
ULONG TxErrorRatio = 0;
#ifdef RT2860
BOOLEAN bTxRateChanged = TRUE; //, bUpgradeQuality = FALSE;
#endif
#ifdef RT2870
BOOLEAN bTxRateChanged; //, bUpgradeQuality = FALSE;
#endif
PRTMP_TX_RATE_SWITCH pCurrTxRate, pNextTxRate = NULL;
PUCHAR pTable;
UCHAR TableSize = 0;
UCHAR InitTxRateIdx = 0, TrainUp, TrainDown;
TX_STA_CNT1_STRUC StaTx1;
TX_STA_CNT0_STRUC TxStaCnt0;
CHAR Rssi, ratio;
ULONG TxRetransmit = 0, TxSuccess = 0, TxFailCount = 0;
MAC_TABLE_ENTRY *pEntry;
ULONG i;
pAd->StaCfg.StaQuickResponeForRateUpTimerRunning = FALSE;
//
// walk through MAC table, see if need to change AP's TX rate toward each entry
//
for (i = 1; i < MAX_LEN_OF_MAC_TABLE; i++)
{
pEntry = &pAd->MacTab.Content[i];
// check if this entry need to switch rate automatically
if (RTMPCheckEntryEnableAutoRateSwitch(pAd, pEntry) == FALSE)
continue;
#ifdef RT2860
//Rssi = RTMPMaxRssi(pAd, (CHAR)pAd->StaCfg.AvgRssi0, (CHAR)pAd->StaCfg.AvgRssi1, (CHAR)pAd->StaCfg.AvgRssi2);
if (pAd->Antenna.field.TxPath > 1)
Rssi = (pAd->StaCfg.RssiSample.AvgRssi0 + pAd->StaCfg.RssiSample.AvgRssi1) >> 1;
else
Rssi = pAd->StaCfg.RssiSample.AvgRssi0;
#endif
#ifdef RT2870
if (INFRA_ON(pAd) && (i == 1))
Rssi = RTMPMaxRssi(pAd,
pAd->StaCfg.RssiSample.AvgRssi0,
pAd->StaCfg.RssiSample.AvgRssi1,
pAd->StaCfg.RssiSample.AvgRssi2);
else
Rssi = RTMPMaxRssi(pAd,
pEntry->RssiSample.AvgRssi0,
pEntry->RssiSample.AvgRssi1,
pEntry->RssiSample.AvgRssi2);
#endif
CurrRateIdx = pAd->CommonCfg.TxRateIndex;
MlmeSelectTxRateTable(pAd, pEntry, &pTable, &TableSize, &InitTxRateIdx);
// decide the next upgrade rate and downgrade rate, if any
if ((CurrRateIdx > 0) && (CurrRateIdx < (TableSize - 1)))
{
UpRateIdx = CurrRateIdx + 1;
DownRateIdx = CurrRateIdx -1;
}
else if (CurrRateIdx == 0)
{
UpRateIdx = CurrRateIdx + 1;
DownRateIdx = CurrRateIdx;
}
else if (CurrRateIdx == (TableSize - 1))
{
UpRateIdx = CurrRateIdx;
DownRateIdx = CurrRateIdx - 1;
}
pCurrTxRate = (PRTMP_TX_RATE_SWITCH) &pTable[(CurrRateIdx+1)*5];
if ((Rssi > -65) && (pCurrTxRate->Mode >= MODE_HTMIX))
{
TrainUp = (pCurrTxRate->TrainUp + (pCurrTxRate->TrainUp >> 1));
TrainDown = (pCurrTxRate->TrainDown + (pCurrTxRate->TrainDown >> 1));
}
else
{
TrainUp = pCurrTxRate->TrainUp;
TrainDown = pCurrTxRate->TrainDown;
}
if (pAd->MacTab.Size == 1)
{
// Update statistic counter
RTMP_IO_READ32(pAd, TX_STA_CNT0, &TxStaCnt0.word);
RTMP_IO_READ32(pAd, TX_STA_CNT1, &StaTx1.word);
TxRetransmit = StaTx1.field.TxRetransmit;
TxSuccess = StaTx1.field.TxSuccess;
TxFailCount = TxStaCnt0.field.TxFailCount;
TxTotalCnt = TxRetransmit + TxSuccess + TxFailCount;
pAd->RalinkCounters.OneSecTxRetryOkCount += StaTx1.field.TxRetransmit;
pAd->RalinkCounters.OneSecTxNoRetryOkCount += StaTx1.field.TxSuccess;
pAd->RalinkCounters.OneSecTxFailCount += TxStaCnt0.field.TxFailCount;
pAd->WlanCounters.TransmittedFragmentCount.u.LowPart += StaTx1.field.TxSuccess;
pAd->WlanCounters.RetryCount.u.LowPart += StaTx1.field.TxRetransmit;
pAd->WlanCounters.FailedCount.u.LowPart += TxStaCnt0.field.TxFailCount;
if (TxTotalCnt)
TxErrorRatio = ((TxRetransmit + TxFailCount) * 100) / TxTotalCnt;
}
else
{
TxTotalCnt = pEntry->OneSecTxNoRetryOkCount +
pEntry->OneSecTxRetryOkCount +
pEntry->OneSecTxFailCount;
if (TxTotalCnt)
TxErrorRatio = ((pEntry->OneSecTxRetryOkCount + pEntry->OneSecTxFailCount) * 100) / TxTotalCnt;
}
//
// CASE 1. when TX samples are fewer than 15, then decide TX rate solely on RSSI
// (criteria copied from RT2500 for Netopia case)
//
if (TxTotalCnt <= 12)
{
NdisZeroMemory(pAd->DrsCounters.TxQuality, sizeof(USHORT) * MAX_STEP_OF_TX_RATE_SWITCH);
NdisZeroMemory(pAd->DrsCounters.PER, sizeof(UCHAR) * MAX_STEP_OF_TX_RATE_SWITCH);
if ((pAd->DrsCounters.LastSecTxRateChangeAction == 1) && (CurrRateIdx != DownRateIdx))
{
pAd->CommonCfg.TxRateIndex = DownRateIdx;
pAd->DrsCounters.TxQuality[CurrRateIdx] = DRS_TX_QUALITY_WORST_BOUND;
}
else if ((pAd->DrsCounters.LastSecTxRateChangeAction == 2) && (CurrRateIdx != UpRateIdx))
{
pAd->CommonCfg.TxRateIndex = UpRateIdx;
}
DBGPRINT_RAW(RT_DEBUG_TRACE,("QuickDRS: TxTotalCnt <= 15, train back to original rate \n"));
return;
}
do
{
ULONG OneSecTxNoRetryOKRationCount;
if (pAd->DrsCounters.LastTimeTxRateChangeAction == 0)
ratio = 5;
else
ratio = 4;
// downgrade TX quality if PER >= Rate-Down threshold
if (TxErrorRatio >= TrainDown)
{
pAd->DrsCounters.TxQuality[CurrRateIdx] = DRS_TX_QUALITY_WORST_BOUND;
}
pAd->DrsCounters.PER[CurrRateIdx] = (UCHAR)TxErrorRatio;
OneSecTxNoRetryOKRationCount = (TxSuccess * ratio);
// perform DRS - consider TxRate Down first, then rate up.
if ((pAd->DrsCounters.LastSecTxRateChangeAction == 1) && (CurrRateIdx != DownRateIdx))
{
if ((pAd->DrsCounters.LastTxOkCount + 2) >= OneSecTxNoRetryOKRationCount)
{
pAd->CommonCfg.TxRateIndex = DownRateIdx;
pAd->DrsCounters.TxQuality[CurrRateIdx] = DRS_TX_QUALITY_WORST_BOUND;
}
}
else if ((pAd->DrsCounters.LastSecTxRateChangeAction == 2) && (CurrRateIdx != UpRateIdx))
{
if ((TxErrorRatio >= 50) || (TxErrorRatio >= TrainDown))
{
}
else if ((pAd->DrsCounters.LastTxOkCount + 2) >= OneSecTxNoRetryOKRationCount)
{
pAd->CommonCfg.TxRateIndex = UpRateIdx;
}
}
}while (FALSE);
// if rate-up happen, clear all bad history of all TX rates
if (pAd->CommonCfg.TxRateIndex > CurrRateIdx)
{
pAd->DrsCounters.TxRateUpPenalty = 0;
NdisZeroMemory(pAd->DrsCounters.TxQuality, sizeof(USHORT) * MAX_STEP_OF_TX_RATE_SWITCH);
NdisZeroMemory(pAd->DrsCounters.PER, sizeof(UCHAR) * MAX_STEP_OF_TX_RATE_SWITCH);
#ifdef RT2870
bTxRateChanged = TRUE;
#endif
}
// if rate-down happen, only clear DownRate's bad history
else if (pAd->CommonCfg.TxRateIndex < CurrRateIdx)
{
DBGPRINT_RAW(RT_DEBUG_TRACE,("QuickDRS: --TX rate from %d to %d \n", CurrRateIdx, pAd->CommonCfg.TxRateIndex));
pAd->DrsCounters.TxRateUpPenalty = 0; // no penalty
pAd->DrsCounters.TxQuality[pAd->CommonCfg.TxRateIndex] = 0;
pAd->DrsCounters.PER[pAd->CommonCfg.TxRateIndex] = 0;
#ifdef RT2870
bTxRateChanged = TRUE;
#endif
}
else
{
bTxRateChanged = FALSE;
}
pNextTxRate = (PRTMP_TX_RATE_SWITCH) &pTable[(pAd->CommonCfg.TxRateIndex+1)*5];
if (bTxRateChanged && pNextTxRate)
{
MlmeSetTxRate(pAd, pEntry, pNextTxRate);
}
}
}
/*
==========================================================================
Description:
This routine is executed periodically inside MlmePeriodicExec() after
association with an AP.
It checks if StaCfg.Psm is consistent with user policy (recorded in
StaCfg.WindowsPowerMode). If not, enforce user policy. However,
there're some conditions to consider:
1. we don't support power-saving in ADHOC mode, so Psm=PWR_ACTIVE all
the time when Mibss==TRUE
2. When link up in INFRA mode, Psm should not be switch to PWR_SAVE
if outgoing traffic available in TxRing or MgmtRing.
Output:
1. change pAd->StaCfg.Psm to PWR_SAVE or leave it untouched
IRQL = DISPATCH_LEVEL
==========================================================================
*/
VOID MlmeCheckPsmChange(
IN PRTMP_ADAPTER pAd,
IN ULONG Now32)
{
ULONG PowerMode;
// condition -
// 1. Psm maybe ON only happen in INFRASTRUCTURE mode
// 2. user wants either MAX_PSP or FAST_PSP
// 3. but current psm is not in PWR_SAVE
// 4. CNTL state machine is not doing SCANning
// 5. no TX SUCCESS event for the past 1-sec period
#ifdef NDIS51_MINIPORT
if (pAd->StaCfg.WindowsPowerProfile == NdisPowerProfileBattery)
PowerMode = pAd->StaCfg.WindowsBatteryPowerMode;
else
#endif
PowerMode = pAd->StaCfg.WindowsPowerMode;
if (INFRA_ON(pAd) &&
(PowerMode != Ndis802_11PowerModeCAM) &&
(pAd->StaCfg.Psm == PWR_ACTIVE) &&
#ifdef RT2860
RTMP_TEST_PSFLAG(pAd, fRTMP_PS_CAN_GO_SLEEP))
#endif
#if !defined(RT2860) && !defined(RT30xx)
(pAd->Mlme.CntlMachine.CurrState == CNTL_IDLE))
#endif
#ifndef RT30xx
{
NdisGetSystemUpTime(&pAd->Mlme.LastSendNULLpsmTime);
pAd->RalinkCounters.RxCountSinceLastNULL = 0;
MlmeSetPsmBit(pAd, PWR_SAVE);
if (!(pAd->CommonCfg.bAPSDCapable && pAd->CommonCfg.APEdcaParm.bAPSDCapable))
{
RTMPSendNullFrame(pAd, pAd->CommonCfg.TxRate, FALSE);
}
else
{
RTMPSendNullFrame(pAd, pAd->CommonCfg.TxRate, TRUE);
}
}
#endif
#ifdef RT30xx
// (! RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS))
(pAd->Mlme.CntlMachine.CurrState == CNTL_IDLE) /*&&
(pAd->RalinkCounters.OneSecTxNoRetryOkCount == 0) &&
(pAd->RalinkCounters.OneSecTxRetryOkCount == 0)*/)
{
// add by johnli, use Rx OK data count per second to calculate throughput
// If Ttraffic is too high ( > 400 Rx per second), don't go to sleep mode. If tx rate is low, use low criteria
// Mode=CCK/MCS=3 => 11 Mbps, Mode=OFDM/MCS=3 => 18 Mbps
if (((pAd->StaCfg.HTPhyMode.field.MCS <= 3) &&
/* Iverson mark
(pAd->StaCfg.HTPhyMode.field.MODE <= MODE_OFDM) &&
*/
(pAd->RalinkCounters.OneSecRxOkDataCnt < (ULONG)100)) ||
((pAd->StaCfg.HTPhyMode.field.MCS > 3) &&
/* Iverson mark
(pAd->StaCfg.HTPhyMode.field.MODE > MODE_OFDM) &&
*/
(pAd->RalinkCounters.OneSecRxOkDataCnt < (ULONG)400)))
{
// Get this time
NdisGetSystemUpTime(&pAd->Mlme.LastSendNULLpsmTime);
pAd->RalinkCounters.RxCountSinceLastNULL = 0;
MlmeSetPsmBit(pAd, PWR_SAVE);
if (!(pAd->CommonCfg.bAPSDCapable && pAd->CommonCfg.APEdcaParm.bAPSDCapable))
{
RTMPSendNullFrame(pAd, pAd->CommonCfg.TxRate, FALSE);
}
else
{
RTMPSendNullFrame(pAd, pAd->CommonCfg.TxRate, TRUE);
}
}
}
#endif
}
// IRQL = PASSIVE_LEVEL
// IRQL = DISPATCH_LEVEL
VOID MlmeSetPsmBit(
IN PRTMP_ADAPTER pAd,
IN USHORT psm)
{
AUTO_RSP_CFG_STRUC csr4;
pAd->StaCfg.Psm = psm;
RTMP_IO_READ32(pAd, AUTO_RSP_CFG, &csr4.word);
csr4.field.AckCtsPsmBit = (psm == PWR_SAVE)? 1:0;
RTMP_IO_WRITE32(pAd, AUTO_RSP_CFG, csr4.word);
#ifndef RT30xx
DBGPRINT(RT_DEBUG_TRACE, ("MlmeSetPsmBit = %d\n", psm));
#endif
}
// IRQL = DISPATCH_LEVEL
VOID MlmeSetTxPreamble(
IN PRTMP_ADAPTER pAd,
IN USHORT TxPreamble)
{
AUTO_RSP_CFG_STRUC csr4;
//
// Always use Long preamble before verifiation short preamble functionality works well.
// Todo: remove the following line if short preamble functionality works
//
//TxPreamble = Rt802_11PreambleLong;
RTMP_IO_READ32(pAd, AUTO_RSP_CFG, &csr4.word);
if (TxPreamble == Rt802_11PreambleLong)
{
DBGPRINT(RT_DEBUG_TRACE, ("MlmeSetTxPreamble (= LONG PREAMBLE)\n"));
OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_SHORT_PREAMBLE_INUSED);
csr4.field.AutoResponderPreamble = 0;
}
else
{
// NOTE: 1Mbps should always use long preamble
DBGPRINT(RT_DEBUG_TRACE, ("MlmeSetTxPreamble (= SHORT PREAMBLE)\n"));
OPSTATUS_SET_FLAG(pAd, fOP_STATUS_SHORT_PREAMBLE_INUSED);
csr4.field.AutoResponderPreamble = 1;
}
RTMP_IO_WRITE32(pAd, AUTO_RSP_CFG, csr4.word);
}
/*
==========================================================================
Description:
Update basic rate bitmap
==========================================================================
*/
VOID UpdateBasicRateBitmap(
IN PRTMP_ADAPTER pAdapter)
{
INT i, j;
/* 1 2 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54 */
UCHAR rate[] = { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 };
UCHAR *sup_p = pAdapter->CommonCfg.SupRate;
UCHAR *ext_p = pAdapter->CommonCfg.ExtRate;
ULONG bitmap = pAdapter->CommonCfg.BasicRateBitmap;
/* if A mode, always use fix BasicRateBitMap */
//if (pAdapter->CommonCfg.Channel == PHY_11A)
if (pAdapter->CommonCfg.Channel > 14)
pAdapter->CommonCfg.BasicRateBitmap = 0x150; /* 6, 12, 24M */
/* End of if */
if (pAdapter->CommonCfg.BasicRateBitmap > 4095)
{
/* (2 ^ MAX_LEN_OF_SUPPORTED_RATES) -1 */
return;
} /* End of if */
for(i=0; i<MAX_LEN_OF_SUPPORTED_RATES; i++)
{
sup_p[i] &= 0x7f;
ext_p[i] &= 0x7f;
} /* End of for */
for(i=0; i<MAX_LEN_OF_SUPPORTED_RATES; i++)
{
if (bitmap & (1 << i))
{
for(j=0; j<MAX_LEN_OF_SUPPORTED_RATES; j++)
{
if (sup_p[j] == rate[i])
sup_p[j] |= 0x80;
/* End of if */
} /* End of for */
for(j=0; j<MAX_LEN_OF_SUPPORTED_RATES; j++)
{
if (ext_p[j] == rate[i])
ext_p[j] |= 0x80;
/* End of if */
} /* End of for */
} /* End of if */
} /* End of for */
} /* End of UpdateBasicRateBitmap */
// IRQL = PASSIVE_LEVEL
// IRQL = DISPATCH_LEVEL
// bLinkUp is to identify the inital link speed.
// TRUE indicates the rate update at linkup, we should not try to set the rate at 54Mbps.
VOID MlmeUpdateTxRates(
IN PRTMP_ADAPTER pAd,
IN BOOLEAN bLinkUp,
IN UCHAR apidx)
{
int i, num;
UCHAR Rate = RATE_6, MaxDesire = RATE_1, MaxSupport = RATE_1;
UCHAR MinSupport = RATE_54;
ULONG BasicRateBitmap = 0;
UCHAR CurrBasicRate = RATE_1;
UCHAR *pSupRate, SupRateLen, *pExtRate, ExtRateLen;
PHTTRANSMIT_SETTING pHtPhy = NULL;
PHTTRANSMIT_SETTING pMaxHtPhy = NULL;
PHTTRANSMIT_SETTING pMinHtPhy = NULL;
BOOLEAN *auto_rate_cur_p;
UCHAR HtMcs = MCS_AUTO;
// find max desired rate
UpdateBasicRateBitmap(pAd);
num = 0;
auto_rate_cur_p = NULL;
for (i=0; i<MAX_LEN_OF_SUPPORTED_RATES; i++)
{
switch (pAd->CommonCfg.DesireRate[i] & 0x7f)
{
case 2: Rate = RATE_1; num++; break;
case 4: Rate = RATE_2; num++; break;
case 11: Rate = RATE_5_5; num++; break;
case 22: Rate = RATE_11; num++; break;
case 12: Rate = RATE_6; num++; break;
case 18: Rate = RATE_9; num++; break;
case 24: Rate = RATE_12; num++; break;
case 36: Rate = RATE_18; num++; break;
case 48: Rate = RATE_24; num++; break;
case 72: Rate = RATE_36; num++; break;
case 96: Rate = RATE_48; num++; break;
case 108: Rate = RATE_54; num++; break;
//default: Rate = RATE_1; break;
}
if (MaxDesire < Rate) MaxDesire = Rate;
}
//===========================================================================
//===========================================================================
{
pHtPhy = &pAd->StaCfg.HTPhyMode;
pMaxHtPhy = &pAd->StaCfg.MaxHTPhyMode;
pMinHtPhy = &pAd->StaCfg.MinHTPhyMode;
auto_rate_cur_p = &pAd->StaCfg.bAutoTxRateSwitch;
HtMcs = pAd->StaCfg.DesiredTransmitSetting.field.MCS;
if ((pAd->StaCfg.BssType == BSS_ADHOC) &&
(pAd->CommonCfg.PhyMode == PHY_11B) &&
(MaxDesire > RATE_11))
{
MaxDesire = RATE_11;
}
}
pAd->CommonCfg.MaxDesiredRate = MaxDesire;
pMinHtPhy->word = 0;
pMaxHtPhy->word = 0;
pHtPhy->word = 0;
// Auto rate switching is enabled only if more than one DESIRED RATES are
// specified; otherwise disabled
if (num <= 1)
{
*auto_rate_cur_p = FALSE;
}
else
{
*auto_rate_cur_p = TRUE;
}
#if 1
if (HtMcs != MCS_AUTO)
{
*auto_rate_cur_p = FALSE;
}
else
{
*auto_rate_cur_p = TRUE;
}
#endif
if ((ADHOC_ON(pAd) || INFRA_ON(pAd)) && (pAd->OpMode == OPMODE_STA))
{
pSupRate = &pAd->StaActive.SupRate[0];
pExtRate = &pAd->StaActive.ExtRate[0];
SupRateLen = pAd->StaActive.SupRateLen;
ExtRateLen = pAd->StaActive.ExtRateLen;
}
else
{
pSupRate = &pAd->CommonCfg.SupRate[0];
pExtRate = &pAd->CommonCfg.ExtRate[0];
SupRateLen = pAd->CommonCfg.SupRateLen;
ExtRateLen = pAd->CommonCfg.ExtRateLen;
}
// find max supported rate
for (i=0; i<SupRateLen; i++)
{
switch (pSupRate[i] & 0x7f)
{
case 2: Rate = RATE_1; if (pSupRate[i] & 0x80) BasicRateBitmap |= 0x0001; break;
case 4: Rate = RATE_2; if (pSupRate[i] & 0x80) BasicRateBitmap |= 0x0002; break;
case 11: Rate = RATE_5_5; if (pSupRate[i] & 0x80) BasicRateBitmap |= 0x0004; break;
case 22: Rate = RATE_11; if (pSupRate[i] & 0x80) BasicRateBitmap |= 0x0008; break;
case 12: Rate = RATE_6; /*if (pSupRate[i] & 0x80)*/ BasicRateBitmap |= 0x0010; break;
case 18: Rate = RATE_9; if (pSupRate[i] & 0x80) BasicRateBitmap |= 0x0020; break;
case 24: Rate = RATE_12; /*if (pSupRate[i] & 0x80)*/ BasicRateBitmap |= 0x0040; break;
case 36: Rate = RATE_18; if (pSupRate[i] & 0x80) BasicRateBitmap |= 0x0080; break;
case 48: Rate = RATE_24; /*if (pSupRate[i] & 0x80)*/ BasicRateBitmap |= 0x0100; break;
case 72: Rate = RATE_36; if (pSupRate[i] & 0x80) BasicRateBitmap |= 0x0200; break;
case 96: Rate = RATE_48; if (pSupRate[i] & 0x80) BasicRateBitmap |= 0x0400; break;
case 108: Rate = RATE_54; if (pSupRate[i] & 0x80) BasicRateBitmap |= 0x0800; break;
default: Rate = RATE_1; break;
}
if (MaxSupport < Rate) MaxSupport = Rate;
if (MinSupport > Rate) MinSupport = Rate;
}
for (i=0; i<ExtRateLen; i++)
{
switch (pExtRate[i] & 0x7f)
{
case 2: Rate = RATE_1; if (pExtRate[i] & 0x80) BasicRateBitmap |= 0x0001; break;
case 4: Rate = RATE_2; if (pExtRate[i] & 0x80) BasicRateBitmap |= 0x0002; break;
case 11: Rate = RATE_5_5; if (pExtRate[i] & 0x80) BasicRateBitmap |= 0x0004; break;
case 22: Rate = RATE_11; if (pExtRate[i] & 0x80) BasicRateBitmap |= 0x0008; break;
case 12: Rate = RATE_6; /*if (pExtRate[i] & 0x80)*/ BasicRateBitmap |= 0x0010; break;
case 18: Rate = RATE_9; if (pExtRate[i] & 0x80) BasicRateBitmap |= 0x0020; break;
case 24: Rate = RATE_12; /*if (pExtRate[i] & 0x80)*/ BasicRateBitmap |= 0x0040; break;
case 36: Rate = RATE_18; if (pExtRate[i] & 0x80) BasicRateBitmap |= 0x0080; break;
case 48: Rate = RATE_24; /*if (pExtRate[i] & 0x80)*/ BasicRateBitmap |= 0x0100; break;
case 72: Rate = RATE_36; if (pExtRate[i] & 0x80) BasicRateBitmap |= 0x0200; break;
case 96: Rate = RATE_48; if (pExtRate[i] & 0x80) BasicRateBitmap |= 0x0400; break;
case 108: Rate = RATE_54; if (pExtRate[i] & 0x80) BasicRateBitmap |= 0x0800; break;
default: Rate = RATE_1; break;
}
if (MaxSupport < Rate) MaxSupport = Rate;
if (MinSupport > Rate) MinSupport = Rate;
}
RTMP_IO_WRITE32(pAd, LEGACY_BASIC_RATE, BasicRateBitmap);
// calculate the exptected ACK rate for each TX rate. This info is used to caculate
// the DURATION field of outgoing uniicast DATA/MGMT frame
for (i=0; i<MAX_LEN_OF_SUPPORTED_RATES; i++)
{
if (BasicRateBitmap & (0x01 << i))
CurrBasicRate = (UCHAR)i;
pAd->CommonCfg.ExpectedACKRate[i] = CurrBasicRate;
}
DBGPRINT(RT_DEBUG_TRACE,("MlmeUpdateTxRates[MaxSupport = %d] = MaxDesire %d Mbps\n", RateIdToMbps[MaxSupport], RateIdToMbps[MaxDesire]));
// max tx rate = min {max desire rate, max supported rate}
if (MaxSupport < MaxDesire)
pAd->CommonCfg.MaxTxRate = MaxSupport;
else
pAd->CommonCfg.MaxTxRate = MaxDesire;
pAd->CommonCfg.MinTxRate = MinSupport;
if (*auto_rate_cur_p)
{
short dbm = 0;
dbm = pAd->StaCfg.RssiSample.AvgRssi0 - pAd->BbpRssiToDbmDelta;
if (bLinkUp == TRUE)
pAd->CommonCfg.TxRate = RATE_24;
else
pAd->CommonCfg.TxRate = pAd->CommonCfg.MaxTxRate;
if (dbm < -75)
pAd->CommonCfg.TxRate = RATE_11;
else if (dbm < -70)
pAd->CommonCfg.TxRate = RATE_24;
// should never exceed MaxTxRate (consider 11B-only mode)
if (pAd->CommonCfg.TxRate > pAd->CommonCfg.MaxTxRate)
pAd->CommonCfg.TxRate = pAd->CommonCfg.MaxTxRate;
pAd->CommonCfg.TxRateIndex = 0;
}
else
{
pAd->CommonCfg.TxRate = pAd->CommonCfg.MaxTxRate;
pHtPhy->field.MCS = (pAd->CommonCfg.MaxTxRate > 3) ? (pAd->CommonCfg.MaxTxRate - 4) : pAd->CommonCfg.MaxTxRate;
pHtPhy->field.MODE = (pAd->CommonCfg.MaxTxRate > 3) ? MODE_OFDM : MODE_CCK;
pAd->MacTab.Content[BSSID_WCID].HTPhyMode.field.STBC = pHtPhy->field.STBC;
pAd->MacTab.Content[BSSID_WCID].HTPhyMode.field.ShortGI = pHtPhy->field.ShortGI;
pAd->MacTab.Content[BSSID_WCID].HTPhyMode.field.MCS = pHtPhy->field.MCS;
pAd->MacTab.Content[BSSID_WCID].HTPhyMode.field.MODE = pHtPhy->field.MODE;
}
if (pAd->CommonCfg.TxRate <= RATE_11)
{
pMaxHtPhy->field.MODE = MODE_CCK;
pMaxHtPhy->field.MCS = pAd->CommonCfg.TxRate;
pMinHtPhy->field.MCS = pAd->CommonCfg.MinTxRate;
}
else
{
pMaxHtPhy->field.MODE = MODE_OFDM;
pMaxHtPhy->field.MCS = OfdmRateToRxwiMCS[pAd->CommonCfg.TxRate];
if (pAd->CommonCfg.MinTxRate >= RATE_6 && (pAd->CommonCfg.MinTxRate <= RATE_54))
{pMinHtPhy->field.MCS = OfdmRateToRxwiMCS[pAd->CommonCfg.MinTxRate];}
else
{pMinHtPhy->field.MCS = pAd->CommonCfg.MinTxRate;}
}
pHtPhy->word = (pMaxHtPhy->word);
if (bLinkUp && (pAd->OpMode == OPMODE_STA))
{
pAd->MacTab.Content[BSSID_WCID].HTPhyMode.word = pHtPhy->word;
pAd->MacTab.Content[BSSID_WCID].MaxHTPhyMode.word = pMaxHtPhy->word;
pAd->MacTab.Content[BSSID_WCID].MinHTPhyMode.word = pMinHtPhy->word;
}
else
{
switch (pAd->CommonCfg.PhyMode)
{
case PHY_11BG_MIXED:
case PHY_11B:
case PHY_11BGN_MIXED:
pAd->CommonCfg.MlmeRate = RATE_1;
pAd->CommonCfg.MlmeTransmit.field.MODE = MODE_CCK;
pAd->CommonCfg.MlmeTransmit.field.MCS = RATE_1;
pAd->CommonCfg.RtsRate = RATE_11;
break;
case PHY_11G:
case PHY_11A:
case PHY_11AGN_MIXED:
case PHY_11GN_MIXED:
case PHY_11N_2_4G:
case PHY_11AN_MIXED:
case PHY_11N_5G:
pAd->CommonCfg.MlmeRate = RATE_6;
pAd->CommonCfg.RtsRate = RATE_6;
pAd->CommonCfg.MlmeTransmit.field.MODE = MODE_OFDM;
pAd->CommonCfg.MlmeTransmit.field.MCS = OfdmRateToRxwiMCS[pAd->CommonCfg.MlmeRate];
break;
case PHY_11ABG_MIXED:
case PHY_11ABGN_MIXED:
if (pAd->CommonCfg.Channel <= 14)
{
pAd->CommonCfg.MlmeRate = RATE_1;
pAd->CommonCfg.RtsRate = RATE_1;
pAd->CommonCfg.MlmeTransmit.field.MODE = MODE_CCK;
pAd->CommonCfg.MlmeTransmit.field.MCS = RATE_1;
}
else
{
pAd->CommonCfg.MlmeRate = RATE_6;
pAd->CommonCfg.RtsRate = RATE_6;
pAd->CommonCfg.MlmeTransmit.field.MODE = MODE_OFDM;
pAd->CommonCfg.MlmeTransmit.field.MCS = OfdmRateToRxwiMCS[pAd->CommonCfg.MlmeRate];
}
break;
default: // error
pAd->CommonCfg.MlmeRate = RATE_6;
pAd->CommonCfg.MlmeTransmit.field.MODE = MODE_OFDM;
pAd->CommonCfg.MlmeTransmit.field.MCS = OfdmRateToRxwiMCS[pAd->CommonCfg.MlmeRate];
pAd->CommonCfg.RtsRate = RATE_1;
break;
}
//
// Keep Basic Mlme Rate.
//
pAd->MacTab.Content[MCAST_WCID].HTPhyMode.word = pAd->CommonCfg.MlmeTransmit.word;
if (pAd->CommonCfg.MlmeTransmit.field.MODE == MODE_OFDM)
pAd->MacTab.Content[MCAST_WCID].HTPhyMode.field.MCS = OfdmRateToRxwiMCS[RATE_24];
else
pAd->MacTab.Content[MCAST_WCID].HTPhyMode.field.MCS = RATE_1;
pAd->CommonCfg.BasicMlmeRate = pAd->CommonCfg.MlmeRate;
}
DBGPRINT(RT_DEBUG_TRACE, (" MlmeUpdateTxRates (MaxDesire=%d, MaxSupport=%d, MaxTxRate=%d, MinRate=%d, Rate Switching =%d)\n",
RateIdToMbps[MaxDesire], RateIdToMbps[MaxSupport], RateIdToMbps[pAd->CommonCfg.MaxTxRate], RateIdToMbps[pAd->CommonCfg.MinTxRate],
/*OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_TX_RATE_SWITCH_ENABLED)*/*auto_rate_cur_p));
DBGPRINT(RT_DEBUG_TRACE, (" MlmeUpdateTxRates (TxRate=%d, RtsRate=%d, BasicRateBitmap=0x%04lx)\n",
RateIdToMbps[pAd->CommonCfg.TxRate], RateIdToMbps[pAd->CommonCfg.RtsRate], BasicRateBitmap));
DBGPRINT(RT_DEBUG_TRACE, ("MlmeUpdateTxRates (MlmeTransmit=0x%x, MinHTPhyMode=%x, MaxHTPhyMode=0x%x, HTPhyMode=0x%x)\n",
pAd->CommonCfg.MlmeTransmit.word, pAd->MacTab.Content[BSSID_WCID].MinHTPhyMode.word ,pAd->MacTab.Content[BSSID_WCID].MaxHTPhyMode.word ,pAd->MacTab.Content[BSSID_WCID].HTPhyMode.word ));
}
/*
==========================================================================
Description:
This function update HT Rate setting.
Input Wcid value is valid for 2 case :
1. it's used for Station in infra mode that copy AP rate to Mactable.
2. OR Station in adhoc mode to copy peer's HT rate to Mactable.
IRQL = DISPATCH_LEVEL
==========================================================================
*/
VOID MlmeUpdateHtTxRates(
IN PRTMP_ADAPTER pAd,
IN UCHAR apidx)
{
UCHAR StbcMcs; //j, StbcMcs, bitmask;
CHAR i; // 3*3
RT_HT_CAPABILITY *pRtHtCap = NULL;
RT_HT_PHY_INFO *pActiveHtPhy = NULL;
ULONG BasicMCS;
UCHAR j, bitmask;
PRT_HT_PHY_INFO pDesireHtPhy = NULL;
PHTTRANSMIT_SETTING pHtPhy = NULL;
PHTTRANSMIT_SETTING pMaxHtPhy = NULL;
PHTTRANSMIT_SETTING pMinHtPhy = NULL;
BOOLEAN *auto_rate_cur_p;
DBGPRINT(RT_DEBUG_TRACE,("MlmeUpdateHtTxRates===> \n"));
auto_rate_cur_p = NULL;
{
pDesireHtPhy = &pAd->StaCfg.DesiredHtPhyInfo;
pActiveHtPhy = &pAd->StaCfg.DesiredHtPhyInfo;
pHtPhy = &pAd->StaCfg.HTPhyMode;
pMaxHtPhy = &pAd->StaCfg.MaxHTPhyMode;
pMinHtPhy = &pAd->StaCfg.MinHTPhyMode;
auto_rate_cur_p = &pAd->StaCfg.bAutoTxRateSwitch;
}
if ((ADHOC_ON(pAd) || INFRA_ON(pAd)) && (pAd->OpMode == OPMODE_STA))
{
if (pAd->StaActive.SupportedPhyInfo.bHtEnable == FALSE)
return;
pRtHtCap = &pAd->StaActive.SupportedHtPhy;
pActiveHtPhy = &pAd->StaActive.SupportedPhyInfo;
StbcMcs = (UCHAR)pAd->MlmeAux.AddHtInfo.AddHtInfo3.StbcMcs;
BasicMCS =pAd->MlmeAux.AddHtInfo.MCSSet[0]+(pAd->MlmeAux.AddHtInfo.MCSSet[1]<<8)+(StbcMcs<<16);
if ((pAd->CommonCfg.DesiredHtPhy.TxSTBC) && (pRtHtCap->RxSTBC) && (pAd->Antenna.field.TxPath == 2))
pMaxHtPhy->field.STBC = STBC_USE;
else
pMaxHtPhy->field.STBC = STBC_NONE;
}
else
{
if (pDesireHtPhy->bHtEnable == FALSE)
return;
pRtHtCap = &pAd->CommonCfg.DesiredHtPhy;
StbcMcs = (UCHAR)pAd->CommonCfg.AddHTInfo.AddHtInfo3.StbcMcs;
BasicMCS = pAd->CommonCfg.AddHTInfo.MCSSet[0]+(pAd->CommonCfg.AddHTInfo.MCSSet[1]<<8)+(StbcMcs<<16);
if ((pAd->CommonCfg.DesiredHtPhy.TxSTBC) && (pRtHtCap->RxSTBC) && (pAd->Antenna.field.TxPath == 2))
pMaxHtPhy->field.STBC = STBC_USE;
else
pMaxHtPhy->field.STBC = STBC_NONE;
}
// Decide MAX ht rate.
if ((pRtHtCap->GF) && (pAd->CommonCfg.DesiredHtPhy.GF))
pMaxHtPhy->field.MODE = MODE_HTGREENFIELD;
else
pMaxHtPhy->field.MODE = MODE_HTMIX;
if ((pAd->CommonCfg.DesiredHtPhy.ChannelWidth) && (pRtHtCap->ChannelWidth))
pMaxHtPhy->field.BW = BW_40;
else
pMaxHtPhy->field.BW = BW_20;
if (pMaxHtPhy->field.BW == BW_20)
pMaxHtPhy->field.ShortGI = (pAd->CommonCfg.DesiredHtPhy.ShortGIfor20 & pRtHtCap->ShortGIfor20);
else
pMaxHtPhy->field.ShortGI = (pAd->CommonCfg.DesiredHtPhy.ShortGIfor40 & pRtHtCap->ShortGIfor40);
for (i=23; i>=0; i--) // 3*3
{
j = i/8;
bitmask = (1<<(i-(j*8)));
if ((pActiveHtPhy->MCSSet[j] & bitmask) && (pDesireHtPhy->MCSSet[j] & bitmask))
{
pMaxHtPhy->field.MCS = i;
break;
}
if (i==0)
break;
}
// Copy MIN ht rate. rt2860???
pMinHtPhy->field.BW = BW_20;
pMinHtPhy->field.MCS = 0;
pMinHtPhy->field.STBC = 0;
pMinHtPhy->field.ShortGI = 0;
//If STA assigns fixed rate. update to fixed here.
if ( (pAd->OpMode == OPMODE_STA) && (pDesireHtPhy->MCSSet[0] != 0xff))
{
if (pDesireHtPhy->MCSSet[4] != 0)
{
pMaxHtPhy->field.MCS = 32;
pMinHtPhy->field.MCS = 32;
DBGPRINT(RT_DEBUG_TRACE,("MlmeUpdateHtTxRates<=== Use Fixed MCS = %d\n",pMinHtPhy->field.MCS));
}
for (i=23; (CHAR)i >= 0; i--) // 3*3
{
j = i/8;
bitmask = (1<<(i-(j*8)));
if ( (pDesireHtPhy->MCSSet[j] & bitmask) && (pActiveHtPhy->MCSSet[j] & bitmask))
{
pMaxHtPhy->field.MCS = i;
pMinHtPhy->field.MCS = i;
break;
}
if (i==0)
break;
}
}
// Decide ht rate
pHtPhy->field.STBC = pMaxHtPhy->field.STBC;
pHtPhy->field.BW = pMaxHtPhy->field.BW;
pHtPhy->field.MODE = pMaxHtPhy->field.MODE;
pHtPhy->field.MCS = pMaxHtPhy->field.MCS;
pHtPhy->field.ShortGI = pMaxHtPhy->field.ShortGI;
// use default now. rt2860
if (pDesireHtPhy->MCSSet[0] != 0xff)
*auto_rate_cur_p = FALSE;
else
*auto_rate_cur_p = TRUE;
DBGPRINT(RT_DEBUG_TRACE, (" MlmeUpdateHtTxRates<---.AMsduSize = %d \n", pAd->CommonCfg.DesiredHtPhy.AmsduSize ));
DBGPRINT(RT_DEBUG_TRACE,("TX: MCS[0] = %x (choose %d), BW = %d, ShortGI = %d, MODE = %d, \n", pActiveHtPhy->MCSSet[0],pHtPhy->field.MCS,
pHtPhy->field.BW, pHtPhy->field.ShortGI, pHtPhy->field.MODE));
DBGPRINT(RT_DEBUG_TRACE,("MlmeUpdateHtTxRates<=== \n"));
}
// IRQL = DISPATCH_LEVEL
VOID MlmeRadioOff(
IN PRTMP_ADAPTER pAd)
{
RT28XX_MLME_RADIO_OFF(pAd);
}
// IRQL = DISPATCH_LEVEL
VOID MlmeRadioOn(
IN PRTMP_ADAPTER pAd)
{
RT28XX_MLME_RADIO_ON(pAd);
}
// ===========================================================================================
// bss_table.c
// ===========================================================================================
/*! \brief initialize BSS table
* \param p_tab pointer to the table
* \return none
* \pre
* \post
IRQL = PASSIVE_LEVEL
IRQL = DISPATCH_LEVEL
*/
VOID BssTableInit(
IN BSS_TABLE *Tab)
{
int i;
Tab->BssNr = 0;
Tab->BssOverlapNr = 0;
for (i = 0; i < MAX_LEN_OF_BSS_TABLE; i++)
{
NdisZeroMemory(&Tab->BssEntry[i], sizeof(BSS_ENTRY));
Tab->BssEntry[i].Rssi = -127; // initial the rssi as a minimum value
}
}
VOID BATableInit(
IN PRTMP_ADAPTER pAd,
IN BA_TABLE *Tab)
{
int i;
Tab->numAsOriginator = 0;
Tab->numAsRecipient = 0;
NdisAllocateSpinLock(&pAd->BATabLock);
for (i = 0; i < MAX_LEN_OF_BA_REC_TABLE; i++)
{
Tab->BARecEntry[i].REC_BA_Status = Recipient_NONE;
NdisAllocateSpinLock(&(Tab->BARecEntry[i].RxReRingLock));
}
for (i = 0; i < MAX_LEN_OF_BA_ORI_TABLE; i++)
{
Tab->BAOriEntry[i].ORI_BA_Status = Originator_NONE;
}
}
/*! \brief search the BSS table by SSID
* \param p_tab pointer to the bss table
* \param ssid SSID string
* \return index of the table, BSS_NOT_FOUND if not in the table
* \pre
* \post
* \note search by sequential search
IRQL = DISPATCH_LEVEL
*/
ULONG BssTableSearch(
IN BSS_TABLE *Tab,
IN PUCHAR pBssid,
IN UCHAR Channel)
{
UCHAR i;
for (i = 0; i < Tab->BssNr; i++)
{
//
// Some AP that support A/B/G mode that may used the same BSSID on 11A and 11B/G.
// We should distinguish this case.
//
if ((((Tab->BssEntry[i].Channel <= 14) && (Channel <= 14)) ||
((Tab->BssEntry[i].Channel > 14) && (Channel > 14))) &&
MAC_ADDR_EQUAL(Tab->BssEntry[i].Bssid, pBssid))
{
return i;
}
}
return (ULONG)BSS_NOT_FOUND;
}
ULONG BssSsidTableSearch(
IN BSS_TABLE *Tab,
IN PUCHAR pBssid,
IN PUCHAR pSsid,
IN UCHAR SsidLen,
IN UCHAR Channel)
{
UCHAR i;
for (i = 0; i < Tab->BssNr; i++)
{
//
// Some AP that support A/B/G mode that may used the same BSSID on 11A and 11B/G.
// We should distinguish this case.
//
if ((((Tab->BssEntry[i].Channel <= 14) && (Channel <= 14)) ||
((Tab->BssEntry[i].Channel > 14) && (Channel > 14))) &&
MAC_ADDR_EQUAL(Tab->BssEntry[i].Bssid, pBssid) &&
SSID_EQUAL(pSsid, SsidLen, Tab->BssEntry[i].Ssid, Tab->BssEntry[i].SsidLen))
{
return i;
}
}
return (ULONG)BSS_NOT_FOUND;
}
ULONG BssTableSearchWithSSID(
IN BSS_TABLE *Tab,
IN PUCHAR Bssid,
IN PUCHAR pSsid,
IN UCHAR SsidLen,
IN UCHAR Channel)
{
UCHAR i;
for (i = 0; i < Tab->BssNr; i++)
{
if ((((Tab->BssEntry[i].Channel <= 14) && (Channel <= 14)) ||
((Tab->BssEntry[i].Channel > 14) && (Channel > 14))) &&
MAC_ADDR_EQUAL(&(Tab->BssEntry[i].Bssid), Bssid) &&
(SSID_EQUAL(pSsid, SsidLen, Tab->BssEntry[i].Ssid, Tab->BssEntry[i].SsidLen) ||
(NdisEqualMemory(pSsid, ZeroSsid, SsidLen)) ||
(NdisEqualMemory(Tab->BssEntry[i].Ssid, ZeroSsid, Tab->BssEntry[i].SsidLen))))
{
return i;
}
}
return (ULONG)BSS_NOT_FOUND;
}
// IRQL = DISPATCH_LEVEL
VOID BssTableDeleteEntry(
IN OUT BSS_TABLE *Tab,
IN PUCHAR pBssid,
IN UCHAR Channel)
{
UCHAR i, j;
for (i = 0; i < Tab->BssNr; i++)
{
if ((Tab->BssEntry[i].Channel == Channel) &&
(MAC_ADDR_EQUAL(Tab->BssEntry[i].Bssid, pBssid)))
{
for (j = i; j < Tab->BssNr - 1; j++)
{
NdisMoveMemory(&(Tab->BssEntry[j]), &(Tab->BssEntry[j + 1]), sizeof(BSS_ENTRY));
}
NdisZeroMemory(&(Tab->BssEntry[Tab->BssNr - 1]), sizeof(BSS_ENTRY));
Tab->BssNr -= 1;
return;
}
}
}
/*
========================================================================
Routine Description:
Delete the Originator Entry in BAtable. Or decrease numAs Originator by 1 if needed.
Arguments:
// IRQL = DISPATCH_LEVEL
========================================================================
*/
VOID BATableDeleteORIEntry(
IN OUT PRTMP_ADAPTER pAd,
IN BA_ORI_ENTRY *pBAORIEntry)
{
if (pBAORIEntry->ORI_BA_Status != Originator_NONE)
{
NdisAcquireSpinLock(&pAd->BATabLock);
if (pBAORIEntry->ORI_BA_Status == Originator_Done)
{
pAd->BATable.numAsOriginator -= 1;
DBGPRINT(RT_DEBUG_TRACE, ("BATableDeleteORIEntry numAsOriginator= %ld\n", pAd->BATable.numAsRecipient));
// Erase Bitmap flag.
}
pAd->MacTab.Content[pBAORIEntry->Wcid].TXBAbitmap &= (~(1<<(pBAORIEntry->TID) )); // If STA mode, erase flag here
pAd->MacTab.Content[pBAORIEntry->Wcid].BAOriWcidArray[pBAORIEntry->TID] = 0; // If STA mode, erase flag here
pBAORIEntry->ORI_BA_Status = Originator_NONE;
pBAORIEntry->Token = 1;
// Not clear Sequence here.
NdisReleaseSpinLock(&pAd->BATabLock);
}
}
/*! \brief
* \param
* \return
* \pre
* \post
IRQL = DISPATCH_LEVEL
*/
VOID BssEntrySet(
IN PRTMP_ADAPTER pAd,
OUT BSS_ENTRY *pBss,
IN PUCHAR pBssid,
IN CHAR Ssid[],
IN UCHAR SsidLen,
IN UCHAR BssType,
IN USHORT BeaconPeriod,
IN PCF_PARM pCfParm,
IN USHORT AtimWin,
IN USHORT CapabilityInfo,
IN UCHAR SupRate[],
IN UCHAR SupRateLen,
IN UCHAR ExtRate[],
IN UCHAR ExtRateLen,
IN HT_CAPABILITY_IE *pHtCapability,
IN ADD_HT_INFO_IE *pAddHtInfo, // AP might use this additional ht info IE
IN UCHAR HtCapabilityLen,
IN UCHAR AddHtInfoLen,
IN UCHAR NewExtChanOffset,
IN UCHAR Channel,
IN CHAR Rssi,
IN LARGE_INTEGER TimeStamp,
IN UCHAR CkipFlag,
IN PEDCA_PARM pEdcaParm,
IN PQOS_CAPABILITY_PARM pQosCapability,
IN PQBSS_LOAD_PARM pQbssLoad,
IN USHORT LengthVIE,
IN PNDIS_802_11_VARIABLE_IEs pVIE)
{
COPY_MAC_ADDR(pBss->Bssid, pBssid);
// Default Hidden SSID to be TRUE, it will be turned to FALSE after coping SSID
pBss->Hidden = 1;
if (SsidLen > 0)
{
// For hidden SSID AP, it might send beacon with SSID len equal to 0
// Or send beacon /probe response with SSID len matching real SSID length,
// but SSID is all zero. such as "00-00-00-00" with length 4.
// We have to prevent this case overwrite correct table
if (NdisEqualMemory(Ssid, ZeroSsid, SsidLen) == 0)
{
NdisZeroMemory(pBss->Ssid, MAX_LEN_OF_SSID);
NdisMoveMemory(pBss->Ssid, Ssid, SsidLen);
pBss->SsidLen = SsidLen;
pBss->Hidden = 0;
}
}
else
pBss->SsidLen = 0;
pBss->BssType = BssType;
pBss->BeaconPeriod = BeaconPeriod;
if (BssType == BSS_INFRA)
{
if (pCfParm->bValid)
{
pBss->CfpCount = pCfParm->CfpCount;
pBss->CfpPeriod = pCfParm->CfpPeriod;
pBss->CfpMaxDuration = pCfParm->CfpMaxDuration;
pBss->CfpDurRemaining = pCfParm->CfpDurRemaining;
}
}
else
{
pBss->AtimWin = AtimWin;
}
pBss->CapabilityInfo = CapabilityInfo;
// The privacy bit indicate security is ON, it maight be WEP, TKIP or AES
// Combine with AuthMode, they will decide the connection methods.
pBss->Privacy = CAP_IS_PRIVACY_ON(pBss->CapabilityInfo);
ASSERT(SupRateLen <= MAX_LEN_OF_SUPPORTED_RATES);
if (SupRateLen <= MAX_LEN_OF_SUPPORTED_RATES)
NdisMoveMemory(pBss->SupRate, SupRate, SupRateLen);
else
NdisMoveMemory(pBss->SupRate, SupRate, MAX_LEN_OF_SUPPORTED_RATES);
pBss->SupRateLen = SupRateLen;
ASSERT(ExtRateLen <= MAX_LEN_OF_SUPPORTED_RATES);
NdisMoveMemory(pBss->ExtRate, ExtRate, ExtRateLen);
NdisMoveMemory(&pBss->HtCapability, pHtCapability, HtCapabilityLen);
NdisMoveMemory(&pBss->AddHtInfo, pAddHtInfo, AddHtInfoLen);
pBss->NewExtChanOffset = NewExtChanOffset;
pBss->ExtRateLen = ExtRateLen;
pBss->Channel = Channel;
pBss->CentralChannel = Channel;
pBss->Rssi = Rssi;
// Update CkipFlag. if not exists, the value is 0x0
pBss->CkipFlag = CkipFlag;
// New for microsoft Fixed IEs
NdisMoveMemory(pBss->FixIEs.Timestamp, &TimeStamp, 8);
pBss->FixIEs.BeaconInterval = BeaconPeriod;
pBss->FixIEs.Capabilities = CapabilityInfo;
// New for microsoft Variable IEs
if (LengthVIE != 0)
{
pBss->VarIELen = LengthVIE;
NdisMoveMemory(pBss->VarIEs, pVIE, pBss->VarIELen);
}
else
{
pBss->VarIELen = 0;
}
pBss->AddHtInfoLen = 0;
pBss->HtCapabilityLen = 0;
if (HtCapabilityLen> 0)
{
pBss->HtCapabilityLen = HtCapabilityLen;
NdisMoveMemory(&pBss->HtCapability, pHtCapability, HtCapabilityLen);
if (AddHtInfoLen > 0)
{
pBss->AddHtInfoLen = AddHtInfoLen;
NdisMoveMemory(&pBss->AddHtInfo, pAddHtInfo, AddHtInfoLen);
if ((pAddHtInfo->ControlChan > 2)&& (pAddHtInfo->AddHtInfo.ExtChanOffset == EXTCHA_BELOW) && (pHtCapability->HtCapInfo.ChannelWidth == BW_40))
{
pBss->CentralChannel = pAddHtInfo->ControlChan - 2;
}
else if ((pAddHtInfo->AddHtInfo.ExtChanOffset == EXTCHA_ABOVE) && (pHtCapability->HtCapInfo.ChannelWidth == BW_40))
{
pBss->CentralChannel = pAddHtInfo->ControlChan + 2;
}
}
}
BssCipherParse(pBss);
// new for QOS
if (pEdcaParm)
NdisMoveMemory(&pBss->EdcaParm, pEdcaParm, sizeof(EDCA_PARM));
else
pBss->EdcaParm.bValid = FALSE;
if (pQosCapability)
NdisMoveMemory(&pBss->QosCapability, pQosCapability, sizeof(QOS_CAPABILITY_PARM));
else
pBss->QosCapability.bValid = FALSE;
if (pQbssLoad)
NdisMoveMemory(&pBss->QbssLoad, pQbssLoad, sizeof(QBSS_LOAD_PARM));
else
pBss->QbssLoad.bValid = FALSE;
{
PEID_STRUCT pEid;
USHORT Length = 0;
NdisZeroMemory(&pBss->WpaIE.IE[0], MAX_CUSTOM_LEN);
NdisZeroMemory(&pBss->RsnIE.IE[0], MAX_CUSTOM_LEN);
pEid = (PEID_STRUCT) pVIE;
while ((Length + 2 + (USHORT)pEid->Len) <= LengthVIE)
{
switch(pEid->Eid)
{
case IE_WPA:
if (NdisEqualMemory(pEid->Octet, WPA_OUI, 4))
{
if ((pEid->Len + 2) > MAX_CUSTOM_LEN)
{
pBss->WpaIE.IELen = 0;
break;
}
pBss->WpaIE.IELen = pEid->Len + 2;
NdisMoveMemory(pBss->WpaIE.IE, pEid, pBss->WpaIE.IELen);
}
break;
case IE_RSN:
if (NdisEqualMemory(pEid->Octet + 2, RSN_OUI, 3))
{
if ((pEid->Len + 2) > MAX_CUSTOM_LEN)
{
pBss->RsnIE.IELen = 0;
break;
}
pBss->RsnIE.IELen = pEid->Len + 2;
NdisMoveMemory(pBss->RsnIE.IE, pEid, pBss->RsnIE.IELen);
}
break;
}
Length = Length + 2 + (USHORT)pEid->Len; // Eid[1] + Len[1]+ content[Len]
pEid = (PEID_STRUCT)((UCHAR*)pEid + 2 + pEid->Len);
}
}
}
/*!
* \brief insert an entry into the bss table
* \param p_tab The BSS table
* \param Bssid BSSID
* \param ssid SSID
* \param ssid_len Length of SSID
* \param bss_type
* \param beacon_period
* \param timestamp
* \param p_cf
* \param atim_win
* \param cap
* \param rates
* \param rates_len
* \param channel_idx
* \return none
* \pre
* \post
* \note If SSID is identical, the old entry will be replaced by the new one
IRQL = DISPATCH_LEVEL
*/
ULONG BssTableSetEntry(
IN PRTMP_ADAPTER pAd,
OUT BSS_TABLE *Tab,
IN PUCHAR pBssid,
IN CHAR Ssid[],
IN UCHAR SsidLen,
IN UCHAR BssType,
IN USHORT BeaconPeriod,
IN CF_PARM *CfParm,
IN USHORT AtimWin,
IN USHORT CapabilityInfo,
IN UCHAR SupRate[],
IN UCHAR SupRateLen,
IN UCHAR ExtRate[],
IN UCHAR ExtRateLen,
IN HT_CAPABILITY_IE *pHtCapability,
IN ADD_HT_INFO_IE *pAddHtInfo, // AP might use this additional ht info IE
IN UCHAR HtCapabilityLen,
IN UCHAR AddHtInfoLen,
IN UCHAR NewExtChanOffset,
IN UCHAR ChannelNo,
IN CHAR Rssi,
IN LARGE_INTEGER TimeStamp,
IN UCHAR CkipFlag,
IN PEDCA_PARM pEdcaParm,
IN PQOS_CAPABILITY_PARM pQosCapability,
IN PQBSS_LOAD_PARM pQbssLoad,
IN USHORT LengthVIE,
IN PNDIS_802_11_VARIABLE_IEs pVIE)
{
ULONG Idx;
Idx = BssTableSearchWithSSID(Tab, pBssid, Ssid, SsidLen, ChannelNo);
if (Idx == BSS_NOT_FOUND)
{
if (Tab->BssNr >= MAX_LEN_OF_BSS_TABLE)
{
//
// It may happen when BSS Table was full.
// The desired AP will not be added into BSS Table
// In this case, if we found the desired AP then overwrite BSS Table.
//
if(!OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_MEDIA_STATE_CONNECTED))
{
if (MAC_ADDR_EQUAL(pAd->MlmeAux.Bssid, pBssid) ||
SSID_EQUAL(pAd->MlmeAux.Ssid, pAd->MlmeAux.SsidLen, Ssid, SsidLen))
{
Idx = Tab->BssOverlapNr;
BssEntrySet(pAd, &Tab->BssEntry[Idx], pBssid, Ssid, SsidLen, BssType, BeaconPeriod, CfParm, AtimWin,
CapabilityInfo, SupRate, SupRateLen, ExtRate, ExtRateLen,pHtCapability, pAddHtInfo,HtCapabilityLen, AddHtInfoLen,
NewExtChanOffset, ChannelNo, Rssi, TimeStamp, CkipFlag, pEdcaParm, pQosCapability, pQbssLoad, LengthVIE, pVIE);
Tab->BssOverlapNr = (Tab->BssOverlapNr++) % MAX_LEN_OF_BSS_TABLE;
}
return Idx;
}
else
{
return BSS_NOT_FOUND;
}
}
Idx = Tab->BssNr;
BssEntrySet(pAd, &Tab->BssEntry[Idx], pBssid, Ssid, SsidLen, BssType, BeaconPeriod, CfParm, AtimWin,
CapabilityInfo, SupRate, SupRateLen, ExtRate, ExtRateLen,pHtCapability, pAddHtInfo,HtCapabilityLen, AddHtInfoLen,
NewExtChanOffset, ChannelNo, Rssi, TimeStamp, CkipFlag, pEdcaParm, pQosCapability, pQbssLoad, LengthVIE, pVIE);
Tab->BssNr++;
}
else
{
#ifdef RT30xx
/* avoid Hidden SSID form beacon to overwirite correct SSID from probe response */
if ((SSID_EQUAL(Ssid, SsidLen, Tab->BssEntry[Idx].Ssid, Tab->BssEntry[Idx].SsidLen)) ||
(NdisEqualMemory(Tab->BssEntry[Idx].Ssid, ZeroSsid, Tab->BssEntry[Idx].SsidLen)))
{
#endif
BssEntrySet(pAd, &Tab->BssEntry[Idx], pBssid, Ssid, SsidLen, BssType, BeaconPeriod,CfParm, AtimWin,
CapabilityInfo, SupRate, SupRateLen, ExtRate, ExtRateLen,pHtCapability, pAddHtInfo,HtCapabilityLen, AddHtInfoLen,
NewExtChanOffset, ChannelNo, Rssi, TimeStamp, CkipFlag, pEdcaParm, pQosCapability, pQbssLoad, LengthVIE, pVIE);
#ifdef RT30xx
}
#endif
}
return Idx;
}
// IRQL = DISPATCH_LEVEL
VOID BssTableSsidSort(
IN PRTMP_ADAPTER pAd,
OUT BSS_TABLE *OutTab,
IN CHAR Ssid[],
IN UCHAR SsidLen)
{
INT i;
BssTableInit(OutTab);
for (i = 0; i < pAd->ScanTab.BssNr; i++)
{
BSS_ENTRY *pInBss = &pAd->ScanTab.BssEntry[i];
BOOLEAN bIsHiddenApIncluded = FALSE;
if (((pAd->CommonCfg.bIEEE80211H == 1) &&
(pAd->MlmeAux.Channel > 14) &&
RadarChannelCheck(pAd, pInBss->Channel))
)
{
if (pInBss->Hidden)
bIsHiddenApIncluded = TRUE;
}
if ((pInBss->BssType == pAd->StaCfg.BssType) &&
(SSID_EQUAL(Ssid, SsidLen, pInBss->Ssid, pInBss->SsidLen) || bIsHiddenApIncluded))
{
BSS_ENTRY *pOutBss = &OutTab->BssEntry[OutTab->BssNr];
// 2.4G/5G N only mode
if ((pInBss->HtCapabilityLen == 0) &&
((pAd->CommonCfg.PhyMode == PHY_11N_2_4G) || (pAd->CommonCfg.PhyMode == PHY_11N_5G)))
{
DBGPRINT(RT_DEBUG_TRACE,("STA is in N-only Mode, this AP don't have Ht capability in Beacon.\n"));
continue;
}
// New for WPA2
// Check the Authmode first
if (pAd->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
{
// Check AuthMode and AuthModeAux for matching, in case AP support dual-mode
if ((pAd->StaCfg.AuthMode != pInBss->AuthMode) && (pAd->StaCfg.AuthMode != pInBss->AuthModeAux))
// None matched
continue;
// Check cipher suite, AP must have more secured cipher than station setting
if ((pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPA) || (pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPAPSK))
{
// If it's not mixed mode, we should only let BSS pass with the same encryption
if (pInBss->WPA.bMixMode == FALSE)
if (pAd->StaCfg.WepStatus != pInBss->WPA.GroupCipher)
continue;
// check group cipher
#ifndef RT30xx
if ((pAd->StaCfg.WepStatus < pInBss->WPA.GroupCipher) &&
(pInBss->WPA.GroupCipher != Ndis802_11GroupWEP40Enabled) &&
(pInBss->WPA.GroupCipher != Ndis802_11GroupWEP104Enabled))
#endif
#ifdef RT30xx
if (pAd->StaCfg.WepStatus < pInBss->WPA.GroupCipher)
#endif
continue;
// check pairwise cipher, skip if none matched
// If profile set to AES, let it pass without question.
// If profile set to TKIP, we must find one mateched
if ((pAd->StaCfg.WepStatus == Ndis802_11Encryption2Enabled) &&
(pAd->StaCfg.WepStatus != pInBss->WPA.PairCipher) &&
(pAd->StaCfg.WepStatus != pInBss->WPA.PairCipherAux))
continue;
}
else if ((pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPA2) || (pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPA2PSK))
{
// If it's not mixed mode, we should only let BSS pass with the same encryption
if (pInBss->WPA2.bMixMode == FALSE)
if (pAd->StaCfg.WepStatus != pInBss->WPA2.GroupCipher)
continue;
// check group cipher
#ifndef RT30xx
if ((pAd->StaCfg.WepStatus < pInBss->WPA.GroupCipher) &&
(pInBss->WPA2.GroupCipher != Ndis802_11GroupWEP40Enabled) &&
(pInBss->WPA2.GroupCipher != Ndis802_11GroupWEP104Enabled))
#endif
#ifdef RT30xx
if (pAd->StaCfg.WepStatus < pInBss->WPA2.GroupCipher)
#endif
continue;
// check pairwise cipher, skip if none matched
// If profile set to AES, let it pass without question.
// If profile set to TKIP, we must find one mateched
if ((pAd->StaCfg.WepStatus == Ndis802_11Encryption2Enabled) &&
(pAd->StaCfg.WepStatus != pInBss->WPA2.PairCipher) &&
(pAd->StaCfg.WepStatus != pInBss->WPA2.PairCipherAux))
continue;
}
}
// Bss Type matched, SSID matched.
// We will check wepstatus for qualification Bss
else if (pAd->StaCfg.WepStatus != pInBss->WepStatus)
{
DBGPRINT(RT_DEBUG_TRACE,("StaCfg.WepStatus=%d, while pInBss->WepStatus=%d\n", pAd->StaCfg.WepStatus, pInBss->WepStatus));
//
// For the SESv2 case, we will not qualify WepStatus.
//
if (!pInBss->bSES)
continue;
}
// Since the AP is using hidden SSID, and we are trying to connect to ANY
// It definitely will fail. So, skip it.
// CCX also require not even try to connect it!!
if (SsidLen == 0)
continue;
// If both station and AP use 40MHz, still need to check if the 40MHZ band's legality in my country region
// If this 40MHz wideband is not allowed in my country list, use bandwidth 20MHZ instead,
if ((pInBss->CentralChannel != pInBss->Channel) &&
(pAd->CommonCfg.RegTransmitSetting.field.BW == BW_40))
{
if (RTMPCheckChannel(pAd, pInBss->CentralChannel, pInBss->Channel) == FALSE)
{
pAd->CommonCfg.RegTransmitSetting.field.BW = BW_20;
SetCommonHT(pAd);
pAd->CommonCfg.RegTransmitSetting.field.BW = BW_40;
}
else
{
if (pAd->CommonCfg.DesiredHtPhy.ChannelWidth == BAND_WIDTH_20)
{
SetCommonHT(pAd);
}
}
}
// copy matching BSS from InTab to OutTab
NdisMoveMemory(pOutBss, pInBss, sizeof(BSS_ENTRY));
OutTab->BssNr++;
}
else if ((pInBss->BssType == pAd->StaCfg.BssType) && (SsidLen == 0))
{
BSS_ENTRY *pOutBss = &OutTab->BssEntry[OutTab->BssNr];
// 2.4G/5G N only mode
if ((pInBss->HtCapabilityLen == 0) &&
((pAd->CommonCfg.PhyMode == PHY_11N_2_4G) || (pAd->CommonCfg.PhyMode == PHY_11N_5G)))
{
DBGPRINT(RT_DEBUG_TRACE,("STA is in N-only Mode, this AP don't have Ht capability in Beacon.\n"));
continue;
}
// New for WPA2
// Check the Authmode first
if (pAd->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
{
// Check AuthMode and AuthModeAux for matching, in case AP support dual-mode
if ((pAd->StaCfg.AuthMode != pInBss->AuthMode) && (pAd->StaCfg.AuthMode != pInBss->AuthModeAux))
// None matched
continue;
// Check cipher suite, AP must have more secured cipher than station setting
if ((pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPA) || (pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPAPSK))
{
// If it's not mixed mode, we should only let BSS pass with the same encryption
if (pInBss->WPA.bMixMode == FALSE)
if (pAd->StaCfg.WepStatus != pInBss->WPA.GroupCipher)
continue;
// check group cipher
if (pAd->StaCfg.WepStatus < pInBss->WPA.GroupCipher)
continue;
// check pairwise cipher, skip if none matched
// If profile set to AES, let it pass without question.
// If profile set to TKIP, we must find one mateched
if ((pAd->StaCfg.WepStatus == Ndis802_11Encryption2Enabled) &&
(pAd->StaCfg.WepStatus != pInBss->WPA.PairCipher) &&
(pAd->StaCfg.WepStatus != pInBss->WPA.PairCipherAux))
continue;
}
else if ((pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPA2) || (pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPA2PSK))
{
// If it's not mixed mode, we should only let BSS pass with the same encryption
if (pInBss->WPA2.bMixMode == FALSE)
if (pAd->StaCfg.WepStatus != pInBss->WPA2.GroupCipher)
continue;
// check group cipher
if (pAd->StaCfg.WepStatus < pInBss->WPA2.GroupCipher)
continue;
// check pairwise cipher, skip if none matched
// If profile set to AES, let it pass without question.
// If profile set to TKIP, we must find one mateched
if ((pAd->StaCfg.WepStatus == Ndis802_11Encryption2Enabled) &&
(pAd->StaCfg.WepStatus != pInBss->WPA2.PairCipher) &&
(pAd->StaCfg.WepStatus != pInBss->WPA2.PairCipherAux))
continue;
}
}
// Bss Type matched, SSID matched.
// We will check wepstatus for qualification Bss
else if (pAd->StaCfg.WepStatus != pInBss->WepStatus)
continue;
// If both station and AP use 40MHz, still need to check if the 40MHZ band's legality in my country region
// If this 40MHz wideband is not allowed in my country list, use bandwidth 20MHZ instead,
if ((pInBss->CentralChannel != pInBss->Channel) &&
(pAd->CommonCfg.RegTransmitSetting.field.BW == BW_40))
{
if (RTMPCheckChannel(pAd, pInBss->CentralChannel, pInBss->Channel) == FALSE)
{
pAd->CommonCfg.RegTransmitSetting.field.BW = BW_20;
SetCommonHT(pAd);
pAd->CommonCfg.RegTransmitSetting.field.BW = BW_40;
}
}
// copy matching BSS from InTab to OutTab
NdisMoveMemory(pOutBss, pInBss, sizeof(BSS_ENTRY));
OutTab->BssNr++;
}
if (OutTab->BssNr >= MAX_LEN_OF_BSS_TABLE)
break;
}
BssTableSortByRssi(OutTab);
}
// IRQL = DISPATCH_LEVEL
VOID BssTableSortByRssi(
IN OUT BSS_TABLE *OutTab)
{
INT i, j;
BSS_ENTRY TmpBss;
for (i = 0; i < OutTab->BssNr - 1; i++)
{
for (j = i+1; j < OutTab->BssNr; j++)
{
if (OutTab->BssEntry[j].Rssi > OutTab->BssEntry[i].Rssi)
{
NdisMoveMemory(&TmpBss, &OutTab->BssEntry[j], sizeof(BSS_ENTRY));
NdisMoveMemory(&OutTab->BssEntry[j], &OutTab->BssEntry[i], sizeof(BSS_ENTRY));
NdisMoveMemory(&OutTab->BssEntry[i], &TmpBss, sizeof(BSS_ENTRY));
}
}
}
}
VOID BssCipherParse(
IN OUT PBSS_ENTRY pBss)
{
PEID_STRUCT pEid;
PUCHAR pTmp;
PRSN_IE_HEADER_STRUCT pRsnHeader;
PCIPHER_SUITE_STRUCT pCipher;
PAKM_SUITE_STRUCT pAKM;
USHORT Count;
INT Length;
NDIS_802_11_ENCRYPTION_STATUS TmpCipher;
//
// WepStatus will be reset later, if AP announce TKIP or AES on the beacon frame.
//
if (pBss->Privacy)
{
pBss->WepStatus = Ndis802_11WEPEnabled;
}
else
{
pBss->WepStatus = Ndis802_11WEPDisabled;
}
// Set default to disable & open authentication before parsing variable IE
pBss->AuthMode = Ndis802_11AuthModeOpen;
pBss->AuthModeAux = Ndis802_11AuthModeOpen;
// Init WPA setting
pBss->WPA.PairCipher = Ndis802_11WEPDisabled;
pBss->WPA.PairCipherAux = Ndis802_11WEPDisabled;
pBss->WPA.GroupCipher = Ndis802_11WEPDisabled;
pBss->WPA.RsnCapability = 0;
pBss->WPA.bMixMode = FALSE;
// Init WPA2 setting
pBss->WPA2.PairCipher = Ndis802_11WEPDisabled;
pBss->WPA2.PairCipherAux = Ndis802_11WEPDisabled;
pBss->WPA2.GroupCipher = Ndis802_11WEPDisabled;
pBss->WPA2.RsnCapability = 0;
pBss->WPA2.bMixMode = FALSE;
Length = (INT) pBss->VarIELen;
while (Length > 0)
{
// Parse cipher suite base on WPA1 & WPA2, they should be parsed differently
pTmp = ((PUCHAR) pBss->VarIEs) + pBss->VarIELen - Length;
pEid = (PEID_STRUCT) pTmp;
switch (pEid->Eid)
{
case IE_WPA:
//Parse Cisco IE_WPA (LEAP, CCKM, etc.)
if ( NdisEqualMemory((pTmp+8), CISCO_OUI, 3))
{
pTmp += 11;
switch (*pTmp)
{
case 1:
case 5: // Although WEP is not allowed in WPA related auth mode, we parse it anyway
pBss->WepStatus = Ndis802_11Encryption1Enabled;
pBss->WPA.PairCipher = Ndis802_11Encryption1Enabled;
pBss->WPA.GroupCipher = Ndis802_11Encryption1Enabled;
break;
case 2:
pBss->WepStatus = Ndis802_11Encryption2Enabled;
pBss->WPA.PairCipher = Ndis802_11Encryption1Enabled;
pBss->WPA.GroupCipher = Ndis802_11Encryption1Enabled;
break;
case 4:
pBss->WepStatus = Ndis802_11Encryption3Enabled;
pBss->WPA.PairCipher = Ndis802_11Encryption1Enabled;
pBss->WPA.GroupCipher = Ndis802_11Encryption1Enabled;
break;
default:
break;
}
// if Cisco IE_WPA, break
break;
}
else if (NdisEqualMemory(pEid->Octet, SES_OUI, 3) && (pEid->Len == 7))
{
pBss->bSES = TRUE;
break;
}
else if (NdisEqualMemory(pEid->Octet, WPA_OUI, 4) != 1)
{
// if unsupported vendor specific IE
break;
}
// Skip OUI, version, and multicast suite
// This part should be improved in the future when AP supported multiple cipher suite.
// For now, it's OK since almost all APs have fixed cipher suite supported.
// pTmp = (PUCHAR) pEid->Octet;
pTmp += 11;
// Cipher Suite Selectors from Spec P802.11i/D3.2 P26.
// Value Meaning
// 0 None
// 1 WEP-40
// 2 Tkip
// 3 WRAP
// 4 AES
// 5 WEP-104
// Parse group cipher
switch (*pTmp)
{
case 1:
#ifndef RT30xx
pBss->WPA.GroupCipher = Ndis802_11GroupWEP40Enabled;
break;
case 5:
pBss->WPA.GroupCipher = Ndis802_11GroupWEP104Enabled;
#endif
#ifdef RT30xx
case 5: // Although WEP is not allowed in WPA related auth mode, we parse it anyway
pBss->WPA.GroupCipher = Ndis802_11Encryption1Enabled;
#endif
break;
case 2:
pBss->WPA.GroupCipher = Ndis802_11Encryption2Enabled;
break;
case 4:
pBss->WPA.GroupCipher = Ndis802_11Encryption3Enabled;
break;
default:
break;
}
// number of unicast suite
pTmp += 1;
// skip all unicast cipher suites
//Count = *(PUSHORT) pTmp;
Count = (pTmp[1]<<8) + pTmp[0];
pTmp += sizeof(USHORT);
// Parsing all unicast cipher suite
while (Count > 0)
{
// Skip OUI
pTmp += 3;
TmpCipher = Ndis802_11WEPDisabled;
switch (*pTmp)
{
case 1:
case 5: // Although WEP is not allowed in WPA related auth mode, we parse it anyway
TmpCipher = Ndis802_11Encryption1Enabled;
break;
case 2:
TmpCipher = Ndis802_11Encryption2Enabled;
break;
case 4:
TmpCipher = Ndis802_11Encryption3Enabled;
break;
default:
break;
}
if (TmpCipher > pBss->WPA.PairCipher)
{
// Move the lower cipher suite to PairCipherAux
pBss->WPA.PairCipherAux = pBss->WPA.PairCipher;
pBss->WPA.PairCipher = TmpCipher;
}
else
{
pBss->WPA.PairCipherAux = TmpCipher;
}
pTmp++;
Count--;
}
// 4. get AKM suite counts
//Count = *(PUSHORT) pTmp;
Count = (pTmp[1]<<8) + pTmp[0];
pTmp += sizeof(USHORT);
pTmp += 3;
switch (*pTmp)
{
case 1:
// Set AP support WPA mode
if (pBss->AuthMode == Ndis802_11AuthModeOpen)
pBss->AuthMode = Ndis802_11AuthModeWPA;
else
pBss->AuthModeAux = Ndis802_11AuthModeWPA;
break;
case 2:
// Set AP support WPA mode
if (pBss->AuthMode == Ndis802_11AuthModeOpen)
pBss->AuthMode = Ndis802_11AuthModeWPAPSK;
else
pBss->AuthModeAux = Ndis802_11AuthModeWPAPSK;
break;
default:
break;
}
pTmp += 1;
// Fixed for WPA-None
if (pBss->BssType == BSS_ADHOC)
{
pBss->AuthMode = Ndis802_11AuthModeWPANone;
pBss->AuthModeAux = Ndis802_11AuthModeWPANone;
pBss->WepStatus = pBss->WPA.GroupCipher;
if (pBss->WPA.PairCipherAux == Ndis802_11WEPDisabled)
pBss->WPA.PairCipherAux = pBss->WPA.GroupCipher;
}
else
pBss->WepStatus = pBss->WPA.PairCipher;
// Check the Pair & Group, if different, turn on mixed mode flag
if (pBss->WPA.GroupCipher != pBss->WPA.PairCipher)
pBss->WPA.bMixMode = TRUE;
break;
case IE_RSN:
pRsnHeader = (PRSN_IE_HEADER_STRUCT) pTmp;
// 0. Version must be 1
if (le2cpu16(pRsnHeader->Version) != 1)
break;
pTmp += sizeof(RSN_IE_HEADER_STRUCT);
// 1. Check group cipher
pCipher = (PCIPHER_SUITE_STRUCT) pTmp;
if (!RTMPEqualMemory(pTmp, RSN_OUI, 3))
break;
// Parse group cipher
switch (pCipher->Type)
{
case 1:
#ifndef RT30xx
pBss->WPA2.GroupCipher = Ndis802_11GroupWEP40Enabled;
break;
case 5:
pBss->WPA2.GroupCipher = Ndis802_11GroupWEP104Enabled;
#endif
#ifdef RT30xx
case 5: // Although WEP is not allowed in WPA related auth mode, we parse it anyway
pBss->WPA2.GroupCipher = Ndis802_11Encryption1Enabled;
#endif
break;
case 2:
pBss->WPA2.GroupCipher = Ndis802_11Encryption2Enabled;
break;
case 4:
pBss->WPA2.GroupCipher = Ndis802_11Encryption3Enabled;
break;
default:
break;
}
// set to correct offset for next parsing
pTmp += sizeof(CIPHER_SUITE_STRUCT);
// 2. Get pairwise cipher counts
//Count = *(PUSHORT) pTmp;
Count = (pTmp[1]<<8) + pTmp[0];
pTmp += sizeof(USHORT);
// 3. Get pairwise cipher
// Parsing all unicast cipher suite
while (Count > 0)
{
// Skip OUI
pCipher = (PCIPHER_SUITE_STRUCT) pTmp;
TmpCipher = Ndis802_11WEPDisabled;
switch (pCipher->Type)
{
case 1:
case 5: // Although WEP is not allowed in WPA related auth mode, we parse it anyway
TmpCipher = Ndis802_11Encryption1Enabled;
break;
case 2:
TmpCipher = Ndis802_11Encryption2Enabled;
break;
case 4:
TmpCipher = Ndis802_11Encryption3Enabled;
break;
default:
break;
}
if (TmpCipher > pBss->WPA2.PairCipher)
{
// Move the lower cipher suite to PairCipherAux
pBss->WPA2.PairCipherAux = pBss->WPA2.PairCipher;
pBss->WPA2.PairCipher = TmpCipher;
}
else
{
pBss->WPA2.PairCipherAux = TmpCipher;
}
pTmp += sizeof(CIPHER_SUITE_STRUCT);
Count--;
}
// 4. get AKM suite counts
//Count = *(PUSHORT) pTmp;
Count = (pTmp[1]<<8) + pTmp[0];
pTmp += sizeof(USHORT);
// 5. Get AKM ciphers
pAKM = (PAKM_SUITE_STRUCT) pTmp;
if (!RTMPEqualMemory(pTmp, RSN_OUI, 3))
break;
switch (pAKM->Type)
{
case 1:
// Set AP support WPA mode
if (pBss->AuthMode == Ndis802_11AuthModeOpen)
pBss->AuthMode = Ndis802_11AuthModeWPA2;
else
pBss->AuthModeAux = Ndis802_11AuthModeWPA2;
break;
case 2:
// Set AP support WPA mode
if (pBss->AuthMode == Ndis802_11AuthModeOpen)
pBss->AuthMode = Ndis802_11AuthModeWPA2PSK;
else
pBss->AuthModeAux = Ndis802_11AuthModeWPA2PSK;
break;
default:
break;
}
pTmp += (Count * sizeof(AKM_SUITE_STRUCT));
// Fixed for WPA-None
if (pBss->BssType == BSS_ADHOC)
{
pBss->AuthMode = Ndis802_11AuthModeWPANone;
pBss->AuthModeAux = Ndis802_11AuthModeWPANone;
pBss->WPA.PairCipherAux = pBss->WPA2.PairCipherAux;
pBss->WPA.GroupCipher = pBss->WPA2.GroupCipher;
pBss->WepStatus = pBss->WPA.GroupCipher;
if (pBss->WPA.PairCipherAux == Ndis802_11WEPDisabled)
pBss->WPA.PairCipherAux = pBss->WPA.GroupCipher;
}
pBss->WepStatus = pBss->WPA2.PairCipher;
// 6. Get RSN capability
//pBss->WPA2.RsnCapability = *(PUSHORT) pTmp;
pBss->WPA2.RsnCapability = (pTmp[1]<<8) + pTmp[0];
pTmp += sizeof(USHORT);
// Check the Pair & Group, if different, turn on mixed mode flag
if (pBss->WPA2.GroupCipher != pBss->WPA2.PairCipher)
pBss->WPA2.bMixMode = TRUE;
break;
default:
break;
}
Length -= (pEid->Len + 2);
}
}
// ===========================================================================================
// mac_table.c
// ===========================================================================================
/*! \brief generates a random mac address value for IBSS BSSID
* \param Addr the bssid location
* \return none
* \pre
* \post
*/
VOID MacAddrRandomBssid(
IN PRTMP_ADAPTER pAd,
OUT PUCHAR pAddr)
{
INT i;
for (i = 0; i < MAC_ADDR_LEN; i++)
{
pAddr[i] = RandomByte(pAd);
}
pAddr[0] = (pAddr[0] & 0xfe) | 0x02; // the first 2 bits must be 01xxxxxxxx
}
/*! \brief init the management mac frame header
* \param p_hdr mac header
* \param subtype subtype of the frame
* \param p_ds destination address, don't care if it is a broadcast address
* \return none
* \pre the station has the following information in the pAd->StaCfg
* - bssid
* - station address
* \post
* \note this function initializes the following field
IRQL = PASSIVE_LEVEL
IRQL = DISPATCH_LEVEL
*/
VOID MgtMacHeaderInit(
IN PRTMP_ADAPTER pAd,
IN OUT PHEADER_802_11 pHdr80211,
IN UCHAR SubType,
IN UCHAR ToDs,
IN PUCHAR pDA,
IN PUCHAR pBssid)
{
NdisZeroMemory(pHdr80211, sizeof(HEADER_802_11));
pHdr80211->FC.Type = BTYPE_MGMT;
pHdr80211->FC.SubType = SubType;
pHdr80211->FC.ToDs = ToDs;
COPY_MAC_ADDR(pHdr80211->Addr1, pDA);
COPY_MAC_ADDR(pHdr80211->Addr2, pAd->CurrentAddress);
COPY_MAC_ADDR(pHdr80211->Addr3, pBssid);
}
// ===========================================================================================
// mem_mgmt.c
// ===========================================================================================
/*!***************************************************************************
* This routine build an outgoing frame, and fill all information specified
* in argument list to the frame body. The actual frame size is the summation
* of all arguments.
* input params:
* Buffer - pointer to a pre-allocated memory segment
* args - a list of <int arg_size, arg> pairs.
* NOTE NOTE NOTE!!!! the last argument must be NULL, otherwise this
* function will FAIL!!!
* return:
* Size of the buffer
* usage:
* MakeOutgoingFrame(Buffer, output_length, 2, &fc, 2, &dur, 6, p_addr1, 6,p_addr2, END_OF_ARGS);
IRQL = PASSIVE_LEVEL
IRQL = DISPATCH_LEVEL
****************************************************************************/
ULONG MakeOutgoingFrame(
OUT CHAR *Buffer,
OUT ULONG *FrameLen, ...)
{
CHAR *p;
int leng;
ULONG TotLeng;
va_list Args;
// calculates the total length
TotLeng = 0;
va_start(Args, FrameLen);
do
{
leng = va_arg(Args, int);
if (leng == END_OF_ARGS)
{
break;
}
p = va_arg(Args, PVOID);
NdisMoveMemory(&Buffer[TotLeng], p, leng);
TotLeng = TotLeng + leng;
} while(TRUE);
va_end(Args); /* clean up */
*FrameLen = TotLeng;
return TotLeng;
}
// ===========================================================================================
// mlme_queue.c
// ===========================================================================================
/*! \brief Initialize The MLME Queue, used by MLME Functions
* \param *Queue The MLME Queue
* \return Always Return NDIS_STATE_SUCCESS in this implementation
* \pre
* \post
* \note Because this is done only once (at the init stage), no need to be locked
IRQL = PASSIVE_LEVEL
*/
NDIS_STATUS MlmeQueueInit(
IN MLME_QUEUE *Queue)
{
INT i;
NdisAllocateSpinLock(&Queue->Lock);
Queue->Num = 0;
Queue->Head = 0;
Queue->Tail = 0;
for (i = 0; i < MAX_LEN_OF_MLME_QUEUE; i++)
{
Queue->Entry[i].Occupied = FALSE;
Queue->Entry[i].MsgLen = 0;
NdisZeroMemory(Queue->Entry[i].Msg, MGMT_DMA_BUFFER_SIZE);
}
return NDIS_STATUS_SUCCESS;
}
/*! \brief Enqueue a message for other threads, if they want to send messages to MLME thread
* \param *Queue The MLME Queue
* \param Machine The State Machine Id
* \param MsgType The Message Type
* \param MsgLen The Message length
* \param *Msg The message pointer
* \return TRUE if enqueue is successful, FALSE if the queue is full
* \pre
* \post
* \note The message has to be initialized
IRQL = PASSIVE_LEVEL
IRQL = DISPATCH_LEVEL
*/
BOOLEAN MlmeEnqueue(
IN PRTMP_ADAPTER pAd,
IN ULONG Machine,
IN ULONG MsgType,
IN ULONG MsgLen,
IN VOID *Msg)
{
INT Tail;
MLME_QUEUE *Queue = (MLME_QUEUE *)&pAd->Mlme.Queue;
// Do nothing if the driver is starting halt state.
// This might happen when timer already been fired before cancel timer with mlmehalt
if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST))
return FALSE;
// First check the size, it MUST not exceed the mlme queue size
if (MsgLen > MGMT_DMA_BUFFER_SIZE)
{
DBGPRINT_ERR(("MlmeEnqueue: msg too large, size = %ld \n", MsgLen));
return FALSE;
}
if (MlmeQueueFull(Queue))
{
return FALSE;
}
NdisAcquireSpinLock(&(Queue->Lock));
Tail = Queue->Tail;
Queue->Tail++;
Queue->Num++;
if (Queue->Tail == MAX_LEN_OF_MLME_QUEUE)
{
Queue->Tail = 0;
}
Queue->Entry[Tail].Wcid = RESERVED_WCID;
Queue->Entry[Tail].Occupied = TRUE;
Queue->Entry[Tail].Machine = Machine;
Queue->Entry[Tail].MsgType = MsgType;
Queue->Entry[Tail].MsgLen = MsgLen;
if (Msg != NULL)
{
NdisMoveMemory(Queue->Entry[Tail].Msg, Msg, MsgLen);
}
NdisReleaseSpinLock(&(Queue->Lock));
return TRUE;
}
/*! \brief This function is used when Recv gets a MLME message
* \param *Queue The MLME Queue
* \param TimeStampHigh The upper 32 bit of timestamp
* \param TimeStampLow The lower 32 bit of timestamp
* \param Rssi The receiving RSSI strength
* \param MsgLen The length of the message
* \param *Msg The message pointer
* \return TRUE if everything ok, FALSE otherwise (like Queue Full)
* \pre
* \post
IRQL = DISPATCH_LEVEL
*/
BOOLEAN MlmeEnqueueForRecv(
IN PRTMP_ADAPTER pAd,
IN ULONG Wcid,
IN ULONG TimeStampHigh,
IN ULONG TimeStampLow,
IN UCHAR Rssi0,
IN UCHAR Rssi1,
IN UCHAR Rssi2,
IN ULONG MsgLen,
IN VOID *Msg,
IN UCHAR Signal)
{
INT Tail, Machine;
PFRAME_802_11 pFrame = (PFRAME_802_11)Msg;
INT MsgType;
MLME_QUEUE *Queue = (MLME_QUEUE *)&pAd->Mlme.Queue;
// Do nothing if the driver is starting halt state.
// This might happen when timer already been fired before cancel timer with mlmehalt
if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST))
{
DBGPRINT_ERR(("MlmeEnqueueForRecv: fRTMP_ADAPTER_HALT_IN_PROGRESS\n"));
return FALSE;
}
// First check the size, it MUST not exceed the mlme queue size
if (MsgLen > MGMT_DMA_BUFFER_SIZE)
{
DBGPRINT_ERR(("MlmeEnqueueForRecv: frame too large, size = %ld \n", MsgLen));
return FALSE;
}
if (MlmeQueueFull(Queue))
{
return FALSE;
}
{
if (!MsgTypeSubst(pAd, pFrame, &Machine, &MsgType))
{
DBGPRINT_ERR(("MlmeEnqueueForRecv: un-recongnized mgmt->subtype=%d\n",pFrame->Hdr.FC.SubType));
return FALSE;
}
}
// OK, we got all the informations, it is time to put things into queue
NdisAcquireSpinLock(&(Queue->Lock));
Tail = Queue->Tail;
Queue->Tail++;
Queue->Num++;
if (Queue->Tail == MAX_LEN_OF_MLME_QUEUE)
{
Queue->Tail = 0;
}
Queue->Entry[Tail].Occupied = TRUE;
Queue->Entry[Tail].Machine = Machine;
Queue->Entry[Tail].MsgType = MsgType;
Queue->Entry[Tail].MsgLen = MsgLen;
Queue->Entry[Tail].TimeStamp.u.LowPart = TimeStampLow;
Queue->Entry[Tail].TimeStamp.u.HighPart = TimeStampHigh;
Queue->Entry[Tail].Rssi0 = Rssi0;
Queue->Entry[Tail].Rssi1 = Rssi1;
Queue->Entry[Tail].Rssi2 = Rssi2;
Queue->Entry[Tail].Signal = Signal;
Queue->Entry[Tail].Wcid = (UCHAR)Wcid;
Queue->Entry[Tail].Channel = pAd->LatchRfRegs.Channel;
if (Msg != NULL)
{
NdisMoveMemory(Queue->Entry[Tail].Msg, Msg, MsgLen);
}
NdisReleaseSpinLock(&(Queue->Lock));
RT28XX_MLME_HANDLER(pAd);
return TRUE;
}
/*! \brief Dequeue a message from the MLME Queue
* \param *Queue The MLME Queue
* \param *Elem The message dequeued from MLME Queue
* \return TRUE if the Elem contains something, FALSE otherwise
* \pre
* \post
IRQL = DISPATCH_LEVEL
*/
BOOLEAN MlmeDequeue(
IN MLME_QUEUE *Queue,
OUT MLME_QUEUE_ELEM **Elem)
{
NdisAcquireSpinLock(&(Queue->Lock));
*Elem = &(Queue->Entry[Queue->Head]);
Queue->Num--;
Queue->Head++;
if (Queue->Head == MAX_LEN_OF_MLME_QUEUE)
{
Queue->Head = 0;
}
NdisReleaseSpinLock(&(Queue->Lock));
return TRUE;
}
// IRQL = DISPATCH_LEVEL
VOID MlmeRestartStateMachine(
IN PRTMP_ADAPTER pAd)
{
#ifdef RT2860
MLME_QUEUE_ELEM *Elem = NULL;
#endif
BOOLEAN Cancelled;
DBGPRINT(RT_DEBUG_TRACE, ("MlmeRestartStateMachine \n"));
#ifdef RT2860
NdisAcquireSpinLock(&pAd->Mlme.TaskLock);
if(pAd->Mlme.bRunning)
{
NdisReleaseSpinLock(&pAd->Mlme.TaskLock);
return;
}
else
{
pAd->Mlme.bRunning = TRUE;
}
NdisReleaseSpinLock(&pAd->Mlme.TaskLock);
// Remove all Mlme queues elements
while (!MlmeQueueEmpty(&pAd->Mlme.Queue))
{
//From message type, determine which state machine I should drive
if (MlmeDequeue(&pAd->Mlme.Queue, &Elem))
{
// free MLME element
Elem->Occupied = FALSE;
Elem->MsgLen = 0;
}
else {
DBGPRINT_ERR(("MlmeRestartStateMachine: MlmeQueue empty\n"));
}
}
#endif /* RT2860 */
{
// Cancel all timer events
// Be careful to cancel new added timer
RTMPCancelTimer(&pAd->MlmeAux.AssocTimer, &Cancelled);
RTMPCancelTimer(&pAd->MlmeAux.ReassocTimer, &Cancelled);
RTMPCancelTimer(&pAd->MlmeAux.DisassocTimer, &Cancelled);
RTMPCancelTimer(&pAd->MlmeAux.AuthTimer, &Cancelled);
RTMPCancelTimer(&pAd->MlmeAux.BeaconTimer, &Cancelled);
RTMPCancelTimer(&pAd->MlmeAux.ScanTimer, &Cancelled);
}
// Change back to original channel in case of doing scan
AsicSwitchChannel(pAd, pAd->CommonCfg.Channel, FALSE);
AsicLockChannel(pAd, pAd->CommonCfg.Channel);
// Resume MSDU which is turned off durning scan
RTMPResumeMsduTransmission(pAd);
{
// Set all state machines back IDLE
pAd->Mlme.CntlMachine.CurrState = CNTL_IDLE;
pAd->Mlme.AssocMachine.CurrState = ASSOC_IDLE;
pAd->Mlme.AuthMachine.CurrState = AUTH_REQ_IDLE;
pAd->Mlme.AuthRspMachine.CurrState = AUTH_RSP_IDLE;
pAd->Mlme.SyncMachine.CurrState = SYNC_IDLE;
pAd->Mlme.ActMachine.CurrState = ACT_IDLE;
}
#ifdef RT2860
// Remove running state
NdisAcquireSpinLock(&pAd->Mlme.TaskLock);
pAd->Mlme.bRunning = FALSE;
NdisReleaseSpinLock(&pAd->Mlme.TaskLock);
#endif
}
/*! \brief test if the MLME Queue is empty
* \param *Queue The MLME Queue
* \return TRUE if the Queue is empty, FALSE otherwise
* \pre
* \post
IRQL = DISPATCH_LEVEL
*/
BOOLEAN MlmeQueueEmpty(
IN MLME_QUEUE *Queue)
{
BOOLEAN Ans;
NdisAcquireSpinLock(&(Queue->Lock));
Ans = (Queue->Num == 0);
NdisReleaseSpinLock(&(Queue->Lock));
return Ans;
}
/*! \brief test if the MLME Queue is full
* \param *Queue The MLME Queue
* \return TRUE if the Queue is empty, FALSE otherwise
* \pre
* \post
IRQL = PASSIVE_LEVEL
IRQL = DISPATCH_LEVEL
*/
BOOLEAN MlmeQueueFull(
IN MLME_QUEUE *Queue)
{
BOOLEAN Ans;
NdisAcquireSpinLock(&(Queue->Lock));
Ans = (Queue->Num == MAX_LEN_OF_MLME_QUEUE || Queue->Entry[Queue->Tail].Occupied);
NdisReleaseSpinLock(&(Queue->Lock));
return Ans;
}
/*! \brief The destructor of MLME Queue
* \param
* \return
* \pre
* \post
* \note Clear Mlme Queue, Set Queue->Num to Zero.
IRQL = PASSIVE_LEVEL
*/
VOID MlmeQueueDestroy(
IN MLME_QUEUE *pQueue)
{
NdisAcquireSpinLock(&(pQueue->Lock));
pQueue->Num = 0;
pQueue->Head = 0;
pQueue->Tail = 0;
NdisReleaseSpinLock(&(pQueue->Lock));
NdisFreeSpinLock(&(pQueue->Lock));
}
/*! \brief To substitute the message type if the message is coming from external
* \param pFrame The frame received
* \param *Machine The state machine
* \param *MsgType the message type for the state machine
* \return TRUE if the substitution is successful, FALSE otherwise
* \pre
* \post
IRQL = DISPATCH_LEVEL
*/
BOOLEAN MsgTypeSubst(
IN PRTMP_ADAPTER pAd,
IN PFRAME_802_11 pFrame,
OUT INT *Machine,
OUT INT *MsgType)
{
USHORT Seq;
UCHAR EAPType;
PUCHAR pData;
// Pointer to start of data frames including SNAP header
pData = (PUCHAR) pFrame + LENGTH_802_11;
// The only data type will pass to this function is EAPOL frame
if (pFrame->Hdr.FC.Type == BTYPE_DATA)
{
if (NdisEqualMemory(SNAP_AIRONET, pData, LENGTH_802_1_H))
{
// Cisco Aironet SNAP header
*Machine = AIRONET_STATE_MACHINE;
*MsgType = MT2_AIRONET_MSG;
return (TRUE);
}
{
*Machine = WPA_PSK_STATE_MACHINE;
EAPType = *((UCHAR*)pFrame + LENGTH_802_11 + LENGTH_802_1_H + 1);
return(WpaMsgTypeSubst(EAPType, MsgType));
}
}
switch (pFrame->Hdr.FC.SubType)
{
case SUBTYPE_ASSOC_REQ:
*Machine = ASSOC_STATE_MACHINE;
*MsgType = MT2_PEER_ASSOC_REQ;
break;
case SUBTYPE_ASSOC_RSP:
*Machine = ASSOC_STATE_MACHINE;
*MsgType = MT2_PEER_ASSOC_RSP;
break;
case SUBTYPE_REASSOC_REQ:
*Machine = ASSOC_STATE_MACHINE;
*MsgType = MT2_PEER_REASSOC_REQ;
break;
case SUBTYPE_REASSOC_RSP:
*Machine = ASSOC_STATE_MACHINE;
*MsgType = MT2_PEER_REASSOC_RSP;
break;
case SUBTYPE_PROBE_REQ:
*Machine = SYNC_STATE_MACHINE;
*MsgType = MT2_PEER_PROBE_REQ;
break;
case SUBTYPE_PROBE_RSP:
*Machine = SYNC_STATE_MACHINE;
*MsgType = MT2_PEER_PROBE_RSP;
break;
case SUBTYPE_BEACON:
*Machine = SYNC_STATE_MACHINE;
*MsgType = MT2_PEER_BEACON;
break;
case SUBTYPE_ATIM:
*Machine = SYNC_STATE_MACHINE;
*MsgType = MT2_PEER_ATIM;
break;
case SUBTYPE_DISASSOC:
*Machine = ASSOC_STATE_MACHINE;
*MsgType = MT2_PEER_DISASSOC_REQ;
break;
case SUBTYPE_AUTH:
// get the sequence number from payload 24 Mac Header + 2 bytes algorithm
NdisMoveMemory(&Seq, &pFrame->Octet[2], sizeof(USHORT));
if (Seq == 1 || Seq == 3)
{
*Machine = AUTH_RSP_STATE_MACHINE;
*MsgType = MT2_PEER_AUTH_ODD;
}
else if (Seq == 2 || Seq == 4)
{
*Machine = AUTH_STATE_MACHINE;
*MsgType = MT2_PEER_AUTH_EVEN;
}
else
{
return FALSE;
}
break;
case SUBTYPE_DEAUTH:
*Machine = AUTH_RSP_STATE_MACHINE;
*MsgType = MT2_PEER_DEAUTH;
break;
case SUBTYPE_ACTION:
*Machine = ACTION_STATE_MACHINE;
// Sometimes Sta will return with category bytes with MSB = 1, if they receive catogory out of their support
if ((pFrame->Octet[0]&0x7F) > MAX_PEER_CATE_MSG)
{
*MsgType = MT2_ACT_INVALID;
}
else
{
*MsgType = (pFrame->Octet[0]&0x7F);
}
break;
default:
return FALSE;
break;
}
return TRUE;
}
// ===========================================================================================
// state_machine.c
// ===========================================================================================
/*! \brief Initialize the state machine.
* \param *S pointer to the state machine
* \param Trans State machine transition function
* \param StNr number of states
* \param MsgNr number of messages
* \param DefFunc default function, when there is invalid state/message combination
* \param InitState initial state of the state machine
* \param Base StateMachine base, internal use only
* \pre p_sm should be a legal pointer
* \post
IRQL = PASSIVE_LEVEL
*/
VOID StateMachineInit(
IN STATE_MACHINE *S,
IN STATE_MACHINE_FUNC Trans[],
IN ULONG StNr,
IN ULONG MsgNr,
IN STATE_MACHINE_FUNC DefFunc,
IN ULONG InitState,
IN ULONG Base)
{
ULONG i, j;
// set number of states and messages
S->NrState = StNr;
S->NrMsg = MsgNr;
S->Base = Base;
S->TransFunc = Trans;
// init all state transition to default function
for (i = 0; i < StNr; i++)
{
for (j = 0; j < MsgNr; j++)
{
S->TransFunc[i * MsgNr + j] = DefFunc;
}
}
// set the starting state
S->CurrState = InitState;
}
/*! \brief This function fills in the function pointer into the cell in the state machine
* \param *S pointer to the state machine
* \param St state
* \param Msg incoming message
* \param f the function to be executed when (state, message) combination occurs at the state machine
* \pre *S should be a legal pointer to the state machine, st, msg, should be all within the range, Base should be set in the initial state
* \post
IRQL = PASSIVE_LEVEL
*/
VOID StateMachineSetAction(
IN STATE_MACHINE *S,
IN ULONG St,
IN ULONG Msg,
IN STATE_MACHINE_FUNC Func)
{
ULONG MsgIdx;
MsgIdx = Msg - S->Base;
if (St < S->NrState && MsgIdx < S->NrMsg)
{
// boundary checking before setting the action
S->TransFunc[St * S->NrMsg + MsgIdx] = Func;
}
}
/*! \brief This function does the state transition
* \param *Adapter the NIC adapter pointer
* \param *S the state machine
* \param *Elem the message to be executed
* \return None
IRQL = DISPATCH_LEVEL
*/
VOID StateMachinePerformAction(
IN PRTMP_ADAPTER pAd,
IN STATE_MACHINE *S,
IN MLME_QUEUE_ELEM *Elem)
{
(*(S->TransFunc[S->CurrState * S->NrMsg + Elem->MsgType - S->Base]))(pAd, Elem);
}
/*
==========================================================================
Description:
The drop function, when machine executes this, the message is simply
ignored. This function does nothing, the message is freed in
StateMachinePerformAction()
==========================================================================
*/
VOID Drop(
IN PRTMP_ADAPTER pAd,
IN MLME_QUEUE_ELEM *Elem)
{
}
// ===========================================================================================
// lfsr.c
// ===========================================================================================
/*
==========================================================================
Description:
IRQL = PASSIVE_LEVEL
==========================================================================
*/
VOID LfsrInit(
IN PRTMP_ADAPTER pAd,
IN ULONG Seed)
{
if (Seed == 0)
pAd->Mlme.ShiftReg = 1;
else
pAd->Mlme.ShiftReg = Seed;
}
/*
==========================================================================
Description:
==========================================================================
*/
UCHAR RandomByte(
IN PRTMP_ADAPTER pAd)
{
ULONG i;
UCHAR R, Result;
R = 0;
if (pAd->Mlme.ShiftReg == 0)
NdisGetSystemUpTime((ULONG *)&pAd->Mlme.ShiftReg);
for (i = 0; i < 8; i++)
{
if (pAd->Mlme.ShiftReg & 0x00000001)
{
pAd->Mlme.ShiftReg = ((pAd->Mlme.ShiftReg ^ LFSR_MASK) >> 1) | 0x80000000;
Result = 1;
}
else
{
pAd->Mlme.ShiftReg = pAd->Mlme.ShiftReg >> 1;
Result = 0;
}
R = (R << 1) | Result;
}
return R;
}
VOID AsicUpdateAutoFallBackTable(
IN PRTMP_ADAPTER pAd,
IN PUCHAR pRateTable)
{
UCHAR i;
HT_FBK_CFG0_STRUC HtCfg0;
HT_FBK_CFG1_STRUC HtCfg1;
LG_FBK_CFG0_STRUC LgCfg0;
LG_FBK_CFG1_STRUC LgCfg1;
PRTMP_TX_RATE_SWITCH pCurrTxRate, pNextTxRate;
// set to initial value
HtCfg0.word = 0x65432100;
HtCfg1.word = 0xedcba988;
LgCfg0.word = 0xedcba988;
LgCfg1.word = 0x00002100;
pNextTxRate = (PRTMP_TX_RATE_SWITCH)pRateTable+1;
for (i = 1; i < *((PUCHAR) pRateTable); i++)
{
pCurrTxRate = (PRTMP_TX_RATE_SWITCH)pRateTable+1+i;
switch (pCurrTxRate->Mode)
{
case 0: //CCK
break;
case 1: //OFDM
{
switch(pCurrTxRate->CurrMCS)
{
case 0:
LgCfg0.field.OFDMMCS0FBK = (pNextTxRate->Mode == MODE_OFDM) ? (pNextTxRate->CurrMCS+8): pNextTxRate->CurrMCS;
break;
case 1:
LgCfg0.field.OFDMMCS1FBK = (pNextTxRate->Mode == MODE_OFDM) ? (pNextTxRate->CurrMCS+8): pNextTxRate->CurrMCS;
break;
case 2:
LgCfg0.field.OFDMMCS2FBK = (pNextTxRate->Mode == MODE_OFDM) ? (pNextTxRate->CurrMCS+8): pNextTxRate->CurrMCS;
break;
case 3:
LgCfg0.field.OFDMMCS3FBK = (pNextTxRate->Mode == MODE_OFDM) ? (pNextTxRate->CurrMCS+8): pNextTxRate->CurrMCS;
break;
case 4:
LgCfg0.field.OFDMMCS4FBK = (pNextTxRate->Mode == MODE_OFDM) ? (pNextTxRate->CurrMCS+8): pNextTxRate->CurrMCS;
break;
case 5:
LgCfg0.field.OFDMMCS5FBK = (pNextTxRate->Mode == MODE_OFDM) ? (pNextTxRate->CurrMCS+8): pNextTxRate->CurrMCS;
break;
case 6:
LgCfg0.field.OFDMMCS6FBK = (pNextTxRate->Mode == MODE_OFDM) ? (pNextTxRate->CurrMCS+8): pNextTxRate->CurrMCS;
break;
case 7:
LgCfg0.field.OFDMMCS7FBK = (pNextTxRate->Mode == MODE_OFDM) ? (pNextTxRate->CurrMCS+8): pNextTxRate->CurrMCS;
break;
}
}
break;
case 2: //HT-MIX
case 3: //HT-GF
{
if ((pNextTxRate->Mode >= MODE_HTMIX) && (pCurrTxRate->CurrMCS != pNextTxRate->CurrMCS))
{
switch(pCurrTxRate->CurrMCS)
{
case 0:
HtCfg0.field.HTMCS0FBK = pNextTxRate->CurrMCS;
break;
case 1:
HtCfg0.field.HTMCS1FBK = pNextTxRate->CurrMCS;
break;
case 2:
HtCfg0.field.HTMCS2FBK = pNextTxRate->CurrMCS;
break;
case 3:
HtCfg0.field.HTMCS3FBK = pNextTxRate->CurrMCS;
break;
case 4:
HtCfg0.field.HTMCS4FBK = pNextTxRate->CurrMCS;
break;
case 5:
HtCfg0.field.HTMCS5FBK = pNextTxRate->CurrMCS;
break;
case 6:
HtCfg0.field.HTMCS6FBK = pNextTxRate->CurrMCS;
break;
case 7:
HtCfg0.field.HTMCS7FBK = pNextTxRate->CurrMCS;
break;
case 8:
HtCfg1.field.HTMCS8FBK = pNextTxRate->CurrMCS;
break;
case 9:
HtCfg1.field.HTMCS9FBK = pNextTxRate->CurrMCS;
break;
case 10:
HtCfg1.field.HTMCS10FBK = pNextTxRate->CurrMCS;
break;
case 11:
HtCfg1.field.HTMCS11FBK = pNextTxRate->CurrMCS;
break;
case 12:
HtCfg1.field.HTMCS12FBK = pNextTxRate->CurrMCS;
break;
case 13:
HtCfg1.field.HTMCS13FBK = pNextTxRate->CurrMCS;
break;
case 14:
HtCfg1.field.HTMCS14FBK = pNextTxRate->CurrMCS;
break;
case 15:
HtCfg1.field.HTMCS15FBK = pNextTxRate->CurrMCS;
break;
default:
DBGPRINT(RT_DEBUG_ERROR, ("AsicUpdateAutoFallBackTable: not support CurrMCS=%d\n", pCurrTxRate->CurrMCS));
}
}
}
break;
}
pNextTxRate = pCurrTxRate;
}
RTMP_IO_WRITE32(pAd, HT_FBK_CFG0, HtCfg0.word);
RTMP_IO_WRITE32(pAd, HT_FBK_CFG1, HtCfg1.word);
RTMP_IO_WRITE32(pAd, LG_FBK_CFG0, LgCfg0.word);
RTMP_IO_WRITE32(pAd, LG_FBK_CFG1, LgCfg1.word);
}
/*
========================================================================
Routine Description:
Set MAC register value according operation mode.
OperationMode AND bNonGFExist are for MM and GF Proteciton.
If MM or GF mask is not set, those passing argument doesn't not take effect.
Operation mode meaning:
= 0 : Pure HT, no preotection.
= 0x01; there may be non-HT devices in both the control and extension channel, protection is optional in BSS.
= 0x10: No Transmission in 40M is protected.
= 0x11: Transmission in both 40M and 20M shall be protected
if (bNonGFExist)
we should choose not to use GF. But still set correct ASIC registers.
========================================================================
*/
VOID AsicUpdateProtect(
IN PRTMP_ADAPTER pAd,
IN USHORT OperationMode,
IN UCHAR SetMask,
IN BOOLEAN bDisableBGProtect,
IN BOOLEAN bNonGFExist)
{
PROT_CFG_STRUC ProtCfg, ProtCfg4;
UINT32 Protect[6];
USHORT offset;
UCHAR i;
UINT32 MacReg = 0;
if (!(pAd->CommonCfg.bHTProtect) && (OperationMode != 8))
{
return;
}
if (pAd->BATable.numAsOriginator)
{
//
// enable the RTS/CTS to avoid channel collision
//
SetMask = ALLN_SETPROTECT;
OperationMode = 8;
}
// Config ASIC RTS threshold register
RTMP_IO_READ32(pAd, TX_RTS_CFG, &MacReg);
MacReg &= 0xFF0000FF;
// If the user want disable RtsThreshold and enable Amsdu/Ralink-Aggregation, set the RtsThreshold as 4096
if ((
(pAd->CommonCfg.BACapability.field.AmsduEnable) ||
(pAd->CommonCfg.bAggregationCapable == TRUE))
&& pAd->CommonCfg.RtsThreshold == MAX_RTS_THRESHOLD)
{
MacReg |= (0x1000 << 8);
}
else
{
MacReg |= (pAd->CommonCfg.RtsThreshold << 8);
}
RTMP_IO_WRITE32(pAd, TX_RTS_CFG, MacReg);
// Initial common protection settings
RTMPZeroMemory(Protect, sizeof(Protect));
ProtCfg4.word = 0;
ProtCfg.word = 0;
ProtCfg.field.TxopAllowGF40 = 1;
ProtCfg.field.TxopAllowGF20 = 1;
ProtCfg.field.TxopAllowMM40 = 1;
ProtCfg.field.TxopAllowMM20 = 1;
ProtCfg.field.TxopAllowOfdm = 1;
ProtCfg.field.TxopAllowCck = 1;
ProtCfg.field.RTSThEn = 1;
ProtCfg.field.ProtectNav = ASIC_SHORTNAV;
// update PHY mode and rate
if (pAd->CommonCfg.Channel > 14)
ProtCfg.field.ProtectRate = 0x4000;
ProtCfg.field.ProtectRate |= pAd->CommonCfg.RtsRate;
// Handle legacy(B/G) protection
if (bDisableBGProtect)
{
//ProtCfg.field.ProtectRate = pAd->CommonCfg.RtsRate;
ProtCfg.field.ProtectCtrl = 0;
Protect[0] = ProtCfg.word;
Protect[1] = ProtCfg.word;
}
else
{
//ProtCfg.field.ProtectRate = pAd->CommonCfg.RtsRate;
ProtCfg.field.ProtectCtrl = 0; // CCK do not need to be protected
Protect[0] = ProtCfg.word;
ProtCfg.field.ProtectCtrl = ASIC_CTS; // OFDM needs using CCK to protect
Protect[1] = ProtCfg.word;
}
// Decide HT frame protection.
if ((SetMask & ALLN_SETPROTECT) != 0)
{
switch(OperationMode)
{
case 0x0:
// NO PROTECT
// 1.All STAs in the BSS are 20/40 MHz HT
// 2. in ai 20/40MHz BSS
// 3. all STAs are 20MHz in a 20MHz BSS
// Pure HT. no protection.
// MM20_PROT_CFG
// Reserved (31:27)
// PROT_TXOP(25:20) -- 010111
// PROT_NAV(19:18) -- 01 (Short NAV protection)
// PROT_CTRL(17:16) -- 00 (None)
// PROT_RATE(15:0) -- 0x4004 (OFDM 24M)
Protect[2] = 0x01744004;
// MM40_PROT_CFG
// Reserved (31:27)
// PROT_TXOP(25:20) -- 111111
// PROT_NAV(19:18) -- 01 (Short NAV protection)
// PROT_CTRL(17:16) -- 00 (None)
// PROT_RATE(15:0) -- 0x4084 (duplicate OFDM 24M)
Protect[3] = 0x03f44084;
// CF20_PROT_CFG
// Reserved (31:27)
// PROT_TXOP(25:20) -- 010111
// PROT_NAV(19:18) -- 01 (Short NAV protection)
// PROT_CTRL(17:16) -- 00 (None)
// PROT_RATE(15:0) -- 0x4004 (OFDM 24M)
Protect[4] = 0x01744004;
// CF40_PROT_CFG
// Reserved (31:27)
// PROT_TXOP(25:20) -- 111111
// PROT_NAV(19:18) -- 01 (Short NAV protection)
// PROT_CTRL(17:16) -- 00 (None)
// PROT_RATE(15:0) -- 0x4084 (duplicate OFDM 24M)
Protect[5] = 0x03f44084;
if (bNonGFExist)
{
// PROT_NAV(19:18) -- 01 (Short NAV protectiion)
// PROT_CTRL(17:16) -- 01 (RTS/CTS)
Protect[4] = 0x01754004;
Protect[5] = 0x03f54084;
}
pAd->CommonCfg.IOTestParm.bRTSLongProtOn = FALSE;
break;
case 1:
// This is "HT non-member protection mode."
// If there may be non-HT STAs my BSS
ProtCfg.word = 0x01744004; // PROT_CTRL(17:16) : 0 (None)
ProtCfg4.word = 0x03f44084; // duplicaet legacy 24M. BW set 1.
if (OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_BG_PROTECTION_INUSED))
{
ProtCfg.word = 0x01740003; //ERP use Protection bit is set, use protection rate at Clause 18..
ProtCfg4.word = 0x03f40003; // Don't duplicate RTS/CTS in CCK mode. 0x03f40083;
}
//Assign Protection method for 20&40 MHz packets
ProtCfg.field.ProtectCtrl = ASIC_RTS;
ProtCfg.field.ProtectNav = ASIC_SHORTNAV;
ProtCfg4.field.ProtectCtrl = ASIC_RTS;
ProtCfg4.field.ProtectNav = ASIC_SHORTNAV;
Protect[2] = ProtCfg.word;
Protect[3] = ProtCfg4.word;
Protect[4] = ProtCfg.word;
Protect[5] = ProtCfg4.word;
pAd->CommonCfg.IOTestParm.bRTSLongProtOn = TRUE;
break;
case 2:
// If only HT STAs are in BSS. at least one is 20MHz. Only protect 40MHz packets
ProtCfg.word = 0x01744004; // PROT_CTRL(17:16) : 0 (None)
ProtCfg4.word = 0x03f44084; // duplicaet legacy 24M. BW set 1.
//Assign Protection method for 40MHz packets
ProtCfg4.field.ProtectCtrl = ASIC_RTS;
ProtCfg4.field.ProtectNav = ASIC_SHORTNAV;
Protect[2] = ProtCfg.word;
Protect[3] = ProtCfg4.word;
if (bNonGFExist)
{
ProtCfg.field.ProtectCtrl = ASIC_RTS;
ProtCfg.field.ProtectNav = ASIC_SHORTNAV;
}
Protect[4] = ProtCfg.word;
Protect[5] = ProtCfg4.word;
pAd->CommonCfg.IOTestParm.bRTSLongProtOn = FALSE;
break;
case 3:
// HT mixed mode. PROTECT ALL!
// Assign Rate
ProtCfg.word = 0x01744004; //duplicaet legacy 24M. BW set 1.
ProtCfg4.word = 0x03f44084;
// both 20MHz and 40MHz are protected. Whether use RTS or CTS-to-self depends on the
if (OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_BG_PROTECTION_INUSED))
{
ProtCfg.word = 0x01740003; //ERP use Protection bit is set, use protection rate at Clause 18..
ProtCfg4.word = 0x03f40003; // Don't duplicate RTS/CTS in CCK mode. 0x03f40083
}
//Assign Protection method for 20&40 MHz packets
ProtCfg.field.ProtectCtrl = ASIC_RTS;
ProtCfg.field.ProtectNav = ASIC_SHORTNAV;
ProtCfg4.field.ProtectCtrl = ASIC_RTS;
ProtCfg4.field.ProtectNav = ASIC_SHORTNAV;
Protect[2] = ProtCfg.word;
Protect[3] = ProtCfg4.word;
Protect[4] = ProtCfg.word;
Protect[5] = ProtCfg4.word;
pAd->CommonCfg.IOTestParm.bRTSLongProtOn = TRUE;
break;
case 8:
// Special on for Atheros problem n chip.
Protect[2] = 0x01754004;
Protect[3] = 0x03f54084;
Protect[4] = 0x01754004;
Protect[5] = 0x03f54084;
pAd->CommonCfg.IOTestParm.bRTSLongProtOn = TRUE;
break;
}
}
offset = CCK_PROT_CFG;
for (i = 0;i < 6;i++)
{
if ((SetMask & (1<< i)))
{
RTMP_IO_WRITE32(pAd, offset + i*4, Protect[i]);
}
}
}
#ifdef RT30xx
/*
========================================================================
Routine Description: Write RT30xx RF register through MAC
Arguments:
Return Value:
IRQL =
Note:
========================================================================
*/
NTSTATUS RT30xxWriteRFRegister(
IN PRTMP_ADAPTER pAd,
IN UCHAR RegID,
IN UCHAR Value)
{
RF_CSR_CFG_STRUC rfcsr;
UINT i = 0;
do
{
RTMP_IO_READ32(pAd, RF_CSR_CFG, &rfcsr.word);
if (!rfcsr.field.RF_CSR_KICK)
break;
i++;
}
while ((i < RETRY_LIMIT) && (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)));
if ((i == RETRY_LIMIT) || (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)))
{
DBGPRINT_RAW(RT_DEBUG_ERROR, ("Retry count exhausted or device removed!!!\n"));
return STATUS_UNSUCCESSFUL;
}
rfcsr.field.RF_CSR_WR = 1;
rfcsr.field.RF_CSR_KICK = 1;
rfcsr.field.TESTCSR_RFACC_REGNUM = RegID;
rfcsr.field.RF_CSR_DATA = Value;
RTMP_IO_WRITE32(pAd, RF_CSR_CFG, rfcsr.word);
return STATUS_SUCCESS;
}
/*
========================================================================
Routine Description: Read RT30xx RF register through MAC
Arguments:
Return Value:
IRQL =
Note:
========================================================================
*/
NTSTATUS RT30xxReadRFRegister(
IN PRTMP_ADAPTER pAd,
IN UCHAR RegID,
IN PUCHAR pValue)
{
RF_CSR_CFG_STRUC rfcsr;
UINT i=0, k=0;
for (i=0; i<MAX_BUSY_COUNT; i++)
{
RTMP_IO_READ32(pAd, RF_CSR_CFG, &rfcsr.word);
if (rfcsr.field.RF_CSR_KICK == BUSY)
{
continue;
}
rfcsr.word = 0;
rfcsr.field.RF_CSR_WR = 0;
rfcsr.field.RF_CSR_KICK = 1;
rfcsr.field.TESTCSR_RFACC_REGNUM = RegID;
RTMP_IO_WRITE32(pAd, RF_CSR_CFG, rfcsr.word);
for (k=0; k<MAX_BUSY_COUNT; k++)
{
RTMP_IO_READ32(pAd, RF_CSR_CFG, &rfcsr.word);
if (rfcsr.field.RF_CSR_KICK == IDLE)
break;
}
if ((rfcsr.field.RF_CSR_KICK == IDLE) &&
(rfcsr.field.TESTCSR_RFACC_REGNUM == RegID))
{
*pValue = (UCHAR)rfcsr.field.RF_CSR_DATA;
break;
}
}
if (rfcsr.field.RF_CSR_KICK == BUSY)
{
DBGPRINT_ERR(("RF read R%d=0x%x fail, i[%d], k[%d]\n", RegID, rfcsr.word,i,k));
return STATUS_UNSUCCESSFUL;
}
return STATUS_SUCCESS;
}
#endif // RT30xx //
#ifdef RT30xx
// add by johnli, RF power sequence setup
/*
==========================================================================
Description:
Load RF normal operation-mode setup
==========================================================================
*/
VOID RT30xxLoadRFNormalModeSetup(
IN PRTMP_ADAPTER pAd)
{
UCHAR RFValue;
// RX0_PD & TX0_PD, RF R1 register Bit 2 & Bit 3 to 0 and RF_BLOCK_en,RX1_PD & TX1_PD, Bit0, Bit 4 & Bit5 to 1
RT30xxReadRFRegister(pAd, RF_R01, &RFValue);
RFValue = (RFValue & (~0x0C)) | 0x31;
RT30xxWriteRFRegister(pAd, RF_R01, RFValue);
// TX_LO2_en, RF R15 register Bit 3 to 0
RT30xxReadRFRegister(pAd, RF_R15, &RFValue);
RFValue &= (~0x08);
RT30xxWriteRFRegister(pAd, RF_R15, RFValue);
// TX_LO1_en, RF R17 register Bit 3 to 0
RT30xxReadRFRegister(pAd, RF_R17, &RFValue);
RFValue &= (~0x08);
// to fix rx long range issue
if (((pAd->MACVersion & 0xffff) >= 0x0211) && (pAd->NicConfig2.field.ExternalLNAForG == 0))
{
RFValue |= 0x20;
}
RT30xxWriteRFRegister(pAd, RF_R17, RFValue);
// RX_LO1_en, RF R20 register Bit 3 to 0
RT30xxReadRFRegister(pAd, RF_R20, &RFValue);
RFValue &= (~0x08);
RT30xxWriteRFRegister(pAd, RF_R20, RFValue);
// RX_LO2_en, RF R21 register Bit 3 to 0
RT30xxReadRFRegister(pAd, RF_R21, &RFValue);
RFValue &= (~0x08);
RT30xxWriteRFRegister(pAd, RF_R21, RFValue);
// LDORF_VC, RF R27 register Bit 2 to 0
RT30xxReadRFRegister(pAd, RF_R27, &RFValue);
if ((pAd->MACVersion & 0xffff) < 0x0211)
RFValue = (RFValue & (~0x77)) | 0x3;
else
RFValue = (RFValue & (~0x77));
RT30xxWriteRFRegister(pAd, RF_R27, RFValue);
/* end johnli */
}
/*
==========================================================================
Description:
Load RF sleep-mode setup
==========================================================================
*/
VOID RT30xxLoadRFSleepModeSetup(
IN PRTMP_ADAPTER pAd)
{
UCHAR RFValue;
UINT32 MACValue;
// RF_BLOCK_en. RF R1 register Bit 0 to 0
RT30xxReadRFRegister(pAd, RF_R01, &RFValue);
RFValue &= (~0x01);
RT30xxWriteRFRegister(pAd, RF_R01, RFValue);
// VCO_IC, RF R7 register Bit 4 & Bit 5 to 0
RT30xxReadRFRegister(pAd, RF_R07, &RFValue);
RFValue &= (~0x30);
RT30xxWriteRFRegister(pAd, RF_R07, RFValue);
// Idoh, RF R9 register Bit 1, Bit 2 & Bit 3 to 0
RT30xxReadRFRegister(pAd, RF_R09, &RFValue);
RFValue &= (~0x0E);
RT30xxWriteRFRegister(pAd, RF_R09, RFValue);
// RX_CTB_en, RF R21 register Bit 7 to 0
RT30xxReadRFRegister(pAd, RF_R21, &RFValue);
RFValue &= (~0x80);
RT30xxWriteRFRegister(pAd, RF_R21, RFValue);
// LDORF_VC, RF R27 register Bit 0, Bit 1 & Bit 2 to 1
RT30xxReadRFRegister(pAd, RF_R27, &RFValue);
RFValue |= 0x77;
RT30xxWriteRFRegister(pAd, RF_R27, RFValue);
RTMP_IO_READ32(pAd, LDO_CFG0, &MACValue);
MACValue |= 0x1D000000;
RTMP_IO_WRITE32(pAd, LDO_CFG0, MACValue);
}
/*
==========================================================================
Description:
Reverse RF sleep-mode setup
==========================================================================
*/
VOID RT30xxReverseRFSleepModeSetup(
IN PRTMP_ADAPTER pAd)
{
UCHAR RFValue;
UINT32 MACValue;
// RF_BLOCK_en, RF R1 register Bit 0 to 1
RT30xxReadRFRegister(pAd, RF_R01, &RFValue);
RFValue |= 0x01;
RT30xxWriteRFRegister(pAd, RF_R01, RFValue);
// VCO_IC, RF R7 register Bit 4 & Bit 5 to 1
RT30xxReadRFRegister(pAd, RF_R07, &RFValue);
RFValue |= 0x30;
RT30xxWriteRFRegister(pAd, RF_R07, RFValue);
// Idoh, RF R9 register Bit 1, Bit 2 & Bit 3 to 1
RT30xxReadRFRegister(pAd, RF_R09, &RFValue);
RFValue |= 0x0E;
RT30xxWriteRFRegister(pAd, RF_R09, RFValue);
// RX_CTB_en, RF R21 register Bit 7 to 1
RT30xxReadRFRegister(pAd, RF_R21, &RFValue);
RFValue |= 0x80;
RT30xxWriteRFRegister(pAd, RF_R21, RFValue);
// LDORF_VC, RF R27 register Bit 2 to 0
RT30xxReadRFRegister(pAd, RF_R27, &RFValue);
if ((pAd->MACVersion & 0xffff) < 0x0211)
RFValue = (RFValue & (~0x77)) | 0x3;
else
RFValue = (RFValue & (~0x77));
RT30xxWriteRFRegister(pAd, RF_R27, RFValue);
// RT3071 version E has fixed this issue
if ((pAd->NicConfig2.field.DACTestBit == 1) && ((pAd->MACVersion & 0xffff) < 0x0211))
{
// patch tx EVM issue temporarily
RTMP_IO_READ32(pAd, LDO_CFG0, &MACValue);
MACValue = ((MACValue & 0xE0FFFFFF) | 0x0D000000);
RTMP_IO_WRITE32(pAd, LDO_CFG0, MACValue);
}
else
{
RTMP_IO_READ32(pAd, LDO_CFG0, &MACValue);
MACValue = ((MACValue & 0xE0FFFFFF) | 0x01000000);
RTMP_IO_WRITE32(pAd, LDO_CFG0, MACValue);
}
}
// end johnli
#endif // RT30xx //
/*
==========================================================================
Description:
IRQL = PASSIVE_LEVEL
IRQL = DISPATCH_LEVEL
==========================================================================
*/
VOID AsicSwitchChannel(
IN PRTMP_ADAPTER pAd,
IN UCHAR Channel,
IN BOOLEAN bScan)
{
ULONG R2 = 0, R3 = DEFAULT_RF_TX_POWER, R4 = 0;
CHAR TxPwer = 0, TxPwer2 = DEFAULT_RF_TX_POWER; //Bbp94 = BBPR94_DEFAULT, TxPwer2 = DEFAULT_RF_TX_POWER;
UCHAR index;
UINT32 Value = 0; //BbpReg, Value;
RTMP_RF_REGS *RFRegTable;
// Search Tx power value
#ifdef RT30xx
// We can't use ChannelList to search channel, since some central channl's txpowr doesn't list
// in ChannelList, so use TxPower array instead.
//
for (index = 0; index < MAX_NUM_OF_CHANNELS; index++)
{
if (Channel == pAd->TxPower[index].Channel)
{
TxPwer = pAd->TxPower[index].Power;
TxPwer2 = pAd->TxPower[index].Power2;
break;
}
}
#endif
#ifndef RT30xx
for (index = 0; index < pAd->ChannelListNum; index++)
{
if (Channel == pAd->ChannelList[index].Channel)
{
TxPwer = pAd->ChannelList[index].Power;
TxPwer2 = pAd->ChannelList[index].Power2;
break;
}
}
#endif
if (index == MAX_NUM_OF_CHANNELS)
{
#ifndef RT30xx
DBGPRINT(RT_DEBUG_ERROR, ("AsicSwitchChannel: Cant find the Channel#%d \n", Channel));
#endif
#ifdef RT30xx
DBGPRINT(RT_DEBUG_ERROR, ("AsicSwitchChannel: Can't find the Channel#%d \n", Channel));
#endif
}
#ifdef RT2870
// The RF programming sequence is difference between 3xxx and 2xxx
#ifdef RT30xx
if ((IS_RT3070(pAd) || IS_RT3090(pAd)) && ((pAd->RfIcType == RFIC_3020) || (pAd->RfIcType == RFIC_2020) ||
(pAd->RfIcType == RFIC_3021) || (pAd->RfIcType == RFIC_3022)))
#endif
#ifndef RT30xx
if (IS_RT3070(pAd) && ((pAd->RfIcType == RFIC_3020) || (pAd->RfIcType == RFIC_2020)))
#endif
{
/* modify by WY for Read RF Reg. error */
UCHAR RFValue;
for (index = 0; index < NUM_OF_3020_CHNL; index++)
{
if (Channel == FreqItems3020[index].Channel)
{
// Programming channel parameters
RT30xxWriteRFRegister(pAd, RF_R02, FreqItems3020[index].N);
RT30xxWriteRFRegister(pAd, RF_R03, FreqItems3020[index].K);
#ifndef RT30xx
RT30xxReadRFRegister(pAd, RF_R06, (PUCHAR)&RFValue);
RFValue = (RFValue & 0xFC) | FreqItems3020[index].R;
RT30xxWriteRFRegister(pAd, RF_R06, (UCHAR)RFValue);
// Set Tx Power
RT30xxReadRFRegister(pAd, RF_R12, (PUCHAR)&RFValue);
RFValue = (RFValue & 0xE0) | TxPwer;
RT30xxWriteRFRegister(pAd, RF_R12, (UCHAR)RFValue);
// Set RF offset
RT30xxReadRFRegister(pAd, RF_R23, (PUCHAR)&RFValue);
RFValue = (RFValue & 0x80) | pAd->RfFreqOffset;
RT30xxWriteRFRegister(pAd, RF_R23, (UCHAR)RFValue);
#endif
#ifdef RT30xx
RT30xxReadRFRegister(pAd, RF_R06, &RFValue);
RFValue = (RFValue & 0xFC) | FreqItems3020[index].R;
RT30xxWriteRFRegister(pAd, RF_R06, RFValue);
// Set Tx0 Power
RT30xxReadRFRegister(pAd, RF_R12, &RFValue);
RFValue = (RFValue & 0xE0) | TxPwer;
RT30xxWriteRFRegister(pAd, RF_R12, RFValue);
// Set Tx1 Power
RT30xxReadRFRegister(pAd, RF_R13, &RFValue);
RFValue = (RFValue & 0xE0) | TxPwer2;
RT30xxWriteRFRegister(pAd, RF_R13, RFValue);
// Tx/Rx Stream setting
RT30xxReadRFRegister(pAd, RF_R01, &RFValue);
//if (IS_RT3090(pAd))
// RFValue |= 0x01; // Enable RF block.
RFValue &= 0x03; //clear bit[7~2]
if (pAd->Antenna.field.TxPath == 1)
RFValue |= 0xA0;
else if (pAd->Antenna.field.TxPath == 2)
RFValue |= 0x80;
if (pAd->Antenna.field.RxPath == 1)
RFValue |= 0x50;
else if (pAd->Antenna.field.RxPath == 2)
RFValue |= 0x40;
RT30xxWriteRFRegister(pAd, RF_R01, RFValue);
// Set RF offset
RT30xxReadRFRegister(pAd, RF_R23, &RFValue);
RFValue = (RFValue & 0x80) | pAd->RfFreqOffset;
RT30xxWriteRFRegister(pAd, RF_R23, RFValue);
#endif
// Set BW
if (!bScan && (pAd->CommonCfg.BBPCurrentBW == BW_40))
{
RFValue = pAd->Mlme.CaliBW40RfR24;
//DISABLE_11N_CHECK(pAd);
}
else
{
RFValue = pAd->Mlme.CaliBW20RfR24;
}
#ifndef RT30xx
RT30xxWriteRFRegister(pAd, RF_R24, (UCHAR)RFValue);
// Enable RF tuning
RT30xxReadRFRegister(pAd, RF_R07, (PUCHAR)&RFValue);
RFValue = RFValue | 0x1;
RT30xxWriteRFRegister(pAd, RF_R07, (UCHAR)RFValue);
// latch channel for future usage.
pAd->LatchRfRegs.Channel = Channel;
#endif
#ifdef RT30xx
RT30xxWriteRFRegister(pAd, RF_R24, RFValue);
RT30xxWriteRFRegister(pAd, RF_R31, RFValue);
// Enable RF tuning
RT30xxReadRFRegister(pAd, RF_R07, &RFValue);
RFValue = RFValue | 0x1;
RT30xxWriteRFRegister(pAd, RF_R07, RFValue);
// latch channel for future usage.
pAd->LatchRfRegs.Channel = Channel;
DBGPRINT(RT_DEBUG_TRACE, ("SwitchChannel#%d(RF=%d, Pwr0=%d, Pwr1=%d, %dT), N=0x%02X, K=0x%02X, R=0x%02X\n",
Channel,
pAd->RfIcType,
TxPwer,
TxPwer2,
pAd->Antenna.field.TxPath,
FreqItems3020[index].N,
FreqItems3020[index].K,
FreqItems3020[index].R));
#endif
break;
}
}
#ifndef RT30xx
DBGPRINT(RT_DEBUG_TRACE, ("SwitchChannel#%d(RF=%d, Pwr0=%d, Pwr1=%d, %dT), N=0x%02X, K=0x%02X, R=0x%02X\n",
Channel,
pAd->RfIcType,
TxPwer,
TxPwer2,
pAd->Antenna.field.TxPath,
FreqItems3020[index].N,
FreqItems3020[index].K,
FreqItems3020[index].R));
#endif
}
else
#endif // RT2870 //
{
RFRegTable = RF2850RegTable;
switch (pAd->RfIcType)
{
case RFIC_2820:
case RFIC_2850:
case RFIC_2720:
case RFIC_2750:
for (index = 0; index < NUM_OF_2850_CHNL; index++)
{
if (Channel == RFRegTable[index].Channel)
{
R2 = RFRegTable[index].R2;
if (pAd->Antenna.field.TxPath == 1)
{
R2 |= 0x4000; // If TXpath is 1, bit 14 = 1;
}
if (pAd->Antenna.field.RxPath == 2)
{
R2 |= 0x40; // write 1 to off Rxpath.
}
else if (pAd->Antenna.field.RxPath == 1)
{
R2 |= 0x20040; // write 1 to off RxPath
}
if (Channel > 14)
{
// initialize R3, R4
R3 = (RFRegTable[index].R3 & 0xffffc1ff);
R4 = (RFRegTable[index].R4 & (~0x001f87c0)) | (pAd->RfFreqOffset << 15);
// 5G band power range: 0xF9~0X0F, TX0 Reg3 bit9/TX1 Reg4 bit6="0" means the TX power reduce 7dB
// R3
if ((TxPwer >= -7) && (TxPwer < 0))
{
TxPwer = (7+TxPwer);
TxPwer = (TxPwer > 0xF) ? (0xF) : (TxPwer);
R3 |= (TxPwer << 10);
DBGPRINT(RT_DEBUG_ERROR, ("AsicSwitchChannel: TxPwer=%d \n", TxPwer));
}
else
{
TxPwer = (TxPwer > 0xF) ? (0xF) : (TxPwer);
R3 |= (TxPwer << 10) | (1 << 9);
}
// R4
if ((TxPwer2 >= -7) && (TxPwer2 < 0))
{
TxPwer2 = (7+TxPwer2);
TxPwer2 = (TxPwer2 > 0xF) ? (0xF) : (TxPwer2);
R4 |= (TxPwer2 << 7);
DBGPRINT(RT_DEBUG_ERROR, ("AsicSwitchChannel: TxPwer2=%d \n", TxPwer2));
}
else
{
TxPwer2 = (TxPwer2 > 0xF) ? (0xF) : (TxPwer2);
R4 |= (TxPwer2 << 7) | (1 << 6);
}
}
else
{
R3 = (RFRegTable[index].R3 & 0xffffc1ff) | (TxPwer << 9); // set TX power0
R4 = (RFRegTable[index].R4 & (~0x001f87c0)) | (pAd->RfFreqOffset << 15) | (TxPwer2 <<6);// Set freq Offset & TxPwr1
}
// Based on BBP current mode before changing RF channel.
if (!bScan && (pAd->CommonCfg.BBPCurrentBW == BW_40))
{
R4 |=0x200000;
}
// Update variables
pAd->LatchRfRegs.Channel = Channel;
pAd->LatchRfRegs.R1 = RFRegTable[index].R1;
pAd->LatchRfRegs.R2 = R2;
pAd->LatchRfRegs.R3 = R3;
pAd->LatchRfRegs.R4 = R4;
// Set RF value 1's set R3[bit2] = [0]
RTMP_RF_IO_WRITE32(pAd, pAd->LatchRfRegs.R1);
RTMP_RF_IO_WRITE32(pAd, pAd->LatchRfRegs.R2);
RTMP_RF_IO_WRITE32(pAd, (pAd->LatchRfRegs.R3 & (~0x04)));
RTMP_RF_IO_WRITE32(pAd, pAd->LatchRfRegs.R4);
RTMPusecDelay(200);
// Set RF value 2's set R3[bit2] = [1]
RTMP_RF_IO_WRITE32(pAd, pAd->LatchRfRegs.R1);
RTMP_RF_IO_WRITE32(pAd, pAd->LatchRfRegs.R2);
RTMP_RF_IO_WRITE32(pAd, (pAd->LatchRfRegs.R3 | 0x04));
RTMP_RF_IO_WRITE32(pAd, pAd->LatchRfRegs.R4);
RTMPusecDelay(200);
// Set RF value 3's set R3[bit2] = [0]
RTMP_RF_IO_WRITE32(pAd, pAd->LatchRfRegs.R1);
RTMP_RF_IO_WRITE32(pAd, pAd->LatchRfRegs.R2);
RTMP_RF_IO_WRITE32(pAd, (pAd->LatchRfRegs.R3 & (~0x04)));
RTMP_RF_IO_WRITE32(pAd, pAd->LatchRfRegs.R4);
break;
}
}
break;
default:
break;
}
}
// Change BBP setting during siwtch from a->g, g->a
if (Channel <= 14)
{
ULONG TxPinCfg = 0x00050F0A;//Gary 2007/08/09 0x050A0A
RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R62, (0x37 - GET_LNA_GAIN(pAd)));
RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R63, (0x37 - GET_LNA_GAIN(pAd)));
RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R64, (0x37 - GET_LNA_GAIN(pAd)));
RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R86, 0);//(0x44 - GET_LNA_GAIN(pAd))); // According the Rory's suggestion to solve the middle range issue.
//RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R82, 0x62);
// Rx High power VGA offset for LNA select
if (pAd->NicConfig2.field.ExternalLNAForG)
{
RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R82, 0x62);
RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R75, 0x46);
}
else
{
RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R82, 0x84);
RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R75, 0x50);
}
// 5G band selection PIN, bit1 and bit2 are complement
RTMP_IO_READ32(pAd, TX_BAND_CFG, &Value);
Value &= (~0x6);
Value |= (0x04);
RTMP_IO_WRITE32(pAd, TX_BAND_CFG, Value);
// Turn off unused PA or LNA when only 1T or 1R
if (pAd->Antenna.field.TxPath == 1)
{
TxPinCfg &= 0xFFFFFFF3;
}
if (pAd->Antenna.field.RxPath == 1)
{
TxPinCfg &= 0xFFFFF3FF;
}
RTMP_IO_WRITE32(pAd, TX_PIN_CFG, TxPinCfg);
}
else
{
ULONG TxPinCfg = 0x00050F05;//Gary 2007/8/9 0x050505
RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R62, (0x37 - GET_LNA_GAIN(pAd)));
RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R63, (0x37 - GET_LNA_GAIN(pAd)));
RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R64, (0x37 - GET_LNA_GAIN(pAd)));
RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R86, 0);//(0x44 - GET_LNA_GAIN(pAd))); // According the Rory's suggestion to solve the middle range issue.
RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R82, 0xF2);
// Rx High power VGA offset for LNA select
if (pAd->NicConfig2.field.ExternalLNAForA)
{
RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R75, 0x46);
}
else
{
RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R75, 0x50);
}
// 5G band selection PIN, bit1 and bit2 are complement
RTMP_IO_READ32(pAd, TX_BAND_CFG, &Value);
Value &= (~0x6);
Value |= (0x02);
RTMP_IO_WRITE32(pAd, TX_BAND_CFG, Value);
// Turn off unused PA or LNA when only 1T or 1R
if (pAd->Antenna.field.TxPath == 1)
{
TxPinCfg &= 0xFFFFFFF3;
}
if (pAd->Antenna.field.RxPath == 1)
{
TxPinCfg &= 0xFFFFF3FF;
}
RTMP_IO_WRITE32(pAd, TX_PIN_CFG, TxPinCfg);
}
// R66 should be set according to Channel and use 20MHz when scanning
//RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R66, (0x2E + GET_LNA_GAIN(pAd)));
if (bScan)
RTMPSetAGCInitValue(pAd, BW_20);
else
RTMPSetAGCInitValue(pAd, pAd->CommonCfg.BBPCurrentBW);
//
// On 11A, We should delay and wait RF/BBP to be stable
// and the appropriate time should be 1000 micro seconds
// 2005/06/05 - On 11G, We also need this delay time. Otherwise it's difficult to pass the WHQL.
//
RTMPusecDelay(1000);
DBGPRINT(RT_DEBUG_TRACE, ("SwitchChannel#%d(RF=%d, Pwr0=%lu, Pwr1=%lu, %dT) to , R1=0x%08lx, R2=0x%08lx, R3=0x%08lx, R4=0x%08lx\n",
Channel,
pAd->RfIcType,
(R3 & 0x00003e00) >> 9,
(R4 & 0x000007c0) >> 6,
pAd->Antenna.field.TxPath,
pAd->LatchRfRegs.R1,
pAd->LatchRfRegs.R2,
pAd->LatchRfRegs.R3,
pAd->LatchRfRegs.R4));
}
/*
==========================================================================
Description:
This function is required for 2421 only, and should not be used during
site survey. It's only required after NIC decided to stay at a channel
for a longer period.
When this function is called, it's always after AsicSwitchChannel().
IRQL = PASSIVE_LEVEL
IRQL = DISPATCH_LEVEL
==========================================================================
*/
VOID AsicLockChannel(
IN PRTMP_ADAPTER pAd,
IN UCHAR Channel)
{
}
/*
==========================================================================
Description:
IRQL = PASSIVE_LEVEL
IRQL = DISPATCH_LEVEL
==========================================================================
*/
VOID AsicAntennaSelect(
IN PRTMP_ADAPTER pAd,
IN UCHAR Channel)
{
#ifdef RT30xx
if (pAd->Mlme.OneSecPeriodicRound % 2 == 1)
{
// patch for AsicSetRxAnt failed
pAd->RxAnt.EvaluatePeriod = 0;
// check every 2 second. If rcv-beacon less than 5 in the past 2 second, then AvgRSSI is no longer a
// valid indication of the distance between this AP and its clients.
if (OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_MEDIA_STATE_CONNECTED))
{
SHORT realavgrssi1;
// if no traffic then reset average rssi to trigger evaluation
if (pAd->StaCfg.NumOfAvgRssiSample < 5)
{
pAd->RxAnt.Pair1LastAvgRssi = (-99);
pAd->RxAnt.Pair2LastAvgRssi = (-99);
DBGPRINT(RT_DEBUG_TRACE, ("MlmePeriodicExec: no traffic/beacon, reset RSSI\n"));
}
pAd->StaCfg.NumOfAvgRssiSample = 0;
realavgrssi1 = (pAd->RxAnt.Pair1AvgRssi[pAd->RxAnt.Pair1PrimaryRxAnt] >> 3);
DBGPRINT(RT_DEBUG_TRACE,("Ant-realrssi0(%d), Lastrssi0(%d), EvaluateStableCnt=%d\n", realavgrssi1, pAd->RxAnt.Pair1LastAvgRssi, pAd->RxAnt.EvaluateStableCnt));
// if the difference between two rssi is larger or less than 5, then evaluate the other antenna
if ((pAd->RxAnt.EvaluateStableCnt < 2) || (realavgrssi1 > (pAd->RxAnt.Pair1LastAvgRssi + 5)) || (realavgrssi1 < (pAd->RxAnt.Pair1LastAvgRssi - 5)))
{
pAd->RxAnt.Pair1LastAvgRssi = realavgrssi1;
AsicEvaluateRxAnt(pAd);
}
}
else
{
// if not connected, always switch antenna to try to connect
UCHAR temp;
temp = pAd->RxAnt.Pair1PrimaryRxAnt;
pAd->RxAnt.Pair1PrimaryRxAnt = pAd->RxAnt.Pair1SecondaryRxAnt;
pAd->RxAnt.Pair1SecondaryRxAnt = temp;
DBGPRINT(RT_DEBUG_TRACE, ("MlmePeriodicExec: no connect, switch to another one to try connection\n"));
AsicSetRxAnt(pAd, pAd->RxAnt.Pair1PrimaryRxAnt);
}
}
#endif /* RT30xx */
}
/*
========================================================================
Routine Description:
Antenna miscellaneous setting.
Arguments:
pAd Pointer to our adapter
BandState Indicate current Band State.
Return Value:
None
IRQL <= DISPATCH_LEVEL
Note:
1.) Frame End type control
only valid for G only (RF_2527 & RF_2529)
0: means DPDT, set BBP R4 bit 5 to 1
1: means SPDT, set BBP R4 bit 5 to 0
========================================================================
*/
VOID AsicAntennaSetting(
IN PRTMP_ADAPTER pAd,
IN ABGBAND_STATE BandState)
{
}
VOID AsicRfTuningExec(
IN PVOID SystemSpecific1,
IN PVOID FunctionContext,
IN PVOID SystemSpecific2,
IN PVOID SystemSpecific3)
{
}
/*
==========================================================================
Description:
Gives CCK TX rate 2 more dB TX power.
This routine works only in LINK UP in INFRASTRUCTURE mode.
calculate desired Tx power in RF R3.Tx0~5, should consider -
0. if current radio is a noisy environment (pAd->DrsCounters.fNoisyEnvironment)
1. TxPowerPercentage
2. auto calibration based on TSSI feedback
3. extra 2 db for CCK
4. -10 db upon very-short distance (AvgRSSI >= -40db) to AP
NOTE: Since this routine requires the value of (pAd->DrsCounters.fNoisyEnvironment),
it should be called AFTER MlmeDynamicTxRatSwitching()
==========================================================================
*/
VOID AsicAdjustTxPower(
IN PRTMP_ADAPTER pAd)
{
INT i, j;
CHAR DeltaPwr = 0;
BOOLEAN bAutoTxAgc = FALSE;
UCHAR TssiRef, *pTssiMinusBoundary, *pTssiPlusBoundary, TxAgcStep;
UCHAR BbpR1 = 0, BbpR49 = 0, idx;
PCHAR pTxAgcCompensate;
ULONG TxPwr[5];
CHAR Value;
#ifdef RT2860
if (OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_DOZE)
|| (pAd->bPCIclkOff == TRUE)
|| RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_IDLE_RADIO_OFF)
|| RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS))
return;
#endif
if (pAd->CommonCfg.BBPCurrentBW == BW_40)
{
if (pAd->CommonCfg.CentralChannel > 14)
{
TxPwr[0] = pAd->Tx40MPwrCfgABand[0];
TxPwr[1] = pAd->Tx40MPwrCfgABand[1];
TxPwr[2] = pAd->Tx40MPwrCfgABand[2];
TxPwr[3] = pAd->Tx40MPwrCfgABand[3];
TxPwr[4] = pAd->Tx40MPwrCfgABand[4];
}
else
{
TxPwr[0] = pAd->Tx40MPwrCfgGBand[0];
TxPwr[1] = pAd->Tx40MPwrCfgGBand[1];
TxPwr[2] = pAd->Tx40MPwrCfgGBand[2];
TxPwr[3] = pAd->Tx40MPwrCfgGBand[3];
TxPwr[4] = pAd->Tx40MPwrCfgGBand[4];
}
}
else
{
if (pAd->CommonCfg.Channel > 14)
{
TxPwr[0] = pAd->Tx20MPwrCfgABand[0];
TxPwr[1] = pAd->Tx20MPwrCfgABand[1];
TxPwr[2] = pAd->Tx20MPwrCfgABand[2];
TxPwr[3] = pAd->Tx20MPwrCfgABand[3];
TxPwr[4] = pAd->Tx20MPwrCfgABand[4];
}
else
{
TxPwr[0] = pAd->Tx20MPwrCfgGBand[0];
TxPwr[1] = pAd->Tx20MPwrCfgGBand[1];
TxPwr[2] = pAd->Tx20MPwrCfgGBand[2];
TxPwr[3] = pAd->Tx20MPwrCfgGBand[3];
TxPwr[4] = pAd->Tx20MPwrCfgGBand[4];
}
}
// TX power compensation for temperature variation based on TSSI. try every 4 second
if (pAd->Mlme.OneSecPeriodicRound % 4 == 0)
{
if (pAd->CommonCfg.Channel <= 14)
{
/* bg channel */
bAutoTxAgc = pAd->bAutoTxAgcG;
TssiRef = pAd->TssiRefG;
pTssiMinusBoundary = &pAd->TssiMinusBoundaryG[0];
pTssiPlusBoundary = &pAd->TssiPlusBoundaryG[0];
TxAgcStep = pAd->TxAgcStepG;
pTxAgcCompensate = &pAd->TxAgcCompensateG;
}
else
{
/* a channel */
bAutoTxAgc = pAd->bAutoTxAgcA;
TssiRef = pAd->TssiRefA;
pTssiMinusBoundary = &pAd->TssiMinusBoundaryA[0];
pTssiPlusBoundary = &pAd->TssiPlusBoundaryA[0];
TxAgcStep = pAd->TxAgcStepA;
pTxAgcCompensate = &pAd->TxAgcCompensateA;
}
if (bAutoTxAgc)
{
/* BbpR1 is unsigned char */
RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R49, &BbpR49);
/* (p) TssiPlusBoundaryG[0] = 0 = (m) TssiMinusBoundaryG[0] */
/* compensate: +4 +3 +2 +1 0 -1 -2 -3 -4 * steps */
/* step value is defined in pAd->TxAgcStepG for tx power value */
/* [4]+1+[4] p4 p3 p2 p1 o1 m1 m2 m3 m4 */
/* ex: 0x00 0x15 0x25 0x45 0x88 0xA0 0xB5 0xD0 0xF0
above value are examined in mass factory production */
/* [4] [3] [2] [1] [0] [1] [2] [3] [4] */
/* plus (+) is 0x00 ~ 0x45, minus (-) is 0xa0 ~ 0xf0 */
/* if value is between p1 ~ o1 or o1 ~ s1, no need to adjust tx power */
/* if value is 0xa5, tx power will be -= TxAgcStep*(2-1) */
if (BbpR49 > pTssiMinusBoundary[1])
{
// Reading is larger than the reference value
// check for how large we need to decrease the Tx power
for (idx = 1; idx < 5; idx++)
{
if (BbpR49 <= pTssiMinusBoundary[idx]) // Found the range
break;
}
// The index is the step we should decrease, idx = 0 means there is nothing to compensate
*pTxAgcCompensate = -(TxAgcStep * (idx-1));
DeltaPwr += (*pTxAgcCompensate);
DBGPRINT(RT_DEBUG_TRACE, ("-- Tx Power, BBP R1=%x, TssiRef=%x, TxAgcStep=%x, step = -%d\n",
BbpR49, TssiRef, TxAgcStep, idx-1));
}
else if (BbpR49 < pTssiPlusBoundary[1])
{
// Reading is smaller than the reference value
// check for how large we need to increase the Tx power
for (idx = 1; idx < 5; idx++)
{
if (BbpR49 >= pTssiPlusBoundary[idx]) // Found the range
break;
}
// The index is the step we should increase, idx = 0 means there is nothing to compensate
*pTxAgcCompensate = TxAgcStep * (idx-1);
DeltaPwr += (*pTxAgcCompensate);
DBGPRINT(RT_DEBUG_TRACE, ("++ Tx Power, BBP R1=%x, TssiRef=%x, TxAgcStep=%x, step = +%d\n",
BbpR49, TssiRef, TxAgcStep, idx-1));
}
else
{
*pTxAgcCompensate = 0;
DBGPRINT(RT_DEBUG_TRACE, (" Tx Power, BBP R49=%x, TssiRef=%x, TxAgcStep=%x, step = +%d\n",
BbpR49, TssiRef, TxAgcStep, 0));
}
}
}
else
{
if (pAd->CommonCfg.Channel <= 14)
{
bAutoTxAgc = pAd->bAutoTxAgcG;
pTxAgcCompensate = &pAd->TxAgcCompensateG;
}
else
{
bAutoTxAgc = pAd->bAutoTxAgcA;
pTxAgcCompensate = &pAd->TxAgcCompensateA;
}
if (bAutoTxAgc)
DeltaPwr += (*pTxAgcCompensate);
}
RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R1, &BbpR1);
BbpR1 &= 0xFC;
/* calculate delta power based on the percentage specified from UI */
// E2PROM setting is calibrated for maximum TX power (i.e. 100%)
// We lower TX power here according to the percentage specified from UI
if (pAd->CommonCfg.TxPowerPercentage == 0xffffffff) // AUTO TX POWER control
;
else if (pAd->CommonCfg.TxPowerPercentage > 90) // 91 ~ 100% & AUTO, treat as 100% in terms of mW
;
else if (pAd->CommonCfg.TxPowerPercentage > 60) // 61 ~ 90%, treat as 75% in terms of mW // DeltaPwr -= 1;
{
DeltaPwr -= 1;
}
else if (pAd->CommonCfg.TxPowerPercentage > 30) // 31 ~ 60%, treat as 50% in terms of mW // DeltaPwr -= 3;
{
DeltaPwr -= 3;
}
else if (pAd->CommonCfg.TxPowerPercentage > 15) // 16 ~ 30%, treat as 25% in terms of mW // DeltaPwr -= 6;
{
BbpR1 |= 0x01;
}
else if (pAd->CommonCfg.TxPowerPercentage > 9) // 10 ~ 15%, treat as 12.5% in terms of mW // DeltaPwr -= 9;
{
BbpR1 |= 0x01;
DeltaPwr -= 3;
}
else // 0 ~ 9 %, treat as MIN(~3%) in terms of mW // DeltaPwr -= 12;
{
BbpR1 |= 0x02;
}
RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R1, BbpR1);
/* reset different new tx power for different TX rate */
for(i=0; i<5; i++)
{
if (TxPwr[i] != 0xffffffff)
{
for (j=0; j<8; j++)
{
Value = (CHAR)((TxPwr[i] >> j*4) & 0x0F); /* 0 ~ 15 */
if ((Value + DeltaPwr) < 0)
{
Value = 0; /* min */
}
else if ((Value + DeltaPwr) > 0xF)
{
Value = 0xF; /* max */
}
else
{
Value += DeltaPwr; /* temperature compensation */
}
/* fill new value to CSR offset */
TxPwr[i] = (TxPwr[i] & ~(0x0000000F << j*4)) | (Value << j*4);
}
/* write tx power value to CSR */
/* TX_PWR_CFG_0 (8 tx rate) for TX power for OFDM 12M/18M
TX power for OFDM 6M/9M
TX power for CCK5.5M/11M
TX power for CCK1M/2M */
/* TX_PWR_CFG_1 ~ TX_PWR_CFG_4 */
RTMP_IO_WRITE32(pAd, TX_PWR_CFG_0 + i*4, TxPwr[i]);
}
}
}
/*
==========================================================================
Description:
put PHY to sleep here, and set next wakeup timer. PHY doesn't not wakeup
automatically. Instead, MCU will issue a TwakeUpInterrupt to host after
the wakeup timer timeout. Driver has to issue a separate command to wake
PHY up.
IRQL = DISPATCH_LEVEL
==========================================================================
*/
VOID AsicSleepThenAutoWakeup(
IN PRTMP_ADAPTER pAd,
IN USHORT TbttNumToNextWakeUp)
{
RT28XX_STA_SLEEP_THEN_AUTO_WAKEUP(pAd, TbttNumToNextWakeUp);
}
/*
==========================================================================
Description:
AsicForceWakeup() is used whenever manual wakeup is required
AsicForceSleep() should only be used when not in INFRA BSS. When
in INFRA BSS, we should use AsicSleepThenAutoWakeup() instead.
==========================================================================
*/
VOID AsicForceSleep(
IN PRTMP_ADAPTER pAd)
{
}
/*
==========================================================================
Description:
AsicForceWakeup() is used whenever Twakeup timer (set via AsicSleepThenAutoWakeup)
expired.
IRQL = PASSIVE_LEVEL
IRQL = DISPATCH_LEVEL
==========================================================================
*/
VOID AsicForceWakeup(
IN PRTMP_ADAPTER pAd,
#ifdef RT2860
IN UCHAR Level)
#endif
#ifdef RT2870
IN BOOLEAN bFromTx)
#endif
{
DBGPRINT(RT_DEBUG_TRACE, ("--> AsicForceWakeup \n"));
#ifdef RT2860
RT28XX_STA_FORCE_WAKEUP(pAd, Level);
#endif
#ifdef RT2870
RT28XX_STA_FORCE_WAKEUP(pAd, bFromTx);
#endif
}
/*
==========================================================================
Description:
Set My BSSID
IRQL = DISPATCH_LEVEL
==========================================================================
*/
VOID AsicSetBssid(
IN PRTMP_ADAPTER pAd,
IN PUCHAR pBssid)
{
ULONG Addr4;
DBGPRINT(RT_DEBUG_TRACE, ("==============> AsicSetBssid %x:%x:%x:%x:%x:%x\n",
pBssid[0],pBssid[1],pBssid[2],pBssid[3], pBssid[4],pBssid[5]));
Addr4 = (ULONG)(pBssid[0]) |
(ULONG)(pBssid[1] << 8) |
(ULONG)(pBssid[2] << 16) |
(ULONG)(pBssid[3] << 24);
RTMP_IO_WRITE32(pAd, MAC_BSSID_DW0, Addr4);
Addr4 = 0;
// always one BSSID in STA mode
Addr4 = (ULONG)(pBssid[4]) | (ULONG)(pBssid[5] << 8);
RTMP_IO_WRITE32(pAd, MAC_BSSID_DW1, Addr4);
}
VOID AsicSetMcastWC(
IN PRTMP_ADAPTER pAd)
{
MAC_TABLE_ENTRY *pEntry = &pAd->MacTab.Content[MCAST_WCID];
USHORT offset;
pEntry->Sst = SST_ASSOC;
pEntry->Aid = MCAST_WCID; // Softap supports 1 BSSID and use WCID=0 as multicast Wcid index
pEntry->PsMode = PWR_ACTIVE;
pEntry->CurrTxRate = pAd->CommonCfg.MlmeRate;
offset = MAC_WCID_BASE + BSS0Mcast_WCID * HW_WCID_ENTRY_SIZE;
}
/*
==========================================================================
Description:
IRQL = DISPATCH_LEVEL
==========================================================================
*/
VOID AsicDelWcidTab(
IN PRTMP_ADAPTER pAd,
IN UCHAR Wcid)
{
ULONG Addr0 = 0x0, Addr1 = 0x0;
ULONG offset;
DBGPRINT(RT_DEBUG_TRACE, ("AsicDelWcidTab==>Wcid = 0x%x\n",Wcid));
offset = MAC_WCID_BASE + Wcid * HW_WCID_ENTRY_SIZE;
RTMP_IO_WRITE32(pAd, offset, Addr0);
offset += 4;
RTMP_IO_WRITE32(pAd, offset, Addr1);
}
/*
==========================================================================
Description:
IRQL = DISPATCH_LEVEL
==========================================================================
*/
VOID AsicEnableRDG(
IN PRTMP_ADAPTER pAd)
{
TX_LINK_CFG_STRUC TxLinkCfg;
UINT32 Data = 0;
RTMP_IO_READ32(pAd, TX_LINK_CFG, &TxLinkCfg.word);
TxLinkCfg.field.TxRDGEn = 1;
RTMP_IO_WRITE32(pAd, TX_LINK_CFG, TxLinkCfg.word);
RTMP_IO_READ32(pAd, EDCA_AC0_CFG, &Data);
Data &= 0xFFFFFF00;
Data |= 0x80;
RTMP_IO_WRITE32(pAd, EDCA_AC0_CFG, Data);
//OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_AGGREGATION_INUSED);
}
/*
==========================================================================
Description:
IRQL = DISPATCH_LEVEL
==========================================================================
*/
VOID AsicDisableRDG(
IN PRTMP_ADAPTER pAd)
{
TX_LINK_CFG_STRUC TxLinkCfg;
UINT32 Data = 0;
RTMP_IO_READ32(pAd, TX_LINK_CFG, &TxLinkCfg.word);
TxLinkCfg.field.TxRDGEn = 0;
RTMP_IO_WRITE32(pAd, TX_LINK_CFG, TxLinkCfg.word);
RTMP_IO_READ32(pAd, EDCA_AC0_CFG, &Data);
Data &= 0xFFFFFF00;
if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_DYNAMIC_BE_TXOP_ACTIVE)
&& (pAd->MacTab.fAnyStationMIMOPSDynamic == FALSE)
)
{
// For CWC test, change txop from 0x30 to 0x20 in TxBurst mode
if (pAd->CommonCfg.bEnableTxBurst)
Data |= 0x20;
}
RTMP_IO_WRITE32(pAd, EDCA_AC0_CFG, Data);
}
/*
==========================================================================
Description:
IRQL = PASSIVE_LEVEL
IRQL = DISPATCH_LEVEL
==========================================================================
*/
VOID AsicDisableSync(
IN PRTMP_ADAPTER pAd)
{
BCN_TIME_CFG_STRUC csr;
DBGPRINT(RT_DEBUG_TRACE, ("--->Disable TSF synchronization\n"));
// 2003-12-20 disable TSF and TBTT while NIC in power-saving have side effect
// that NIC will never wakes up because TSF stops and no more
// TBTT interrupts
pAd->TbttTickCount = 0;
RTMP_IO_READ32(pAd, BCN_TIME_CFG, &csr.word);
csr.field.bBeaconGen = 0;
csr.field.bTBTTEnable = 0;
csr.field.TsfSyncMode = 0;
csr.field.bTsfTicking = 0;
RTMP_IO_WRITE32(pAd, BCN_TIME_CFG, csr.word);
}
/*
==========================================================================
Description:
IRQL = DISPATCH_LEVEL
==========================================================================
*/
VOID AsicEnableBssSync(
IN PRTMP_ADAPTER pAd)
{
BCN_TIME_CFG_STRUC csr;
DBGPRINT(RT_DEBUG_TRACE, ("--->AsicEnableBssSync(INFRA mode)\n"));
RTMP_IO_READ32(pAd, BCN_TIME_CFG, &csr.word);
{
csr.field.BeaconInterval = pAd->CommonCfg.BeaconPeriod << 4; // ASIC register in units of 1/16 TU
csr.field.bTsfTicking = 1;
csr.field.TsfSyncMode = 1; // sync TSF in INFRASTRUCTURE mode
csr.field.bBeaconGen = 0; // do NOT generate BEACON
csr.field.bTBTTEnable = 1;
}
RTMP_IO_WRITE32(pAd, BCN_TIME_CFG, csr.word);
}
/*
==========================================================================
Description:
Note:
BEACON frame in shared memory should be built ok before this routine
can be called. Otherwise, a garbage frame maybe transmitted out every
Beacon period.
IRQL = DISPATCH_LEVEL
==========================================================================
*/
VOID AsicEnableIbssSync(
IN PRTMP_ADAPTER pAd)
{
BCN_TIME_CFG_STRUC csr9;
PUCHAR ptr;
UINT i;
DBGPRINT(RT_DEBUG_TRACE, ("--->AsicEnableIbssSync(ADHOC mode. MPDUtotalByteCount = %d)\n", pAd->BeaconTxWI.MPDUtotalByteCount));
RTMP_IO_READ32(pAd, BCN_TIME_CFG, &csr9.word);
csr9.field.bBeaconGen = 0;
csr9.field.bTBTTEnable = 0;
csr9.field.bTsfTicking = 0;
RTMP_IO_WRITE32(pAd, BCN_TIME_CFG, csr9.word);
#ifdef RT2860
// move BEACON TXD and frame content to on-chip memory
ptr = (PUCHAR)&pAd->BeaconTxWI;
for (i=0; i<TXWI_SIZE; i+=4) // 16-byte TXWI field
{
UINT32 longptr = *ptr + (*(ptr+1)<<8) + (*(ptr+2)<<16) + (*(ptr+3)<<24);
RTMP_IO_WRITE32(pAd, HW_BEACON_BASE0 + i, longptr);
ptr += 4;
}
// start right after the 16-byte TXWI field
ptr = pAd->BeaconBuf;
for (i=0; i< pAd->BeaconTxWI.MPDUtotalByteCount; i+=4)
{
UINT32 longptr = *ptr + (*(ptr+1)<<8) + (*(ptr+2)<<16) + (*(ptr+3)<<24);
RTMP_IO_WRITE32(pAd, HW_BEACON_BASE0 + TXWI_SIZE + i, longptr);
ptr +=4;
}
#endif
#ifdef RT2870
// move BEACON TXD and frame content to on-chip memory
ptr = (PUCHAR)&pAd->BeaconTxWI;
for (i=0; i<TXWI_SIZE; i+=2) // 16-byte TXWI field
{
RTUSBMultiWrite(pAd, HW_BEACON_BASE0 + i, ptr, 2);
ptr += 2;
}
// start right after the 16-byte TXWI field
ptr = pAd->BeaconBuf;
for (i=0; i< pAd->BeaconTxWI.MPDUtotalByteCount; i+=2)
{
RTUSBMultiWrite(pAd, HW_BEACON_BASE0 + TXWI_SIZE + i, ptr, 2);
ptr +=2;
}
#endif // RT2870 //
// start sending BEACON
csr9.field.BeaconInterval = pAd->CommonCfg.BeaconPeriod << 4; // ASIC register in units of 1/16 TU
csr9.field.bTsfTicking = 1;
csr9.field.TsfSyncMode = 2; // sync TSF in IBSS mode
csr9.field.bTBTTEnable = 1;
csr9.field.bBeaconGen = 1;
RTMP_IO_WRITE32(pAd, BCN_TIME_CFG, csr9.word);
}
/*
==========================================================================
Description:
IRQL = PASSIVE_LEVEL
IRQL = DISPATCH_LEVEL
==========================================================================
*/
VOID AsicSetEdcaParm(
IN PRTMP_ADAPTER pAd,
IN PEDCA_PARM pEdcaParm)
{
EDCA_AC_CFG_STRUC Ac0Cfg, Ac1Cfg, Ac2Cfg, Ac3Cfg;
AC_TXOP_CSR0_STRUC csr0;
AC_TXOP_CSR1_STRUC csr1;
AIFSN_CSR_STRUC AifsnCsr;
CWMIN_CSR_STRUC CwminCsr;
CWMAX_CSR_STRUC CwmaxCsr;
int i;
Ac0Cfg.word = 0;
Ac1Cfg.word = 0;
Ac2Cfg.word = 0;
Ac3Cfg.word = 0;
if ((pEdcaParm == NULL) || (pEdcaParm->bValid == FALSE))
{
DBGPRINT(RT_DEBUG_TRACE,("AsicSetEdcaParm\n"));
OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_WMM_INUSED);
for (i=0; i<MAX_LEN_OF_MAC_TABLE; i++)
{
if (pAd->MacTab.Content[i].ValidAsCLI || pAd->MacTab.Content[i].ValidAsApCli)
CLIENT_STATUS_CLEAR_FLAG(&pAd->MacTab.Content[i], fCLIENT_STATUS_WMM_CAPABLE);
}
//========================================================
// MAC Register has a copy .
//========================================================
if( pAd->CommonCfg.bEnableTxBurst )
{
// For CWC test, change txop from 0x30 to 0x20 in TxBurst mode
Ac0Cfg.field.AcTxop = 0x20; // Suggest by John for TxBurst in HT Mode
}
else
Ac0Cfg.field.AcTxop = 0; // QID_AC_BE
Ac0Cfg.field.Cwmin = CW_MIN_IN_BITS;
Ac0Cfg.field.Cwmax = CW_MAX_IN_BITS;
Ac0Cfg.field.Aifsn = 2;
RTMP_IO_WRITE32(pAd, EDCA_AC0_CFG, Ac0Cfg.word);
Ac1Cfg.field.AcTxop = 0; // QID_AC_BK
Ac1Cfg.field.Cwmin = CW_MIN_IN_BITS;
Ac1Cfg.field.Cwmax = CW_MAX_IN_BITS;
Ac1Cfg.field.Aifsn = 2;
RTMP_IO_WRITE32(pAd, EDCA_AC1_CFG, Ac1Cfg.word);
if (pAd->CommonCfg.PhyMode == PHY_11B)
{
Ac2Cfg.field.AcTxop = 192; // AC_VI: 192*32us ~= 6ms
Ac3Cfg.field.AcTxop = 96; // AC_VO: 96*32us ~= 3ms
}
else
{
Ac2Cfg.field.AcTxop = 96; // AC_VI: 96*32us ~= 3ms
Ac3Cfg.field.AcTxop = 48; // AC_VO: 48*32us ~= 1.5ms
}
Ac2Cfg.field.Cwmin = CW_MIN_IN_BITS;
Ac2Cfg.field.Cwmax = CW_MAX_IN_BITS;
Ac2Cfg.field.Aifsn = 2;
RTMP_IO_WRITE32(pAd, EDCA_AC2_CFG, Ac2Cfg.word);
Ac3Cfg.field.Cwmin = CW_MIN_IN_BITS;
Ac3Cfg.field.Cwmax = CW_MAX_IN_BITS;
Ac3Cfg.field.Aifsn = 2;
RTMP_IO_WRITE32(pAd, EDCA_AC3_CFG, Ac3Cfg.word);
//========================================================
// DMA Register has a copy too.
//========================================================
csr0.field.Ac0Txop = 0; // QID_AC_BE
csr0.field.Ac1Txop = 0; // QID_AC_BK
RTMP_IO_WRITE32(pAd, WMM_TXOP0_CFG, csr0.word);
if (pAd->CommonCfg.PhyMode == PHY_11B)
{
csr1.field.Ac2Txop = 192; // AC_VI: 192*32us ~= 6ms
csr1.field.Ac3Txop = 96; // AC_VO: 96*32us ~= 3ms
}
else
{
csr1.field.Ac2Txop = 96; // AC_VI: 96*32us ~= 3ms
csr1.field.Ac3Txop = 48; // AC_VO: 48*32us ~= 1.5ms
}
RTMP_IO_WRITE32(pAd, WMM_TXOP1_CFG, csr1.word);
CwminCsr.word = 0;
CwminCsr.field.Cwmin0 = CW_MIN_IN_BITS;
CwminCsr.field.Cwmin1 = CW_MIN_IN_BITS;
CwminCsr.field.Cwmin2 = CW_MIN_IN_BITS;
CwminCsr.field.Cwmin3 = CW_MIN_IN_BITS;
RTMP_IO_WRITE32(pAd, WMM_CWMIN_CFG, CwminCsr.word);
CwmaxCsr.word = 0;
CwmaxCsr.field.Cwmax0 = CW_MAX_IN_BITS;
CwmaxCsr.field.Cwmax1 = CW_MAX_IN_BITS;
CwmaxCsr.field.Cwmax2 = CW_MAX_IN_BITS;
CwmaxCsr.field.Cwmax3 = CW_MAX_IN_BITS;
RTMP_IO_WRITE32(pAd, WMM_CWMAX_CFG, CwmaxCsr.word);
RTMP_IO_WRITE32(pAd, WMM_AIFSN_CFG, 0x00002222);
NdisZeroMemory(&pAd->CommonCfg.APEdcaParm, sizeof(EDCA_PARM));
}
else
{
OPSTATUS_SET_FLAG(pAd, fOP_STATUS_WMM_INUSED);
//========================================================
// MAC Register has a copy.
//========================================================
//
// Modify Cwmin/Cwmax/Txop on queue[QID_AC_VI], Recommend by Jerry 2005/07/27
// To degrade our VIDO Queue's throughput for WiFi WMM S3T07 Issue.
//
//pEdcaParm->Txop[QID_AC_VI] = pEdcaParm->Txop[QID_AC_VI] * 7 / 10; // rt2860c need this
Ac0Cfg.field.AcTxop = pEdcaParm->Txop[QID_AC_BE];
Ac0Cfg.field.Cwmin= pEdcaParm->Cwmin[QID_AC_BE];
Ac0Cfg.field.Cwmax = pEdcaParm->Cwmax[QID_AC_BE];
Ac0Cfg.field.Aifsn = pEdcaParm->Aifsn[QID_AC_BE]; //+1;
Ac1Cfg.field.AcTxop = pEdcaParm->Txop[QID_AC_BK];
Ac1Cfg.field.Cwmin = pEdcaParm->Cwmin[QID_AC_BK]; //+2;
Ac1Cfg.field.Cwmax = pEdcaParm->Cwmax[QID_AC_BK];
Ac1Cfg.field.Aifsn = pEdcaParm->Aifsn[QID_AC_BK]; //+1;
Ac2Cfg.field.AcTxop = (pEdcaParm->Txop[QID_AC_VI] * 6) / 10;
Ac2Cfg.field.Cwmin = pEdcaParm->Cwmin[QID_AC_VI];
Ac2Cfg.field.Cwmax = pEdcaParm->Cwmax[QID_AC_VI];
Ac2Cfg.field.Aifsn = pEdcaParm->Aifsn[QID_AC_VI];
{
// Tuning for Wi-Fi WMM S06
if (pAd->CommonCfg.bWiFiTest &&
pEdcaParm->Aifsn[QID_AC_VI] == 10)
Ac2Cfg.field.Aifsn -= 1;
// Tuning for TGn Wi-Fi 5.2.32
// STA TestBed changes in this item: connexant legacy sta ==> broadcom 11n sta
if (STA_TGN_WIFI_ON(pAd) &&
pEdcaParm->Aifsn[QID_AC_VI] == 10)
{
Ac0Cfg.field.Aifsn = 3;
Ac2Cfg.field.AcTxop = 5;
}
#ifdef RT30xx
if (pAd->RfIcType == RFIC_3020 || pAd->RfIcType == RFIC_2020)
{
// Tuning for WiFi WMM S3-T07: connexant legacy sta ==> broadcom 11n sta.
Ac2Cfg.field.Aifsn = 5;
}
#endif // RT30xx //
}
Ac3Cfg.field.AcTxop = pEdcaParm->Txop[QID_AC_VO];
Ac3Cfg.field.Cwmin = pEdcaParm->Cwmin[QID_AC_VO];
Ac3Cfg.field.Cwmax = pEdcaParm->Cwmax[QID_AC_VO];
Ac3Cfg.field.Aifsn = pEdcaParm->Aifsn[QID_AC_VO];
//#ifdef WIFI_TEST
if (pAd->CommonCfg.bWiFiTest)
{
if (Ac3Cfg.field.AcTxop == 102)
{
Ac0Cfg.field.AcTxop = pEdcaParm->Txop[QID_AC_BE] ? pEdcaParm->Txop[QID_AC_BE] : 10;
Ac0Cfg.field.Aifsn = pEdcaParm->Aifsn[QID_AC_BE]-1; /* AIFSN must >= 1 */
Ac1Cfg.field.AcTxop = pEdcaParm->Txop[QID_AC_BK];
Ac1Cfg.field.Aifsn = pEdcaParm->Aifsn[QID_AC_BK];
Ac2Cfg.field.AcTxop = pEdcaParm->Txop[QID_AC_VI];
} /* End of if */
}
//#endif // WIFI_TEST //
RTMP_IO_WRITE32(pAd, EDCA_AC0_CFG, Ac0Cfg.word);
RTMP_IO_WRITE32(pAd, EDCA_AC1_CFG, Ac1Cfg.word);
RTMP_IO_WRITE32(pAd, EDCA_AC2_CFG, Ac2Cfg.word);
RTMP_IO_WRITE32(pAd, EDCA_AC3_CFG, Ac3Cfg.word);
//========================================================
// DMA Register has a copy too.
//========================================================
csr0.field.Ac0Txop = Ac0Cfg.field.AcTxop;
csr0.field.Ac1Txop = Ac1Cfg.field.AcTxop;
RTMP_IO_WRITE32(pAd, WMM_TXOP0_CFG, csr0.word);
csr1.field.Ac2Txop = Ac2Cfg.field.AcTxop;
csr1.field.Ac3Txop = Ac3Cfg.field.AcTxop;
RTMP_IO_WRITE32(pAd, WMM_TXOP1_CFG, csr1.word);
CwminCsr.word = 0;
CwminCsr.field.Cwmin0 = pEdcaParm->Cwmin[QID_AC_BE];
CwminCsr.field.Cwmin1 = pEdcaParm->Cwmin[QID_AC_BK];
CwminCsr.field.Cwmin2 = pEdcaParm->Cwmin[QID_AC_VI];
CwminCsr.field.Cwmin3 = pEdcaParm->Cwmin[QID_AC_VO] - 1; //for TGn wifi test
RTMP_IO_WRITE32(pAd, WMM_CWMIN_CFG, CwminCsr.word);
CwmaxCsr.word = 0;
CwmaxCsr.field.Cwmax0 = pEdcaParm->Cwmax[QID_AC_BE];
CwmaxCsr.field.Cwmax1 = pEdcaParm->Cwmax[QID_AC_BK];
CwmaxCsr.field.Cwmax2 = pEdcaParm->Cwmax[QID_AC_VI];
CwmaxCsr.field.Cwmax3 = pEdcaParm->Cwmax[QID_AC_VO];
RTMP_IO_WRITE32(pAd, WMM_CWMAX_CFG, CwmaxCsr.word);
AifsnCsr.word = 0;
AifsnCsr.field.Aifsn0 = Ac0Cfg.field.Aifsn; //pEdcaParm->Aifsn[QID_AC_BE];
AifsnCsr.field.Aifsn1 = Ac1Cfg.field.Aifsn; //pEdcaParm->Aifsn[QID_AC_BK];
AifsnCsr.field.Aifsn2 = Ac2Cfg.field.Aifsn; //pEdcaParm->Aifsn[QID_AC_VI];
{
// Tuning for Wi-Fi WMM S06
if (pAd->CommonCfg.bWiFiTest &&
pEdcaParm->Aifsn[QID_AC_VI] == 10)
AifsnCsr.field.Aifsn2 = Ac2Cfg.field.Aifsn - 4;
// Tuning for TGn Wi-Fi 5.2.32
// STA TestBed changes in this item: connexant legacy sta ==> broadcom 11n sta
if (STA_TGN_WIFI_ON(pAd) &&
pEdcaParm->Aifsn[QID_AC_VI] == 10)
{
AifsnCsr.field.Aifsn0 = 3;
AifsnCsr.field.Aifsn2 = 7;
}
#ifdef RT2870
if (INFRA_ON(pAd))
CLIENT_STATUS_SET_FLAG(&pAd->MacTab.Content[BSSID_WCID], fCLIENT_STATUS_WMM_CAPABLE);
#endif
}
AifsnCsr.field.Aifsn3 = Ac3Cfg.field.Aifsn - 1; //pEdcaParm->Aifsn[QID_AC_VO]; //for TGn wifi test
#ifdef RT30xx
if (pAd->RfIcType == RFIC_3020 || pAd->RfIcType == RFIC_2020)
AifsnCsr.field.Aifsn2 = 0x2; //pEdcaParm->Aifsn[QID_AC_VI]; //for WiFi WMM S4-T04.
#endif // RT30xx //
RTMP_IO_WRITE32(pAd, WMM_AIFSN_CFG, AifsnCsr.word);
NdisMoveMemory(&pAd->CommonCfg.APEdcaParm, pEdcaParm, sizeof(EDCA_PARM));
if (!ADHOC_ON(pAd))
{
DBGPRINT(RT_DEBUG_TRACE,("EDCA [#%d]: AIFSN CWmin CWmax TXOP(us) ACM\n", pEdcaParm->EdcaUpdateCount));
DBGPRINT(RT_DEBUG_TRACE,(" AC_BE %2d %2d %2d %4d %d\n",
pEdcaParm->Aifsn[0],
pEdcaParm->Cwmin[0],
pEdcaParm->Cwmax[0],
pEdcaParm->Txop[0]<<5,
pEdcaParm->bACM[0]));
DBGPRINT(RT_DEBUG_TRACE,(" AC_BK %2d %2d %2d %4d %d\n",
pEdcaParm->Aifsn[1],
pEdcaParm->Cwmin[1],
pEdcaParm->Cwmax[1],
pEdcaParm->Txop[1]<<5,
pEdcaParm->bACM[1]));
DBGPRINT(RT_DEBUG_TRACE,(" AC_VI %2d %2d %2d %4d %d\n",
pEdcaParm->Aifsn[2],
pEdcaParm->Cwmin[2],
pEdcaParm->Cwmax[2],
pEdcaParm->Txop[2]<<5,
pEdcaParm->bACM[2]));
DBGPRINT(RT_DEBUG_TRACE,(" AC_VO %2d %2d %2d %4d %d\n",
pEdcaParm->Aifsn[3],
pEdcaParm->Cwmin[3],
pEdcaParm->Cwmax[3],
pEdcaParm->Txop[3]<<5,
pEdcaParm->bACM[3]));
}
}
}
/*
==========================================================================
Description:
IRQL = PASSIVE_LEVEL
IRQL = DISPATCH_LEVEL
==========================================================================
*/
VOID AsicSetSlotTime(
IN PRTMP_ADAPTER pAd,
IN BOOLEAN bUseShortSlotTime)
{
ULONG SlotTime;
UINT32 RegValue = 0;
if (pAd->CommonCfg.Channel > 14)
bUseShortSlotTime = TRUE;
if (bUseShortSlotTime)
OPSTATUS_SET_FLAG(pAd, fOP_STATUS_SHORT_SLOT_INUSED);
else
OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_SHORT_SLOT_INUSED);
SlotTime = (bUseShortSlotTime)? 9 : 20;
{
#ifndef RT30xx
// force using short SLOT time for FAE to demo performance when TxBurst is ON
if (((pAd->StaActive.SupportedPhyInfo.bHtEnable == FALSE) && (OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_WMM_INUSED)))
|| ((pAd->StaActive.SupportedPhyInfo.bHtEnable == TRUE) && (pAd->CommonCfg.BACapability.field.Policy == BA_NOTUSE))
)
{
// In this case, we will think it is doing Wi-Fi test
// And we will not set to short slot when bEnableTxBurst is TRUE.
}
else if (pAd->CommonCfg.bEnableTxBurst)
#endif
#ifdef RT30xx
if (pAd->CommonCfg.bEnableTxBurst)
#endif
SlotTime = 9;
}
//
// For some reasons, always set it to short slot time.
//
// ToDo: Should consider capability with 11B
//
if (pAd->StaCfg.BssType == BSS_ADHOC)
SlotTime = 20;
RTMP_IO_READ32(pAd, BKOFF_SLOT_CFG, &RegValue);
RegValue = RegValue & 0xFFFFFF00;
RegValue |= SlotTime;
RTMP_IO_WRITE32(pAd, BKOFF_SLOT_CFG, RegValue);
}
/*
========================================================================
Description:
Add Shared key information into ASIC.
Update shared key, TxMic and RxMic to Asic Shared key table
Update its cipherAlg to Asic Shared key Mode.
Return:
========================================================================
*/
VOID AsicAddSharedKeyEntry(
IN PRTMP_ADAPTER pAd,
IN UCHAR BssIndex,
IN UCHAR KeyIdx,
IN UCHAR CipherAlg,
IN PUCHAR pKey,
IN PUCHAR pTxMic,
IN PUCHAR pRxMic)
{
ULONG offset; //, csr0;
SHAREDKEY_MODE_STRUC csr1;
#ifdef RT2860
INT i;
#endif
DBGPRINT(RT_DEBUG_TRACE, ("AsicAddSharedKeyEntry BssIndex=%d, KeyIdx=%d\n", BssIndex,KeyIdx));
//============================================================================================
DBGPRINT(RT_DEBUG_TRACE,("AsicAddSharedKeyEntry: %s key #%d\n", CipherName[CipherAlg], BssIndex*4 + KeyIdx));
DBGPRINT_RAW(RT_DEBUG_TRACE, (" Key = %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
pKey[0],pKey[1],pKey[2],pKey[3],pKey[4],pKey[5],pKey[6],pKey[7],pKey[8],pKey[9],pKey[10],pKey[11],pKey[12],pKey[13],pKey[14],pKey[15]));
if (pRxMic)
{
DBGPRINT_RAW(RT_DEBUG_TRACE, (" Rx MIC Key = %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
pRxMic[0],pRxMic[1],pRxMic[2],pRxMic[3],pRxMic[4],pRxMic[5],pRxMic[6],pRxMic[7]));
}
if (pTxMic)
{
DBGPRINT_RAW(RT_DEBUG_TRACE, (" Tx MIC Key = %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
pTxMic[0],pTxMic[1],pTxMic[2],pTxMic[3],pTxMic[4],pTxMic[5],pTxMic[6],pTxMic[7]));
}
//============================================================================================
//
// fill key material - key + TX MIC + RX MIC
//
offset = SHARED_KEY_TABLE_BASE + (4*BssIndex + KeyIdx)*HW_KEY_ENTRY_SIZE;
#ifdef RT2860
for (i=0; i<MAX_LEN_OF_SHARE_KEY; i++)
{
RTMP_IO_WRITE8(pAd, offset + i, pKey[i]);
}
#endif
#ifdef RT2870
RTUSBMultiWrite(pAd, offset, pKey, MAX_LEN_OF_SHARE_KEY);
#endif
offset += MAX_LEN_OF_SHARE_KEY;
if (pTxMic)
{
#ifdef RT2860
for (i=0; i<8; i++)
{
RTMP_IO_WRITE8(pAd, offset + i, pTxMic[i]);
}
#endif
#ifdef RT2870
RTUSBMultiWrite(pAd, offset, pTxMic, 8);
#endif
}
offset += 8;
if (pRxMic)
{
#ifdef RT2860
for (i=0; i<8; i++)
{
RTMP_IO_WRITE8(pAd, offset + i, pRxMic[i]);
}
#endif
#ifdef RT2870
RTUSBMultiWrite(pAd, offset, pRxMic, 8);
#endif
}
//
// Update cipher algorithm. WSTA always use BSS0
//
RTMP_IO_READ32(pAd, SHARED_KEY_MODE_BASE+4*(BssIndex/2), &csr1.word);
DBGPRINT(RT_DEBUG_TRACE,("Read: SHARED_KEY_MODE_BASE at this Bss[%d] KeyIdx[%d]= 0x%x \n", BssIndex,KeyIdx, csr1.word));
if ((BssIndex%2) == 0)
{
if (KeyIdx == 0)
csr1.field.Bss0Key0CipherAlg = CipherAlg;
else if (KeyIdx == 1)
csr1.field.Bss0Key1CipherAlg = CipherAlg;
else if (KeyIdx == 2)
csr1.field.Bss0Key2CipherAlg = CipherAlg;
else
csr1.field.Bss0Key3CipherAlg = CipherAlg;
}
else
{
if (KeyIdx == 0)
csr1.field.Bss1Key0CipherAlg = CipherAlg;
else if (KeyIdx == 1)
csr1.field.Bss1Key1CipherAlg = CipherAlg;
else if (KeyIdx == 2)
csr1.field.Bss1Key2CipherAlg = CipherAlg;
else
csr1.field.Bss1Key3CipherAlg = CipherAlg;
}
DBGPRINT(RT_DEBUG_TRACE,("Write: SHARED_KEY_MODE_BASE at this Bss[%d] = 0x%x \n", BssIndex, csr1.word));
RTMP_IO_WRITE32(pAd, SHARED_KEY_MODE_BASE+4*(BssIndex/2), csr1.word);
}
// IRQL = DISPATCH_LEVEL
VOID AsicRemoveSharedKeyEntry(
IN PRTMP_ADAPTER pAd,
IN UCHAR BssIndex,
IN UCHAR KeyIdx)
{
//ULONG SecCsr0;
SHAREDKEY_MODE_STRUC csr1;
DBGPRINT(RT_DEBUG_TRACE,("AsicRemoveSharedKeyEntry: #%d \n", BssIndex*4 + KeyIdx));
RTMP_IO_READ32(pAd, SHARED_KEY_MODE_BASE+4*(BssIndex/2), &csr1.word);
if ((BssIndex%2) == 0)
{
if (KeyIdx == 0)
csr1.field.Bss0Key0CipherAlg = 0;
else if (KeyIdx == 1)
csr1.field.Bss0Key1CipherAlg = 0;
else if (KeyIdx == 2)
csr1.field.Bss0Key2CipherAlg = 0;
else
csr1.field.Bss0Key3CipherAlg = 0;
}
else
{
if (KeyIdx == 0)
csr1.field.Bss1Key0CipherAlg = 0;
else if (KeyIdx == 1)
csr1.field.Bss1Key1CipherAlg = 0;
else if (KeyIdx == 2)
csr1.field.Bss1Key2CipherAlg = 0;
else
csr1.field.Bss1Key3CipherAlg = 0;
}
DBGPRINT(RT_DEBUG_TRACE,("Write: SHARED_KEY_MODE_BASE at this Bss[%d] = 0x%x \n", BssIndex, csr1.word));
RTMP_IO_WRITE32(pAd, SHARED_KEY_MODE_BASE+4*(BssIndex/2), csr1.word);
ASSERT(BssIndex < 4);
ASSERT(KeyIdx < 4);
}
VOID AsicUpdateWCIDAttribute(
IN PRTMP_ADAPTER pAd,
IN USHORT WCID,
IN UCHAR BssIndex,
IN UCHAR CipherAlg,
IN BOOLEAN bUsePairewiseKeyTable)
{
ULONG WCIDAttri = 0, offset;
//
// Update WCID attribute.
// Only TxKey could update WCID attribute.
//
offset = MAC_WCID_ATTRIBUTE_BASE + (WCID * HW_WCID_ATTRI_SIZE);
WCIDAttri = (BssIndex << 4) | (CipherAlg << 1) | (bUsePairewiseKeyTable);
RTMP_IO_WRITE32(pAd, offset, WCIDAttri);
}
VOID AsicUpdateWCIDIVEIV(
IN PRTMP_ADAPTER pAd,
IN USHORT WCID,
IN ULONG uIV,
IN ULONG uEIV)
{
ULONG offset;
offset = MAC_IVEIV_TABLE_BASE + (WCID * HW_IVEIV_ENTRY_SIZE);
RTMP_IO_WRITE32(pAd, offset, uIV);
RTMP_IO_WRITE32(pAd, offset + 4, uEIV);
}
VOID AsicUpdateRxWCIDTable(
IN PRTMP_ADAPTER pAd,
IN USHORT WCID,
IN PUCHAR pAddr)
{
ULONG offset;
ULONG Addr;
offset = MAC_WCID_BASE + (WCID * HW_WCID_ENTRY_SIZE);
Addr = pAddr[0] + (pAddr[1] << 8) +(pAddr[2] << 16) +(pAddr[3] << 24);
RTMP_IO_WRITE32(pAd, offset, Addr);
Addr = pAddr[4] + (pAddr[5] << 8);
RTMP_IO_WRITE32(pAd, offset + 4, Addr);
}
/*
========================================================================
Routine Description:
Set Cipher Key, Cipher algorithm, IV/EIV to Asic
Arguments:
pAd Pointer to our adapter
WCID WCID Entry number.
BssIndex BSSID index, station or none multiple BSSID support
this value should be 0.
KeyIdx This KeyIdx will set to IV's KeyID if bTxKey enabled
pCipherKey Pointer to Cipher Key.
bUsePairewiseKeyTable TRUE means saved the key in SharedKey table,
otherwise PairewiseKey table
bTxKey This is the transmit key if enabled.
Return Value:
None
Note:
This routine will set the relative key stuff to Asic including WCID attribute,
Cipher Key, Cipher algorithm and IV/EIV.
IV/EIV will be update if this CipherKey is the transmission key because
ASIC will base on IV's KeyID value to select Cipher Key.
If bTxKey sets to FALSE, this is not the TX key, but it could be
RX key
For AP mode bTxKey must be always set to TRUE.
========================================================================
*/
VOID AsicAddKeyEntry(
IN PRTMP_ADAPTER pAd,
IN USHORT WCID,
IN UCHAR BssIndex,
IN UCHAR KeyIdx,
IN PCIPHER_KEY pCipherKey,
IN BOOLEAN bUsePairewiseKeyTable,
IN BOOLEAN bTxKey)
{
ULONG offset;
UCHAR IV4 = 0;
PUCHAR pKey = pCipherKey->Key;
PUCHAR pTxMic = pCipherKey->TxMic;
PUCHAR pRxMic = pCipherKey->RxMic;
PUCHAR pTxtsc = pCipherKey->TxTsc;
UCHAR CipherAlg = pCipherKey->CipherAlg;
SHAREDKEY_MODE_STRUC csr1;
#ifdef RT2860
UCHAR i;
#endif
DBGPRINT(RT_DEBUG_TRACE, ("==> AsicAddKeyEntry\n"));
//
// 1.) decide key table offset
//
if (bUsePairewiseKeyTable)
offset = PAIRWISE_KEY_TABLE_BASE + (WCID * HW_KEY_ENTRY_SIZE);
else
offset = SHARED_KEY_TABLE_BASE + (4 * BssIndex + KeyIdx) * HW_KEY_ENTRY_SIZE;
//
// 2.) Set Key to Asic
//
//for (i = 0; i < KeyLen; i++)
#ifdef RT2860
for (i = 0; i < MAX_LEN_OF_PEER_KEY; i++)
{
RTMP_IO_WRITE8(pAd, offset + i, pKey[i]);
}
#endif
#ifdef RT2870
RTUSBMultiWrite(pAd, offset, pKey, MAX_LEN_OF_PEER_KEY);
#endif
offset += MAX_LEN_OF_PEER_KEY;
//
// 3.) Set MIC key if available
//
if (pTxMic)
{
#ifdef RT2860
for (i = 0; i < 8; i++)
{
RTMP_IO_WRITE8(pAd, offset + i, pTxMic[i]);
}
#endif
#ifdef RT2870
RTUSBMultiWrite(pAd, offset, pTxMic, 8);
#endif
}
offset += LEN_TKIP_TXMICK;
if (pRxMic)
{
#ifdef RT2860
for (i = 0; i < 8; i++)
{
RTMP_IO_WRITE8(pAd, offset + i, pRxMic[i]);
}
#endif
#ifdef RT2870
RTUSBMultiWrite(pAd, offset, pRxMic, 8);
#endif
}
//
// 4.) Modify IV/EIV if needs
// This will force Asic to use this key ID by setting IV.
//
if (bTxKey)
{
#ifdef RT2860
offset = MAC_IVEIV_TABLE_BASE + (WCID * HW_IVEIV_ENTRY_SIZE);
//
// Write IV
//
RTMP_IO_WRITE8(pAd, offset, pTxtsc[1]);
RTMP_IO_WRITE8(pAd, offset + 1, ((pTxtsc[1] | 0x20) & 0x7f));
RTMP_IO_WRITE8(pAd, offset + 2, pTxtsc[0]);
IV4 = (KeyIdx << 6);
if ((CipherAlg == CIPHER_TKIP) || (CipherAlg == CIPHER_TKIP_NO_MIC) ||(CipherAlg == CIPHER_AES))
IV4 |= 0x20; // turn on extension bit means EIV existence
RTMP_IO_WRITE8(pAd, offset + 3, IV4);
//
// Write EIV
//
offset += 4;
for (i = 0; i < 4; i++)
{
RTMP_IO_WRITE8(pAd, offset + i, pTxtsc[i + 2]);
}
#endif
#ifdef RT2870
UINT32 tmpVal;
//
// Write IV
//
IV4 = (KeyIdx << 6);
if ((CipherAlg == CIPHER_TKIP) || (CipherAlg == CIPHER_TKIP_NO_MIC) ||(CipherAlg == CIPHER_AES))
IV4 |= 0x20; // turn on extension bit means EIV existence
tmpVal = pTxtsc[1] + (((pTxtsc[1] | 0x20) & 0x7f) << 8) + (pTxtsc[0] << 16) + (IV4 << 24);
RTMP_IO_WRITE32(pAd, offset, tmpVal);
//
// Write EIV
//
offset += 4;
RTMP_IO_WRITE32(pAd, offset, *(PUINT32)&pCipherKey->TxTsc[2]);
#endif // RT2870 //
AsicUpdateWCIDAttribute(pAd, WCID, BssIndex, CipherAlg, bUsePairewiseKeyTable);
}
if (!bUsePairewiseKeyTable)
{
//
// Only update the shared key security mode
//
RTMP_IO_READ32(pAd, SHARED_KEY_MODE_BASE + 4 * (BssIndex / 2), &csr1.word);
if ((BssIndex % 2) == 0)
{
if (KeyIdx == 0)
csr1.field.Bss0Key0CipherAlg = CipherAlg;
else if (KeyIdx == 1)
csr1.field.Bss0Key1CipherAlg = CipherAlg;
else if (KeyIdx == 2)
csr1.field.Bss0Key2CipherAlg = CipherAlg;
else
csr1.field.Bss0Key3CipherAlg = CipherAlg;
}
else
{
if (KeyIdx == 0)
csr1.field.Bss1Key0CipherAlg = CipherAlg;
else if (KeyIdx == 1)
csr1.field.Bss1Key1CipherAlg = CipherAlg;
else if (KeyIdx == 2)
csr1.field.Bss1Key2CipherAlg = CipherAlg;
else
csr1.field.Bss1Key3CipherAlg = CipherAlg;
}
RTMP_IO_WRITE32(pAd, SHARED_KEY_MODE_BASE + 4 * (BssIndex / 2), csr1.word);
}
DBGPRINT(RT_DEBUG_TRACE, ("<== AsicAddKeyEntry\n"));
}
/*
========================================================================
Description:
Add Pair-wise key material into ASIC.
Update pairwise key, TxMic and RxMic to Asic Pair-wise key table
Return:
========================================================================
*/
VOID AsicAddPairwiseKeyEntry(
IN PRTMP_ADAPTER pAd,
IN PUCHAR pAddr,
IN UCHAR WCID,
IN CIPHER_KEY *pCipherKey)
{
INT i;
ULONG offset;
PUCHAR pKey = pCipherKey->Key;
PUCHAR pTxMic = pCipherKey->TxMic;
PUCHAR pRxMic = pCipherKey->RxMic;
#ifdef DBG
UCHAR CipherAlg = pCipherKey->CipherAlg;
#endif // DBG //
// EKEY
offset = PAIRWISE_KEY_TABLE_BASE + (WCID * HW_KEY_ENTRY_SIZE);
#ifdef RT2860
for (i=0; i<MAX_LEN_OF_PEER_KEY; i++)
{
RTMP_IO_WRITE8(pAd, offset + i, pKey[i]);
}
#endif
#ifdef RT2870
RTUSBMultiWrite(pAd, offset, &pCipherKey->Key[0], MAX_LEN_OF_PEER_KEY);
#endif // RT2870 //
for (i=0; i<MAX_LEN_OF_PEER_KEY; i+=4)
{
UINT32 Value;
RTMP_IO_READ32(pAd, offset + i, &Value);
}
offset += MAX_LEN_OF_PEER_KEY;
// MIC KEY
if (pTxMic)
{
#ifdef RT2860
for (i=0; i<8; i++)
{
RTMP_IO_WRITE8(pAd, offset+i, pTxMic[i]);
}
#endif
#ifdef RT2870
RTUSBMultiWrite(pAd, offset, &pCipherKey->TxMic[0], 8);
#endif // RT2870 //
}
offset += 8;
if (pRxMic)
{
#ifdef RT2860
for (i=0; i<8; i++)
{
RTMP_IO_WRITE8(pAd, offset+i, pRxMic[i]);
}
#endif
#ifdef RT2870
RTUSBMultiWrite(pAd, offset, &pCipherKey->RxMic[0], 8);
#endif // RT2870 //
}
DBGPRINT(RT_DEBUG_TRACE,("AsicAddPairwiseKeyEntry: WCID #%d Alg=%s\n",WCID, CipherName[CipherAlg]));
DBGPRINT(RT_DEBUG_TRACE,(" Key = %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
pKey[0],pKey[1],pKey[2],pKey[3],pKey[4],pKey[5],pKey[6],pKey[7],pKey[8],pKey[9],pKey[10],pKey[11],pKey[12],pKey[13],pKey[14],pKey[15]));
if (pRxMic)
{
DBGPRINT(RT_DEBUG_TRACE, (" Rx MIC Key = %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
pRxMic[0],pRxMic[1],pRxMic[2],pRxMic[3],pRxMic[4],pRxMic[5],pRxMic[6],pRxMic[7]));
}
if (pTxMic)
{
DBGPRINT(RT_DEBUG_TRACE, (" Tx MIC Key = %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
pTxMic[0],pTxMic[1],pTxMic[2],pTxMic[3],pTxMic[4],pTxMic[5],pTxMic[6],pTxMic[7]));
}
}
/*
========================================================================
Description:
Remove Pair-wise key material from ASIC.
Return:
========================================================================
*/
VOID AsicRemovePairwiseKeyEntry(
IN PRTMP_ADAPTER pAd,
IN UCHAR BssIdx,
IN UCHAR Wcid)
{
ULONG WCIDAttri;
USHORT offset;
// re-set the entry's WCID attribute as OPEN-NONE.
offset = MAC_WCID_ATTRIBUTE_BASE + (Wcid * HW_WCID_ATTRI_SIZE);
WCIDAttri = (BssIdx<<4) | PAIRWISEKEYTABLE;
RTMP_IO_WRITE32(pAd, offset, WCIDAttri);
}
BOOLEAN AsicSendCommandToMcu(
IN PRTMP_ADAPTER pAd,
IN UCHAR Command,
IN UCHAR Token,
IN UCHAR Arg0,
IN UCHAR Arg1)
{
HOST_CMD_CSR_STRUC H2MCmd;
H2M_MAILBOX_STRUC H2MMailbox;
ULONG i = 0;
do
{
RTMP_IO_READ32(pAd, H2M_MAILBOX_CSR, &H2MMailbox.word);
if (H2MMailbox.field.Owner == 0)
break;
RTMPusecDelay(2);
} while(i++ < 100);
if (i >= 100)
{
{
#ifdef RT2860
UINT32 Data;
// Reset DMA
RTMP_IO_READ32(pAd, PBF_SYS_CTRL, &Data);
Data |= 0x2;
RTMP_IO_WRITE32(pAd, PBF_SYS_CTRL, Data);
// After Reset DMA, DMA index will become Zero. So Driver need to reset all ring indexs too.
// Reset DMA/CPU ring index
RTMPRingCleanUp(pAd, QID_AC_BK);
RTMPRingCleanUp(pAd, QID_AC_BE);
RTMPRingCleanUp(pAd, QID_AC_VI);
RTMPRingCleanUp(pAd, QID_AC_VO);
RTMPRingCleanUp(pAd, QID_HCCA);
RTMPRingCleanUp(pAd, QID_MGMT);
RTMPRingCleanUp(pAd, QID_RX);
// Clear Reset
RTMP_IO_READ32(pAd, PBF_SYS_CTRL, &Data);
Data &= 0xfffffffd;
RTMP_IO_WRITE32(pAd, PBF_SYS_CTRL, Data);
#endif /* RT2860 */
DBGPRINT_ERR(("H2M_MAILBOX still hold by MCU. command fail\n"));
}
//return FALSE;
#ifdef RT2870
return FALSE;
#endif
}
H2MMailbox.field.Owner = 1; // pass ownership to MCU
H2MMailbox.field.CmdToken = Token;
H2MMailbox.field.HighByte = Arg1;
H2MMailbox.field.LowByte = Arg0;
RTMP_IO_WRITE32(pAd, H2M_MAILBOX_CSR, H2MMailbox.word);
H2MCmd.word = 0;
H2MCmd.field.HostCommand = Command;
RTMP_IO_WRITE32(pAd, HOST_CMD_CSR, H2MCmd.word);
if (Command != 0x80)
{
}
return TRUE;
}
#ifdef RT2860
BOOLEAN AsicCheckCommanOk(
IN PRTMP_ADAPTER pAd,
IN UCHAR Command)
{
UINT32 CmdStatus = 0, CID = 0, i;
UINT32 ThisCIDMask = 0;
i = 0;
do
{
RTMP_IO_READ32(pAd, H2M_MAILBOX_CID, &CID);
// Find where the command is. Because this is randomly specified by firmware.
if ((CID & CID0MASK) == Command)
{
ThisCIDMask = CID0MASK;
break;
}
else if ((((CID & CID1MASK)>>8) & 0xff) == Command)
{
ThisCIDMask = CID1MASK;
break;
}
else if ((((CID & CID2MASK)>>16) & 0xff) == Command)
{
ThisCIDMask = CID2MASK;
break;
}
else if ((((CID & CID3MASK)>>24) & 0xff) == Command)
{
ThisCIDMask = CID3MASK;
break;
}
RTMPusecDelay(100);
i++;
}while (i < 200);
// Get CommandStatus Value
RTMP_IO_READ32(pAd, H2M_MAILBOX_STATUS, &CmdStatus);
// This command's status is at the same position as command. So AND command position's bitmask to read status.
if (i < 200)
{
// If Status is 1, the comamnd is success.
if (((CmdStatus & ThisCIDMask) == 0x1) || ((CmdStatus & ThisCIDMask) == 0x100)
|| ((CmdStatus & ThisCIDMask) == 0x10000) || ((CmdStatus & ThisCIDMask) == 0x1000000))
{
DBGPRINT(RT_DEBUG_TRACE, ("--> AsicCheckCommanOk CID = 0x%x, CmdStatus= 0x%x \n", CID, CmdStatus));
RTMP_IO_WRITE32(pAd, H2M_MAILBOX_STATUS, 0xffffffff);
RTMP_IO_WRITE32(pAd, H2M_MAILBOX_CID, 0xffffffff);
return TRUE;
}
DBGPRINT(RT_DEBUG_TRACE, ("--> AsicCheckCommanFail1 CID = 0x%x, CmdStatus= 0x%x \n", CID, CmdStatus));
}
else
{
DBGPRINT(RT_DEBUG_TRACE, ("--> AsicCheckCommanFail2 Timeout Command = %d, CmdStatus= 0x%x \n", Command, CmdStatus));
}
// Clear Command and Status.
RTMP_IO_WRITE32(pAd, H2M_MAILBOX_STATUS, 0xffffffff);
RTMP_IO_WRITE32(pAd, H2M_MAILBOX_CID, 0xffffffff);
return FALSE;
}
#endif /* RT8260 */
/*
========================================================================
Routine Description:
Verify the support rate for different PHY type
Arguments:
pAd Pointer to our adapter
Return Value:
None
IRQL = PASSIVE_LEVEL
========================================================================
*/
VOID RTMPCheckRates(
IN PRTMP_ADAPTER pAd,
IN OUT UCHAR SupRate[],
IN OUT UCHAR *SupRateLen)
{
UCHAR RateIdx, i, j;
UCHAR NewRate[12], NewRateLen;
NewRateLen = 0;
if (pAd->CommonCfg.PhyMode == PHY_11B)
RateIdx = 4;
else
RateIdx = 12;
// Check for support rates exclude basic rate bit
for (i = 0; i < *SupRateLen; i++)
for (j = 0; j < RateIdx; j++)
if ((SupRate[i] & 0x7f) == RateIdTo500Kbps[j])
NewRate[NewRateLen++] = SupRate[i];
*SupRateLen = NewRateLen;
NdisMoveMemory(SupRate, NewRate, NewRateLen);
}
BOOLEAN RTMPCheckChannel(
IN PRTMP_ADAPTER pAd,
IN UCHAR CentralChannel,
IN UCHAR Channel)
{
UCHAR k;
UCHAR UpperChannel = 0, LowerChannel = 0;
UCHAR NoEffectChannelinList = 0;
// Find upper and lower channel according to 40MHz current operation.
if (CentralChannel < Channel)
{
UpperChannel = Channel;
if (CentralChannel > 2)
LowerChannel = CentralChannel - 2;
else
return FALSE;
}
else if (CentralChannel > Channel)
{
UpperChannel = CentralChannel + 2;
LowerChannel = Channel;
}
for (k = 0;k < pAd->ChannelListNum;k++)
{
if (pAd->ChannelList[k].Channel == UpperChannel)
{
NoEffectChannelinList ++;
}
if (pAd->ChannelList[k].Channel == LowerChannel)
{
NoEffectChannelinList ++;
}
}
DBGPRINT(RT_DEBUG_TRACE,("Total Channel in Channel List = [%d]\n", NoEffectChannelinList));
if (NoEffectChannelinList == 2)
return TRUE;
else
return FALSE;
}
/*
========================================================================
Routine Description:
Verify the support rate for HT phy type
Arguments:
pAd Pointer to our adapter
Return Value:
FALSE if pAd->CommonCfg.SupportedHtPhy doesn't accept the pHtCapability. (AP Mode)
IRQL = PASSIVE_LEVEL
========================================================================
*/
BOOLEAN RTMPCheckHt(
IN PRTMP_ADAPTER pAd,
IN UCHAR Wcid,
IN HT_CAPABILITY_IE *pHtCapability,
IN ADD_HT_INFO_IE *pAddHtInfo)
{
if (Wcid >= MAX_LEN_OF_MAC_TABLE)
return FALSE;
// If use AMSDU, set flag.
if (pAd->CommonCfg.DesiredHtPhy.AmsduEnable)
CLIENT_STATUS_SET_FLAG(&pAd->MacTab.Content[Wcid], fCLIENT_STATUS_AMSDU_INUSED);
// Save Peer Capability
if (pHtCapability->HtCapInfo.ShortGIfor20)
CLIENT_STATUS_SET_FLAG(&pAd->MacTab.Content[Wcid], fCLIENT_STATUS_SGI20_CAPABLE);
if (pHtCapability->HtCapInfo.ShortGIfor40)
CLIENT_STATUS_SET_FLAG(&pAd->MacTab.Content[Wcid], fCLIENT_STATUS_SGI40_CAPABLE);
if (pHtCapability->HtCapInfo.TxSTBC)
CLIENT_STATUS_SET_FLAG(&pAd->MacTab.Content[Wcid], fCLIENT_STATUS_TxSTBC_CAPABLE);
if (pHtCapability->HtCapInfo.RxSTBC)
CLIENT_STATUS_SET_FLAG(&pAd->MacTab.Content[Wcid], fCLIENT_STATUS_RxSTBC_CAPABLE);
if (pAd->CommonCfg.bRdg && pHtCapability->ExtHtCapInfo.RDGSupport)
{
CLIENT_STATUS_SET_FLAG(&pAd->MacTab.Content[Wcid], fCLIENT_STATUS_RDG_CAPABLE);
}
if (Wcid < MAX_LEN_OF_MAC_TABLE)
{
pAd->MacTab.Content[Wcid].MpduDensity = pHtCapability->HtCapParm.MpduDensity;
}
// Will check ChannelWidth for MCSSet[4] below
pAd->MlmeAux.HtCapability.MCSSet[4] = 0x1;
switch (pAd->CommonCfg.RxStream)
{
case 1:
pAd->MlmeAux.HtCapability.MCSSet[0] = 0xff;
pAd->MlmeAux.HtCapability.MCSSet[1] = 0x00;
pAd->MlmeAux.HtCapability.MCSSet[2] = 0x00;
pAd->MlmeAux.HtCapability.MCSSet[3] = 0x00;
break;
case 2:
pAd->MlmeAux.HtCapability.MCSSet[0] = 0xff;
pAd->MlmeAux.HtCapability.MCSSet[1] = 0xff;
pAd->MlmeAux.HtCapability.MCSSet[2] = 0x00;
pAd->MlmeAux.HtCapability.MCSSet[3] = 0x00;
break;
case 3:
pAd->MlmeAux.HtCapability.MCSSet[0] = 0xff;
pAd->MlmeAux.HtCapability.MCSSet[1] = 0xff;
pAd->MlmeAux.HtCapability.MCSSet[2] = 0xff;
pAd->MlmeAux.HtCapability.MCSSet[3] = 0x00;
break;
}
pAd->MlmeAux.HtCapability.HtCapInfo.ChannelWidth = pAddHtInfo->AddHtInfo.RecomWidth & pAd->CommonCfg.DesiredHtPhy.ChannelWidth;
DBGPRINT(RT_DEBUG_TRACE, ("RTMPCheckHt:: HtCapInfo.ChannelWidth=%d, RecomWidth=%d, DesiredHtPhy.ChannelWidth=%d, BW40MAvailForA/G=%d/%d, PhyMode=%d \n",
pAd->MlmeAux.HtCapability.HtCapInfo.ChannelWidth, pAddHtInfo->AddHtInfo.RecomWidth, pAd->CommonCfg.DesiredHtPhy.ChannelWidth,
pAd->NicConfig2.field.BW40MAvailForA, pAd->NicConfig2.field.BW40MAvailForG, pAd->CommonCfg.PhyMode));
pAd->MlmeAux.HtCapability.HtCapInfo.GF = pHtCapability->HtCapInfo.GF &pAd->CommonCfg.DesiredHtPhy.GF;
// Send Assoc Req with my HT capability.
pAd->MlmeAux.HtCapability.HtCapInfo.AMsduSize = pAd->CommonCfg.DesiredHtPhy.AmsduSize;
pAd->MlmeAux.HtCapability.HtCapInfo.MimoPs = pAd->CommonCfg.DesiredHtPhy.MimoPs;
pAd->MlmeAux.HtCapability.HtCapInfo.ShortGIfor20 = (pAd->CommonCfg.DesiredHtPhy.ShortGIfor20) & (pHtCapability->HtCapInfo.ShortGIfor20);
pAd->MlmeAux.HtCapability.HtCapInfo.ShortGIfor40 = (pAd->CommonCfg.DesiredHtPhy.ShortGIfor40) & (pHtCapability->HtCapInfo.ShortGIfor40);
pAd->MlmeAux.HtCapability.HtCapInfo.TxSTBC = (pAd->CommonCfg.DesiredHtPhy.TxSTBC)&(pHtCapability->HtCapInfo.RxSTBC);
pAd->MlmeAux.HtCapability.HtCapInfo.RxSTBC = (pAd->CommonCfg.DesiredHtPhy.RxSTBC)&(pHtCapability->HtCapInfo.TxSTBC);
pAd->MlmeAux.HtCapability.HtCapParm.MaxRAmpduFactor = pAd->CommonCfg.DesiredHtPhy.MaxRAmpduFactor;
pAd->MlmeAux.HtCapability.HtCapParm.MpduDensity = pAd->CommonCfg.HtCapability.HtCapParm.MpduDensity;
pAd->MlmeAux.HtCapability.ExtHtCapInfo.PlusHTC = pHtCapability->ExtHtCapInfo.PlusHTC;
pAd->MacTab.Content[Wcid].HTCapability.ExtHtCapInfo.PlusHTC = pHtCapability->ExtHtCapInfo.PlusHTC;
if (pAd->CommonCfg.bRdg)
{
pAd->MlmeAux.HtCapability.ExtHtCapInfo.RDGSupport = pHtCapability->ExtHtCapInfo.RDGSupport;
pAd->MlmeAux.HtCapability.ExtHtCapInfo.PlusHTC = 1;
}
if (pAd->MlmeAux.HtCapability.HtCapInfo.ChannelWidth == BW_20)
pAd->MlmeAux.HtCapability.MCSSet[4] = 0x0; // BW20 can't transmit MCS32
COPY_AP_HTSETTINGS_FROM_BEACON(pAd, pHtCapability);
return TRUE;
}
/*
========================================================================
Routine Description:
Verify the support rate for different PHY type
Arguments:
pAd Pointer to our adapter
Return Value:
None
IRQL = PASSIVE_LEVEL
========================================================================
*/
VOID RTMPUpdateMlmeRate(
IN PRTMP_ADAPTER pAd)
{
UCHAR MinimumRate;
UCHAR ProperMlmeRate; //= RATE_54;
UCHAR i, j, RateIdx = 12; //1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54
BOOLEAN bMatch = FALSE;
switch (pAd->CommonCfg.PhyMode)
{
case PHY_11B:
ProperMlmeRate = RATE_11;
MinimumRate = RATE_1;
break;
case PHY_11BG_MIXED:
case PHY_11ABGN_MIXED:
case PHY_11BGN_MIXED:
if ((pAd->MlmeAux.SupRateLen == 4) &&
(pAd->MlmeAux.ExtRateLen == 0))
// B only AP
ProperMlmeRate = RATE_11;
else
ProperMlmeRate = RATE_24;
if (pAd->MlmeAux.Channel <= 14)
MinimumRate = RATE_1;
else
MinimumRate = RATE_6;
break;
case PHY_11A:
case PHY_11N_2_4G: // rt2860 need to check mlmerate for 802.11n
case PHY_11GN_MIXED:
case PHY_11AGN_MIXED:
case PHY_11AN_MIXED:
case PHY_11N_5G:
ProperMlmeRate = RATE_24;
MinimumRate = RATE_6;
break;
case PHY_11ABG_MIXED:
ProperMlmeRate = RATE_24;
if (pAd->MlmeAux.Channel <= 14)
MinimumRate = RATE_1;
else
MinimumRate = RATE_6;
break;
default: // error
ProperMlmeRate = RATE_1;
MinimumRate = RATE_1;
break;
}
for (i = 0; i < pAd->MlmeAux.SupRateLen; i++)
{
for (j = 0; j < RateIdx; j++)
{
if ((pAd->MlmeAux.SupRate[i] & 0x7f) == RateIdTo500Kbps[j])
{
if (j == ProperMlmeRate)
{
bMatch = TRUE;
break;
}
}
}
if (bMatch)
break;
}
if (bMatch == FALSE)
{
for (i = 0; i < pAd->MlmeAux.ExtRateLen; i++)
{
for (j = 0; j < RateIdx; j++)
{
if ((pAd->MlmeAux.ExtRate[i] & 0x7f) == RateIdTo500Kbps[j])
{
if (j == ProperMlmeRate)
{
bMatch = TRUE;
break;
}
}
}
if (bMatch)
break;
}
}
if (bMatch == FALSE)
{
ProperMlmeRate = MinimumRate;
}
pAd->CommonCfg.MlmeRate = MinimumRate;
pAd->CommonCfg.RtsRate = ProperMlmeRate;
if (pAd->CommonCfg.MlmeRate >= RATE_6)
{
pAd->CommonCfg.MlmeTransmit.field.MODE = MODE_OFDM;
pAd->CommonCfg.MlmeTransmit.field.MCS = OfdmRateToRxwiMCS[pAd->CommonCfg.MlmeRate];
pAd->MacTab.Content[BSS0Mcast_WCID].HTPhyMode.field.MODE = MODE_OFDM;
pAd->MacTab.Content[BSS0Mcast_WCID].HTPhyMode.field.MCS = OfdmRateToRxwiMCS[pAd->CommonCfg.MlmeRate];
}
else
{
pAd->CommonCfg.MlmeTransmit.field.MODE = MODE_CCK;
pAd->CommonCfg.MlmeTransmit.field.MCS = pAd->CommonCfg.MlmeRate;
pAd->MacTab.Content[BSS0Mcast_WCID].HTPhyMode.field.MODE = MODE_CCK;
pAd->MacTab.Content[BSS0Mcast_WCID].HTPhyMode.field.MCS = pAd->CommonCfg.MlmeRate;
}
DBGPRINT(RT_DEBUG_TRACE, ("RTMPUpdateMlmeRate ==> MlmeTransmit = 0x%x \n" , pAd->CommonCfg.MlmeTransmit.word));
}
CHAR RTMPMaxRssi(
IN PRTMP_ADAPTER pAd,
IN CHAR Rssi0,
IN CHAR Rssi1,
IN CHAR Rssi2)
{
CHAR larger = -127;
if ((pAd->Antenna.field.RxPath == 1) && (Rssi0 != 0))
{
larger = Rssi0;
}
if ((pAd->Antenna.field.RxPath >= 2) && (Rssi1 != 0))
{
larger = max(Rssi0, Rssi1);
}
if ((pAd->Antenna.field.RxPath == 3) && (Rssi2 != 0))
{
larger = max(larger, Rssi2);
}
if (larger == -127)
larger = 0;
return larger;
}
#ifdef RT30xx
// Antenna divesity use GPIO3 and EESK pin for control
// Antenna and EEPROM access are both using EESK pin,
// Therefor we should avoid accessing EESK at the same time
// Then restore antenna after EEPROM access
VOID AsicSetRxAnt(
IN PRTMP_ADAPTER pAd,
IN UCHAR Ant)
{
#ifdef RT30xx
UINT32 Value;
UINT32 x;
if ((pAd->EepromAccess) ||
(RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_RESET_IN_PROGRESS)) ||
(RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS)) ||
(RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_RADIO_OFF)) ||
(RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)))
{
return;
}
// the antenna selection is through firmware and MAC register(GPIO3)
if (Ant == 0)
{
// Main antenna
RTMP_IO_READ32(pAd, E2PROM_CSR, &x);
x |= (EESK);
RTMP_IO_WRITE32(pAd, E2PROM_CSR, x);
RTMP_IO_READ32(pAd, GPIO_CTRL_CFG, &Value);
Value &= ~(0x0808);
RTMP_IO_WRITE32(pAd, GPIO_CTRL_CFG, Value);
DBGPRINT_RAW(RT_DEBUG_TRACE, ("AsicSetRxAnt, switch to main antenna\n"));
}
else
{
// Aux antenna
RTMP_IO_READ32(pAd, E2PROM_CSR, &x);
x &= ~(EESK);
RTMP_IO_WRITE32(pAd, E2PROM_CSR, x);
RTMP_IO_READ32(pAd, GPIO_CTRL_CFG, &Value);
Value &= ~(0x0808);
Value |= 0x08;
RTMP_IO_WRITE32(pAd, GPIO_CTRL_CFG, Value);
DBGPRINT_RAW(RT_DEBUG_TRACE, ("AsicSetRxAnt, switch to aux antenna\n"));
}
#endif // RT30xx //
}
#endif /* RT30xx */
/*
========================================================================
Routine Description:
Periodic evaluate antenna link status
Arguments:
pAd - Adapter pointer
Return Value:
None
========================================================================
*/
VOID AsicEvaluateRxAnt(
IN PRTMP_ADAPTER pAd)
{
UCHAR BBPR3 = 0;
#ifndef RT30xx
{
if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_RESET_IN_PROGRESS |
fRTMP_ADAPTER_HALT_IN_PROGRESS |
fRTMP_ADAPTER_RADIO_OFF |
fRTMP_ADAPTER_NIC_NOT_EXIST |
fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS))
return;
if (pAd->StaCfg.Psm == PWR_SAVE)
return;
}
RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R3, &BBPR3);
BBPR3 &= (~0x18);
if(pAd->Antenna.field.RxPath == 3)
{
BBPR3 |= (0x10);
}
else if(pAd->Antenna.field.RxPath == 2)
{
BBPR3 |= (0x8);
}
else if(pAd->Antenna.field.RxPath == 1)
{
BBPR3 |= (0x0);
}
RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R3, BBPR3);
#ifdef RT2860
pAd->StaCfg.BBPR3 = BBPR3;
#endif
#ifdef RT2870
if (OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_MEDIA_STATE_CONNECTED)
)
{
ULONG TxTotalCnt = pAd->RalinkCounters.OneSecTxNoRetryOkCount +
pAd->RalinkCounters.OneSecTxRetryOkCount +
pAd->RalinkCounters.OneSecTxFailCount;
if (TxTotalCnt > 50)
{
RTMPSetTimer(&pAd->Mlme.RxAntEvalTimer, 20);
pAd->Mlme.bLowThroughput = FALSE;
}
else
{
RTMPSetTimer(&pAd->Mlme.RxAntEvalTimer, 300);
pAd->Mlme.bLowThroughput = TRUE;
}
}
#endif
#endif /* RT30xx */
#ifdef RT30xx
if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_RESET_IN_PROGRESS |
fRTMP_ADAPTER_HALT_IN_PROGRESS |
fRTMP_ADAPTER_RADIO_OFF |
fRTMP_ADAPTER_NIC_NOT_EXIST |
fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS) ||
OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_DOZE)
#ifdef RT30xx
|| (pAd->EepromAccess)
#endif // RT30xx //
)
return;
{
//if (pAd->StaCfg.Psm == PWR_SAVE)
// return;
}
// two antenna selection mechanism- one is antenna diversity, the other is failed antenna remove
// one is antenna diversity:there is only one antenna can rx and tx
// the other is failed antenna remove:two physical antenna can rx and tx
if (pAd->NicConfig2.field.AntDiversity)
{
DBGPRINT(RT_DEBUG_TRACE,("AntDiv - before evaluate Pair1-Ant (%d,%d)\n",
pAd->RxAnt.Pair1PrimaryRxAnt, pAd->RxAnt.Pair1SecondaryRxAnt));
AsicSetRxAnt(pAd, pAd->RxAnt.Pair1SecondaryRxAnt);
pAd->RxAnt.EvaluatePeriod = 1; // 1:Means switch to SecondaryRxAnt, 0:Means switch to Pair1PrimaryRxAnt
pAd->RxAnt.FirstPktArrivedWhenEvaluate = FALSE;
pAd->RxAnt.RcvPktNumWhenEvaluate = 0;
// a one-shot timer to end the evalution
// dynamic adjust antenna evaluation period according to the traffic
if (OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_MEDIA_STATE_CONNECTED))
RTMPSetTimer(&pAd->Mlme.RxAntEvalTimer, 100);
else
RTMPSetTimer(&pAd->Mlme.RxAntEvalTimer, 300);
}
else
{
if (pAd->StaCfg.Psm == PWR_SAVE)
return;
RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R3, &BBPR3);
BBPR3 &= (~0x18);
if(pAd->Antenna.field.RxPath == 3)
{
BBPR3 |= (0x10);
}
else if(pAd->Antenna.field.RxPath == 2)
{
BBPR3 |= (0x8);
}
else if(pAd->Antenna.field.RxPath == 1)
{
BBPR3 |= (0x0);
}
RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R3, BBPR3);
}
#endif /* RT30xx */
if (OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_MEDIA_STATE_CONNECTED)
)
{
ULONG TxTotalCnt = pAd->RalinkCounters.OneSecTxNoRetryOkCount +
pAd->RalinkCounters.OneSecTxRetryOkCount +
pAd->RalinkCounters.OneSecTxFailCount;
// dynamic adjust antenna evaluation period according to the traffic
if (TxTotalCnt > 50)
{
RTMPSetTimer(&pAd->Mlme.RxAntEvalTimer, 20);
pAd->Mlme.bLowThroughput = FALSE;
}
else
{
RTMPSetTimer(&pAd->Mlme.RxAntEvalTimer, 300);
pAd->Mlme.bLowThroughput = TRUE;
}
}
}
/*
========================================================================
Routine Description:
After evaluation, check antenna link status
Arguments:
pAd - Adapter pointer
Return Value:
None
========================================================================
*/
VOID AsicRxAntEvalTimeout(
IN PVOID SystemSpecific1,
IN PVOID FunctionContext,
IN PVOID SystemSpecific2,
IN PVOID SystemSpecific3)
{
RTMP_ADAPTER *pAd = (RTMP_ADAPTER *)FunctionContext;
UCHAR BBPR3 = 0;
CHAR larger = -127, rssi0, rssi1, rssi2;
#ifndef RT30xx
{
if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_RESET_IN_PROGRESS) ||
RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS) ||
RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_RADIO_OFF) ||
RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST))
return;
if (pAd->StaCfg.Psm == PWR_SAVE)
return;
// if the traffic is low, use average rssi as the criteria
if (pAd->Mlme.bLowThroughput == TRUE)
{
rssi0 = pAd->StaCfg.RssiSample.LastRssi0;
rssi1 = pAd->StaCfg.RssiSample.LastRssi1;
rssi2 = pAd->StaCfg.RssiSample.LastRssi2;
}
else
{
rssi0 = pAd->StaCfg.RssiSample.AvgRssi0;
rssi1 = pAd->StaCfg.RssiSample.AvgRssi1;
rssi2 = pAd->StaCfg.RssiSample.AvgRssi2;
}
if(pAd->Antenna.field.RxPath == 3)
{
larger = max(rssi0, rssi1);
if (larger > (rssi2 + 20))
pAd->Mlme.RealRxPath = 2;
else
pAd->Mlme.RealRxPath = 3;
}
else if(pAd->Antenna.field.RxPath == 2)
{
if (rssi0 > (rssi1 + 20))
pAd->Mlme.RealRxPath = 1;
else
pAd->Mlme.RealRxPath = 2;
}
RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R3, &BBPR3);
BBPR3 &= (~0x18);
if(pAd->Mlme.RealRxPath == 3)
{
BBPR3 |= (0x10);
}
else if(pAd->Mlme.RealRxPath == 2)
{
BBPR3 |= (0x8);
}
else if(pAd->Mlme.RealRxPath == 1)
{
BBPR3 |= (0x0);
}
RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R3, BBPR3);
#ifdef RT2860
pAd->StaCfg.BBPR3 = BBPR3;
#endif
}
#endif /* RT30xx */
#ifdef RT30xx
if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_RESET_IN_PROGRESS |
fRTMP_ADAPTER_HALT_IN_PROGRESS |
fRTMP_ADAPTER_RADIO_OFF |
fRTMP_ADAPTER_NIC_NOT_EXIST) ||
OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_DOZE)
#ifdef RT30xx
|| (pAd->EepromAccess)
#endif // RT30xx //
)
return;
{
//if (pAd->StaCfg.Psm == PWR_SAVE)
// return;
if (pAd->NicConfig2.field.AntDiversity)
{
if ((pAd->RxAnt.RcvPktNumWhenEvaluate != 0) && (pAd->RxAnt.Pair1AvgRssi[pAd->RxAnt.Pair1SecondaryRxAnt] >= pAd->RxAnt.Pair1AvgRssi[pAd->RxAnt.Pair1PrimaryRxAnt]))
{
UCHAR temp;
//
// select PrimaryRxAntPair
// Role change, Used Pair1SecondaryRxAnt as PrimaryRxAntPair.
// Since Pair1SecondaryRxAnt Quality good than Pair1PrimaryRxAnt
//
temp = pAd->RxAnt.Pair1PrimaryRxAnt;
pAd->RxAnt.Pair1PrimaryRxAnt = pAd->RxAnt.Pair1SecondaryRxAnt;
pAd->RxAnt.Pair1SecondaryRxAnt = temp;
pAd->RxAnt.Pair1LastAvgRssi = (pAd->RxAnt.Pair1AvgRssi[pAd->RxAnt.Pair1SecondaryRxAnt] >> 3);
pAd->RxAnt.EvaluateStableCnt = 0;
}
else
{
// if the evaluated antenna is not better than original, switch back to original antenna
AsicSetRxAnt(pAd, pAd->RxAnt.Pair1PrimaryRxAnt);
pAd->RxAnt.EvaluateStableCnt ++;
}
pAd->RxAnt.EvaluatePeriod = 0; // 1:Means switch to SecondaryRxAnt, 0:Means switch to Pair1PrimaryRxAnt
DBGPRINT(RT_DEBUG_TRACE,("AsicRxAntEvalAction::After Eval(fix in #%d), <%d, %d>, RcvPktNumWhenEvaluate=%ld\n",
pAd->RxAnt.Pair1PrimaryRxAnt, (pAd->RxAnt.Pair1AvgRssi[0] >> 3), (pAd->RxAnt.Pair1AvgRssi[1] >> 3), pAd->RxAnt.RcvPktNumWhenEvaluate));
}
else
{
if (pAd->StaCfg.Psm == PWR_SAVE)
return;
// if the traffic is low, use average rssi as the criteria
if (pAd->Mlme.bLowThroughput == TRUE)
{
rssi0 = pAd->StaCfg.RssiSample.LastRssi0;
rssi1 = pAd->StaCfg.RssiSample.LastRssi1;
rssi2 = pAd->StaCfg.RssiSample.LastRssi2;
}
else
{
rssi0 = pAd->StaCfg.RssiSample.AvgRssi0;
rssi1 = pAd->StaCfg.RssiSample.AvgRssi1;
rssi2 = pAd->StaCfg.RssiSample.AvgRssi2;
}
if(pAd->Antenna.field.RxPath == 3)
{
larger = max(rssi0, rssi1);
if (larger > (rssi2 + 20))
pAd->Mlme.RealRxPath = 2;
else
pAd->Mlme.RealRxPath = 3;
}
else if(pAd->Antenna.field.RxPath == 2)
{
if (rssi0 > (rssi1 + 20))
pAd->Mlme.RealRxPath = 1;
else
pAd->Mlme.RealRxPath = 2;
}
RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R3, &BBPR3);
BBPR3 &= (~0x18);
if(pAd->Mlme.RealRxPath == 3)
{
BBPR3 |= (0x10);
}
else if(pAd->Mlme.RealRxPath == 2)
{
BBPR3 |= (0x8);
}
else if(pAd->Mlme.RealRxPath == 1)
{
BBPR3 |= (0x0);
}
RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R3, BBPR3);
}
}
#endif /* RT30xx */
}
VOID APSDPeriodicExec(
IN PVOID SystemSpecific1,
IN PVOID FunctionContext,
IN PVOID SystemSpecific2,
IN PVOID SystemSpecific3)
{
RTMP_ADAPTER *pAd = (RTMP_ADAPTER *)FunctionContext;
if (!OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_MEDIA_STATE_CONNECTED))
return;
pAd->CommonCfg.TriggerTimerCount++;
}
/*
========================================================================
Routine Description:
Set/reset MAC registers according to bPiggyBack parameter
Arguments:
pAd - Adapter pointer
bPiggyBack - Enable / Disable Piggy-Back
Return Value:
None
========================================================================
*/
VOID RTMPSetPiggyBack(
IN PRTMP_ADAPTER pAd,
IN BOOLEAN bPiggyBack)
{
TX_LINK_CFG_STRUC TxLinkCfg;
RTMP_IO_READ32(pAd, TX_LINK_CFG, &TxLinkCfg.word);
TxLinkCfg.field.TxCFAckEn = bPiggyBack;
RTMP_IO_WRITE32(pAd, TX_LINK_CFG, TxLinkCfg.word);
}
/*
========================================================================
Routine Description:
check if this entry need to switch rate automatically
Arguments:
pAd
pEntry
Return Value:
TURE
FALSE
========================================================================
*/
BOOLEAN RTMPCheckEntryEnableAutoRateSwitch(
IN PRTMP_ADAPTER pAd,
IN PMAC_TABLE_ENTRY pEntry)
{
BOOLEAN result = TRUE;
{
// only associated STA counts
if (pEntry && (pEntry->ValidAsCLI) && (pEntry->Sst == SST_ASSOC))
{
result = pAd->StaCfg.bAutoTxRateSwitch;
}
else
result = FALSE;
}
return result;
}
BOOLEAN RTMPAutoRateSwitchCheck(
IN PRTMP_ADAPTER pAd)
{
if (pAd->StaCfg.bAutoTxRateSwitch)
return TRUE;
return FALSE;
}
/*
========================================================================
Routine Description:
check if this entry need to fix tx legacy rate
Arguments:
pAd
pEntry
Return Value:
TURE
FALSE
========================================================================
*/
UCHAR RTMPStaFixedTxMode(
IN PRTMP_ADAPTER pAd,
IN PMAC_TABLE_ENTRY pEntry)
{
UCHAR tx_mode = FIXED_TXMODE_HT;
tx_mode = (UCHAR)pAd->StaCfg.DesiredTransmitSetting.field.FixedTxMode;
return tx_mode;
}
/*
========================================================================
Routine Description:
Overwrite HT Tx Mode by Fixed Legency Tx Mode, if specified.
Arguments:
pAd
pEntry
Return Value:
TURE
FALSE
========================================================================
*/
VOID RTMPUpdateLegacyTxSetting(
UCHAR fixed_tx_mode,
PMAC_TABLE_ENTRY pEntry)
{
HTTRANSMIT_SETTING TransmitSetting;
if (fixed_tx_mode == FIXED_TXMODE_HT)
return;
TransmitSetting.word = 0;
TransmitSetting.field.MODE = pEntry->HTPhyMode.field.MODE;
TransmitSetting.field.MCS = pEntry->HTPhyMode.field.MCS;
if (fixed_tx_mode == FIXED_TXMODE_CCK)
{
TransmitSetting.field.MODE = MODE_CCK;
// CCK mode allow MCS 0~3
if (TransmitSetting.field.MCS > MCS_3)
TransmitSetting.field.MCS = MCS_3;
}
else
{
TransmitSetting.field.MODE = MODE_OFDM;
// OFDM mode allow MCS 0~7
if (TransmitSetting.field.MCS > MCS_7)
TransmitSetting.field.MCS = MCS_7;
}
if (pEntry->HTPhyMode.field.MODE >= TransmitSetting.field.MODE)
{
pEntry->HTPhyMode.word = TransmitSetting.word;
DBGPRINT(RT_DEBUG_TRACE, ("RTMPUpdateLegacyTxSetting : wcid-%d, MODE=%s, MCS=%d \n",
pEntry->Aid, GetPhyMode(pEntry->HTPhyMode.field.MODE), pEntry->HTPhyMode.field.MCS));
}
}
/*
==========================================================================
Description:
dynamic tune BBP R66 to find a balance between sensibility and
noise isolation
IRQL = DISPATCH_LEVEL
==========================================================================
*/
VOID AsicStaBbpTuning(
IN PRTMP_ADAPTER pAd)
{
UCHAR OrigR66Value = 0, R66;//, R66UpperBound = 0x30, R66LowerBound = 0x30;
CHAR Rssi;
// 2860C did not support Fase CCA, therefore can't tune
if (pAd->MACVersion == 0x28600100)
return;
//
// work as a STA
//
if (pAd->Mlme.CntlMachine.CurrState != CNTL_IDLE) // no R66 tuning when SCANNING
return;
if ((pAd->OpMode == OPMODE_STA)
&& (OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_MEDIA_STATE_CONNECTED)
)
&& !(OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_DOZE))
#ifdef RT2860
&& (pAd->bPCIclkOff == FALSE))
#endif
#ifdef RT2870
)
#endif
{
RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R66, &OrigR66Value);
R66 = OrigR66Value;
if (pAd->Antenna.field.RxPath > 1)
Rssi = (pAd->StaCfg.RssiSample.AvgRssi0 + pAd->StaCfg.RssiSample.AvgRssi1) >> 1;
else
Rssi = pAd->StaCfg.RssiSample.AvgRssi0;
if (pAd->LatchRfRegs.Channel <= 14)
{ //BG band
#ifdef RT2870
// RT3070 is a no LNA solution, it should have different control regarding to AGC gain control
// Otherwise, it will have some throughput side effect when low RSSI
#ifndef RT30xx
if (IS_RT3070(pAd))
#endif
#ifdef RT30xx
if (IS_RT30xx(pAd))
#endif
{
if (Rssi > RSSI_FOR_MID_LOW_SENSIBILITY)
{
R66 = 0x1C + 2*GET_LNA_GAIN(pAd) + 0x20;
if (OrigR66Value != R66)
{
#ifndef RT30xx
RTUSBWriteBBPRegister(pAd, BBP_R66, R66);
#endif
#ifdef RT30xx
RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R66, R66);
#endif
}
}
else
{
R66 = 0x1C + 2*GET_LNA_GAIN(pAd);
if (OrigR66Value != R66)
{
#ifndef RT30xx
RTUSBWriteBBPRegister(pAd, BBP_R66, R66);
#endif
#ifdef RT30xx
RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R66, R66);
#endif
}
}
}
else
#endif // RT2870 //
{
if (Rssi > RSSI_FOR_MID_LOW_SENSIBILITY)
{
R66 = (0x2E + GET_LNA_GAIN(pAd)) + 0x10;
if (OrigR66Value != R66)
{
RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R66, R66);
}
}
else
{
R66 = 0x2E + GET_LNA_GAIN(pAd);
if (OrigR66Value != R66)
{
RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R66, R66);
}
}
}
}
else
{ //A band
if (pAd->CommonCfg.BBPCurrentBW == BW_20)
{
if (Rssi > RSSI_FOR_MID_LOW_SENSIBILITY)
{
R66 = 0x32 + (GET_LNA_GAIN(pAd)*5)/3 + 0x10;
if (OrigR66Value != R66)
{
RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R66, R66);
}
}
else
{
R66 = 0x32 + (GET_LNA_GAIN(pAd)*5)/3;
if (OrigR66Value != R66)
{
RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R66, R66);
}
}
}
else
{
if (Rssi > RSSI_FOR_MID_LOW_SENSIBILITY)
{
R66 = 0x3A + (GET_LNA_GAIN(pAd)*5)/3 + 0x10;
if (OrigR66Value != R66)
{
RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R66, R66);
}
}
else
{
R66 = 0x3A + (GET_LNA_GAIN(pAd)*5)/3;
if (OrigR66Value != R66)
{
RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R66, R66);
}
}
}
}
}
}
#ifdef RT2860
VOID AsicResetFromDMABusy(
IN PRTMP_ADAPTER pAd)
{
UINT32 Data;
BOOLEAN bCtrl = FALSE;
DBGPRINT(RT_DEBUG_TRACE, ("---> AsicResetFromDMABusy !!!!!!!!!!!!!!!!!!!!!!! \n"));
// Be sure restore link control value so we can write register.
RTMP_CLEAR_PSFLAG(pAd, fRTMP_PS_CAN_GO_SLEEP);
if (RTMP_TEST_PSFLAG(pAd, fRTMP_PS_SET_PCI_CLK_OFF_COMMAND))
{
DBGPRINT(RT_DEBUG_TRACE,("AsicResetFromDMABusy==>\n"));
RTMPPCIeLinkCtrlValueRestore(pAd, RESTORE_HALT);
RTMPusecDelay(6000);
pAd->bPCIclkOff = FALSE;
bCtrl = TRUE;
}
// Reset DMA
RTMP_IO_READ32(pAd, PBF_SYS_CTRL, &Data);
Data |= 0x2;
RTMP_IO_WRITE32(pAd, PBF_SYS_CTRL, Data);
// After Reset DMA, DMA index will become Zero. So Driver need to reset all ring indexs too.
// Reset DMA/CPU ring index
RTMPRingCleanUp(pAd, QID_AC_BK);
RTMPRingCleanUp(pAd, QID_AC_BE);
RTMPRingCleanUp(pAd, QID_AC_VI);
RTMPRingCleanUp(pAd, QID_AC_VO);
RTMPRingCleanUp(pAd, QID_HCCA);
RTMPRingCleanUp(pAd, QID_MGMT);
RTMPRingCleanUp(pAd, QID_RX);
// Clear Reset
RTMP_IO_READ32(pAd, PBF_SYS_CTRL, &Data);
Data &= 0xfffffffd;
RTMP_IO_WRITE32(pAd, PBF_SYS_CTRL, Data);
// If in Radio off, should call RTMPPCIePowerLinkCtrl again.
if ((bCtrl == TRUE) && (pAd->StaCfg.bRadio == FALSE))
RTMPPCIeLinkCtrlSetting(pAd, 3);
RTMP_SET_PSFLAG(pAd, fRTMP_PS_CAN_GO_SLEEP);
RTMP_CLEAR_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST | fRTMP_ADAPTER_HALT_IN_PROGRESS);
DBGPRINT(RT_DEBUG_TRACE, ("<--- AsicResetFromDMABusy !!!!!!!!!!!!!!!!!!!!!!! \n"));
}
VOID AsicResetBBP(
IN PRTMP_ADAPTER pAd)
{
DBGPRINT(RT_DEBUG_TRACE, ("---> Asic HardReset BBP !!!!!!!!!!!!!!!!!!!!!!! \n"));
RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, 0x0);
RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, 0x2);
RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, 0xc);
// After hard-reset BBP, initialize all BBP values.
NICRestoreBBPValue(pAd);
DBGPRINT(RT_DEBUG_TRACE, ("<--- Asic HardReset BBP !!!!!!!!!!!!!!!!!!!!!!! \n"));
}
VOID AsicResetMAC(
IN PRTMP_ADAPTER pAd)
{
ULONG Data;
DBGPRINT(RT_DEBUG_TRACE, ("---> AsicResetMAC !!!! \n"));
RTMP_IO_READ32(pAd, PBF_SYS_CTRL, &Data);
Data |= 0x4;
RTMP_IO_WRITE32(pAd, PBF_SYS_CTRL, Data);
Data &= 0xfffffffb;
RTMP_IO_WRITE32(pAd, PBF_SYS_CTRL, Data);
DBGPRINT(RT_DEBUG_TRACE, ("<--- AsicResetMAC !!!! \n"));
}
VOID AsicResetPBF(
IN PRTMP_ADAPTER pAd)
{
ULONG Value1, Value2;
ULONG Data;
RTMP_IO_READ32(pAd, TXRXQ_PCNT, &Value1);
RTMP_IO_READ32(pAd, PBF_DBG, &Value2);
Value2 &= 0xff;
// sum should be equals to 0xff, which is the total buffer size.
if ((Value1 + Value2) < 0xff)
{
DBGPRINT(RT_DEBUG_TRACE, ("---> Asic HardReset PBF !!!! \n"));
RTMP_IO_READ32(pAd, PBF_SYS_CTRL, &Data);
Data |= 0x8;
RTMP_IO_WRITE32(pAd, PBF_SYS_CTRL, Data);
Data &= 0xfffffff7;
RTMP_IO_WRITE32(pAd, PBF_SYS_CTRL, Data);
DBGPRINT(RT_DEBUG_TRACE, ("<--- Asic HardReset PBF !!!! \n"));
}
}
#endif /* RT2860 */
VOID RTMPSetAGCInitValue(
IN PRTMP_ADAPTER pAd,
IN UCHAR BandWidth)
{
UCHAR R66 = 0x30;
if (pAd->LatchRfRegs.Channel <= 14)
{ // BG band
R66 = 0x2E + GET_LNA_GAIN(pAd);
RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R66, R66);
}
else
{ //A band
if (BandWidth == BW_20)
{
R66 = (UCHAR)(0x32 + (GET_LNA_GAIN(pAd)*5)/3);
RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R66, R66);
}
else
{
R66 = (UCHAR)(0x3A + (GET_LNA_GAIN(pAd)*5)/3);
RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R66, R66);
}
}
}
VOID AsicTurnOffRFClk(
IN PRTMP_ADAPTER pAd,
IN UCHAR Channel)
{
// RF R2 bit 18 = 0
UINT32 R1 = 0, R2 = 0, R3 = 0;
UCHAR index;
RTMP_RF_REGS *RFRegTable;
#ifdef RT30xx
// The RF programming sequence is difference between 3xxx and 2xxx
if (IS_RT3090(pAd))
{
RT30xxLoadRFSleepModeSetup(pAd); // add by johnli, RF power sequence setup, load RF sleep-mode setup
}
else
{
#endif // RT30xx //
RFRegTable = RF2850RegTable;
switch (pAd->RfIcType)
{
case RFIC_2820:
case RFIC_2850:
case RFIC_2720:
case RFIC_2750:
for (index = 0; index < NUM_OF_2850_CHNL; index++)
{
if (Channel == RFRegTable[index].Channel)
{
R1 = RFRegTable[index].R1 & 0xffffdfff;
R2 = RFRegTable[index].R2 & 0xfffbffff;
R3 = RFRegTable[index].R3 & 0xfff3ffff;
RTMP_RF_IO_WRITE32(pAd, R1);
RTMP_RF_IO_WRITE32(pAd, R2);
// Program R1b13 to 1, R3/b18,19 to 0, R2b18 to 0.
// Set RF R2 bit18=0, R3 bit[18:19]=0
//if (pAd->StaCfg.bRadio == FALSE)
if (1)
{
RTMP_RF_IO_WRITE32(pAd, R3);
DBGPRINT(RT_DEBUG_TRACE, ("AsicTurnOffRFClk#%d(RF=%d, ) , R2=0x%08x, R3 = 0x%08x \n",
Channel, pAd->RfIcType, R2, R3));
}
else
DBGPRINT(RT_DEBUG_TRACE, ("AsicTurnOffRFClk#%d(RF=%d, ) , R2=0x%08x \n",
Channel, pAd->RfIcType, R2));
break;
}
}
break;
default:
break;
}
#ifdef RT30xx
}
#endif // RT30xx //
}
VOID AsicTurnOnRFClk(
IN PRTMP_ADAPTER pAd,
IN UCHAR Channel)
{
// RF R2 bit 18 = 0
UINT32 R1 = 0, R2 = 0, R3 = 0;
UCHAR index;
RTMP_RF_REGS *RFRegTable;
#ifdef RT30xx
// The RF programming sequence is difference between 3xxx and 2xxx
if (IS_RT3090(pAd))
{
}
else
{
#endif // RT30xx //
RFRegTable = RF2850RegTable;
switch (pAd->RfIcType)
{
case RFIC_2820:
case RFIC_2850:
case RFIC_2720:
case RFIC_2750:
for (index = 0; index < NUM_OF_2850_CHNL; index++)
{
if (Channel == RFRegTable[index].Channel)
{
R3 = pAd->LatchRfRegs.R3;
R3 &= 0xfff3ffff;
R3 |= 0x00080000;
RTMP_RF_IO_WRITE32(pAd, R3);
R1 = RFRegTable[index].R1;
RTMP_RF_IO_WRITE32(pAd, R1);
R2 = RFRegTable[index].R2;
if (pAd->Antenna.field.TxPath == 1)
{
R2 |= 0x4000; // If TXpath is 1, bit 14 = 1;
}
if (pAd->Antenna.field.RxPath == 2)
{
R2 |= 0x40; // write 1 to off Rxpath.
}
else if (pAd->Antenna.field.RxPath == 1)
{
R2 |= 0x20040; // write 1 to off RxPath
}
RTMP_RF_IO_WRITE32(pAd, R2);
break;
}
}
break;
default:
break;
}
#ifndef RT30xx
DBGPRINT(RT_DEBUG_TRACE, ("AsicTurnOnRFClk#%d(RF=%d, ) , R2=0x%08x\n",
Channel,
pAd->RfIcType,
R2));
#endif
#ifdef RT30xx
}
#endif // RT30xx //
}
|