summaryrefslogtreecommitdiff
path: root/drivers/net/wireless/sd8797/mlan/mlan_11h.c
blob: 23970861db1e0027876a53cc5d0dedef4307eab9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
/** @file mlan_11h.c
 *
 *  @brief This file contains functions for 802.11H.
 *
 *  Copyright (C) 2008-2011, Marvell International Ltd.
 *
 *  This software file (the "File") is distributed by Marvell International
 *  Ltd. under the terms of the GNU General Public License Version 2, June 1991
 *  (the "License").  You may use, redistribute and/or modify this File in
 *  accordance with the terms and conditions of the License, a copy of which
 *  is available by writing to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
 *  worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
 *
 *  THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
 *  IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
 *  ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
 *  this warranty disclaimer.
 *
 */

/*************************************************************
Change Log:
    03/26/2009: initial version
************************************************************/

#include "mlan.h"
#include "mlan_join.h"
#include "mlan_util.h"
#include "mlan_fw.h"
#include "mlan_main.h"
#include "mlan_ioctl.h"
#include "mlan_11h.h"
#ifdef UAP_SUPPORT
#include "mlan_uap.h"
#endif

/********************************************************
                Local Variables
********************************************************/

/** Default IBSS DFS recovery interval (in TBTTs); used for adhoc start */
#define WLAN_11H_DEFAULT_DFS_RECOVERY_INTERVAL   100

/** Default 11h power constraint used to offset the maximum transmit power */
#define WLAN_11H_TPC_POWERCONSTRAINT  0

/** 11h TPC Power capability minimum setting, sent in TPC_INFO command to fw */
#define WLAN_11H_TPC_POWERCAPABILITY_MIN     5

/** 11h TPC Power capability maximum setting, sent in TPC_INFO command to fw */
#define WLAN_11H_TPC_POWERCAPABILITY_MAX     20

/** Regulatory requirement for the duration of a channel availability check */
#define WLAN_11H_CHANNEL_AVAIL_CHECK_DURATION    60000  /* in ms */

/** Starting Frequency for 11A band */
#define START_FREQ_11A_BAND     5000    /* in MHz */

/** Regulatory requirement for the duration of a non-occupancy period */
#define WLAN_11H_NON_OCCUPANCY_PERIOD    1800   /* in sec (30mins) */

/** Maximum allowable age (seconds) on DFS report data */
#define MAX_DFS_REPORT_USABLE_AGE_SEC  (120)    // 2 minutes

/** Minimum delay for CHAN_SW IE to broadcast by FW */
#define MIN_RDH_CHAN_SW_IE_PERIOD_MSEC  (500)   // 5 beacons @ 100ms

/** Maximum delay for CHAN_SW IE to broadcast by FW */
#define MAX_RDH_CHAN_SW_IE_PERIOD_MSEC  (3000)  // 5 beacons @ 600ms

/** Maximum retries on selecting new random channel */
#define MAX_RANDOM_CHANNEL_RETRIES  (20)

/** Maximum retries on selecting new random non-dfs channel */
#define MAX_SWITCH_CHANNEL_RETRIES  (30)

/** Value for undetermined priv_curr_idx on first entry to new RDH stage */
#define RDH_STAGE_FIRST_ENTRY_PRIV_IDX  (0xff)

/** Region codes 0x10, 0x20:  channels 1 thru 11 supported */
static const
    IEEEtypes_SupportChan_Subband_t wlan_11h_2_4G_region_FCC = { 1, 11 };

/** Region codes 0x30, 0x32, 0x41, 0x50:  channels 1 thru 13 supported */
static const
    IEEEtypes_SupportChan_Subband_t wlan_11h_2_4G_region_EU = { 1, 13 };

/** Region code 0x40:  only channel 14 supported */
static const
    IEEEtypes_SupportChan_Subband_t wlan_11h_2_4G_region_JPN40 = { 14, 1 };

/** JPN sub-band config : Start Channel = 8, NumChans = 3 */
static const
    IEEEtypes_SupportChan_Subband_t wlan_11h_JPN_bottom_band = { 8, 3 };

/** U-NII sub-band config : Start Channel = 36, NumChans = 4 */
static const
    IEEEtypes_SupportChan_Subband_t wlan_11h_unii_lower_band = { 36, 4 };

/** U-NII sub-band config : Start Channel = 52, NumChans = 4 */
static const
    IEEEtypes_SupportChan_Subband_t wlan_11h_unii_middle_band = { 52, 4 };

/** U-NII sub-band config : Start Channel = 100, NumChans = 11 */
static const
    IEEEtypes_SupportChan_Subband_t wlan_11h_unii_mid_upper_band = { 100, 11 };

/** U-NII sub-band config : Start Channel = 149, NumChans = 5 */
static const
    IEEEtypes_SupportChan_Subband_t wlan_11h_unii_upper_band = { 149, 5 };

/** Internally passed structure used to send a CMD_802_11_TPC_INFO command */
typedef struct
{
    t_u8 chan;                  /**< Channel to which the power constraint applies */
    t_u8 power_constraint;      /**< Local power constraint to send to firmware */
} wlan_11h_tpc_info_param_t;

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

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

/**
 *  @brief Utility function to get a random number based on the underlying OS
 *
 *  @param pmadapter Pointer to mlan_adapter
 *  @return random integer
 */
static t_u32
wlan_11h_get_random_num(pmlan_adapter pmadapter)
{
    t_u32 sec, usec;

    ENTER();
    pmadapter->callbacks.moal_get_system_time(pmadapter->pmoal_handle, &sec,
                                              &usec);
    sec = (sec & 0xFFFF) + (sec >> 16);
    usec = (usec & 0xFFFF) + (usec >> 16);

    LEAVE();
    return ((usec << 16) | sec);
}

/**
 *  @brief Convert an IEEE formatted IE to 16-bit ID/Len Marvell
 *         proprietary format
 *
 *  @param pmadapter Pointer to mlan_adapter
 *  @param pout_buf Output parameter: Buffer to output Marvell formatted IE
 *  @param pin_ie   Pointer to IEEE IE to be converted to Marvell format
 *
 *  @return         Number of bytes output to pout_buf parameter return
 */
static t_u32
wlan_11h_convert_ieee_to_mrvl_ie(mlan_adapter * pmadapter,
                                 t_u8 * pout_buf, const t_u8 * pin_ie)
{
    MrvlIEtypesHeader_t mrvl_ie_hdr;
    t_u8 *ptmp_buf = pout_buf;

    ENTER();
    /* Assign the Element Id and Len to the Marvell struct attributes */
    mrvl_ie_hdr.type = wlan_cpu_to_le16(pin_ie[0]);
    mrvl_ie_hdr.len = wlan_cpu_to_le16(pin_ie[1]);

    /* If the element ID is zero, return without doing any copying */
    if (!mrvl_ie_hdr.type) {
        LEAVE();
        return 0;
    }

    /* Copy the header to the buffer pointer */
    memcpy(pmadapter, ptmp_buf, &mrvl_ie_hdr, sizeof(mrvl_ie_hdr));

    /* Increment the temp buffer pointer by the size appended */
    ptmp_buf += sizeof(mrvl_ie_hdr);

    /* Append the data section of the IE; length given by the IEEE IE length */
    memcpy(pmadapter, ptmp_buf, pin_ie + 2, pin_ie[1]);

    LEAVE();
    /* Return the number of bytes appended to pout_buf */
    return (sizeof(mrvl_ie_hdr) + pin_ie[1]);
}

#ifdef STA_SUPPORT
/**
 *  @brief Setup the IBSS DFS element passed to the firmware in adhoc start
 *         and join commands
 *
 *  The DFS Owner and recovery fields are set to be our MAC address and
 *    a predetermined constant recovery value.  If we are joining an adhoc
 *    network, these values are replaced with the existing IBSS values.
 *    They are valid only when starting a new IBSS.
 *
 *  The IBSS DFS Element is variable in size based on the number of
 *    channels supported in our current region.
 *
 *  @param priv Private driver information structure
 *  @param pdfs Output parameter: Pointer to the IBSS DFS element setup by
 *              this function.
 *
 *  @return
 *    - Length of the returned element in pdfs output parameter
 *    - 0 if returned element is not setup
 */
static t_u32
wlan_11h_set_ibss_dfs_ie(mlan_private * priv, IEEEtypes_IBSS_DFS_t * pdfs)
{
    t_u8 num_chans = 0;
    MeasRptBasicMap_t initial_map;
    mlan_adapter *adapter = priv->adapter;

    ENTER();

    memset(adapter, pdfs, 0x00, sizeof(IEEEtypes_IBSS_DFS_t));

    /* 
     * A basic measurement report is included with each channel in the
     *   map field.  Initial value for the map for each supported channel
     *   is with only the unmeasured bit set.
     */
    memset(adapter, &initial_map, 0x00, sizeof(initial_map));
    initial_map.unmeasured = 1;

    /* Set the DFS Owner and recovery interval fields */
    memcpy(adapter, pdfs->dfs_owner, priv->curr_addr, sizeof(pdfs->dfs_owner));
    pdfs->dfs_recovery_interval = WLAN_11H_DEFAULT_DFS_RECOVERY_INTERVAL;

    for (; (num_chans < adapter->parsed_region_chan.no_of_chan)
         && (num_chans < WLAN_11H_MAX_IBSS_DFS_CHANNELS); num_chans++) {
        pdfs->channel_map[num_chans].channel_number =
            adapter->parsed_region_chan.chan_pwr[num_chans].chan;

        /* 
         * Set the initial map field with a basic measurement
         */
        pdfs->channel_map[num_chans].rpt_map = initial_map;
    }

    /* 
     * If we have an established channel map, include it and return
     *   a valid DFS element
     */
    if (num_chans) {
        PRINTM(MINFO, "11h: Added %d channels to IBSS DFS Map\n", num_chans);

        pdfs->element_id = IBSS_DFS;
        pdfs->len =
            (sizeof(pdfs->dfs_owner) + sizeof(pdfs->dfs_recovery_interval)
             + num_chans * sizeof(IEEEtypes_ChannelMap_t));

        LEAVE();
        return (pdfs->len + sizeof(pdfs->len) + sizeof(pdfs->element_id));
    }

    /* Ensure the element is zeroed out for an invalid return */
    memset(adapter, pdfs, 0x00, sizeof(IEEEtypes_IBSS_DFS_t));

    LEAVE();
    return 0;
}
#endif

/**
 *  @brief Setup the Supported Channel IE sent in association requests
 *
 *  The Supported Channels IE is required to be sent when the spectrum
 *    management capability (11h) is enabled.  The element contains a
 *    starting channel and number of channels tuple for each sub-band
 *    the STA supports.  This information is based on the operating region.
 *
 *  @param priv      Private driver information structure
 *  @param band      Band in use
 *  @param psup_chan Output parameter: Pointer to the Supported Chan element
 *                   setup by this function.
 *
 *  @return
 *    - Length of the returned element in psup_chan output parameter
 *    - 0 if returned element is not setup
 */
static t_u16
wlan_11h_set_supp_channels_ie(mlan_private * priv,
                              t_u8 band,
                              IEEEtypes_SupportedChannels_t * psup_chan)
{
    t_u16 num_subbands = 0;
    t_u16 ret_len = 0;
    t_u8 cfp_bg, cfp_a;

    ENTER();
    memset(priv->adapter, psup_chan, 0x00,
           sizeof(IEEEtypes_SupportedChannels_t));

    cfp_bg = cfp_a = priv->adapter->region_code;

    if ((band & BAND_B) || (band & BAND_G)) {
        /* 
         * Channels are contiguous in 2.4GHz, usually only one subband.
         */
        switch (cfp_bg) {
        case 0x10:             /* USA FCC */
        case 0x20:             /* Canada IC */
        default:
            psup_chan->subband[num_subbands++] = wlan_11h_2_4G_region_FCC;
            break;
        case 0x30:             /* Europe ETSI */
        case 0x32:             /* France */
        case 0x41:             /* Japan */
        case 0x50:             /* China */
            psup_chan->subband[num_subbands++] = wlan_11h_2_4G_region_EU;
            break;
        case 0x40:             /* Japan */
            psup_chan->subband[num_subbands++] = wlan_11h_2_4G_region_JPN40;
            break;
        case 0xff:             /* Japan special */
            psup_chan->subband[num_subbands++] = wlan_11h_2_4G_region_EU;
            psup_chan->subband[num_subbands++] = wlan_11h_2_4G_region_JPN40;
            break;
        }
    } else if (band & BAND_A) {
        /* 
         * Set the supported channel elements based on the region code,
         *   incrementing num_subbands for each sub-band we append to the
         *   element.
         */
        switch (cfp_a) {
        case 0x10:             /* USA FCC */
        case 0x20:             /* Canada IC */
        case 0x32:             /* France */
            psup_chan->subband[num_subbands++] = wlan_11h_unii_lower_band;
            psup_chan->subband[num_subbands++] = wlan_11h_unii_middle_band;
            psup_chan->subband[num_subbands++] = wlan_11h_unii_mid_upper_band;
            psup_chan->subband[num_subbands++] = wlan_11h_unii_upper_band;
            break;
        case 0x30:             /* Europe ETSI */
        default:
            psup_chan->subband[num_subbands++] = wlan_11h_unii_lower_band;
            psup_chan->subband[num_subbands++] = wlan_11h_unii_middle_band;
            psup_chan->subband[num_subbands++] = wlan_11h_unii_mid_upper_band;
            break;
        case 0x50:             /* China */
            psup_chan->subband[num_subbands++] = wlan_11h_unii_upper_band;
            break;
        case 0x40:             /* Japan */
        case 0x41:             /* Japan */
        case 0xff:             /* Japan special */
            psup_chan->subband[num_subbands++] = wlan_11h_JPN_bottom_band;
            psup_chan->subband[num_subbands++] = wlan_11h_unii_lower_band;
            psup_chan->subband[num_subbands++] = wlan_11h_unii_middle_band;
            psup_chan->subband[num_subbands++] = wlan_11h_unii_mid_upper_band;
            break;
        }
    }

    /* 
     * If we have setup any supported subbands in the element, return a
     *    valid IE along with its size, else return 0.
     */
    if (num_subbands) {
        psup_chan->element_id = SUPPORTED_CHANNELS;
        psup_chan->len = num_subbands * sizeof(IEEEtypes_SupportChan_Subband_t);

        ret_len = (t_u16) (psup_chan->len
                           + sizeof(psup_chan->len) +
                           sizeof(psup_chan->element_id));

        HEXDUMP("11h: SupChan", (t_u8 *) psup_chan, ret_len);
    }

    LEAVE();
    return ret_len;
}

/**
 *  @brief Prepare CMD_802_11_TPC_ADAPT_REQ firmware command
 *
 *  @param priv      Private driver information structure
 *  @param pcmd_ptr  Output parameter: Pointer to the command being prepared
 *                   for the firmware
 *  @param pinfo_buf HostCmd_DS_802_11_TPC_ADAPT_REQ passed as void data block
 *
 *  @return          MLAN_STATUS_SUCCESS
 */
static mlan_status
wlan_11h_cmd_tpc_request(mlan_private * priv,
                         HostCmd_DS_COMMAND * pcmd_ptr,
                         const t_void * pinfo_buf)
{
    ENTER();

    memcpy(priv->adapter, &pcmd_ptr->params.tpc_req, pinfo_buf,
           sizeof(HostCmd_DS_802_11_TPC_ADAPT_REQ));

    pcmd_ptr->params.tpc_req.req.timeout =
        wlan_cpu_to_le16(pcmd_ptr->params.tpc_req.req.timeout);

    /* Converted to little endian in wlan_11h_cmd_process */
    pcmd_ptr->size = sizeof(HostCmd_DS_802_11_TPC_ADAPT_REQ) + S_DS_GEN;

    HEXDUMP("11h: 11_TPC_ADAPT_REQ:", (t_u8 *) pcmd_ptr,
            (t_u32) pcmd_ptr->size);

    LEAVE();
    return MLAN_STATUS_SUCCESS;
}

/**
 *  @brief Prepare CMD_802_11_TPC_INFO firmware command
 *
 *  @param priv      Private driver information structure
 *  @param pcmd_ptr  Output parameter: Pointer to the command being prepared
 *                   for the firmware
 *  @param pinfo_buf wlan_11h_tpc_info_param_t passed as void data block
 *
 *  @return          MLAN_STATUS_SUCCESS
 */
static mlan_status
wlan_11h_cmd_tpc_info(mlan_private * priv,
                      HostCmd_DS_COMMAND * pcmd_ptr, const t_void * pinfo_buf)
{
    HostCmd_DS_802_11_TPC_INFO *ptpc_info = &pcmd_ptr->params.tpc_info;
    MrvlIEtypes_LocalPowerConstraint_t *pconstraint =
        &ptpc_info->local_constraint;
    MrvlIEtypes_PowerCapability_t *pcap = &ptpc_info->power_cap;

    wlan_11h_device_state_t *pstate = &priv->adapter->state_11h;
    const wlan_11h_tpc_info_param_t *ptpc_info_param =
        (wlan_11h_tpc_info_param_t *) pinfo_buf;

    ENTER();

    pcap->min_power = pstate->min_tx_power_capability;
    pcap->max_power = pstate->max_tx_power_capability;
    pcap->header.len = wlan_cpu_to_le16(2);
    pcap->header.type = wlan_cpu_to_le16(TLV_TYPE_POWER_CAPABILITY);

    pconstraint->chan = ptpc_info_param->chan;
    pconstraint->constraint = ptpc_info_param->power_constraint;
    pconstraint->header.type = wlan_cpu_to_le16(TLV_TYPE_POWER_CONSTRAINT);
    pconstraint->header.len = wlan_cpu_to_le16(2);

    /* Converted to little endian in wlan_11h_cmd_process */
    pcmd_ptr->size = sizeof(HostCmd_DS_802_11_TPC_INFO) + S_DS_GEN;

    HEXDUMP("11h: TPC INFO", (t_u8 *) pcmd_ptr, (t_u32) pcmd_ptr->size);

    LEAVE();
    return MLAN_STATUS_SUCCESS;
}

/**
 *  @brief  Prepare CMD_802_11_CHAN_SW_ANN firmware command
 *
 *  @param priv      Private driver information structure
 *  @param pcmd_ptr  Output parameter: Pointer to the command being
 *                   prepared to for firmware
 *  @param pinfo_buf HostCmd_DS_802_11_CHAN_SW_ANN passed as void data block
 *
 *  @return          MLAN_STATUS_SUCCESS
 */
static mlan_status
wlan_11h_cmd_chan_sw_ann(mlan_private * priv,
                         HostCmd_DS_COMMAND * pcmd_ptr,
                         const t_void * pinfo_buf)
{
    const HostCmd_DS_802_11_CHAN_SW_ANN *pch_sw_ann =
        (HostCmd_DS_802_11_CHAN_SW_ANN *) pinfo_buf;

    ENTER();

    /* Converted to little endian in wlan_11h_cmd_process */
    pcmd_ptr->size = sizeof(HostCmd_DS_802_11_CHAN_SW_ANN) + S_DS_GEN;

    memcpy(priv->adapter, &pcmd_ptr->params.chan_sw_ann, pch_sw_ann,
           sizeof(HostCmd_DS_802_11_CHAN_SW_ANN));

    PRINTM(MINFO, "11h: ChSwAnn: %#x-%u, Seq=%u, Ret=%u\n",
           pcmd_ptr->command, pcmd_ptr->size, pcmd_ptr->seq_num,
           pcmd_ptr->result);
    PRINTM(MINFO, "11h: ChSwAnn: Ch=%d, Cnt=%d, Mode=%d\n",
           pch_sw_ann->new_chan, pch_sw_ann->switch_count,
           pch_sw_ann->switch_mode);

    LEAVE();
    return MLAN_STATUS_SUCCESS;
}

/**
 *  @brief  Prepare CMD_CHAN_REPORT_REQUEST firmware command
 *
 *  @param priv      Private driver information structure
 *  @param pcmd_ptr  Output parameter: Pointer to the command being
 *                   prepared to for firmware
 *  @param pinfo_buf HostCmd_DS_CHAN_RPT_REQ passed as void data block
 *
 *  @return          MLAN_STATUS_SUCCESS or MLAN_STATUS_PENDING
 */
static mlan_status
wlan_11h_cmd_chan_rpt_req(mlan_private * priv,
                          HostCmd_DS_COMMAND * pcmd_ptr,
                          const t_void * pinfo_buf)
{
    const HostCmd_DS_CHAN_RPT_REQ *pchan_rpt_req =
        (HostCmd_DS_CHAN_RPT_REQ *) pinfo_buf;
    wlan_dfs_device_state_t *pstate_dfs = &priv->adapter->state_dfs;
    MrvlIEtypes_ChanRpt11hBasic_t *ptlv_basic;

    ENTER();

    if (pstate_dfs->dfs_check_pending) {
        PRINTM(MERROR, "11h: ChanRptReq - previous CMD_CHAN_REPORT_REQUEST has"
               " not returned its result yet (as EVENT_CHANNEL_READY)."
               "  This command will be dropped.\n");
        LEAVE();
        return MLAN_STATUS_PENDING;
    }

    /* Converted to little endian in wlan_11h_cmd_process */
    pcmd_ptr->size = sizeof(HostCmd_DS_CHAN_RPT_REQ) + S_DS_GEN;

    memcpy(priv->adapter, &pcmd_ptr->params.chan_rpt_req, pchan_rpt_req,
           sizeof(HostCmd_DS_CHAN_RPT_REQ));

    /* if DFS channel, add BASIC report TLV, and set radar bit */
    if (wlan_11h_radar_detect_required(priv, pchan_rpt_req->chan_desc.chanNum)) {
        ptlv_basic =
            (MrvlIEtypes_ChanRpt11hBasic_t *) (((t_u8 *) (pcmd_ptr)) +
                                               pcmd_ptr->size);
        ptlv_basic->Header.type = wlan_cpu_to_le16(TLV_TYPE_CHANRPT_11H_BASIC);
        ptlv_basic->Header.len = wlan_cpu_to_le16(sizeof(MeasRptBasicMap_t));
        memset(priv->adapter, &ptlv_basic->map, 0, sizeof(MeasRptBasicMap_t));
        ptlv_basic->map.radar = 1;
        pcmd_ptr->size += sizeof(MrvlIEtypes_ChanRpt11hBasic_t);
    }

    /* update dfs sturcture. dfs_check_pending is set when we receive CMD_RESP
       == SUCCESS */
    pstate_dfs->dfs_check_pending = MFALSE;
    pstate_dfs->dfs_radar_found = MFALSE;
    pstate_dfs->dfs_check_channel = pchan_rpt_req->chan_desc.chanNum;

    LEAVE();
    return MLAN_STATUS_SUCCESS;
}

/**
 *  @brief Set the local power capability and constraint TLV
 *
 *  @param ppbuffer                The buffer to add these two TLVs
 *  @param channel                 Channel to which the power constraint applies
 *  @param power_constraint        Power constraint to be applied on the channel
 *  @param min_tx_power_capability Min. Tx Power in Power Capability IE
 *  @param max_tx_power_capability Max. Tx Power in Power Capability IE
 *
 *  @return                        The len increased
 */
static t_u32
wlan_11h_set_local_power_constraint_tlv(t_u8 ** ppbuffer,
                                        t_u8 channel,
                                        t_u8 power_constraint,
                                        t_u8 min_tx_power_capability,
                                        t_u8 max_tx_power_capability)
{
    MrvlIEtypes_PowerCapability_t *pcap;
    MrvlIEtypes_LocalPowerConstraint_t *pconstraint;
    t_u8 *startPtr = MNULL;

    ENTER();

    /* Null Checks */
    if ((ppbuffer == MNULL) || (((t_u8 *) (*ppbuffer)) == MNULL)) {
        LEAVE();
        return 0;
    }

    startPtr = (t_u8 *) (*ppbuffer);

    PRINTM(MINFO,
           "11h: Set local power constraint = %d channel=%d min_tx_pwr=%d max_tx_pwr=%d\n",
           power_constraint, channel, min_tx_power_capability,
           max_tx_power_capability);

    pcap = (MrvlIEtypes_PowerCapability_t *) * ppbuffer;
    pcap->header.type = wlan_cpu_to_le16(TLV_TYPE_POWER_CAPABILITY);
    pcap->header.len = wlan_cpu_to_le16(2);
    pcap->min_power = min_tx_power_capability;
    pcap->max_power = max_tx_power_capability;
    *ppbuffer += sizeof(MrvlIEtypesHeader_t) + 2;

    pconstraint = (MrvlIEtypes_LocalPowerConstraint_t *) * ppbuffer;
    pconstraint->header.type = wlan_cpu_to_le16(TLV_TYPE_POWER_CONSTRAINT);
    pconstraint->header.len = wlan_cpu_to_le16(2);
    pconstraint->chan = channel;
    pconstraint->constraint = power_constraint;
    *ppbuffer += sizeof(MrvlIEtypesHeader_t) + 2;

    LEAVE();
    return (t_u32) (*ppbuffer - startPtr);
}

/**
 *  @brief  Utility function to process a join to an infrastructure BSS
 *
 *  @param priv          Private driver information structure
 *  @param ppbuffer      Output parameter: Pointer to the TLV output buffer,
 *                       modified on return to point after the appended 11h TLVs
 *  @param band          Band on which we are joining the BSS
 *  @param channel       Channel on which we are joining the BSS
 *  @param p11h_bss_info Pointer to the 11h BSS information for this network
 *                       that was parsed out of the scan response.
 *
 *  @return              Integer number of bytes appended to the TLV output
 *                       buffer (ppbuffer)
 */
static t_u32
wlan_11h_process_infra_join(mlan_private * priv,
                            t_u8 ** ppbuffer,
                            t_u8 band,
                            t_u32 channel, wlan_11h_bss_info_t * p11h_bss_info)
{
    MrvlIEtypesHeader_t ie_header;
    IEEEtypes_SupportedChannels_t sup_chan_ie;
    t_u32 ret_len = 0;
    t_u16 sup_chan_len = 0;

    ENTER();

    /* Null Checks */
    if ((ppbuffer == MNULL) || (((t_u8 *) (*ppbuffer)) == MNULL)) {
        LEAVE();
        return 0;
    }

    ret_len += wlan_11h_set_local_power_constraint_tlv(ppbuffer, (t_u8) channel,
                                                       (t_u8) p11h_bss_info->
                                                       power_constraint.
                                                       local_constraint,
                                                       (t_u8) priv->adapter->
                                                       state_11h.
                                                       min_tx_power_capability,
                                                       (t_u8) priv->adapter->
                                                       state_11h.
                                                       max_tx_power_capability);

    /* Setup the Supported Channels IE */
    sup_chan_len = wlan_11h_set_supp_channels_ie(priv, band, &sup_chan_ie);

    /* 
     * If we returned a valid Supported Channels IE, wrap and append it
     */
    if (sup_chan_len) {
        /* Wrap the supported channels IE with a passthrough TLV type */
        ie_header.type = wlan_cpu_to_le16(TLV_TYPE_PASSTHROUGH);
        ie_header.len = sup_chan_len;
        memcpy(priv->adapter, *ppbuffer, &ie_header, sizeof(ie_header));

        /* Increment the return size and the return buffer pointer param */
        *ppbuffer += sizeof(ie_header);
        ret_len += sizeof(ie_header);

        /* Copy the supported channels IE to the output buf, advance pointer */
        memcpy(priv->adapter, *ppbuffer, &sup_chan_ie, sup_chan_len);
        *ppbuffer += sup_chan_len;
        ret_len += sup_chan_len;
    }

    LEAVE();
    return ret_len;
}

/**
 *  @brief Utility function to process a start or join to an adhoc network
 *
 *  Add the elements to the TLV buffer needed in the start/join adhoc commands:
 *       - IBSS DFS IE
 *       - Quiet IE
 *
 *  Also send the local constraint to the firmware in a TPC_INFO command.
 *
 *  @param priv          Private driver information structure
 *  @param ppbuffer      Output parameter: Pointer to the TLV output buffer,
 *                       modified on return to point after the appended 11h TLVs
 *  @param channel       Channel on which we are starting/joining the IBSS
 *  @param p11h_bss_info Pointer to the 11h BSS information for this network
 *                       that was parsed out of the scan response.  NULL
 *                       indicates we are starting the adhoc network
 *
 *  @return              Integer number of bytes appended to the TLV output
 *                       buffer (ppbuffer)
 */
static t_u32
wlan_11h_process_adhoc(mlan_private * priv,
                       t_u8 ** ppbuffer,
                       t_u32 channel, wlan_11h_bss_info_t * p11h_bss_info)
{
    IEEEtypes_IBSS_DFS_t dfs_elem;
    t_u32 size_appended;
    t_u32 ret_len = 0;
    t_s8 local_constraint = 0;
    mlan_adapter *adapter = priv->adapter;

    ENTER();

#ifdef STA_SUPPORT
    /* Format our own IBSS DFS Element.  Include our channel map fields */
    wlan_11h_set_ibss_dfs_ie(priv, &dfs_elem);
#endif

    if (p11h_bss_info) {
        /* 
         * Copy the DFS Owner/Recovery Interval from the BSS we are joining
         */
        memcpy(adapter, dfs_elem.dfs_owner,
               p11h_bss_info->ibss_dfs.dfs_owner, sizeof(dfs_elem.dfs_owner));
        dfs_elem.dfs_recovery_interval =
            p11h_bss_info->ibss_dfs.dfs_recovery_interval;
    }

    /* Append the dfs element to the TLV buffer */
    size_appended =
        wlan_11h_convert_ieee_to_mrvl_ie(adapter, (t_u8 *) * ppbuffer,
                                         (t_u8 *) & dfs_elem);

    HEXDUMP("11h: IBSS-DFS", (t_u8 *) * ppbuffer, size_appended);
    *ppbuffer += size_appended;
    ret_len += size_appended;

    /* 
     * Check to see if we are joining a network.  Join is indicated by the
     *   BSS Info pointer being valid (not NULL)
     */
    if (p11h_bss_info) {
        /* 
         * If there was a quiet element, include it in adhoc join command
         */
        if (p11h_bss_info->quiet.element_id == QUIET) {
            size_appended
                = wlan_11h_convert_ieee_to_mrvl_ie(adapter, (t_u8 *) * ppbuffer,
                                                   (t_u8 *) & p11h_bss_info->
                                                   quiet);
            HEXDUMP("11h: Quiet", (t_u8 *) * ppbuffer, size_appended);
            *ppbuffer += size_appended;
            ret_len += size_appended;
        }

        /* Copy the local constraint from the network */
        local_constraint = p11h_bss_info->power_constraint.local_constraint;
    } else {
        /* 
         * If we are the adhoc starter, we can add a quiet element
         */
        if (adapter->state_11h.quiet_ie.quiet_period) {
            size_appended =
                wlan_11h_convert_ieee_to_mrvl_ie(adapter, (t_u8 *) * ppbuffer,
                                                 (t_u8 *) & adapter->state_11h.
                                                 quiet_ie);
            HEXDUMP("11h: Quiet", (t_u8 *) * ppbuffer, size_appended);
            *ppbuffer += size_appended;
            ret_len += size_appended;
        }
        /* Use the local_constraint configured in the driver state */
        local_constraint = adapter->state_11h.usr_def_power_constraint;
    }

    PRINTM(MINFO, "WEILIE 1: ppbuffer = %p\n", *ppbuffer);

    ret_len +=
        wlan_11h_set_local_power_constraint_tlv(ppbuffer, (t_u8) channel,
                                                (t_u8) local_constraint,
                                                (t_u8) priv->adapter->state_11h.
                                                min_tx_power_capability,
                                                (t_u8) priv->adapter->state_11h.
                                                max_tx_power_capability);
    PRINTM(MINFO, "WEILIE 2: ppbuffer = %p\n", *ppbuffer);

    LEAVE();
    return ret_len;
}

/**
 *  @brief Return whether the driver has enabled 11h for the interface
 *
 *  Association/Join commands are dynamic in that they enable 11h in the
 *    driver/firmware when they are detected in the existing BSS.
 *
 *  @param priv  Private driver information structure
 *
 *  @return
 *    - MTRUE if 11h is enabled
 *    - MFALSE otherwise
 */
static t_bool
wlan_11h_is_enabled(mlan_private * priv)
{
    ENTER();
    LEAVE();
    return (priv->intf_state_11h.is_11h_enabled);
}

/**
 *  @brief Return whether the device has activated slave radar detection.
 *
 *  @param priv  Private driver information structure
 *
 *  @return
 *    - MTRUE if slave radar detection is enabled in firmware
 *    - MFALSE otherwise
 */
static t_bool
wlan_11h_is_slave_radar_det_active(mlan_private * priv)
{
    ENTER();
    LEAVE();
    return (priv->adapter->state_11h.is_slave_radar_det_active);
}

/**
 *  @brief Return whether the slave interface is active, and on DFS channel.
 *  priv is assumed to already be a dfs slave interface, doesn't check this.
 *
 *  @param priv  Private driver information structure
 *
 *  @return
 *    - MTRUE if priv is slave, and meets both conditions
 *    - MFALSE otherwise
 */
static t_bool
wlan_11h_is_slave_active_on_dfs_chan(mlan_private * priv)
{
    t_bool ret = MFALSE;

    ENTER();
    if ((priv->media_connected == MTRUE) &&
        (priv->curr_bss_params.band & BAND_A) &&
        wlan_11h_radar_detect_required(priv,
                                       priv->curr_bss_params.bss_descriptor.
                                       channel))
        ret = MTRUE;

    LEAVE();
    return ret;
}

/**
 *  @brief Return whether the master interface is active, and on DFS channel.
 *  priv is assumed to already be a dfs master interface, doesn't check this.
 *
 *  @param priv  Private driver information structure
 *
 *  @return
 *    - MTRUE if priv is master, and meets both conditions
 *    - MFALSE otherwise
 */
static t_bool
wlan_11h_is_master_active_on_dfs_chan(mlan_private * priv)
{
    t_bool ret = MFALSE;

    ENTER();
    if (GET_BSS_ROLE(priv) == MLAN_BSS_ROLE_STA) {
        /* Ad-hoc creator */
        if (((priv->media_connected == MTRUE)
             || (priv->adhoc_state == ADHOC_STARTING)) &&
            (priv->adapter->adhoc_start_band & BAND_A) &&
            wlan_11h_radar_detect_required(priv, priv->adhoc_channel))
            ret = MTRUE;
    } else if (GET_BSS_ROLE(priv) == MLAN_BSS_ROLE_UAP) {
        /* UAP */
#ifdef UAP_SUPPORT
        if ((priv->uap_bss_started == MTRUE) &&
            (priv->uap_state_chan_cb.band_config & BAND_CONFIG_5GHZ) &&
            wlan_11h_radar_detect_required(priv,
                                           priv->uap_state_chan_cb.channel))
            ret = MTRUE;
#endif
    }
    LEAVE();
    return ret;
}

/**
 *  @brief Determine if priv is DFS Master interface
 *
 *  @param priv Pointer to mlan_private
 *
 *  @return MTRUE or MFALSE
 */
static t_bool
wlan_11h_is_dfs_master(mlan_private * priv)
{
    t_bool ret = MFALSE;

    ENTER();
    /* UAP: all are master */
    if (GET_BSS_ROLE(priv) == MLAN_BSS_ROLE_UAP) {
        ret = MTRUE;
    }
    /* STA: only ad-hoc creator is master */
    else if ((GET_BSS_ROLE(priv) == MLAN_BSS_ROLE_STA) &&
             (priv->bss_mode == MLAN_BSS_MODE_IBSS) &&
             (priv->adhoc_state == ADHOC_STARTED ||
              priv->adhoc_state == ADHOC_STARTING)) {
        ret = MTRUE;
    }
    /* all other cases = slave interface */
    LEAVE();
    return ret;
}

/* Need this as function to pass to wlan_count_priv_cond() */
/**
 *  @brief Determine if priv is DFS Slave interface
 *
 *  @param priv Pointer to mlan_private
 *
 *  @return MTRUE or MFALSE
 */

static t_bool
wlan_11h_is_dfs_slave(mlan_private * priv)
{
    t_bool ret = MFALSE;
    ENTER();
    ret = !wlan_11h_is_dfs_master(priv);
    LEAVE();
    return ret;
}

/**
 *  @brief This function checks if interface is active.
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *
 *  @return             MTRUE or MFALSE
 */
static t_bool
wlan_is_intf_active(mlan_private * pmpriv)
{
    t_bool ret = MFALSE;
    ENTER();

#ifdef UAP_SUPPORT
    if (GET_BSS_ROLE(pmpriv) == MLAN_BSS_ROLE_UAP)
        /* NOTE: UAP's media_connected == true only after first STA associated.
           Need different variable to tell if UAP has been started. */
        ret = pmpriv->uap_bss_started;
    else
#endif
    if (GET_BSS_ROLE(pmpriv) == MLAN_BSS_ROLE_STA)
        ret = pmpriv->media_connected;

    LEAVE();
    return ret;
}

/**
 *  @brief This function gets current radar detect flags
 *
 *  @param pmadapter    A pointer to mlan_adapter structure
 *
 *  @return             11H MIB setting for radar detect
 */
static t_u32
wlan_11h_get_current_radar_detect_flags(mlan_adapter * pmadapter)
{
    t_u32 radar_det_flags = 0;

    ENTER();
    if (pmadapter->state_11h.is_master_radar_det_active)
        radar_det_flags |= MASTER_RADAR_DET_MASK;
    if (pmadapter->state_11h.is_slave_radar_det_active)
        radar_det_flags |= SLAVE_RADAR_DET_MASK;

    PRINTM(MINFO, "%s: radar_det_state_curr=0x%x\n",
           __FUNCTION__, radar_det_flags);

    LEAVE();
    return radar_det_flags;
}

/**
 *  @brief This function checks if radar detect flags have/should be changed.
 *
 *  @param pmadapter    A pointer to mlan_adapter structure
 *  @param pnew_state   Output param with new state, if return MTRUE.
 *
 *  @return             MTRUE (need update) or MFALSE (no change in flags)
 */
static t_bool
wlan_11h_check_radar_det_state(mlan_adapter * pmadapter, OUT t_u32 * pnew_state)
{
    t_u32 radar_det_state_new = 0;
    t_bool ret;

    ENTER();
    PRINTM(MINFO, "%s: master_radar_det_pending=%d, "
           " slave_radar_det_pending=%d\n", __FUNCTION__,
           pmadapter->state_11h.master_radar_det_enable_pending,
           pmadapter->state_11h.slave_radar_det_enable_pending);

    /* new state comes from evaluating interface states & pending starts */
    if (pmadapter->state_11h.master_radar_det_enable_pending ||
        (wlan_count_priv_cond(pmadapter,
                              wlan_11h_is_master_active_on_dfs_chan,
                              wlan_11h_is_dfs_master) > 0))
        radar_det_state_new |= MASTER_RADAR_DET_MASK;
    if (pmadapter->state_11h.slave_radar_det_enable_pending ||
        (wlan_count_priv_cond(pmadapter,
                              wlan_11h_is_slave_active_on_dfs_chan,
                              wlan_11h_is_dfs_slave) > 0))
        radar_det_state_new |= SLAVE_RADAR_DET_MASK;

    PRINTM(MINFO, "%s: radar_det_state_new=0x%x\n",
           __FUNCTION__, radar_det_state_new);

    /* now compare flags with current state */
    ret = (wlan_11h_get_current_radar_detect_flags(pmadapter)
           != radar_det_state_new) ? MTRUE : MFALSE;
    if (ret)
        *pnew_state = radar_det_state_new;

    LEAVE();
    return ret;
}

/**
 *  @brief Determine if mlan_private list only contains UAP interface(s)
 *
 *  @param priv_list        List of mlan_private pointers
 *  @param priv_list_count  Number of mlan_privates in above list
 *
 *  @return MTRUE or MFALSE
 */
static t_bool
wlan_only_uap_priv_in_list(mlan_private ** priv_list, t_u8 priv_list_count)
{
#if defined(STA_SUPPORT) && !defined(UAP_SUPPORT)
    return MFALSE;
#else
    t_u8 uap_count = 0;
    t_u8 sta_count = 0;
    t_u8 i;

    ENTER();
    for (i = 0; i < priv_list_count; i++) {
        if (GET_BSS_ROLE(priv_list[i]) == MLAN_BSS_ROLE_UAP)
            uap_count++;
        else
            sta_count++;
    }

    LEAVE();
    return ((uap_count > 0) && (sta_count == 0)) ? MTRUE : MFALSE;
#endif
}

/**
 *  @brief Prepare ioctl for add/remove CHAN_SW IE - RADAR_DETECTED event handling
 *
 *  @param pmadapter        Pointer to mlan_adapter
 *  @param pioctl_req       Pointer to completed mlan_ioctl_req (allocated inside)
 *  @param is_adding_ie     CHAN_SW IE is to be added (MTRUE), or removed (MFALSE)
 *
 *  @return                 MLAN_STATUS_SUCCESS or MLAN_STATUS_FAILURE
 */
static mlan_status
wlan_11h_prepare_custom_ie_chansw(IN mlan_adapter * pmadapter,
                                  OUT mlan_ioctl_req ** ppioctl_req,
                                  IN t_bool is_adding_ie)
{
    mlan_ioctl_req *pioctl_req = MNULL;
    mlan_ds_misc_cfg *pds_misc_cfg = MNULL;
    custom_ie *pcust_chansw_ie = MNULL;
    IEEEtypes_ChanSwitchAnn_t *pchansw_ie = MNULL;
    mlan_status ret;

    ENTER();

    if (pmadapter == MNULL || ppioctl_req == MNULL) {
        LEAVE();
        return MLAN_STATUS_FAILURE;
    }

    /* allocate buffer for mlan_ioctl_req and mlan_ds_misc_cfg */
    /* FYI - will be freed as part of cmd_response handler */
    ret = pmadapter->callbacks.moal_malloc(pmadapter->pmoal_handle,
                                           sizeof(mlan_ioctl_req) +
                                           sizeof(mlan_ds_misc_cfg),
                                           MLAN_MEM_DEF,
                                           (t_u8 **) & pioctl_req);
    if ((ret != MLAN_STATUS_SUCCESS) || !pioctl_req) {
        PRINTM(MERROR, "%s(): Could not allocate ioctl req\n", __FUNCTION__);
        LEAVE();
        return MLAN_STATUS_FAILURE;
    }
    pds_misc_cfg = (mlan_ds_misc_cfg *) ((t_u8 *) pioctl_req +
                                         sizeof(mlan_ioctl_req));

    /* prepare mlan_ioctl_req */
    memset(pmadapter, pioctl_req, 0x00, sizeof(mlan_ioctl_req));
    pioctl_req->req_id = MLAN_IOCTL_MISC_CFG;
    pioctl_req->action = MLAN_ACT_SET;
    pioctl_req->pbuf = (t_u8 *) pds_misc_cfg;
    pioctl_req->buf_len = sizeof(mlan_ds_misc_cfg);

    /* prepare mlan_ds_misc_cfg */
    memset(pmadapter, pds_misc_cfg, 0x00, sizeof(mlan_ds_misc_cfg));
    pds_misc_cfg->sub_command = MLAN_OID_MISC_CUSTOM_IE;
    pds_misc_cfg->param.cust_ie.type = TLV_TYPE_MGMT_IE;
    pds_misc_cfg->param.cust_ie.len = (sizeof(custom_ie) - MAX_IE_SIZE);

    /* configure custom_ie api settings */
    pcust_chansw_ie =
        (custom_ie *) & pds_misc_cfg->param.cust_ie.ie_data_list[0];
    pcust_chansw_ie->ie_index = 0xffff; /* Auto index */
    pcust_chansw_ie->ie_length = sizeof(IEEEtypes_ChanSwitchAnn_t);
    pcust_chansw_ie->mgmt_subtype_mask = (is_adding_ie)
        ? MBIT(8) | MBIT(5)     /* add IE for BEACON | PROBE_RSP */
        : 0;                    /* remove IE */

    /* prepare CHAN_SW IE inside ioctl */
    pchansw_ie = (IEEEtypes_ChanSwitchAnn_t *) pcust_chansw_ie->ie_buffer;
    pchansw_ie->element_id = CHANNEL_SWITCH_ANN;
    pchansw_ie->len =
        sizeof(IEEEtypes_ChanSwitchAnn_t) - sizeof(IEEEtypes_Header_t);
    pchansw_ie->chan_switch_mode = 1;   /* STA should not transmit */
    pchansw_ie->new_channel_num = pmadapter->state_rdh.new_channel;
    pchansw_ie->chan_switch_count = 0;  /* simplification */

    pds_misc_cfg->param.cust_ie.len += pcust_chansw_ie->ie_length;
    DBG_HEXDUMP(MCMD_D, "11h: custom_ie containing CHAN_SW IE",
                (t_u8 *) pcust_chansw_ie, pds_misc_cfg->param.cust_ie.len);

    /* assign output pointer before returning */
    *ppioctl_req = pioctl_req;
    LEAVE();
    return MLAN_STATUS_SUCCESS;
}

#ifdef UAP_SUPPORT
/**
 *  @brief Retrieve a randomly selected starting channel if needed for 11h
 *
 *  If 11h is enabled and 5GHz band is selected in band_config
 *    return a random channel in A band, else one from BG band.
 *
 *  @param priv          Private driver information structure
 *  @param uap_band_cfg  Private driver information structure
 *
 *  @return      Starting channel
 */
static t_u8
wlan_11h_get_uap_start_channel(mlan_private * priv, t_u8 uap_band_cfg)
{
    t_u8 start_chn;
    mlan_adapter *adapter = priv->adapter;
    t_u32 region;
    t_u32 rand_entry;
    region_chan_t *chn_tbl;
    t_u8 rand_tries = 0;

    // TODO: right now mostly a copy of wlan_11h_get_adhoc_start_channel.
    // Improve to be more specfic to UAP, e.g.
    // 1. take into account COUNTRY_CODE -> region_code
    // 2. check domain_info for value channels

    ENTER();

    /* 
     * Set start_chn to the Default.  Used if 11h is disabled or the band
     *   does not require 11h support.
     */
    start_chn = DEFAULT_AD_HOC_CHANNEL;

    /* 
     * Check that we are looking for a channel in the A Band
     */
    if (uap_band_cfg & UAP_BAND_CONFIG_5GHZ) {
        /* 
         * Set default to the A Band default. Used if random selection fails
         *   or if 11h is not enabled
         */
        start_chn = DEFAULT_AD_HOC_CHANNEL_A;

        /* 
         * Check that 11h is enabled in the driver
         */
        if (wlan_11h_is_enabled(priv)) {
            /* 
             * Search the region_channel tables for a channel table
             *   that is marked for the A Band.
             */
            for (region = 0; (region < MAX_REGION_CHANNEL_NUM); region++) {
                chn_tbl = &adapter->region_channel[region];

                /* Check if table is valid and marked for A Band */
                if (chn_tbl->valid
                    && chn_tbl->region == adapter->region_code
                    && chn_tbl->band & BAND_A) {
                    /* 
                     * Set the start channel.  Get a random number and
                     *   use it to pick an entry in the table between 0
                     *   and the number of channels in the table (NumCFP).
                     */
                    do {
                        rand_entry =
                            wlan_11h_get_random_num(adapter) % chn_tbl->num_cfp;
                        start_chn = (t_u8) chn_tbl->pcfp[rand_entry].channel;
                    } while (wlan_11h_is_channel_under_nop(adapter, start_chn)
                             && (++rand_tries < MAX_RANDOM_CHANNEL_RETRIES));
                }
            }
        }
    }

    PRINTM(MCMD_D, "11h: UAP Get Start Channel %d\n", start_chn);
    LEAVE();
    return start_chn;
}
#endif /* UAP_SUPPORT */

#ifdef DEBUG_LEVEL1
static const char *DFS_TS_REPR_STRINGS[] = { "",
    "NOP_start",
    "CAC_completed"
};
#endif

/**
 *  @brief Search for a dfs timestamp in the list with desired channel.
 *
 *  Assumes there will only be one timestamp per channel in the list.
 *
 *  @param pmadapter  Pointer to mlan_adapter
 *  @param channel    Channel number
 *
 *  @return           Pointer to timestamp if found, or MNULL
 */
static wlan_dfs_timestamp_t *
wlan_11h_find_dfs_timestamp(mlan_adapter * pmadapter, t_u8 channel)
{
    wlan_dfs_timestamp_t *pts = MNULL, *pts_found = MNULL;

    ENTER();
    pts = (wlan_dfs_timestamp_t *) util_peek_list(pmadapter->pmoal_handle,
                                                  &pmadapter->state_dfs.
                                                  dfs_ts_head, MNULL, MNULL);

    while (pts &&
           pts != (wlan_dfs_timestamp_t *) & pmadapter->state_dfs.dfs_ts_head) {
        PRINTM(MINFO,
               "dfs_timestamp(@ %p) - chan=%d, repr=%d(%s),"
               " time(sec.usec)=%lu.%06lu\n", pts, pts->channel,
               pts->represents, DFS_TS_REPR_STRINGS[pts->represents],
               pts->ts_sec, pts->ts_usec);

        if (pts->channel == channel) {
            pts_found = pts;
            break;
        }
        pts = pts->pnext;
    }

    LEAVE();
    return pts_found;
}

/**
 *  @brief Removes dfs timestamp from list.
 *
 *  @param pmadapter  Pointer to mlan_adapter
 *  @param pdfs_ts    Pointer to dfs_timestamp to remove
 */
static t_void
wlan_11h_remove_dfs_timestamp(mlan_adapter * pmadapter,
                              wlan_dfs_timestamp_t * pdfs_ts)
{
    ENTER();
    // dequeue and delete timestamp
    util_unlink_list(pmadapter->pmoal_handle,
                     &pmadapter->state_dfs.dfs_ts_head,
                     (pmlan_linked_list) pdfs_ts, MNULL, MNULL);
    pmadapter->callbacks.moal_mfree(pmadapter->pmoal_handle, (t_u8 *) pdfs_ts);
    LEAVE();
}

/**
 *  @brief Add a dfs timestamp to the list
 *
 *  Assumes there will only be one timestamp per channel in the list,
 *  and that timestamp modes (represents) are mutually exclusive.
 *
 *  @param pmadapter  Pointer to mlan_adapter
 *  @param repr       Timestamp 'represents' value (see _dfs_timestamp_repr_e)
 *  @param channel    Channel number
 *
 *  @return           Pointer to timestamp if found, or MNULL
 */
static mlan_status
wlan_11h_add_dfs_timestamp(mlan_adapter * pmadapter, t_u8 repr, t_u8 channel)
{
    wlan_dfs_timestamp_t *pdfs_ts = MNULL;
    mlan_status ret = MLAN_STATUS_SUCCESS;

    ENTER();
    pdfs_ts = wlan_11h_find_dfs_timestamp(pmadapter, channel);

    if (!pdfs_ts) {
        // need to allocate new timestamp
        ret = pmadapter->callbacks.moal_malloc(pmadapter->pmoal_handle,
                                               sizeof(wlan_dfs_timestamp_t),
                                               MLAN_MEM_DEF,
                                               (t_u8 **) & pdfs_ts);
        if ((ret != MLAN_STATUS_SUCCESS) || !pdfs_ts) {
            PRINTM(MERROR, "%s(): Could not allocate dfs_ts\n", __FUNCTION__);
            LEAVE();
            return MLAN_STATUS_FAILURE;
        }

        memset(pmadapter, (t_u8 *) pdfs_ts, 0, sizeof(wlan_dfs_timestamp_t));

        util_enqueue_list_tail(pmadapter->pmoal_handle,
                               &pmadapter->state_dfs.dfs_ts_head,
                               (pmlan_linked_list) pdfs_ts, MNULL, MNULL);
        pdfs_ts->channel = channel;
    }
    // (else, use existing timestamp for channel; see assumptions above)

    // update params
    pmadapter->callbacks.moal_get_system_time(pmadapter->pmoal_handle,
                                              &pdfs_ts->ts_sec,
                                              &pdfs_ts->ts_usec);
    pdfs_ts->represents = repr;

    PRINTM(MCMD_D, "11h: add/update dfs_timestamp - chan=%d, repr=%d(%s),"
           " time(sec.usec)=%lu.%06lu\n", pdfs_ts->channel,
           pdfs_ts->represents, DFS_TS_REPR_STRINGS[pdfs_ts->represents],
           pdfs_ts->ts_sec, pdfs_ts->ts_usec);

    LEAVE();
    return ret;
}

/********************************************************
                Global functions
********************************************************/

/**
 *  @brief Return whether the device has activated master radar detection.
 *
 *  @param priv  Private driver information structure
 *
 *  @return
 *    - MTRUE if master radar detection is enabled in firmware
 *    - MFALSE otherwise
 */
t_bool
wlan_11h_is_master_radar_det_active(mlan_private * priv)
{
    ENTER();
    LEAVE();
    return (priv->adapter->state_11h.is_master_radar_det_active);
}

/**
 *  @brief Configure master radar detection.
 *  Call wlan_11h_check_update_radar_det_state() afterwards
 *    to push this to firmware.
 *
 *  @param priv  Private driver information structure
 *  @param enable Whether to enable or disable master radar detection
 *
 *  @return  MLAN_STATUS_SUCCESS or MLAN_STATUS_FAILURE
 *
 *  @sa wlan_11h_check_update_radar_det_state
 */
mlan_status
wlan_11h_config_master_radar_det(mlan_private * priv, t_bool enable)
{
    mlan_status ret = MLAN_STATUS_FAILURE;

    ENTER();
    if (wlan_11h_is_dfs_master(priv) &&
        priv->adapter->init_para.dfs_master_radar_det_en) {
        priv->adapter->state_11h.master_radar_det_enable_pending = enable;
        ret = MLAN_STATUS_SUCCESS;
    }

    LEAVE();
    return ret;
}

/**
 *  @brief Configure slave radar detection.
 *  Call wlan_11h_check_update_radar_det_state() afterwards
 *    to push this to firmware.
 *
 *  @param priv  Private driver information structure
 *  @param enable Whether to enable or disable slave radar detection
 *
 *  @return  MLAN_STATUS_SUCCESS or MLAN_STATUS_FAILURE
 *
 *  @sa wlan_11h_check_update_radar_det_state
 */
mlan_status
wlan_11h_config_slave_radar_det(mlan_private * priv, t_bool enable)
{
    mlan_status ret = MLAN_STATUS_FAILURE;

    ENTER();
    if (wlan_11h_is_dfs_slave(priv) &&
        priv->adapter->init_para.dfs_slave_radar_det_en) {
        priv->adapter->state_11h.slave_radar_det_enable_pending = enable;
        ret = MLAN_STATUS_SUCCESS;
    }
    LEAVE();
    return ret;
}

/**
 *  @brief Checks all interfaces and determines if radar_detect flag states
 *         have/should be changed.  If so, sends SNMP_MIB 11H command to FW.
 *         Call this function on any interface enable/disable/channel change.
 *
 *  @param pmpriv  Pointer to mlan_private structure
 *
 *  @return        MLAN_STATUS_SUCCESS (update or not)
 *              or MLAN_STATUS_FAILURE (cmd failure)
 *
 *  @sa    wlan_11h_check_radar_det_state
 */
mlan_status
wlan_11h_check_update_radar_det_state(mlan_private * pmpriv)
{
    t_u32 new_radar_det_state = 0;
    t_u32 mib_11h = 0;
    mlan_status ret = MLAN_STATUS_SUCCESS;

    ENTER();

    if (wlan_11h_check_radar_det_state(pmpriv->adapter, &new_radar_det_state)) {
        PRINTM(MCMD_D, "%s: radar_det_state being updated.\n", __FUNCTION__);

        mib_11h |= new_radar_det_state;
        /* keep priv's existing 11h state */
        if (pmpriv->intf_state_11h.is_11h_active)
            mib_11h |= ENABLE_11H_MASK;

        /* Send cmd to FW to enable/disable 11h function in firmware */
        ret = wlan_prepare_cmd(pmpriv,
                               HostCmd_CMD_802_11_SNMP_MIB,
                               HostCmd_ACT_GEN_SET, Dot11H_i, MNULL, &mib_11h);
        if (ret)
            ret = MLAN_STATUS_FAILURE;
    }

    /* updated state sent OR no change, thus no longer pending */
    pmpriv->adapter->state_11h.master_radar_det_enable_pending = MFALSE;
    pmpriv->adapter->state_11h.slave_radar_det_enable_pending = MFALSE;

    LEAVE();
    return ret;
}

/**
 *  @brief Query 11h firmware enabled state.
 *
 *  Return whether the firmware currently has 11h extensions enabled
 *
 *  @param priv  Private driver information structure
 *
 *  @return
 *    - MTRUE if 11h has been activated in the firmware
 *    - MFALSE otherwise
 *
 *  @sa wlan_11h_activate
 */
t_bool
wlan_11h_is_active(mlan_private * priv)
{
    ENTER();
    LEAVE();
    return (priv->intf_state_11h.is_11h_active);
}

/**
 *  @brief Enable the transmit interface and record the state.
 *
 *  @param priv  Private driver information structure
 *
 *  @return      N/A
 */
t_void
wlan_11h_tx_enable(mlan_private * priv)
{
    ENTER();
    if (priv->intf_state_11h.tx_disabled) {
        wlan_recv_event(priv, MLAN_EVENT_ID_FW_START_TX, MNULL);
        priv->intf_state_11h.tx_disabled = MFALSE;
    }
    LEAVE();
}

/**
 *  @brief Disable the transmit interface and record the state.
 *
 *  @param priv  Private driver information structure
 *
 *  @return      N/A
 */
t_void
wlan_11h_tx_disable(mlan_private * priv)
{
    ENTER();
    if (!priv->intf_state_11h.tx_disabled) {
        priv->intf_state_11h.tx_disabled = MTRUE;
        wlan_recv_event(priv, MLAN_EVENT_ID_FW_STOP_TX, MNULL);
    }
    LEAVE();
}

/**
 *  @brief Enable or Disable the 11h extensions in the firmware
 *
 *  @param priv         Private driver information structure
 *  @param pioctl_buf   A pointer to MLAN IOCTL Request buffer
 *  @param flag         Enable 11h if MTRUE, disable otherwise
 *
 *  @return      MLAN_STATUS_SUCCESS or MLAN_STATUS_FAILURE
 */
mlan_status
wlan_11h_activate(mlan_private * priv, t_void * pioctl_buf, t_bool flag)
{
    t_u32 enable = flag & ENABLE_11H_MASK;
    mlan_status ret = MLAN_STATUS_SUCCESS;

    ENTER();
    /* add bits for master/slave radar detect into enable. */
    enable |= wlan_11h_get_current_radar_detect_flags(priv->adapter);

    /* 
     * Send cmd to FW to enable/disable 11h function in firmware
     */
    ret = wlan_prepare_cmd(priv,
                           HostCmd_CMD_802_11_SNMP_MIB,
                           HostCmd_ACT_GEN_SET,
                           Dot11H_i, (t_void *) pioctl_buf, &enable);
    if (ret)
        ret = MLAN_STATUS_FAILURE;
    else
        /* Set boolean flag in driver 11h state */
        priv->intf_state_11h.is_11h_active = flag;

    PRINTM(MINFO, "11h: %s\n", flag ? "Activate" : "Deactivate");

    LEAVE();
    return ret;
}

/**
 *  @brief Initialize the 11h parameters and enable 11h when starting an IBSS
 *
 *  @param adapter mlan_adapter structure
 *
 *  @return      N/A
 */
t_void
wlan_11h_init(mlan_adapter * adapter)
{
    wlan_11h_device_state_t *pstate_11h = &adapter->state_11h;
    IEEEtypes_Quiet_t *pquiet = &adapter->state_11h.quiet_ie;
    wlan_dfs_device_state_t *pstate_dfs = &adapter->state_dfs;
    wlan_radar_det_hndlg_state_t *pstate_rdh = &adapter->state_rdh;
#ifdef DFS_TESTING_SUPPORT
    wlan_dfs_testing_settings_t *pdfs_test = &adapter->dfs_test_params;
#endif

    ENTER();

    /* Initialize 11H struct */
    pstate_11h->usr_def_power_constraint = WLAN_11H_TPC_POWERCONSTRAINT;
    pstate_11h->min_tx_power_capability = WLAN_11H_TPC_POWERCAPABILITY_MIN;
    pstate_11h->max_tx_power_capability = WLAN_11H_TPC_POWERCAPABILITY_MAX;

    pstate_11h->recvd_chanswann_event = MFALSE;
    pstate_11h->master_radar_det_enable_pending = MFALSE;
    pstate_11h->slave_radar_det_enable_pending = MFALSE;
    pstate_11h->is_master_radar_det_active = MFALSE;
    pstate_11h->is_slave_radar_det_active = MFALSE;

    /* Initialize quiet_ie */
    memset(adapter, pquiet, 0, sizeof(IEEEtypes_Quiet_t));
    pquiet->element_id = QUIET;
    pquiet->len = (sizeof(pquiet->quiet_count) + sizeof(pquiet->quiet_period)
                   + sizeof(pquiet->quiet_duration)
                   + sizeof(pquiet->quiet_offset));

    /* Initialize DFS struct */
    pstate_dfs->dfs_check_pending = MFALSE;
    pstate_dfs->dfs_radar_found = MFALSE;
    pstate_dfs->dfs_check_channel = 0;
    pstate_dfs->dfs_report_time_sec = 0;
    util_init_list((pmlan_linked_list) & pstate_dfs->dfs_ts_head);

    /* Initialize RDH struct */
    pstate_rdh->stage = RDH_OFF;
    pstate_rdh->priv_list_count = 0;
    pstate_rdh->priv_curr_idx = 0;
    pstate_rdh->curr_channel = 0;
    pstate_rdh->new_channel = 0;
    pstate_rdh->uap_band_cfg = 0;
    pstate_rdh->max_bcn_dtim_ms = 0;
    memset(adapter, pstate_rdh->priv_list, 0, sizeof(pstate_rdh->priv_list));

#ifdef DFS_TESTING_SUPPORT
    /* Initialize DFS testing struct */
    pdfs_test->user_cac_period_msec = 0;
    pdfs_test->user_nop_period_sec = 0;
    pdfs_test->no_channel_change_on_radar = MFALSE;
    pdfs_test->fixed_new_channel_on_radar = 0;
#endif

    LEAVE();
}

/**
 *  @brief Cleanup for the 11h parameters that allocated memory, etc.
 *
 *  @param adapter mlan_adapter structure
 *
 *  @return      N/A
 */
t_void
wlan_11h_cleanup(mlan_adapter * adapter)
{
    wlan_dfs_device_state_t *pstate_dfs = &adapter->state_dfs;
    wlan_dfs_timestamp_t *pdfs_ts;

    ENTER();

    /* cleanup dfs_timestamp list */
    pdfs_ts = (wlan_dfs_timestamp_t *) util_peek_list(adapter->pmoal_handle,
                                                      &pstate_dfs->dfs_ts_head,
                                                      MNULL, MNULL);
    while (pdfs_ts) {
        util_unlink_list(adapter->pmoal_handle, &pstate_dfs->dfs_ts_head,
                         (pmlan_linked_list) pdfs_ts, MNULL, MNULL);
        adapter->callbacks.moal_mfree(adapter->pmoal_handle, (t_u8 *) pdfs_ts);

        pdfs_ts = (wlan_dfs_timestamp_t *) util_peek_list(adapter->pmoal_handle,
                                                          &pstate_dfs->
                                                          dfs_ts_head, MNULL,
                                                          MNULL);
    }

    LEAVE();
}

/**
 *  @brief Initialize the 11h parameters and enable 11h when starting an IBSS
 *
 *  @param pmpriv Pointer to mlan_private structure
 *
 *  @return      N/A
 */
t_void
wlan_11h_priv_init(mlan_private * pmpriv)
{
    wlan_11h_interface_state_t *pistate_11h = &pmpriv->intf_state_11h;

    ENTER();

    pistate_11h->is_11h_enabled = MTRUE;
    pistate_11h->is_11h_active = MFALSE;
    pistate_11h->adhoc_auto_sel_chan = MTRUE;
    pistate_11h->tx_disabled = MFALSE;

    LEAVE();
}

/**
 *  @brief Retrieve a randomly selected starting channel if needed for 11h
 *
 *  If 11h is enabled and an A-Band channel start band preference
 *    configured in the driver, the start channel must be random in order
 *    to meet with
 *
 *  @param priv  Private driver information structure
 *
 *  @return      Starting channel
 */
t_u8
wlan_11h_get_adhoc_start_channel(mlan_private * priv)
{
    t_u8 start_chn;
    mlan_adapter *adapter = priv->adapter;
    t_u32 region;
    t_u32 rand_entry;
    region_chan_t *chn_tbl;
    t_u8 rand_tries = 0;

    ENTER();

    /* 
     * Set start_chn to the Default.  Used if 11h is disabled or the band
     *   does not require 11h support.
     */
    start_chn = DEFAULT_AD_HOC_CHANNEL;

    /* 
     * Check that we are looking for a channel in the A Band
     */
    if ((adapter->adhoc_start_band & BAND_A)
        || (adapter->adhoc_start_band & BAND_AN)
        ) {
        /* 
         * Set default to the A Band default. Used if random selection fails
         *   or if 11h is not enabled
         */
        start_chn = DEFAULT_AD_HOC_CHANNEL_A;

        /* 
         * Check that 11h is enabled in the driver
         */
        if (wlan_11h_is_enabled(priv)) {
            /* 
             * Search the region_channel tables for a channel table
             *   that is marked for the A Band.
             */
            for (region = 0; (region < MAX_REGION_CHANNEL_NUM); region++) {
                chn_tbl = &adapter->region_channel[region];

                /* Check if table is valid and marked for A Band */
                if (chn_tbl->valid
                    && chn_tbl->region == adapter->region_code
                    && chn_tbl->band & BAND_A) {
                    /* 
                     * Set the start channel.  Get a random number and
                     *   use it to pick an entry in the table between 0
                     *   and the number of channels in the table (NumCFP).
                     */
                    do {
                        rand_entry =
                            wlan_11h_get_random_num(adapter) % chn_tbl->num_cfp;
                        start_chn = (t_u8) chn_tbl->pcfp[rand_entry].channel;
                    } while ((wlan_11h_is_channel_under_nop(adapter, start_chn)
                              ||
                              ((adapter->state_rdh.stage ==
                                RDH_GET_INFO_CHANNEL) &&
                               wlan_11h_radar_detect_required(priv, start_chn)))
                             && (++rand_tries < MAX_RANDOM_CHANNEL_RETRIES));
                }
            }
        }
    }

    PRINTM(MINFO, "11h: %s: AdHoc Channel set to %u\n",
           wlan_11h_is_enabled(priv) ? "Enabled" : "Disabled", start_chn);

    LEAVE();
    return start_chn;
}

/**
 *  @brief Check if the current region's regulations require the input channel
 *         to be scanned for radar.
 *
 *  Based on statically defined requirements for sub-bands per regulatory
 *    agency requirements.
 *
 *  Used in adhoc start to determine if channel availability check is required
 *
 *  @param priv    Private driver information structure
 *  @param channel Channel to determine radar detection requirements
 *
 *  @return
 *    - MTRUE if radar detection is required
 *    - MFALSE otherwise
 */
/**  @sa wlan_11h_issue_radar_detect
 */
t_bool
wlan_11h_radar_detect_required(mlan_private * priv, t_u8 channel)
{
    t_bool required = MFALSE;

    ENTER();

    /* 
     *   No checks for 11h or measurement code being enabled is placed here
     *   since regulatory requirements exist whether we support them or not.
     */

    required = wlan_get_cfp_radar_detect(priv, channel);

    PRINTM(MINFO, "11h: Radar detection in region %#02x "
           "is %srequired for channel %d\n",
           priv->adapter->region_code, (required ? "" : "not "), channel);

    if (required == MTRUE && priv->media_connected == MTRUE
        && priv->curr_bss_params.bss_descriptor.channel == channel) {
        required = MFALSE;

        PRINTM(MINFO, "11h: Radar detection not required. "
               "Already operating on the channel\n");
    }

    LEAVE();
    return required;
}

/**
 *  @brief Perform a radar measurement if required on given channel
 *
 *  Check to see if the provided channel requires a channel availability
 *    check (60 second radar detection measurement).  If required, perform
 *    measurement, stalling calling thread until the measurement completes
 *    and then report result.
 *
 *  Used when starting an adhoc or AP network.
 *
 *  @param priv         Private driver information structure
 *  @param pioctl_req   Pointer to IOCTL request buffer
 *  @param channel      Channel on which to perform radar measurement
 *
 *  @return
 *    - MTRUE  if radar measurement request was successfully issued
 *    - MFALSE if radar detection is not required
 *    - < 0 for error during radar detection (if performed)
 *
 *  @sa wlan_11h_radar_detect_required
 */
t_s32
wlan_11h_issue_radar_detect(mlan_private * priv,
                            pmlan_ioctl_req pioctl_req, t_u8 channel)
{
    t_s32 ret;
    HostCmd_DS_CHAN_RPT_REQ chan_rpt_req;

    ENTER();

    ret = wlan_11h_radar_detect_required(priv, channel);
    if (ret) {
        /* Prepare and issue CMD_CHAN_RPT_REQ. */
        memset(priv->adapter, &chan_rpt_req, 0x00, sizeof(chan_rpt_req));

        chan_rpt_req.chan_desc.startFreq = START_FREQ_11A_BAND;
        chan_rpt_req.chan_desc.chanWidth = 0;   // 1 for 40Mhz
        chan_rpt_req.chan_desc.chanNum = channel;
        chan_rpt_req.millisec_dwell_time =
            WLAN_11H_CHANNEL_AVAIL_CHECK_DURATION;
#ifdef DFS_TESTING_SUPPORT
        if (priv->adapter->dfs_test_params.user_cac_period_msec) {
            PRINTM(MCMD_D, "dfs_testing - user CAC period=%d (msec)\n",
                   priv->adapter->dfs_test_params.user_cac_period_msec);
            chan_rpt_req.millisec_dwell_time =
                priv->adapter->dfs_test_params.user_cac_period_msec;
        }
#endif

        PRINTM(MMSG, "11h: issuing DFS Radar check for channel=%d."
               "  Please wait for response...\n", channel);

        ret = wlan_prepare_cmd(priv, HostCmd_CMD_CHAN_REPORT_REQUEST,
                               HostCmd_ACT_GEN_SET, 0,
                               (t_void *) pioctl_req,
                               (t_void *) & chan_rpt_req);
    }

    LEAVE();
    return ret;
}

/**
 *  @brief Checks if a radar measurement was performed on channel,
 *         and if so, whether radar was detected on it.
 *
 *  Used when starting an adhoc network.
 *
 *  @param priv         Private driver information structure
 *  @param chan         Channel to check upon
 *
 *  @return
 *    - MLAN_STATUS_SUCCESS if no radar on channel
 *    - MLAN_STATUS_FAILURE if radar was found on channel
 *    - (TBD??) MLAN_STATUS_PENDING if radar report NEEDS TO BE REISSUED
 *
 *  @sa wlan_11h_issue_radar_detect
 *  @sa wlan_11h_process_start
 */
mlan_status
wlan_11h_check_chan_report(mlan_private * priv, t_u8 chan)
{
    mlan_status ret = MLAN_STATUS_SUCCESS;
    wlan_dfs_device_state_t *pstate_dfs = &priv->adapter->state_dfs;
    t_u32 sec, usec;

    ENTER();

    /* check report we hold is valid or not */
    priv->adapter->callbacks.moal_get_system_time(priv->adapter->pmoal_handle,
                                                  &sec, &usec);

    PRINTM(MINFO, "11h: %s()\n", __FUNCTION__);
    PRINTM(MINFO, "- sec_now=%d, sec_report=%d.\n",
           sec, pstate_dfs->dfs_report_time_sec);
    PRINTM(MINFO, "- rpt_channel=%d, rpt_radar=%d.\n",
           pstate_dfs->dfs_check_channel, pstate_dfs->dfs_radar_found);

    if ((!pstate_dfs->dfs_check_pending) &&
        (chan == pstate_dfs->dfs_check_channel) &&
        ((sec - pstate_dfs->dfs_report_time_sec) <
         MAX_DFS_REPORT_USABLE_AGE_SEC)) {
        /* valid and not out-dated, check if radar */
        if (pstate_dfs->dfs_radar_found) {
            PRINTM(MMSG, "Radar was detected on channel %d.\n", chan);
            ret = MLAN_STATUS_FAILURE;
        }
    } else {
        // TODO: reissue report request if not pending.
        // BUT HOW to make the code wait for it???
        /* For now, just fail since we don't have the info. */
        ret = MLAN_STATUS_PENDING;
    }

    LEAVE();
    return ret;
}

/**
 *  @brief Process an TLV buffer for a pending BSS Adhoc start command.
 *
 *  Activate 11h functionality in the firmware if driver has is enabled
 *    for 11h (configured by the application via IOCTL).
 *
 *  @param priv          Private driver information structure
 *  @param ppbuffer      Output parameter: Pointer to the TLV output buffer,
 *                       modified on return to point after the appended 11h TLVs
 *  @param pcap_info     Pointer to the capability info for the BSS to join
 *  @param channel       Channel on which we are starting the IBSS
 *  @param p11h_bss_info Input/Output parameter: Pointer to the 11h BSS
 *                       information for this network that we are establishing.
 *                       11h sensed flag set on output if warranted.
 *
 *  @return
 *      - MLAN_STATUS_SUCCESS if 11h is disabled
 *      - Integer number of bytes appended to the TLV output buffer (ppbuffer)
 *      - < 0 for error (e.g. radar detected on channel)
 */
t_s32
wlan_11h_process_start(mlan_private * priv,
                       t_u8 ** ppbuffer,
                       IEEEtypes_CapInfo_t * pcap_info,
                       t_u32 channel, wlan_11h_bss_info_t * p11h_bss_info)
{
    mlan_adapter *adapter = priv->adapter;
    t_s32 ret = MLAN_STATUS_SUCCESS;
    t_bool is_dfs_chan = MFALSE;

    ENTER();
    if (wlan_11h_is_enabled(priv)
        && ((adapter->adhoc_start_band & BAND_A)
            || (adapter->adhoc_start_band & BAND_AN)
        )
        ) {
        if (!wlan_11d_is_enabled(priv)) {
            /* No use having 11h enabled without 11d enabled */
            wlan_11d_enable(priv, MNULL, ENABLE_11D);
#ifdef STA_SUPPORT
            wlan_11d_create_dnld_countryinfo(priv, adapter->adhoc_start_band);
#endif
        }

        /* Activate 11h functions in firmware, turns on capability bit */
        wlan_11h_activate(priv, MNULL, MTRUE);
        pcap_info->spectrum_mgmt = MTRUE;

        /* If using a DFS channel, enable radar detection. */
        is_dfs_chan = wlan_11h_radar_detect_required(priv, channel);
        if (is_dfs_chan) {
            if (!wlan_11h_is_master_radar_det_active(priv))
                wlan_11h_config_master_radar_det(priv, MTRUE);
        }
        wlan_11h_check_update_radar_det_state(priv);

        /* Set flag indicating this BSS we are starting is using 11h */
        p11h_bss_info->sensed_11h = MTRUE;

        if (is_dfs_chan) {
            /* check if this channel is under NOP */
            if (wlan_11h_is_channel_under_nop(adapter, channel))
                ret = MLAN_STATUS_FAILURE;
            /* check last channel report, if this channel is free of radar */
            if (ret == MLAN_STATUS_SUCCESS)
                ret = wlan_11h_check_chan_report(priv, channel);
        }
        if (ret == MLAN_STATUS_SUCCESS)
            ret = wlan_11h_process_adhoc(priv, ppbuffer, channel, MNULL);
        else
            ret = MLAN_STATUS_FAILURE;
    } else {
        /* Deactivate 11h functions in the firmware */
        wlan_11h_activate(priv, MNULL, MFALSE);
        pcap_info->spectrum_mgmt = MFALSE;
        wlan_11h_check_update_radar_det_state(priv);
    }
    LEAVE();
    return ret;
}

/**
 *  @brief Process an TLV buffer for a pending BSS Join command for
 *         both adhoc and infra networks
 *
 *  The TLV command processing for a BSS join for either adhoc or
 *    infrastructure network is performed with this function.  The
 *    capability bits are inspected for the IBSS flag and the appropriate
 *    local routines are called to setup the necessary TLVs.
 *
 *  Activate 11h functionality in the firmware if the spectrum management
 *    capability bit is found in the network information for the BSS we are
 *    joining.
 *
 *  @param priv          Private driver information structure
 *  @param ppbuffer      Output parameter: Pointer to the TLV output buffer,
 *                       modified on return to point after the appended 11h TLVs
 *  @param pcap_info     Pointer to the capability info for the BSS to join
 *  @param band          Band on which we are joining the BSS
 *  @param channel       Channel on which we are joining the BSS
 *  @param p11h_bss_info Pointer to the 11h BSS information for this
 *                       network that was parsed out of the scan response.
 *
 *  @return              Integer number of bytes appended to the TLV output
 *                       buffer (ppbuffer), MLAN_STATUS_FAILURE (-1),
 *                       or MLAN_STATUS_SUCCESS (0)
 */
t_s32
wlan_11h_process_join(mlan_private * priv,
                      t_u8 ** ppbuffer,
                      IEEEtypes_CapInfo_t * pcap_info,
                      t_u8 band,
                      t_u32 channel, wlan_11h_bss_info_t * p11h_bss_info)
{
    t_s32 ret = 0;

    ENTER();

    if (priv->media_connected == MTRUE) {
        if (wlan_11h_is_active(priv) == p11h_bss_info->sensed_11h) {
            /* Assume DFS parameters are the same for roaming as long as the
               current & next APs have the same spectrum mgmt capability bit
               setting */
            ret = MLAN_STATUS_SUCCESS;

        } else {
            /* No support for roaming between DFS/non-DFS yet */
            ret = MLAN_STATUS_FAILURE;
        }

        LEAVE();
        return ret;
    }

    if (p11h_bss_info->sensed_11h) {
        if (!wlan_11d_is_enabled(priv)) {
            /* No use having 11h enabled without 11d enabled */
            wlan_11d_enable(priv, MNULL, ENABLE_11D);
#ifdef STA_SUPPORT
            wlan_11d_parse_dnld_countryinfo(priv, priv->pattempted_bss_desc);
#endif
        }
        /* Activate 11h functions in firmware, turns on capability bit */
        wlan_11h_activate(priv, MNULL, MTRUE);
        pcap_info->spectrum_mgmt = MTRUE;

        /* If using a DFS channel, enable radar detection. */
        if ((band & BAND_A) && wlan_11h_radar_detect_required(priv, channel)) {
            if (!wlan_11h_is_slave_radar_det_active(priv))
                wlan_11h_config_slave_radar_det(priv, MTRUE);
        }
        wlan_11h_check_update_radar_det_state(priv);

        if (pcap_info->ibss) {
            PRINTM(MINFO, "11h: Adhoc join: Sensed\n");
            ret = wlan_11h_process_adhoc(priv, ppbuffer, channel,
                                         p11h_bss_info);
        } else {
            PRINTM(MINFO, "11h: Infra join: Sensed\n");
            ret = wlan_11h_process_infra_join(priv, ppbuffer, band,
                                              channel, p11h_bss_info);
        }
    } else {
        /* Deactivate 11h functions in the firmware */
        wlan_11h_activate(priv, MNULL, MFALSE);
        pcap_info->spectrum_mgmt = MFALSE;
        wlan_11h_check_update_radar_det_state(priv);
    }

    LEAVE();
    return ret;
}

/**
 *
 *  @brief  Prepare the HostCmd_DS_Command structure for an 11h command.
 *
 *  Use the Command field to determine if the command being set up is for
 *     11h and call one of the local command handlers accordingly for:
 *
 *        - HostCmd_CMD_802_11_TPC_ADAPT_REQ
 *        - HostCmd_CMD_802_11_TPC_INFO
 *        - HostCmd_CMD_802_11_CHAN_SW_ANN
 */
/**       - HostCmd_CMD_CHAN_REPORT_REQUEST
 */
/**
 *  @param priv      Private driver information structure
 *  @param pcmd_ptr  Output parameter: Pointer to the command being prepared
 *                   for the firmware
 *  @param pinfo_buf Void buffer pass through with data necessary for a
 *                   specific command type
 */
/**  @return          MLAN_STATUS_SUCCESS, MLAN_STATUS_FAILURE or MLAN_STATUS_PENDING
 */
/**  @sa wlan_11h_cmd_tpc_request
 *  @sa wlan_11h_cmd_tpc_info
 *  @sa wlan_11h_cmd_chan_sw_ann
 */
/** @sa wlan_11h_cmd_chan_report_req
 */
mlan_status
wlan_11h_cmd_process(mlan_private * priv,
                     HostCmd_DS_COMMAND * pcmd_ptr, const t_void * pinfo_buf)
{
    mlan_status ret = MLAN_STATUS_SUCCESS;

    ENTER();
    switch (pcmd_ptr->command) {
    case HostCmd_CMD_802_11_TPC_ADAPT_REQ:
        ret = wlan_11h_cmd_tpc_request(priv, pcmd_ptr, pinfo_buf);
        break;
    case HostCmd_CMD_802_11_TPC_INFO:
        ret = wlan_11h_cmd_tpc_info(priv, pcmd_ptr, pinfo_buf);
        break;
    case HostCmd_CMD_802_11_CHAN_SW_ANN:
        ret = wlan_11h_cmd_chan_sw_ann(priv, pcmd_ptr, pinfo_buf);
        break;
    case HostCmd_CMD_CHAN_REPORT_REQUEST:
        ret = wlan_11h_cmd_chan_rpt_req(priv, pcmd_ptr, pinfo_buf);
        break;
    default:
        ret = MLAN_STATUS_FAILURE;
    }

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

    LEAVE();
    return ret;
}

/**
 *  @brief Handle the command response from the firmware if from an 11h command
 *
 *  Use the Command field to determine if the command response being
 *    is for 11h.  Call the local command response handler accordingly for:
 *
 *        - HostCmd_CMD_802_11_TPC_ADAPT_REQ
 *        - HostCmd_CMD_802_11_TPC_INFO
 *        - HostCmd_CMD_802_11_CHAN_SW_ANN
 */
/**       - HostCmd_CMD_CHAN_REPORT_REQUEST
 */
/**
 *  @param priv  Private driver information structure
 *  @param resp  HostCmd_DS_COMMAND struct returned from the firmware
 *               command
 *
 *  @return      MLAN_STATUS_SUCCESS or MLAN_STATUS_FAILURE
 */
mlan_status
wlan_11h_cmdresp_process(mlan_private * priv, const HostCmd_DS_COMMAND * resp)
{
    mlan_status ret = MLAN_STATUS_SUCCESS;

    ENTER();
    switch (resp->command) {
    case HostCmd_CMD_802_11_TPC_ADAPT_REQ:
        HEXDUMP("11h: TPC REQUEST Rsp:", (t_u8 *) resp, (t_u32) resp->size);
        memcpy(priv->adapter, priv->adapter->curr_cmd->pdata_buf,
               &resp->params.tpc_req, sizeof(HostCmd_DS_802_11_TPC_ADAPT_REQ));
        break;

    case HostCmd_CMD_802_11_TPC_INFO:
        HEXDUMP("11h: TPC INFO Rsp Data:", (t_u8 *) resp, (t_u32) resp->size);
        break;

    case HostCmd_CMD_802_11_CHAN_SW_ANN:
        PRINTM(MINFO, "11h: Ret ChSwAnn: Sz=%u, Seq=%u, Ret=%u\n",
               resp->size, resp->seq_num, resp->result);
        break;

    case HostCmd_CMD_CHAN_REPORT_REQUEST:
        PRINTM(MINFO, "11h: Ret ChanRptReq.  Set dfs_check_pending and wait"
               " for EVENT_CHANNEL_REPORT.\n");
        priv->adapter->state_dfs.dfs_check_pending = MTRUE;
        break;

    default:
        ret = MLAN_STATUS_FAILURE;
    }

    LEAVE();
    return ret;
}

/**
 *  @brief Process an element from a scan response, copy relevant info for 11h
 *
 *  @param pmadapter Pointer to mlan_adapter
 *  @param p11h_bss_info Output parameter: Pointer to the 11h BSS information
 *                       for the network that is being processed
 *  @param pelement      Pointer to the current IE we are inspecting for 11h
 *                       relevance
 *
 *  @return              MLAN_STATUS_SUCCESS or MLAN_STATUS_FAILURE
 */
mlan_status
wlan_11h_process_bss_elem(mlan_adapter * pmadapter,
                          wlan_11h_bss_info_t * p11h_bss_info,
                          const t_u8 * pelement)
{
    mlan_status ret = MLAN_STATUS_SUCCESS;
    t_u8 element_len = *((t_u8 *) pelement + 1);

    ENTER();
    switch (*pelement) {
    case POWER_CONSTRAINT:
        PRINTM(MINFO, "11h: Power Constraint IE Found\n");
        p11h_bss_info->sensed_11h = MTRUE;
        memcpy(pmadapter, &p11h_bss_info->power_constraint, pelement,
               MIN((element_len + sizeof(IEEEtypes_Header_t)),
                   sizeof(IEEEtypes_PowerConstraint_t)));
        p11h_bss_info->power_constraint.len =
            MIN(element_len, (sizeof(IEEEtypes_PowerConstraint_t)
                              - sizeof(IEEEtypes_Header_t)));
        break;

    case POWER_CAPABILITY:
        PRINTM(MINFO, "11h: Power Capability IE Found\n");
        p11h_bss_info->sensed_11h = MTRUE;
        memcpy(pmadapter, &p11h_bss_info->power_capability, pelement,
               MIN((element_len + sizeof(IEEEtypes_Header_t)),
                   sizeof(IEEEtypes_PowerCapability_t)));
        p11h_bss_info->power_capability.len =
            MIN(element_len, (sizeof(IEEEtypes_PowerCapability_t)
                              - sizeof(IEEEtypes_Header_t)));
        break;

    case TPC_REPORT:
        PRINTM(MINFO, "11h: Tpc Report IE Found\n");
        p11h_bss_info->sensed_11h = MTRUE;
        memcpy(pmadapter, &p11h_bss_info->tpc_report, pelement,
               MIN((element_len + sizeof(IEEEtypes_Header_t)),
                   sizeof(IEEEtypes_TPCReport_t)));
        p11h_bss_info->tpc_report.len =
            MIN(element_len, (sizeof(IEEEtypes_TPCReport_t)
                              - sizeof(IEEEtypes_Header_t)));
        break;

    case CHANNEL_SWITCH_ANN:
        PRINTM(MINFO, "11h: Channel Switch Ann IE Found\n");
        p11h_bss_info->sensed_11h = MTRUE;
        memcpy(pmadapter, &p11h_bss_info->chan_switch_ann, pelement,
               MIN((element_len + sizeof(IEEEtypes_Header_t)),
                   sizeof(IEEEtypes_ChanSwitchAnn_t)));
        p11h_bss_info->chan_switch_ann.len =
            MIN(element_len, (sizeof(IEEEtypes_ChanSwitchAnn_t)
                              - sizeof(IEEEtypes_Header_t)));
        break;

    case QUIET:
        PRINTM(MINFO, "11h: Quiet IE Found\n");
        p11h_bss_info->sensed_11h = MTRUE;
        memcpy(pmadapter, &p11h_bss_info->quiet, pelement,
               MIN((element_len + sizeof(IEEEtypes_Header_t)),
                   sizeof(IEEEtypes_Quiet_t)));
        p11h_bss_info->quiet.len = MIN(element_len, (sizeof(IEEEtypes_Quiet_t)
                                                     -
                                                     sizeof
                                                     (IEEEtypes_Header_t)));
        break;

    case IBSS_DFS:
        PRINTM(MINFO, "11h: Ibss Dfs IE Found\n");
        p11h_bss_info->sensed_11h = MTRUE;
        memcpy(pmadapter, &p11h_bss_info->ibss_dfs, pelement,
               MIN((element_len + sizeof(IEEEtypes_Header_t)),
                   sizeof(IEEEtypes_IBSS_DFS_t)));
        p11h_bss_info->ibss_dfs.len =
            MIN(element_len, (sizeof(IEEEtypes_IBSS_DFS_t)
                              - sizeof(IEEEtypes_Header_t)));
        break;

    case SUPPORTED_CHANNELS:
    case TPC_REQUEST:
        /* 
         * These elements are not in beacons/probe responses.  Included here
         *   to cover set of enumerated 11h elements.
         */
        break;

    default:
        ret = MLAN_STATUS_FAILURE;
    }

    LEAVE();
    return ret;
}

/**
 *  @brief Driver handling for CHANNEL_SWITCH_ANN event
 *
 *  @param priv Pointer to mlan_private
 *
 *  @return MLAN_STATUS_SUCCESS, MLAN_STATUS_FAILURE or MLAN_STATUS_PENDING
 */
mlan_status
wlan_11h_handle_event_chanswann(mlan_private * priv)
{
    mlan_status ret = MLAN_STATUS_SUCCESS;
    ENTER();
    priv->adapter->state_11h.recvd_chanswann_event = MTRUE;

#ifdef STA_SUPPORT
    /* do directed deauth.  priv flag above will cause different reason code */
    PRINTM(MINFO, "11h: handle_event_chanswann() - sending deauth\n");
    ret = wlan_disconnect(priv, MNULL,
                          &priv->curr_bss_params.bss_descriptor.mac_address);

    /* clear region table so next scan will be all passive */
    PRINTM(MINFO, "11h: handle_event_chanswann() - clear region table\n");
    wlan_11d_clear_parsedtable(priv);
#endif

    priv->adapter->state_11h.recvd_chanswann_event = MFALSE;
    LEAVE();
    return ret;
}

#ifdef DFS_TESTING_SUPPORT
/**
 *  @brief 802.11h DFS Testing configuration
 *
 *  @param pmadapter    Pointer to mlan_adapter
 *  @param pioctl_req   Pointer to mlan_ioctl_req
 *
 *  @return MLAN_STATUS_SUCCESS or MLAN_STATUS_FAILURE
 */
mlan_status
wlan_11h_ioctl_dfs_testing(pmlan_adapter pmadapter, pmlan_ioctl_req pioctl_req)
{
    mlan_ds_11h_cfg *ds_11hcfg = MNULL;
    mlan_ds_11h_dfs_testing *dfs_test = MNULL;
    wlan_dfs_testing_settings_t *pdfs_test_params = MNULL;

    ENTER();

    ds_11hcfg = (mlan_ds_11h_cfg *) pioctl_req->pbuf;
    dfs_test = &ds_11hcfg->param.dfs_testing;
    pdfs_test_params = &pmadapter->dfs_test_params;

    if (pioctl_req->action == MLAN_ACT_GET) {
        dfs_test->usr_cac_period_msec = pdfs_test_params->user_cac_period_msec;
        dfs_test->usr_nop_period_sec = pdfs_test_params->user_nop_period_sec;
        dfs_test->usr_no_chan_change =
            pdfs_test_params->no_channel_change_on_radar;
        dfs_test->usr_fixed_new_chan =
            pdfs_test_params->fixed_new_channel_on_radar;
    } else {
        pdfs_test_params->user_cac_period_msec = dfs_test->usr_cac_period_msec;
        pdfs_test_params->user_nop_period_sec = dfs_test->usr_nop_period_sec;
        pdfs_test_params->no_channel_change_on_radar =
            dfs_test->usr_no_chan_change;
        pdfs_test_params->fixed_new_channel_on_radar =
            dfs_test->usr_fixed_new_chan;
    }

    LEAVE();
    return MLAN_STATUS_SUCCESS;
}
#endif // DFS_TESTING_SUPPORT

/**
 *  @brief Check if channel is under NOP (Non-Occupancy Period)
 *  If so, the channel should not be used until the period expires.
 *
 *  @param pmadapter  Pointer to mlan_adapter
 *  @param channel    Channel number
 *
 *  @return MTRUE or MFALSE
 */
t_bool
wlan_11h_is_channel_under_nop(mlan_adapter * pmadapter, t_u8 channel)
{
    wlan_dfs_timestamp_t *pdfs_ts = MNULL;
    t_u32 now_sec, now_usec;
    t_bool ret = MFALSE;

    ENTER();
    pdfs_ts = wlan_11h_find_dfs_timestamp(pmadapter, channel);

    if (pdfs_ts && (pdfs_ts->channel == channel)
        && (pdfs_ts->represents == DFS_TS_REPR_NOP_START)) {
        /* found NOP_start timestamp entry on channel */
        pmadapter->callbacks.moal_get_system_time(pmadapter->pmoal_handle,
                                                  &now_sec, &now_usec);
#ifdef DFS_TESTING_SUPPORT
        if (pmadapter->dfs_test_params.user_nop_period_sec) {
            PRINTM(MCMD_D, "dfs_testing - user NOP period=%d (sec)\n",
                   pmadapter->dfs_test_params.user_nop_period_sec);
            if ((now_sec - pdfs_ts->ts_sec) <=
                pmadapter->dfs_test_params.user_nop_period_sec) {
                ret = MTRUE;
            }
        } else
#endif
        {
            if ((now_sec - pdfs_ts->ts_sec) <= WLAN_11H_NON_OCCUPANCY_PERIOD)
                ret = MTRUE;
        }

        /* if entry is expired, remove it */
        if (!ret)
            wlan_11h_remove_dfs_timestamp(pmadapter, pdfs_ts);
        else
            PRINTM(MMSG, "11h: channel %d is under NOP - can't use.\n",
                   channel);
    }

    LEAVE();
    return ret;
}

/**
 *  @brief Driver handling for CHANNEL_REPORT_RDY event
 *  This event will have the channel report data appended.
 *
 *  @param priv     Pointer to mlan_private
 *  @param pevent   Pointer to mlan_event
 *
 *  @return MLAN_STATUS_SUCCESS or MLAN_STATUS_FAILURE
 */
mlan_status
wlan_11h_handle_event_chanrpt_ready(mlan_private * priv, mlan_event * pevent)
{
    mlan_status ret = MLAN_STATUS_SUCCESS;
    HostCmd_DS_CHAN_RPT_RSP *pChanRptRsp;
    MrvlIEtypes_Data_t *pTlv;
    MeasRptBasicMap_t *pMeasRptBasic;
    t_u8 *pBuffer;
    t_s32 evtLen;
    t_u16 tlvLen;
    t_u32 sec, uSec;
    wlan_dfs_device_state_t *pstate_dfs = &priv->adapter->state_dfs;

    ENTER();
    pChanRptRsp = (HostCmd_DS_CHAN_RPT_RSP *) & pevent->event_buf;
    DBG_HEXDUMP(MCMD_D, "11h: Event ChanRptReady (HostCmd_DS_CHAN_RPT_RSP)",
                (t_u8 *) pChanRptRsp, wlan_le32_to_cpu(pevent->event_len));

    if (wlan_le32_to_cpu(pChanRptRsp->cmd_result) == MLAN_CMD_RESULT_SUCCESS) {
        pBuffer = (t_u8 *) & pChanRptRsp->tlv_buffer;
        evtLen = wlan_le32_to_cpu(pevent->event_len);
        evtLen -=
            sizeof(HostCmd_DS_CHAN_RPT_RSP) - sizeof(pChanRptRsp->tlv_buffer);

        while (evtLen >= sizeof(MrvlIEtypesHeader_t)) {
            pTlv = (MrvlIEtypes_Data_t *) pBuffer;
            tlvLen = wlan_le16_to_cpu(pTlv->header.len);

            switch (wlan_le16_to_cpu(pTlv->header.type)) {
            case TLV_TYPE_CHANRPT_11H_BASIC:
                pMeasRptBasic = (MeasRptBasicMap_t *) & pTlv->data;
                if (pMeasRptBasic->radar) {
                    pstate_dfs->dfs_radar_found = MTRUE;
                    PRINTM(MMSG, "RADAR Detected on channel %d!\n",
                           pstate_dfs->dfs_check_channel);
                    /* add channel to NOP list */
                    wlan_11h_add_dfs_timestamp(priv->adapter,
                                               DFS_TS_REPR_NOP_START,
                                               pstate_dfs->dfs_check_channel);
                }
                break;

            default:
                break;
            }

            pBuffer += (tlvLen + sizeof(pTlv->header));
            evtLen -= (tlvLen + sizeof(pTlv->header));
            evtLen = (evtLen > 0) ? evtLen : 0;
        }
    } else {
        ret = MLAN_STATUS_FAILURE;
    }

    /* Update DFS structure. */
    priv->adapter->callbacks.moal_get_system_time(priv->adapter->pmoal_handle,
                                                  &sec, &uSec);
    pstate_dfs->dfs_report_time_sec = sec;
    pstate_dfs->dfs_check_pending = MFALSE;

    LEAVE();
    return ret;
}

/**
 *  @brief Check if RADAR_DETECTED handling is blocking data tx
 *
 *  @param pmadapter    Pointer to mlan_adapter
 *
 *  @return MTRUE or MFALSE
 */
t_bool
wlan_11h_radar_detected_tx_blocked(mlan_adapter * pmadapter)
{
    switch (pmadapter->state_rdh.stage) {
    case RDH_OFF:
    case RDH_CHK_INTFS:
    case RDH_STOP_TRAFFIC:
        return MFALSE;
    }
    return MTRUE;
}

/**
 *  @brief Callback for RADAR_DETECTED event driver handling
 *
 *  @param priv    Void pointer to mlan_private
 *
 *  @return MLAN_STATUS_SUCCESS or MLAN_STATUS_FAILURE
 */
mlan_status
wlan_11h_radar_detected_callback(t_void * priv)
{
    mlan_status ret;
    ENTER();
    ret = wlan_11h_radar_detected_handling(((mlan_private *) (priv))->adapter);
    LEAVE();
    return ret;
}

/**
 *  @brief Driver handling for RADAR_DETECTED event
 *
 *  @param pmadapter    Pointer to mlan_adapter
 *
 *  @return MLAN_STATUS_SUCCESS or MLAN_STATUS_FAILURE or MLAN_STATUS_PENDING
 */
mlan_status
wlan_11h_radar_detected_handling(mlan_adapter * pmadapter)
{
#ifdef DEBUG_LEVEL1
    const char *RDH_stage_str[] = {
        "RDH_OFF",
        "RDH_CHK_INTFS",
        "RDH_STOP_TRAFFIC",
        "RDH_GET_INFO_CHANNEL",
        "RDH_GET_INFO_BEACON_DTIM",
        "RDH_SET_CUSTOM_IE",
        "RDH_REM_CUSTOM_IE",
        "RDH_STOP_INTFS",
        "RDH_SET_NEW_CHANNEL",
        "RDH_RESTART_INTFS",
        "RDH_RESTART_TRAFFIC"
    };
#endif

    mlan_status ret = MLAN_STATUS_SUCCESS;
    mlan_private *pmpriv = MNULL;
    t_u32 i;
    wlan_radar_det_hndlg_state_t *pstate_rdh = &pmadapter->state_rdh;

    ENTER();

    switch (pstate_rdh->stage) {
    case RDH_CHK_INTFS:
        PRINTM(MCMD_D, "%s(): stage(%d)=%s\n",
               __FUNCTION__, pstate_rdh->stage,
               RDH_stage_str[pstate_rdh->stage]);

        /* get active interfaces */
        memset(pmadapter, pstate_rdh->priv_list, 0x00,
               sizeof(pstate_rdh->priv_list));
        pstate_rdh->priv_list_count = wlan_get_privs_by_cond(pmadapter,
                                                             wlan_is_intf_active,
                                                             pstate_rdh->
                                                             priv_list);
        PRINTM(MCMD_D, "%s():  priv_list_count = %d\n", __FUNCTION__,
               pstate_rdh->priv_list_count);
        for (i = 0; i < pstate_rdh->priv_list_count; i++)
            PRINTM(MINFO, "%s():  priv_list[%d] = %p\n",
                   __FUNCTION__, i, pstate_rdh->priv_list[i]);

        if (pstate_rdh->priv_list_count == 0) {
            /* no interfaces active... nothing to do */
            PRINTM(MMSG, "11h: Radar Detected - no active priv's,"
                   " skip event handling.\n");
            pstate_rdh->stage = RDH_OFF;
            PRINTM(MCMD_D, "%s(): finished - stage(%d)=%s\n",
                   __FUNCTION__, pstate_rdh->stage,
                   RDH_stage_str[pstate_rdh->stage]);
            break;              // EXIT CASE
        }
        // else: start handling
        pstate_rdh->curr_channel = 0;
        pstate_rdh->new_channel = 0;
        pstate_rdh->uap_band_cfg = 0;
        pstate_rdh->max_bcn_dtim_ms = 0;
        pstate_rdh->priv_curr_idx = RDH_STAGE_FIRST_ENTRY_PRIV_IDX;
        pstate_rdh->stage = RDH_STOP_TRAFFIC;
        // FALL THROUGH TO NEXT STAGE

    case RDH_STOP_TRAFFIC:
        PRINTM(MCMD_D, "%s(): stage(%d)=%s\n",
               __FUNCTION__, pstate_rdh->stage,
               RDH_stage_str[pstate_rdh->stage]);

        PRINTM(MMSG, "11h: Radar Detected - stopping host tx traffic.\n");
        for (i = 0; i < pstate_rdh->priv_list_count; i++) {
            wlan_11h_tx_disable(pstate_rdh->priv_list[i]);
        }

        pstate_rdh->priv_curr_idx = RDH_STAGE_FIRST_ENTRY_PRIV_IDX;
        pstate_rdh->stage = RDH_GET_INFO_CHANNEL;
        // FALL THROUGH TO NEXT STAGE

    case RDH_GET_INFO_CHANNEL:
        PRINTM(MCMD_D, "%s(): stage(%d)=%s, priv_idx=%d\n",
               __FUNCTION__, pstate_rdh->stage,
               RDH_stage_str[pstate_rdh->stage], pstate_rdh->priv_curr_idx);

        /* here, prefer STA info over UAP info - one less CMD to send */
        if (pstate_rdh->priv_curr_idx == RDH_STAGE_FIRST_ENTRY_PRIV_IDX) {
            if (wlan_only_uap_priv_in_list(pstate_rdh->priv_list,
                                           pstate_rdh->priv_list_count)) {
#ifdef UAP_SUPPORT
                /* Assume all UAPs on same channel, use first UAP */
                pmpriv = pstate_rdh->priv_list[0];
                pstate_rdh->priv_curr_idx = 0;
                /* send cmd to get first UAP's info */
                pmpriv->uap_state_chan_cb.pioctl_req_curr = MNULL;
                pmpriv->uap_state_chan_cb.get_chan_callback
                    = wlan_11h_radar_detected_callback;
                ret = wlan_uap_get_channel(pmpriv);
                break;          // EXIT CASE
#endif
            } else {
                /* Assume all STAs on same channel, find first STA */
                MASSERT(pstate_rdh->priv_list_count > 0);
                for (i = 0; i < pstate_rdh->priv_list_count; i++) {
                    pmpriv = pstate_rdh->priv_list[i];
                    if (GET_BSS_ROLE(pmpriv) == MLAN_BSS_ROLE_STA)
                        break;
                }
                /* STA info kept in driver, just copy */
                pstate_rdh->curr_channel =
                    pmpriv->curr_bss_params.bss_descriptor.channel;
            }
        }
#ifdef UAP_SUPPORT
        else if (pstate_rdh->priv_curr_idx < pstate_rdh->priv_list_count) {
            /* repeat entry: UAP return with info */
            pmpriv = pstate_rdh->priv_list[pstate_rdh->priv_curr_idx];
            pstate_rdh->curr_channel = pmpriv->uap_state_chan_cb.channel;
            pstate_rdh->uap_band_cfg = pmpriv->uap_state_chan_cb.band_config;
            PRINTM(MCMD_D, "%s():  uap_band_cfg=0x%02x\n",
                   __FUNCTION__, pstate_rdh->uap_band_cfg);
        }
#endif

        /* add channel to NOP list */
        wlan_11h_add_dfs_timestamp(pmadapter, DFS_TS_REPR_NOP_START,
                                   pstate_rdh->curr_channel);

        /* choose new channel (!= curr channel) and move on */
        i = 0;
        do {
#ifdef UAP_SUPPORT
            if (GET_BSS_ROLE(pmpriv) == MLAN_BSS_ROLE_UAP)
                pstate_rdh->new_channel = wlan_11h_get_uap_start_channel(pmpriv,
                                                                         pmpriv->
                                                                         uap_state_chan_cb.
                                                                         band_config);
            else
#endif
                pstate_rdh->new_channel =
                    wlan_11h_get_adhoc_start_channel(pmpriv);
        } while ((pstate_rdh->new_channel == pstate_rdh->curr_channel) && (++i < MAX_RANDOM_CHANNEL_RETRIES));  /* avoid 
                                                                                                                   deadloop 
                                                                                                                 */
        if (i >= MAX_RANDOM_CHANNEL_RETRIES)    /* report error */
            PRINTM(MERROR, "%s():  ERROR - could not choose new_chan"
                   " (!= curr_chan) !!\n", __FUNCTION__);

#ifdef DFS_TESTING_SUPPORT
        if (pmadapter->dfs_test_params.fixed_new_channel_on_radar) {
            PRINTM(MCMD_D, "dfs_testing - user fixed new_chan=%d\n",
                   pmadapter->dfs_test_params.fixed_new_channel_on_radar);
            pstate_rdh->new_channel =
                pmadapter->dfs_test_params.fixed_new_channel_on_radar;
        }
#endif
        PRINTM(MCMD_D, "%s():  curr_chan=%d, new_chan=%d\n",
               __FUNCTION__, pstate_rdh->curr_channel, pstate_rdh->new_channel);

        pstate_rdh->priv_curr_idx = RDH_STAGE_FIRST_ENTRY_PRIV_IDX;
        pstate_rdh->stage = RDH_GET_INFO_BEACON_DTIM;
        // FALL THROUGH TO NEXT STAGE

    case RDH_GET_INFO_BEACON_DTIM:
        PRINTM(MCMD_D, "%s(): stage(%d)=%s, priv_idx=%d\n",
               __FUNCTION__, pstate_rdh->stage,
               RDH_stage_str[pstate_rdh->stage], pstate_rdh->priv_curr_idx);

#ifdef UAP_SUPPORT
        /* check all intfs in this stage to find longest period */
        /* UAP intf callback returning with info */
        if (pstate_rdh->priv_curr_idx < pstate_rdh->priv_list_count) {
            t_u16 bcn_dtim_msec;
            pmpriv = pstate_rdh->priv_list[pstate_rdh->priv_curr_idx];
            PRINTM(MCMD_D, "%s():  uap.bcn_pd=%d, uap.dtim_pd=%d\n",
                   __FUNCTION__, pmpriv->uap_state_chan_cb.beacon_period,
                   pmpriv->uap_state_chan_cb.dtim_period);
            bcn_dtim_msec = (pmpriv->uap_state_chan_cb.beacon_period
                             * pmpriv->uap_state_chan_cb.dtim_period);
            if (bcn_dtim_msec > pstate_rdh->max_bcn_dtim_ms)
                pstate_rdh->max_bcn_dtim_ms = bcn_dtim_msec;
        }
#endif

        /* check next intf */
        while ((++pstate_rdh->priv_curr_idx) < pstate_rdh->priv_list_count) {
            pmpriv = pstate_rdh->priv_list[pstate_rdh->priv_curr_idx];

#ifdef UAP_SUPPORT
            if (GET_BSS_ROLE(pmpriv) == MLAN_BSS_ROLE_UAP) {
                pmpriv->uap_state_chan_cb.pioctl_req_curr = MNULL;
                pmpriv->uap_state_chan_cb.get_chan_callback
                    = wlan_11h_radar_detected_callback;
                ret = wlan_uap_get_beacon_dtim(pmpriv);
                break;          // leads to exit case
            } else
#endif
            {                   /* get STA info from driver and compare here */
                t_u16 bcn_pd_msec = 100;
                t_u16 dtim_pd_msec = 1;
                t_u16 bcn_dtim_msec;

                if (wlan_11h_is_dfs_master(pmpriv)) {   /* adhoc creator */
                    bcn_pd_msec = pmpriv->beacon_period;
                } else {
                    bcn_pd_msec =
                        pmpriv->curr_bss_params.bss_descriptor.beacon_period;
                    // if (priv->bss_mode != MLAN_BSS_MODE_IBSS)
                    /* TODO: mlan_scan.c needs to parse TLV 0x05 (TIM) for
                       dtim_period */
                }
                PRINTM(MCMD_D, "%s():  sta.bcn_pd=%d, sta.dtim_pd=%d\n",
                       __FUNCTION__, bcn_pd_msec, dtim_pd_msec);
                bcn_dtim_msec = (bcn_pd_msec * dtim_pd_msec);
                if (bcn_dtim_msec > pstate_rdh->max_bcn_dtim_ms)
                    pstate_rdh->max_bcn_dtim_ms = bcn_dtim_msec;
            }
        }

        if (pstate_rdh->priv_curr_idx < pstate_rdh->priv_list_count)
            break;              // EXIT CASE (for UAP)
        // else
        pstate_rdh->priv_curr_idx = RDH_STAGE_FIRST_ENTRY_PRIV_IDX;
        pstate_rdh->stage = RDH_SET_CUSTOM_IE;
        // FALL THROUGH TO NEXT STAGE

    case RDH_SET_CUSTOM_IE:
        PRINTM(MCMD_D, "%s(): stage(%d)=%s, priv_idx=%d\n",
               __FUNCTION__, pstate_rdh->stage,
               RDH_stage_str[pstate_rdh->stage], pstate_rdh->priv_curr_idx);

        /* add CHAN_SW IE - firmware will accept on any interface, and apply to 
           all */
        if (pstate_rdh->priv_curr_idx == RDH_STAGE_FIRST_ENTRY_PRIV_IDX) {
            mlan_ioctl_req *pioctl_req = MNULL;

            ret =
                wlan_11h_prepare_custom_ie_chansw(pmadapter, &pioctl_req,
                                                  MTRUE);
            if ((ret != MLAN_STATUS_SUCCESS) || !pioctl_req) {
                PRINTM(MERROR, "%s(): Error in preparng CHAN_SW IE.\n",
                       __FUNCTION__);
                break;          // EXIT CASE
            }

            PRINTM(MMSG,
                   "11h: Radar Detected - adding CHAN_SW IE to interfaces.\n");
            pmpriv = pstate_rdh->priv_list[0];
            pstate_rdh->priv_curr_idx = 0;
            pioctl_req->bss_index = pmpriv->bss_index;
            ret = wlan_misc_ioctl_custom_ie_list(pmadapter, pioctl_req, MFALSE);
            if (ret != MLAN_STATUS_SUCCESS && ret != MLAN_STATUS_PENDING) {
                PRINTM(MERROR,
                       "%s(): Could not set IE for priv=%p [priv_bss_idx=%d]!\n",
                       __FUNCTION__, pmpriv, pmpriv->bss_index);
                // TODO: how to handle this error case?? ignore & continue?
            }
            /* free ioctl buffer memory before we leave */
            pmadapter->callbacks.moal_mfree(pmadapter->pmoal_handle,
                                            (t_u8 *) pioctl_req);
            break;              // EXIT CASE
        }
        // else
        pstate_rdh->priv_curr_idx = RDH_STAGE_FIRST_ENTRY_PRIV_IDX;
        pstate_rdh->stage = RDH_REM_CUSTOM_IE;
        // FALL THROUGH TO NEXT STAGE

    case RDH_REM_CUSTOM_IE:
        PRINTM(MCMD_D, "%s(): stage(%d)=%s, priv_idx=%d\n",
               __FUNCTION__, pstate_rdh->stage,
               RDH_stage_str[pstate_rdh->stage], pstate_rdh->priv_curr_idx);

        /* remove CHAN_SW IE - firmware will accept on any interface, and apply 
           to all */
        if (pstate_rdh->priv_curr_idx == RDH_STAGE_FIRST_ENTRY_PRIV_IDX) {
            mlan_ioctl_req *pioctl_req = MNULL;

            /* first entry to this stage, do delay DFS requires a minimum of 5
               chances for clients to hear this IE. Use delay: 5 beacons <=
               (BCN_DTIM_MSEC*5) <= 3 seconds). */
            t_u16 delay_ms = MAX(MIN_RDH_CHAN_SW_IE_PERIOD_MSEC,
                                 MIN((5 * pstate_rdh->max_bcn_dtim_ms),
                                     MAX_RDH_CHAN_SW_IE_PERIOD_MSEC));
            PRINTM(MMSG, "11h: Radar Detected - delay %d ms for FW to"
                   " broadcast CHAN_SW IE.\n", delay_ms);
            wlan_mdelay(pmadapter, delay_ms);
            PRINTM(MMSG, "11h: Radar Detected - delay over, removing"
                   " CHAN_SW IE from interfaces.\n");

            ret =
                wlan_11h_prepare_custom_ie_chansw(pmadapter, &pioctl_req,
                                                  MFALSE);
            if ((ret != MLAN_STATUS_SUCCESS) || !pioctl_req) {
                PRINTM(MERROR, "%s(): Error in preparng CHAN_SW IE.\n",
                       __FUNCTION__);
                break;          // EXIT CASE
            }

            pmpriv = pstate_rdh->priv_list[0];
            pstate_rdh->priv_curr_idx = 0;
            pioctl_req->bss_index = pmpriv->bss_index;
            ret = wlan_misc_ioctl_custom_ie_list(pmadapter, pioctl_req, MFALSE);
            if (ret != MLAN_STATUS_SUCCESS && ret != MLAN_STATUS_PENDING) {
                PRINTM(MERROR,
                       "%s(): Could not set IE for priv=%p [priv_bss_idx=%d]!\n",
                       __FUNCTION__, pmpriv, pmpriv->bss_index);
                // TODO: how to handle this error case?? ignore & continue?
            }
            /* free ioctl buffer memory before we leave */
            pmadapter->callbacks.moal_mfree(pmadapter->pmoal_handle,
                                            (t_u8 *) pioctl_req);
            break;              // EXIT CASE
        }
        // else
        pstate_rdh->priv_curr_idx = RDH_STAGE_FIRST_ENTRY_PRIV_IDX;
        pstate_rdh->stage = RDH_STOP_INTFS;
#ifdef DFS_TESTING_SUPPORT
        if (pmadapter->dfs_test_params.no_channel_change_on_radar) {
            PRINTM(MCMD_D, "dfs_testing - no channel change on radar."
                   "  Also skip stop/restart interface stages.\n",
                   pmadapter->dfs_test_params.no_channel_change_on_radar);
            pstate_rdh->priv_curr_idx = RDH_STAGE_FIRST_ENTRY_PRIV_IDX;
            pstate_rdh->stage = RDH_RESTART_TRAFFIC;
            goto rdh_restart_traffic;   // skip several stages
        }
#endif
        // FALL THROUGH TO NEXT STAGE

    case RDH_STOP_INTFS:
        PRINTM(MCMD_D, "%s(): stage(%d)=%s, priv_idx=%d\n",
               __FUNCTION__, pstate_rdh->stage,
               RDH_stage_str[pstate_rdh->stage], pstate_rdh->priv_curr_idx);

        /* issues one cmd (DEAUTH/ADHOC_STOP/BSS_STOP) to each intf */
        while ((++pstate_rdh->priv_curr_idx) < pstate_rdh->priv_list_count) {
            pmpriv = pstate_rdh->priv_list[pstate_rdh->priv_curr_idx];
#ifdef UAP_SUPPORT
            if (GET_BSS_ROLE(pmpriv) == MLAN_BSS_ROLE_UAP) {
                ret = wlan_prepare_cmd(pmpriv, HOST_CMD_APCMD_BSS_STOP,
                                       HostCmd_ACT_GEN_SET, 0, MNULL, MNULL);
                break;          // leads to exit case
            }
#endif
#ifdef STA_SUPPORT
            if (GET_BSS_ROLE(pmpriv) == MLAN_BSS_ROLE_STA) {
                if (wlan_11h_is_dfs_master(pmpriv)) {
                    /* Save ad-hoc creator state before stop clears it */
                    pmpriv->adhoc_state_prev = pmpriv->adhoc_state;
                }
                if (pmpriv->media_connected == MTRUE) {
                    wlan_disconnect(pmpriv, MNULL, MNULL);
                    break;      // leads to exit case
                }
            }
#endif
        }

        if (pstate_rdh->priv_curr_idx < pstate_rdh->priv_list_count)
            break;              // EXIT CASE
        // else
        pstate_rdh->priv_curr_idx = RDH_STAGE_FIRST_ENTRY_PRIV_IDX;
        pstate_rdh->stage = RDH_SET_NEW_CHANNEL;
        // FALL THROUGH TO NEXT STAGE

    case RDH_SET_NEW_CHANNEL:
        PRINTM(MCMD_D, "%s(): stage(%d)=%s, priv_idx=%d\n",
               __FUNCTION__, pstate_rdh->stage,
               RDH_stage_str[pstate_rdh->stage], pstate_rdh->priv_curr_idx);

        /* only set new channel for UAP intfs */
        while ((++pstate_rdh->priv_curr_idx) < pstate_rdh->priv_list_count) {
            pmpriv = pstate_rdh->priv_list[pstate_rdh->priv_curr_idx];
#ifdef UAP_SUPPORT
            if (GET_BSS_ROLE(pmpriv) == MLAN_BSS_ROLE_UAP) {
                pmpriv->uap_state_chan_cb.pioctl_req_curr = MNULL;
                pmpriv->uap_state_chan_cb.get_chan_callback
                    = wlan_11h_radar_detected_callback;
                pstate_rdh->uap_band_cfg |= UAP_BAND_CONFIG_5GHZ;       /* DFS
                                                                           only 
                                                                           in
                                                                           5GHz 
                                                                         */
                ret = wlan_uap_set_channel(pmpriv, pstate_rdh->uap_band_cfg,
                                           pstate_rdh->new_channel);
                break;          // leads to exit case
            }
#endif
        }

        if (pstate_rdh->priv_curr_idx < pstate_rdh->priv_list_count)
            break;              // EXIT CASE (for UAP)
        // else
        pstate_rdh->priv_curr_idx = RDH_STAGE_FIRST_ENTRY_PRIV_IDX;
        pstate_rdh->stage = RDH_RESTART_INTFS;
        // FALL THROUGH TO NEXT STAGE

    case RDH_RESTART_INTFS:
        PRINTM(MCMD_D, "%s(): stage(%d)=%s, priv_idx=%d\n",
               __FUNCTION__, pstate_rdh->stage,
               RDH_stage_str[pstate_rdh->stage], pstate_rdh->priv_curr_idx);

        /* can only restart master intfs */
        while ((++pstate_rdh->priv_curr_idx) < pstate_rdh->priv_list_count) {
            pmpriv = pstate_rdh->priv_list[pstate_rdh->priv_curr_idx];
#ifdef UAP_SUPPORT
            if (GET_BSS_ROLE(pmpriv) == MLAN_BSS_ROLE_UAP) {
                if (wlan_11h_radar_detect_required(pmpriv,
                                                   pstate_rdh->new_channel)) {
                    /* Radar detection is required for this channel, make sure
                       11h is activated in the firmware */
                    ret = wlan_11h_activate(pmpriv, MNULL, MTRUE);
                    ret = wlan_11h_config_master_radar_det(pmpriv, MTRUE);
                    ret = wlan_11h_check_update_radar_det_state(pmpriv);
                }
                ret = wlan_prepare_cmd(pmpriv, HOST_CMD_APCMD_BSS_START,
                                       HostCmd_ACT_GEN_SET, 0, MNULL, MNULL);
                break;          // leads to exit case
            }
#endif
#ifdef STA_SUPPORT
            if (GET_BSS_ROLE(pmpriv) == MLAN_BSS_ROLE_STA) {
                /* Check previous state to find former Ad-hoc creator
                   interface. Set new state to Starting, so it'll be seen as a
                   DFS master. */
                if (pmpriv->adhoc_state_prev == ADHOC_STARTED) {
                    pmpriv->adhoc_state = ADHOC_STARTING;
                    pmpriv->adhoc_state_prev = ADHOC_IDLE;
                }
                if (wlan_11h_is_dfs_master(pmpriv)) {
                    /* set new adhoc channel here */
                    pmpriv->adhoc_channel = pstate_rdh->new_channel;
                    if (wlan_11h_radar_detect_required(pmpriv,
                                                       pstate_rdh->
                                                       new_channel)) {
                        /* Radar detection is required for this channel, make
                           sure 11h is activated in the firmware */
                        ret = wlan_11h_activate(pmpriv, MNULL, MTRUE);
                        ret = wlan_11h_config_master_radar_det(pmpriv, MTRUE);
                        ret = wlan_11h_check_update_radar_det_state(pmpriv);
                    }
                    ret =
                        wlan_prepare_cmd(pmpriv,
                                         HostCmd_CMD_802_11_AD_HOC_START,
                                         HostCmd_ACT_GEN_SET, 0, MNULL,
                                         &pmpriv->adhoc_last_start_ssid);
                    break;      // leads to exit case
                }

                /* NOTE: DON'T reconnect slave STA intfs - infra/adhoc_joiner
                   Do we want to return to same AP/network (on radar channel)?
                   If want to connect back, depend on either: 1. driver's
                   reassoc thread 2. wpa_supplicant, or other user-space app */
            }
#endif
        }

        if (pstate_rdh->priv_curr_idx < pstate_rdh->priv_list_count)
            break;              // EXIT CASE (for UAP)
        // else
        pstate_rdh->priv_curr_idx = RDH_STAGE_FIRST_ENTRY_PRIV_IDX;
        pstate_rdh->stage = RDH_RESTART_TRAFFIC;
        // FALL THROUGH TO NEXT STAGE

    case RDH_RESTART_TRAFFIC:
#ifdef DFS_TESTING_SUPPORT
      rdh_restart_traffic:
#endif
        PRINTM(MCMD_D, "%s(): stage(%d)=%s\n",
               __FUNCTION__, pstate_rdh->stage,
               RDH_stage_str[pstate_rdh->stage]);

        /* continue traffic for reactivated interfaces */
        PRINTM(MMSG, "11h: Radar Detected - restarting host tx traffic.\n");
        for (i = 0; i < pstate_rdh->priv_list_count; i++) {
            wlan_11h_tx_enable(pstate_rdh->priv_list[i]);
        }

        pstate_rdh->stage = RDH_OFF;    /* DONE! */
        PRINTM(MCMD_D, "%s(): finished - stage(%d)=%s\n",
               __FUNCTION__, pstate_rdh->stage,
               RDH_stage_str[pstate_rdh->stage]);
        break;

    default:
        pstate_rdh->stage = RDH_OFF;    /* cancel RDH to unblock Tx packets */
        break;
    }

    LEAVE();
    return ret;
}

/**
 *  @brief DFS Event Preprocessing.
 *  Operates directly on pmadapter variables.
 *
 *  1. EVENT_RADAR_DETECTED comes from firmware without specific
 *     bss_num/bss_type.  Find it an appropriate interface and
 *     update event_cause field in event_buf.
 *
 *  @param pmadapter    Pointer to mlan_adapter
 *
 *  @return    MLAN_STATUS_SUCCESS (update successful)
 *          or MLAN_STATUS_FAILURE (no change)
 */
mlan_status
wlan_11h_dfs_event_preprocessing(mlan_adapter * pmadapter)
{
    mlan_status ret = MLAN_STATUS_FAILURE;
    mlan_private *pmpriv = MNULL;
    mlan_private *priv_list[MLAN_MAX_BSS_NUM];

    ENTER();
    switch (pmadapter->event_cause & EVENT_ID_MASK) {
    case EVENT_RADAR_DETECTED:
        /* find active intf: prefer dfs_master over dfs_slave */
        if (wlan_get_privs_by_two_cond(pmadapter,
                                       wlan_11h_is_master_active_on_dfs_chan,
                                       wlan_11h_is_dfs_master,
                                       MTRUE, priv_list)) {
            pmpriv = priv_list[0];
            PRINTM(MINFO, "%s: found dfs_master priv=%p\n",
                   __FUNCTION__, pmpriv);
        } else if (wlan_get_privs_by_two_cond(pmadapter,
                                              wlan_11h_is_slave_active_on_dfs_chan,
                                              wlan_11h_is_dfs_slave,
                                              MTRUE, priv_list)) {
            pmpriv = priv_list[0];
            PRINTM(MINFO, "%s: found dfs_slave priv=%p\n",
                   __FUNCTION__, pmpriv);
        }

        /* update event_cause if we found an appropriate priv */
        if (pmpriv) {
            pmlan_buffer pmevbuf = pmadapter->pmlan_buffer_event;
            t_u32 new_event_cause = pmadapter->event_cause & EVENT_ID_MASK;
            new_event_cause |= ((GET_BSS_NUM(pmpriv) & 0xff) << 16) |
                ((pmpriv->bss_type & 0xff) << 24);
            PRINTM(MINFO, "%s: priv - bss_num=%d, bss_type=%d\n",
                   __FUNCTION__, GET_BSS_NUM(pmpriv), pmpriv->bss_type);
            memcpy(pmadapter, pmevbuf->pbuf + pmevbuf->data_offset,
                   &new_event_cause, sizeof(new_event_cause));
            ret = MLAN_STATUS_SUCCESS;
        }
        break;
    }

    LEAVE();
    return ret;
}

/**
 *  @brief try to switch to a non-dfs channel
 *
 *  @param priv    Void pointer to mlan_private
 *
 *  @param chan    pointer to channel
 *
 *  @return MLAN_STATUS_SUCCESS or MLAN_STATUS_FAILURE or MLAN_STATUS_PENDING
 */
mlan_status
wlan_11h_switch_non_dfs_chan(mlan_private * priv, t_u8 * chan)
{
    mlan_status ret = MLAN_STATUS_FAILURE;
    t_u32 i;
    t_u32 rand_entry;
    t_u8 def_chan;
    t_u8 rand_tries = 0;
    region_chan_t *chn_tbl = MNULL;
    pmlan_adapter pmadapter = priv->adapter;

    ENTER();

    /* get the channel table first */
    for (i = 0; i < MAX_REGION_CHANNEL_NUM; i++) {
        if (pmadapter->region_channel[i].band == BAND_A
            && pmadapter->region_channel[i].valid) {
            chn_tbl = &pmadapter->region_channel[i];
            break;
        }
    }

    if (!chn_tbl || !chn_tbl->pcfp) {
        goto done;
    }

    do {
        rand_entry = wlan_11h_get_random_num(pmadapter) % chn_tbl->num_cfp;
        def_chan = (t_u8) chn_tbl->pcfp[rand_entry].channel;
        rand_tries++;
    } while ((wlan_11h_is_channel_under_nop(pmadapter, def_chan) ||
              chn_tbl->pcfp[rand_entry].radar_detect == MTRUE) &&
             (rand_tries < MAX_SWITCH_CHANNEL_RETRIES));

    /* meet max retries, use the lowest non-dfs channel */
    if (rand_tries == MAX_SWITCH_CHANNEL_RETRIES) {
        for (i = 0; i < chn_tbl->num_cfp; i++) {
            if (chn_tbl->pcfp[i].radar_detect == MFALSE &&
                !wlan_11h_is_channel_under_nop(pmadapter,
                                               (t_u8) chn_tbl->pcfp[i].
                                               channel)) {
                def_chan = (t_u8) chn_tbl->pcfp[i].channel;
                break;
            }
        }
        if (i == chn_tbl->num_cfp) {
            goto done;
        }
    }

    *chan = def_chan;
    ret = MLAN_STATUS_SUCCESS;
  done:
    LEAVE();
    return ret;
}