summaryrefslogtreecommitdiff
path: root/drivers/net/wireless/sd8797/mlan/mlan_scan.c
blob: 2c4eadf04a2ec4c06b2c7396e159b9a460774075 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
/** @file mlan_scan.c
 *
 *  @brief Functions implementing wlan scan IOCTL and firmware command APIs
 *
 *  IOCTL handlers as well as command preparation and response routines
 *  for sending scan commands to the firmware.
 *
 *  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:
    10/28/2008: initial version
******************************************************/

#include "mlan.h"
#include "mlan_join.h"
#include "mlan_util.h"
#include "mlan_fw.h"
#include "mlan_main.h"
#include "mlan_11n.h"
#include "mlan_11h.h"

/********************************************************
                Local Constants
********************************************************/

/** The maximum number of channels the firmware can scan per command */
#define MRVDRV_MAX_CHANNELS_PER_SPECIFIC_SCAN   14

/**
 * Number of channels to scan per firmware scan command issuance.
 *
 * Number restricted to prevent hitting the limit on the amount of scan data
 * returned in a single firmware scan command.
 */
#define MRVDRV_CHANNELS_PER_SCAN_CMD            4

/** Memory needed to store a max sized Channel List TLV for a firmware scan */
#define CHAN_TLV_MAX_SIZE  (sizeof(MrvlIEtypesHeader_t)                  \
                            + (MRVDRV_MAX_CHANNELS_PER_SPECIFIC_SCAN     \
                               * sizeof(ChanScanParamSet_t)))

/** Memory needed to store supported rate */
#define RATE_TLV_MAX_SIZE   (sizeof(MrvlIEtypes_RatesParamSet_t) + HOSTCMD_SUPPORTED_RATES)

/** Memory needed to store a max number/size WildCard SSID TLV for a firmware scan */
#define WILDCARD_SSID_TLV_MAX_SIZE  \
            (MRVDRV_MAX_SSID_LIST_LENGTH  * (sizeof(MrvlIEtypes_WildCardSsIdParamSet_t) + MRVDRV_MAX_SSID_LENGTH))

/** WPS TLV MAX size is MAX IE size plus 2 bytes for t_u16 MRVL TLV extension */
#define WPS_TLV_MAX_SIZE   (sizeof(IEEEtypes_VendorSpecific_t) + 2)
/** Maximum memory needed for a wlan_scan_cmd_config with all TLVs at max */
#define MAX_SCAN_CFG_ALLOC (sizeof(wlan_scan_cmd_config)        \
                            + sizeof(MrvlIEtypes_NumProbes_t)   \
                            + sizeof(MrvlIETypes_HTCap_t)       \
                            + CHAN_TLV_MAX_SIZE                 \
                            + RATE_TLV_MAX_SIZE                 \
                            + WILDCARD_SSID_TLV_MAX_SIZE        \
                            + WPS_TLV_MAX_SIZE)

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

/**
 * Interally used to send a configured scan cmd between driver routines
 */
typedef union
{
    /** Scan configuration (variable length) */
    wlan_scan_cmd_config config;
    /** Max allocated block */
    t_u8 config_alloc_buf[MAX_SCAN_CFG_ALLOC];
} wlan_scan_cmd_config_tlv;

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

/********************************************************
                Local Functions
********************************************************/
/** Cipher suite definition */
enum cipher_suite
{
    CIPHER_SUITE_TKIP,
    CIPHER_SUITE_CCMP,
    CIPHER_SUITE_MAX
};

static t_u8 wpa_oui[CIPHER_SUITE_MAX][4] = {
    {0x00, 0x50, 0xf2, 0x02},   /* TKIP */
    {0x00, 0x50, 0xf2, 0x04},   /* AES */
};

static t_u8 rsn_oui[CIPHER_SUITE_MAX][4] = {
    {0x00, 0x0f, 0xac, 0x02},   /* TKIP */
    {0x00, 0x0f, 0xac, 0x04},   /* AES */
};

/**
 *  @brief This function will parse a given IE for a given OUI
 *
 *  Parse a given WPA/RSN IE to find if it has a given oui in PTK,
 *  if no OUI found for PTK it returns 0.
 *
 *  @param pbss_desc       A pointer to current BSS descriptor
 *  @return                0 on failure to find OUI, 1 on success.
 */
static t_u8
search_oui_in_ie(mlan_adapter * pmadapter, IEBody * ie_body, t_u8 * oui)
{
    t_u8 count;

    count = ie_body->PtkCnt[0];

    ENTER();
    /* There could be multiple OUIs for PTK hence 1) Take the length. 2) Check
       all the OUIs for AES. 3) If one of them is AES then pass success. */
    while (count) {
        if (!memcmp(pmadapter, ie_body->PtkBody, oui, sizeof(ie_body->PtkBody))) {
            LEAVE();
            return MLAN_OUI_PRESENT;
        }

        --count;
        if (count) {
            ie_body = (IEBody *) ((t_u8 *) ie_body + sizeof(ie_body->PtkBody));
        }
    }

    PRINTM(MINFO, "The OUI %x:%x:%x:%x is not found in PTK \n", oui[0], oui[1],
           oui[2], oui[3]);
    LEAVE();
    return MLAN_OUI_NOT_PRESENT;
}

/**
 *  @brief This function will pass the correct ie and oui to search_oui_in_ie
 *
 *  Check the pbss_desc for appropriate IE and then check if RSN IE has AES
 *  OUI in it. If RSN IE does not have AES in PTK then return 0;
 *
 *  @param pbss_desc       A pointer to current BSS descriptor
 *  @return                0 on failure to find AES OUI, 1 on success.
 */
static t_u8
is_rsn_oui_present(mlan_adapter * pmadapter, BSSDescriptor_t * pbss_desc,
                   t_u32 cipher_suite)
{
    t_u8 *oui = MNULL;
    IEBody *ie_body = MNULL;
    t_u8 ret = MLAN_OUI_NOT_PRESENT;

    ENTER();
    if (((pbss_desc->prsn_ie) &&
         ((*(pbss_desc->prsn_ie)).ieee_hdr.element_id == RSN_IE))) {
        ie_body =
            (IEBody *) (((t_u8 *) pbss_desc->prsn_ie->data) +
                        RSN_GTK_OUI_OFFSET);
        oui = &rsn_oui[cipher_suite][0];
        if ((ret = search_oui_in_ie(pmadapter, ie_body, oui))) {
            LEAVE();
            return ret;
        }
    }
    LEAVE();
    return ret;
}

/**
 *  @brief This function will pass the correct ie and oui to search_oui_in_ie
 *
 *  Check the pbss_desc for appropriate IE and then check if WPA IE has AES
 *  OUI in it. If WPA IE does not have AES in PTK then return 0;
 *
 *  @param pbss_desc       A pointer to current BSS descriptor
 *  @return                0 on failure to find AES OUI, 1 on success.
 */
static t_u8
is_wpa_oui_present(mlan_adapter * pmadapter, BSSDescriptor_t * pbss_desc,
                   t_u32 cipher_suite)
{
    t_u8 *oui = MNULL;
    IEBody *ie_body = MNULL;
    t_u8 ret = MLAN_OUI_NOT_PRESENT;

    ENTER();
    if (((pbss_desc->pwpa_ie) &&
         ((*(pbss_desc->pwpa_ie)).vend_hdr.element_id == WPA_IE))) {
        ie_body = (IEBody *) pbss_desc->pwpa_ie->data;
        oui = &wpa_oui[cipher_suite][0];
        if ((ret = search_oui_in_ie(pmadapter, ie_body, oui))) {
            LEAVE();
            return ret;
        }
    }
    LEAVE();
    return ret;
}

/**
 *  @brief compare config band and a band from the scan result,
 *  which is defined by functiion radio_type_to_band(t_u8 radio_type) above
 *
 *  @param cfg_band:  band configured
 *         scan_band: band from scan result
 *
 *  @return  matched: non-zero. unmatched: 0
 *
 */
static t_u8
wlan_is_band_compatible(t_u8 cfg_band, t_u8 scan_band)
{
    t_u8 band;
    switch (scan_band) {
    case BAND_A:
        band = BAND_A | BAND_AN;
        break;
    case BAND_G:
    default:
        band = BAND_B | BAND_G | BAND_GN;
    }
    return cfg_band & band;
}

/**
 *  @brief This function finds the best SSID in the Scan List
 *
 *  Search the scan table for the best SSID that also matches the current
 *   adapter network preference (infrastructure or adhoc)
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @return             index in BSSID list
 */
static t_s32
wlan_find_best_network_in_list(IN mlan_private * pmpriv)
{
    mlan_adapter *pmadapter = pmpriv->adapter;
    t_u32 mode = pmpriv->bss_mode;
    t_s32 best_net = -1;
    t_s32 best_rssi = 0;
    t_u32 i;

    ENTER();

    PRINTM(MINFO, "Num of BSSIDs = %d\n", pmadapter->num_in_scan_table);

    for (i = 0; i < pmadapter->num_in_scan_table; i++) {
        switch (mode) {
        case MLAN_BSS_MODE_INFRA:
        case MLAN_BSS_MODE_IBSS:
            if (wlan_is_network_compatible(pmpriv, i, mode) >= 0) {
                if (SCAN_RSSI(pmadapter->pscan_table[i].rssi) > best_rssi) {
                    best_rssi = SCAN_RSSI(pmadapter->pscan_table[i].rssi);
                    best_net = i;
                }
            }
            break;
        case MLAN_BSS_MODE_AUTO:
        default:
            if (SCAN_RSSI(pmadapter->pscan_table[i].rssi) > best_rssi) {
                best_rssi = SCAN_RSSI(pmadapter->pscan_table[i].rssi);
                best_net = i;
            }
            break;
        }
    }

    LEAVE();
    return best_net;
}

/**
 *  @brief Create a channel list for the driver to scan based on region info
 *
 *  Use the driver region/band information to construct a comprehensive list
 *    of channels to scan.  This routine is used for any scan that is not
 *    provided a specific channel list to scan.
 *
 *  @param pmpriv           A pointer to mlan_private structure
 *  @param puser_scan_in    MNULL or pointer to scan configuration parameters
 *  @param pscan_chan_list  Output parameter: Resulting channel list to scan
 *  @param filtered_scan    Flag indicating whether or not a BSSID or SSID filter
 *                          is being sent in the command to firmware.  Used to
 *                          increase the number of channels sent in a scan
 *                          command and to disable the firmware channel scan
 *                          filter.
 *
 *  @return                 N/A
 */
static t_void
wlan_scan_create_channel_list(IN mlan_private * pmpriv,
                              IN const wlan_user_scan_cfg * puser_scan_in,
                              OUT ChanScanParamSet_t * pscan_chan_list,
                              IN t_u8 filtered_scan)
{
    mlan_adapter *pmadapter = pmpriv->adapter;
    region_chan_t *pscan_region;
    chan_freq_power_t *cfp;
    t_u32 region_idx;
    t_u32 chan_idx = 0;
    t_u32 next_chan;
    t_u8 scan_type;
    t_u8 radio_type;

    ENTER();

    for (region_idx = 0;
         region_idx < NELEMENTS(pmadapter->region_channel); region_idx++) {

        if (wlan_11d_is_enabled(pmpriv) && pmpriv->media_connected != MTRUE) {
            /* Scan all the supported chan for the first scan */
            if (!pmadapter->universal_channel[region_idx].valid)
                continue;
            pscan_region = &pmadapter->universal_channel[region_idx];
        } else {
            if (!pmadapter->region_channel[region_idx].valid)
                continue;
            pscan_region = &pmadapter->region_channel[region_idx];
        }

        if (puser_scan_in && !puser_scan_in->chan_list[0].chan_number &&
            puser_scan_in->chan_list[0].radio_type & BAND_SPECIFIED) {
            radio_type =
                puser_scan_in->chan_list[0].radio_type & ~BAND_SPECIFIED;
            if (!radio_type && (pscan_region->band != BAND_B) &&
                (pscan_region->band != BAND_G))
                continue;
            if (radio_type && (pscan_region->band != BAND_A))
                continue;
        }
        if (!wlan_is_band_compatible
            (pmpriv->config_bands | pmadapter->adhoc_start_band,
             pscan_region->band))
            continue;
        for (next_chan = 0;
             next_chan < pscan_region->num_cfp; next_chan++, chan_idx++) {
            /* Set the default scan type to the user specified type, will later
               be changed to passive on a per channel basis if restricted by
               regulatory requirements (11d or 11h) */
            scan_type = pmadapter->scan_type;
            cfp = pscan_region->pcfp + next_chan;

            if (scan_type == MLAN_SCAN_TYPE_ACTIVE
                && wlan_11d_is_enabled(pmpriv)) {
                scan_type = wlan_11d_get_scan_type(pmadapter,
                                                   pscan_region->band,
                                                   (t_u8) cfp->channel,
                                                   &pmadapter->
                                                   parsed_region_chan);
            }

            switch (pscan_region->band) {
            case BAND_A:
                pscan_chan_list[chan_idx].radio_type =
                    HostCmd_SCAN_RADIO_TYPE_A;
                if (!wlan_11d_is_enabled(pmpriv)) {
                    /* 11D not available... play it safe on DFS channels */
                    if (wlan_11h_radar_detect_required
                        (pmpriv, (t_u8) cfp->channel))
                        scan_type = MLAN_SCAN_TYPE_PASSIVE;
                }
                break;
            case BAND_B:
            case BAND_G:
            default:
                pscan_chan_list[chan_idx].radio_type =
                    HostCmd_SCAN_RADIO_TYPE_BG;
                break;
            }

            if (puser_scan_in && puser_scan_in->chan_list[0].scan_time) {
                pscan_chan_list[chan_idx].max_scan_time =
                    wlan_cpu_to_le16((t_u16) puser_scan_in->chan_list[0].
                                     scan_time);
            } else if (scan_type == MLAN_SCAN_TYPE_PASSIVE) {
                pscan_chan_list[chan_idx].max_scan_time =
                    wlan_cpu_to_le16(pmadapter->passive_scan_time);
            } else if (filtered_scan) {
                pscan_chan_list[chan_idx].max_scan_time =
                    wlan_cpu_to_le16(pmadapter->specific_scan_time);
            } else {
                pscan_chan_list[chan_idx].max_scan_time =
                    wlan_cpu_to_le16(pmadapter->active_scan_time);
            }

            if (scan_type == MLAN_SCAN_TYPE_PASSIVE) {
                pscan_chan_list[chan_idx].chan_scan_mode.passive_scan = MTRUE;
            } else {
                pscan_chan_list[chan_idx].chan_scan_mode.passive_scan = MFALSE;
            }

            pscan_chan_list[chan_idx].chan_number = (t_u8) cfp->channel;

            if (filtered_scan) {
                pscan_chan_list[chan_idx].chan_scan_mode.disable_chan_filt =
                    MTRUE;
            }
        }
    }

    LEAVE();
}

/**
 *  @brief Add WPS IE to probe request frame
 *
 *  @param pmpriv             A pointer to mlan_private structure
 *  @param pptlv_out          A pointer to TLV to fill in
 *
 *  @return                   N/A
 */
static void
wlan_add_wps_probe_request_ie(IN mlan_private * pmpriv, OUT t_u8 ** pptlv_out)
{
    MrvlIEtypesHeader_t *tlv;

    ENTER();

    if (pmpriv->wps.wps_ie.vend_hdr.len) {
        tlv = (MrvlIEtypesHeader_t *) * pptlv_out;
        tlv->type = wlan_cpu_to_le16(VENDOR_SPECIFIC_221);
        tlv->len = wlan_cpu_to_le16(pmpriv->wps.wps_ie.vend_hdr.len);
        *pptlv_out += sizeof(MrvlIEtypesHeader_t);
        memcpy(pmpriv->adapter, *pptlv_out,
               pmpriv->wps.wps_ie.vend_hdr.oui,
               pmpriv->wps.wps_ie.vend_hdr.len);
        *pptlv_out += (pmpriv->wps.wps_ie.vend_hdr.len
                       + sizeof(MrvlIEtypesHeader_t));
    }
    LEAVE();
}

/**
 *  @brief Construct and send multiple scan config commands to the firmware
 *
 *  Previous routines have created a wlan_scan_cmd_config with any requested
 *   TLVs.  This function splits the channel TLV into max_chan_per_scan lists
 *   and sends the portion of the channel TLV along with the other TLVs
 *   to the wlan_cmd routines for execution in the firmware.
 *
 *  @param pmpriv             A pointer to mlan_private structure
 *  @param pioctl_buf         A pointer to MLAN IOCTL Request buffer
 *  @param max_chan_per_scan  Maximum number channels to be included in each
 *                            scan command sent to firmware
 *  @param filtered_scan      Flag indicating whether or not a BSSID or SSID
 *                            filter is being used for the firmware command
 *                            scan command sent to firmware
 *  @param pscan_cfg_out      Scan configuration used for this scan.
 *  @param pchan_tlv_out      Pointer in the pscan_cfg_out where the channel TLV
 *                            should start.  This is past any other TLVs that
 *                            must be sent down in each firmware command.
 *  @param pscan_chan_list    List of channels to scan in max_chan_per_scan segments
 *
 *  @return                   MLAN_STATUS_SUCCESS or error return otherwise
 */
static mlan_status
wlan_scan_channel_list(IN mlan_private * pmpriv,
                       IN t_void * pioctl_buf,
                       IN t_u32 max_chan_per_scan,
                       IN t_u8 filtered_scan,
                       OUT wlan_scan_cmd_config * pscan_cfg_out,
                       OUT MrvlIEtypes_ChanListParamSet_t * pchan_tlv_out,
                       IN ChanScanParamSet_t * pscan_chan_list)
{
    mlan_status ret = MLAN_STATUS_SUCCESS;
    mlan_adapter *pmadapter = pmpriv->adapter;
    ChanScanParamSet_t *ptmp_chan_list;
    ChanScanParamSet_t *pstart_chan;
    pmlan_ioctl_req pioctl_req = (mlan_ioctl_req *) pioctl_buf;

    t_u32 tlv_idx;
    t_u32 total_scan_time;
    t_u32 done_early;
    t_u32 cmd_no;

    ENTER();

    if (!pscan_cfg_out || !pchan_tlv_out || !pscan_chan_list) {
        PRINTM(MINFO, "Scan: Null detect: %p, %p, %p\n",
               pscan_cfg_out, pchan_tlv_out, pscan_chan_list);
        if (pioctl_req)
            pioctl_req->status_code = MLAN_ERROR_CMD_SCAN_FAIL;
        LEAVE();
        return MLAN_STATUS_FAILURE;
    }
    if (!pscan_chan_list->chan_number) {
        PRINTM(MERROR, "Scan: No channel configured\n");
        if (pioctl_req)
            pioctl_req->status_code = MLAN_ERROR_CMD_SCAN_FAIL;
        LEAVE();
        return MLAN_STATUS_FAILURE;
    }

    pchan_tlv_out->header.type = wlan_cpu_to_le16(TLV_TYPE_CHANLIST);

    /* Set the temp channel struct pointer to the start of the desired list */
    ptmp_chan_list = pscan_chan_list;

    /* Loop through the desired channel list, sending a new firmware scan
       commands for each max_chan_per_scan channels (or for 1,6,11 individually
       if configured accordingly) */
    while (ptmp_chan_list->chan_number) {

        tlv_idx = 0;
        total_scan_time = 0;
        pchan_tlv_out->header.len = 0;
        pstart_chan = ptmp_chan_list;
        done_early = MFALSE;

        /*
         * Construct the Channel TLV for the scan command.  Continue to
         * insert channel TLVs until:
         *   - the tlv_idx hits the maximum configured per scan command
         *   - the next channel to insert is 0 (end of desired channel list)
         *   - done_early is set (controlling individual scanning of 1,6,11)
         */
        while (tlv_idx < max_chan_per_scan && ptmp_chan_list->chan_number &&
               !done_early) {

            PRINTM(MINFO, "Scan: Chan(%3d), Radio(%d), Mode(%d,%d), Dur(%d)\n",
                   ptmp_chan_list->chan_number,
                   ptmp_chan_list->radio_type,
                   ptmp_chan_list->chan_scan_mode.passive_scan,
                   ptmp_chan_list->chan_scan_mode.disable_chan_filt,
                   wlan_le16_to_cpu(ptmp_chan_list->max_scan_time));

            /* Copy the current channel TLV to the command being prepared */
            memcpy(pmadapter, pchan_tlv_out->chan_scan_param + tlv_idx,
                   ptmp_chan_list, sizeof(pchan_tlv_out->chan_scan_param));

            /* Increment the TLV header length by the size appended */
            pchan_tlv_out->header.len += sizeof(pchan_tlv_out->chan_scan_param);

            /*
             * The tlv buffer length is set to the number of bytes of the
             *   between the channel tlv pointer and the start of the
             *   tlv buffer.  This compensates for any TLVs that were appended
             *   before the channel list.
             */
            pscan_cfg_out->tlv_buf_len = (t_u32) ((t_u8 *) pchan_tlv_out
                                                  - pscan_cfg_out->tlv_buf);

            /* Add the size of the channel tlv header and the data length */
            pscan_cfg_out->tlv_buf_len += (sizeof(pchan_tlv_out->header)
                                           + pchan_tlv_out->header.len);

            /* Increment the index to the channel tlv we are constructing */
            tlv_idx++;

            /* Count the total scan time per command */
            total_scan_time += wlan_le16_to_cpu(ptmp_chan_list->max_scan_time);

            done_early = MFALSE;

            /* Stop the loop if the *current* channel is in the 1,6,11 set and
               we are not filtering on a BSSID or SSID. */
            if (!filtered_scan && (ptmp_chan_list->chan_number == 1 ||
                                   ptmp_chan_list->chan_number == 6 ||
                                   ptmp_chan_list->chan_number == 11)) {
                done_early = MTRUE;
            }

            /* Increment the tmp pointer to the next channel to be scanned */
            ptmp_chan_list++;

            /* Stop the loop if the *next* channel is in the 1,6,11 set.  This
               will cause it to be the only channel scanned on the next
               interation */
            if (!filtered_scan && (ptmp_chan_list->chan_number == 1 ||
                                   ptmp_chan_list->chan_number == 6 ||
                                   ptmp_chan_list->chan_number == 11)) {
                done_early = MTRUE;
            }
        }

        /* The total scan time should be less than scan command timeout value */
        if (total_scan_time > MRVDRV_MAX_TOTAL_SCAN_TIME) {
            PRINTM(MMSG,
                   "Total scan time %d ms is over limit (%d ms), scan skipped\n",
                   total_scan_time, MRVDRV_MAX_TOTAL_SCAN_TIME);
            if (pioctl_req)
                pioctl_req->status_code = MLAN_ERROR_CMD_SCAN_FAIL;
            ret = MLAN_STATUS_FAILURE;
            break;
        }

        pchan_tlv_out->header.len = wlan_cpu_to_le16(pchan_tlv_out->header.len);

        pmadapter->pscan_channels = pstart_chan;

        /* Send the scan command to the firmware with the specified cfg */
        if (pmadapter->ext_scan)
            cmd_no = HostCmd_CMD_802_11_SCAN_EXT;
        else
            cmd_no = HostCmd_CMD_802_11_SCAN;
        ret = wlan_prepare_cmd(pmpriv,
                               cmd_no,
                               HostCmd_ACT_GEN_SET,
                               0, pioctl_buf, pscan_cfg_out);
        if (ret)
            break;
    }

    LEAVE();

    if (ret) {
        return MLAN_STATUS_FAILURE;
    }

    return MLAN_STATUS_SUCCESS;
}

/**
 *  @brief Construct a wlan_scan_cmd_config structure to use in scan commands
 *
 *  Application layer or other functions can invoke wlan_scan_networks
 *    with a scan configuration supplied in a wlan_ioctl_user_scan_cfg struct.
 *    This structure is used as the basis of one or many wlan_scan_cmd_config
 *    commands that are sent to the command processing module and sent to
 *    firmware.
 *
 *  Create a wlan_scan_cmd_config based on the following user supplied
 *    parameters (if present):
 *             - SSID filter
 *             - BSSID filter
 *             - Number of Probes to be sent
 *             - Channel list
 *
 *  If the SSID or BSSID filter is not present, disable/clear the filter.
 *  If the number of probes is not set, use the adapter default setting
 *  Qualify the channel
 *
 *  @param pmpriv              A pointer to mlan_private structure
 *  @param puser_scan_in       MNULL or pointer to scan config parameters
 *  @param pscan_cfg_out       Output parameter: Resulting scan configuration
 *  @param ppchan_list_out     Output parameter: Pointer to the start of the
 *                             channel TLV portion of the output scan config
 *  @param pscan_chan_list     Output parameter: Pointer to the resulting
 *                             channel list to scan
 *  @param pmax_chan_per_scan  Output parameter: Number of channels to scan for
 *                             each issuance of the firmware scan command
 *  @param pfiltered_scan      Output parameter: Flag indicating whether or not
 *                             a BSSID or SSID filter is being sent in the
 *                             command to firmware. Used to increase the number
 *                             of channels sent in a scan command and to
 *                             disable the firmware channel scan filter.
 *  @param pscan_current_only  Output parameter: Flag indicating whether or not
 *                             we are only scanning our current active channel
 *
 *  @return                 MLAN_STATUS_SUCCESS or MLAN_STATUS_FAILURE
 */
static mlan_status
wlan_scan_setup_scan_config(IN mlan_private * pmpriv,
                            IN const wlan_user_scan_cfg * puser_scan_in,
                            OUT wlan_scan_cmd_config * pscan_cfg_out,
                            OUT MrvlIEtypes_ChanListParamSet_t **
                            ppchan_list_out,
                            OUT ChanScanParamSet_t * pscan_chan_list,
                            OUT t_u8 * pmax_chan_per_scan,
                            OUT t_u8 * pfiltered_scan,
                            OUT t_u8 * pscan_current_only)
{
    mlan_adapter *pmadapter = pmpriv->adapter;
    mlan_status ret = MLAN_STATUS_SUCCESS;
    MrvlIEtypes_NumProbes_t *pnum_probes_tlv;
    MrvlIEtypes_WildCardSsIdParamSet_t *pwildcard_ssid_tlv;
    MrvlIEtypes_RatesParamSet_t *prates_tlv;
    const t_u8 zero_mac[MLAN_MAC_ADDR_LENGTH] = { 0, 0, 0, 0, 0, 0 };
    t_u8 *ptlv_pos;
    t_u32 num_probes;
    t_u32 ssid_len;
    t_u32 chan_idx;
    t_u32 scan_type;
    t_u16 scan_dur;
    t_u8 channel;
    t_u8 radio_type;
    t_u32 ssid_idx;
    t_u8 ssid_filter;
    WLAN_802_11_RATES rates;
    t_u32 rates_size;
    MrvlIETypes_HTCap_t *pht_cap;

    ENTER();

    /* The tlv_buf_len is calculated for each scan command.  The TLVs added in
       this routine will be preserved since the routine that sends the command
       will append channelTLVs at *ppchan_list_out.  The difference between the
       *ppchan_list_out and the tlv_buf start will be used to calculate the
       size of anything we add in this routine. */
    pscan_cfg_out->tlv_buf_len = 0;

    /* Running tlv pointer.  Assigned to ppchan_list_out at end of function so
       later routines know where channels can be added to the command buf */
    ptlv_pos = pscan_cfg_out->tlv_buf;

    /* Initialize the scan as un-filtered; the flag is later set to TRUE below
       if a SSID or BSSID filter is sent in the command */
    *pfiltered_scan = MFALSE;

    /* Initialize the scan as not being only on the current channel.  If the
       channel list is customized, only contains one channel, and is the active
       channel, this is set true and data flow is not halted. */
    *pscan_current_only = MFALSE;

    if (puser_scan_in) {

        ssid_filter = MFALSE;

        /* Set the bss type scan filter, use Adapter setting if unset */
        pscan_cfg_out->bss_mode = (puser_scan_in->bss_mode
                                   ? (t_u8) puser_scan_in->bss_mode :
                                   (t_u8) pmadapter->scan_mode);

        /* Set the number of probes to send, use Adapter setting if unset */
        num_probes = (puser_scan_in->num_probes ? puser_scan_in->num_probes :
                      pmadapter->scan_probes);

        /*
         * Set the BSSID filter to the incoming configuration,
         *   if non-zero.  If not set, it will remain disabled (all zeros).
         */
        memcpy(pmadapter, pscan_cfg_out->specific_bssid,
               puser_scan_in->specific_bssid,
               sizeof(pscan_cfg_out->specific_bssid));

        for (ssid_idx = 0; ((ssid_idx < NELEMENTS(puser_scan_in->ssid_list))
                            && (*puser_scan_in->ssid_list[ssid_idx].ssid ||
                                puser_scan_in->ssid_list[ssid_idx].max_len));
             ssid_idx++) {

            ssid_len =
                wlan_strlen((t_s8 *) puser_scan_in->ssid_list[ssid_idx].ssid);

            pwildcard_ssid_tlv
                = (MrvlIEtypes_WildCardSsIdParamSet_t *) ptlv_pos;
            pwildcard_ssid_tlv->header.type
                = wlan_cpu_to_le16(TLV_TYPE_WILDCARDSSID);
            pwildcard_ssid_tlv->header.len
                = (t_u16) (ssid_len
                           + sizeof(pwildcard_ssid_tlv->max_ssid_length));
            pwildcard_ssid_tlv->max_ssid_length
                = puser_scan_in->ssid_list[ssid_idx].max_len;

            memcpy(pmadapter, pwildcard_ssid_tlv->ssid,
                   puser_scan_in->ssid_list[ssid_idx].ssid,
                   MIN(MLAN_MAX_SSID_LENGTH, ssid_len));

            ptlv_pos += (sizeof(pwildcard_ssid_tlv->header)
                         + pwildcard_ssid_tlv->header.len);

            pwildcard_ssid_tlv->header.len
                = wlan_cpu_to_le16(pwildcard_ssid_tlv->header.len);

            PRINTM(MINFO, "Scan: ssid_list[%d]: %s, %d\n",
                   ssid_idx,
                   pwildcard_ssid_tlv->ssid,
                   pwildcard_ssid_tlv->max_ssid_length);

            if (ssid_len) {
                ssid_filter = MTRUE;
            }
        }

        /*
         *  The default number of channels sent in the command is low to
         *    ensure the response buffer from the firmware does not truncate
         *    scan results.  That is not an issue with an SSID or BSSID
         *    filter applied to the scan results in the firmware.
         */
        if ((ssid_idx && ssid_filter) ||
            memcmp(pmadapter, pscan_cfg_out->specific_bssid, &zero_mac,
                   sizeof(zero_mac))) {
            *pfiltered_scan = MTRUE;
        }

    } else {
        pscan_cfg_out->bss_mode = (t_u8) pmadapter->scan_mode;
        num_probes = pmadapter->scan_probes;
    }

    /*
     *  If a specific BSSID or SSID is used, the number of channels in the
     *  scan command will be increased to the absolute maximum.
     */
    if (*pfiltered_scan)
        *pmax_chan_per_scan = MRVDRV_MAX_CHANNELS_PER_SPECIFIC_SCAN;
    else
        *pmax_chan_per_scan = MRVDRV_CHANNELS_PER_SCAN_CMD;

    /* If the input config or adapter has the number of Probes set, add tlv */
    if (num_probes) {

        PRINTM(MINFO, "Scan: num_probes = %d\n", num_probes);

        pnum_probes_tlv = (MrvlIEtypes_NumProbes_t *) ptlv_pos;
        pnum_probes_tlv->header.type = wlan_cpu_to_le16(TLV_TYPE_NUMPROBES);
        pnum_probes_tlv->header.len = sizeof(pnum_probes_tlv->num_probes);
        pnum_probes_tlv->num_probes = wlan_cpu_to_le16((t_u16) num_probes);

        ptlv_pos +=
            sizeof(pnum_probes_tlv->header) + pnum_probes_tlv->header.len;

        pnum_probes_tlv->header.len =
            wlan_cpu_to_le16(pnum_probes_tlv->header.len);
    }

    /* Append rates tlv */
    memset(pmadapter, rates, 0, sizeof(rates));

    rates_size = wlan_get_supported_rates(pmpriv, pmpriv->bss_mode,
                                          (pmpriv->bss_mode ==
                                           MLAN_BSS_MODE_INFRA) ? pmadapter->
                                          config_bands : pmadapter->
                                          adhoc_start_band, rates);

    prates_tlv = (MrvlIEtypes_RatesParamSet_t *) ptlv_pos;
    prates_tlv->header.type = wlan_cpu_to_le16(TLV_TYPE_RATES);
    prates_tlv->header.len = wlan_cpu_to_le16((t_u16) rates_size);
    memcpy(pmadapter, prates_tlv->rates, rates, rates_size);
    ptlv_pos += sizeof(prates_tlv->header) + rates_size;

    PRINTM(MINFO, "SCAN_CMD: Rates size = %d\n", rates_size);

    if (ISSUPP_11NENABLED(pmpriv->adapter->fw_cap_info)
        && (pmpriv->adapter->config_bands & BAND_GN
            || pmpriv->adapter->config_bands & BAND_AN)) {
        pht_cap = (MrvlIETypes_HTCap_t *) ptlv_pos;
        memset(pmadapter, pht_cap, 0, sizeof(MrvlIETypes_HTCap_t));
        pht_cap->header.type = wlan_cpu_to_le16(HT_CAPABILITY);
        pht_cap->header.len = sizeof(HTCap_t);
        wlan_fill_ht_cap_tlv(pmpriv, pht_cap, pmpriv->adapter->config_bands);
        HEXDUMP("SCAN: HT_CAPABILITIES IE", (t_u8 *) pht_cap,
                sizeof(MrvlIETypes_HTCap_t));
        ptlv_pos += sizeof(MrvlIETypes_HTCap_t);
        pht_cap->header.len = wlan_cpu_to_le16(pht_cap->header.len);
    }

    wlan_add_wps_probe_request_ie(pmpriv, &ptlv_pos);

    /*
     * Set the output for the channel TLV to the address in the tlv buffer
     *   past any TLVs that were added in this function (SSID, num_probes).
     *   Channel TLVs will be added past this for each scan command, preserving
     *   the TLVs that were previously added.
     */
    *ppchan_list_out = (MrvlIEtypes_ChanListParamSet_t *) ptlv_pos;

    if (puser_scan_in && puser_scan_in->chan_list[0].chan_number) {

        PRINTM(MINFO, "Scan: Using supplied channel list\n");

        for (chan_idx = 0;
             chan_idx < WLAN_USER_SCAN_CHAN_MAX
             && puser_scan_in->chan_list[chan_idx].chan_number; chan_idx++) {

            channel = puser_scan_in->chan_list[chan_idx].chan_number;
            (pscan_chan_list + chan_idx)->chan_number = channel;

            radio_type = puser_scan_in->chan_list[chan_idx].radio_type;
            (pscan_chan_list + chan_idx)->radio_type = radio_type;

            scan_type = puser_scan_in->chan_list[chan_idx].scan_type;
            if (scan_type == MLAN_SCAN_TYPE_UNCHANGED)
                scan_type = pmadapter->scan_type;

            if (radio_type == HostCmd_SCAN_RADIO_TYPE_A) {
                if (pmadapter->fw_bands & BAND_A)
                    PRINTM(MINFO, "UserScan request for A Band channel %d!!\n",
                           channel);
                else {
                    PRINTM(MERROR, "Scan in A band is not allowed!!\n");
                    ret = MLAN_STATUS_FAILURE;
                    LEAVE();
                    return ret;

                }
            }

            /* Prevent active scanning on a radar controlled channel */
            if (radio_type == HostCmd_SCAN_RADIO_TYPE_A) {
                if (wlan_11h_radar_detect_required(pmpriv, channel)) {
                    scan_type = MLAN_SCAN_TYPE_PASSIVE;
                }
            }
            if (scan_type == MLAN_SCAN_TYPE_PASSIVE) {
                (pscan_chan_list + chan_idx)->chan_scan_mode.passive_scan =
                    MTRUE;
            } else {
                (pscan_chan_list + chan_idx)->chan_scan_mode.passive_scan =
                    MFALSE;
            }

            if (puser_scan_in->chan_list[chan_idx].scan_time) {
                scan_dur = (t_u16) puser_scan_in->chan_list[chan_idx].scan_time;
            } else {
                if (scan_type == MLAN_SCAN_TYPE_PASSIVE) {
                    scan_dur = pmadapter->passive_scan_time;
                } else if (*pfiltered_scan) {
                    scan_dur = pmadapter->specific_scan_time;
                } else {
                    scan_dur = pmadapter->active_scan_time;
                }
            }

            (pscan_chan_list + chan_idx)->min_scan_time =
                wlan_cpu_to_le16(scan_dur);
            (pscan_chan_list + chan_idx)->max_scan_time =
                wlan_cpu_to_le16(scan_dur);
        }

        /* Check if we are only scanning the current channel */
        if ((chan_idx == 1)
            && (puser_scan_in->chan_list[0].chan_number
                == pmpriv->curr_bss_params.bss_descriptor.channel)) {
            *pscan_current_only = MTRUE;
            PRINTM(MINFO, "Scan: Scanning current channel only\n");
        }

    } else {
        PRINTM(MINFO, "Scan: Creating full region channel list\n");
        wlan_scan_create_channel_list(pmpriv, puser_scan_in, pscan_chan_list,
                                      *pfiltered_scan);
    }

    LEAVE();
    return ret;
}

/**
 *  @brief Inspect the scan response buffer for pointers to expected TLVs
 *
 *  TLVs can be included at the end of the scan response BSS information.
 *    Parse the data in the buffer for pointers to TLVs that can potentially
 *    be passed back in the response
 *
 *  @param pmadapter        Pointer to the mlan_adapter structure
 *  @param ptlv             Pointer to the start of the TLV buffer to parse
 *  @param tlv_buf_size     Size of the TLV buffer
 *  @param req_tlv_type     Request TLV's type
 *  @param pptlv            Output parameter: Pointer to the request TLV if found
 *
 *  @return                 N/A
 */
static t_void
wlan_ret_802_11_scan_get_tlv_ptrs(IN pmlan_adapter pmadapter,
                                  IN MrvlIEtypes_Data_t * ptlv,
                                  IN t_u32 tlv_buf_size,
                                  IN t_u32 req_tlv_type,
                                  OUT MrvlIEtypes_Data_t ** pptlv)
{
    MrvlIEtypes_Data_t *pcurrent_tlv;
    t_u32 tlv_buf_left;
    t_u32 tlv_type;
    t_u32 tlv_len;

    ENTER();

    pcurrent_tlv = ptlv;
    tlv_buf_left = tlv_buf_size;
    *pptlv = MNULL;

    PRINTM(MINFO, "SCAN_RESP: tlv_buf_size = %d\n", tlv_buf_size);

    while (tlv_buf_left >= sizeof(MrvlIEtypesHeader_t)) {

        tlv_type = wlan_le16_to_cpu(pcurrent_tlv->header.type);
        tlv_len = wlan_le16_to_cpu(pcurrent_tlv->header.len);

        if (sizeof(ptlv->header) + tlv_len > tlv_buf_left) {
            PRINTM(MERROR, "SCAN_RESP: TLV buffer corrupt\n");
            break;
        }

        if (req_tlv_type == tlv_type) {
            switch (tlv_type) {
            case TLV_TYPE_TSFTIMESTAMP:
                PRINTM(MINFO, "SCAN_RESP: TSF Timestamp TLV, len = %d\n",
                       tlv_len);
                *pptlv = (MrvlIEtypes_Data_t *) pcurrent_tlv;
                break;
            case TLV_TYPE_CHANNELBANDLIST:
                PRINTM(MINFO, "SCAN_RESP: CHANNEL BAND LIST TLV, len = %d\n",
                       tlv_len);
                *pptlv = (MrvlIEtypes_Data_t *) pcurrent_tlv;
                break;
            default:
                PRINTM(MERROR, "SCAN_RESP: Unhandled TLV = %d\n", tlv_type);
                /* Give up, this seems corrupted */
                LEAVE();
                return;
            }
        }

        if (*pptlv) {
            // HEXDUMP("SCAN_RESP: TLV Buf", (t_u8 *)*pptlv+4, tlv_len);
            break;
        }

        tlv_buf_left -= (sizeof(ptlv->header) + tlv_len);
        pcurrent_tlv = (MrvlIEtypes_Data_t *) (pcurrent_tlv->data + tlv_len);

    }                           /* while */

    LEAVE();
}

/**
 *  @brief Interpret a BSS scan response returned from the firmware
 *
 *  Parse the various fixed fields and IEs passed back for a BSS probe
 *   response or beacon from the scan command.  Record information as needed
 *   in the scan table BSSDescriptor_t for that entry.
 *
 *  @param pmadapter    A pointer to mlan_adapter structure
 *  @param pbss_entry   Output parameter: Pointer to the BSS Entry
 *  @param pbeacon_info Pointer to the Beacon information
 *  @param bytes_left   Number of bytes left to parse
 *
 *  @return             MLAN_STATUS_SUCCESS or MLAN_STATUS_FAILURE
 */
static mlan_status
wlan_interpret_bss_desc_with_ie(IN pmlan_adapter pmadapter,
                                OUT BSSDescriptor_t * pbss_entry,
                                IN t_u8 ** pbeacon_info, IN t_u32 * bytes_left)
{
    mlan_status ret = MLAN_STATUS_SUCCESS;
    IEEEtypes_ElementId_e element_id;
    IEEEtypes_FhParamSet_t *pfh_param_set;
    IEEEtypes_DsParamSet_t *pds_param_set;
    IEEEtypes_CfParamSet_t *pcf_param_set;
    IEEEtypes_IbssParamSet_t *pibss_param_set;
    IEEEtypes_CapInfo_t *pcap_info;
    WLAN_802_11_FIXED_IEs fixed_ie;
    t_u8 *pcurrent_ptr;
    t_u8 *prate;
    t_u8 element_len;
    t_u16 total_ie_len;
    t_u8 bytes_to_copy;
    t_u8 rate_size;
    t_u16 beacon_size;
    t_u8 found_data_rate_ie;
    t_u32 bytes_left_for_current_beacon;
    IEEEtypes_ERPInfo_t *perp_info;

    IEEEtypes_VendorSpecific_t *pvendor_ie;
    const t_u8 wpa_oui[4] = { 0x00, 0x50, 0xf2, 0x01 };
    const t_u8 wmm_oui[4] = { 0x00, 0x50, 0xf2, 0x02 };

    IEEEtypes_CountryInfoSet_t *pcountry_info;

    ENTER();

    found_data_rate_ie = MFALSE;
    rate_size = 0;
    beacon_size = 0;

    if (*bytes_left >= sizeof(beacon_size)) {
        /* Extract & convert beacon size from the command buffer */
        memcpy(pmadapter, &beacon_size, *pbeacon_info, sizeof(beacon_size));
        beacon_size = wlan_le16_to_cpu(beacon_size);
        *bytes_left -= sizeof(beacon_size);
        *pbeacon_info += sizeof(beacon_size);
    }

    if (!beacon_size || beacon_size > *bytes_left) {

        *pbeacon_info += *bytes_left;
        *bytes_left = 0;

        LEAVE();
        return MLAN_STATUS_FAILURE;
    }

    /* Initialize the current working beacon pointer for this BSS iteration */
    pcurrent_ptr = *pbeacon_info;

    /* Advance the return beacon pointer past the current beacon */
    *pbeacon_info += beacon_size;
    *bytes_left -= beacon_size;

    bytes_left_for_current_beacon = beacon_size;

    if (bytes_left_for_current_beacon <
        (MLAN_MAC_ADDR_LENGTH + sizeof(t_u8) + sizeof(WLAN_802_11_FIXED_IEs))) {
        PRINTM(MERROR, "InterpretIE: Not enough bytes left\n");
        LEAVE();
        return MLAN_STATUS_FAILURE;
    }

    memcpy(pmadapter, pbss_entry->mac_address, pcurrent_ptr,
           MLAN_MAC_ADDR_LENGTH);
    PRINTM(MINFO, "InterpretIE: AP MAC Addr-%02x:%02x:%02x:%02x:%02x:%02x\n",
           pbss_entry->mac_address[0], pbss_entry->mac_address[1],
           pbss_entry->mac_address[2], pbss_entry->mac_address[3],
           pbss_entry->mac_address[4], pbss_entry->mac_address[5]);

    pcurrent_ptr += MLAN_MAC_ADDR_LENGTH;
    bytes_left_for_current_beacon -= MLAN_MAC_ADDR_LENGTH;

    /*
     * Next 4 fields are RSSI (for legacy scan only), time stamp,
     *   beacon interval, and capability information
     */
    if (!pmadapter->ext_scan) {
        /* RSSI is 1 byte long */
        pbss_entry->rssi = (t_s32) (*pcurrent_ptr);
        PRINTM(MINFO, "InterpretIE: RSSI=%02X\n", *pcurrent_ptr);
        pcurrent_ptr += 1;
        bytes_left_for_current_beacon -= 1;
    }

    /*
     *  The RSSI is not part of the beacon/probe response.  After we have
     *    advanced pcurrent_ptr past the RSSI field, save the remaining
     *    data for use at the application layer
     */
    pbss_entry->pbeacon_buf = pcurrent_ptr;
    pbss_entry->beacon_buf_size = bytes_left_for_current_beacon;

    /* Time stamp is 8 bytes long */
    memcpy(pmadapter, fixed_ie.time_stamp, pcurrent_ptr, 8);
    memcpy(pmadapter, pbss_entry->time_stamp, pcurrent_ptr, 8);
    pcurrent_ptr += 8;
    bytes_left_for_current_beacon -= 8;

    /* Beacon interval is 2 bytes long */
    memcpy(pmadapter, &fixed_ie.beacon_interval, pcurrent_ptr, 2);
    pbss_entry->beacon_period = wlan_le16_to_cpu(fixed_ie.beacon_interval);
    pcurrent_ptr += 2;
    bytes_left_for_current_beacon -= 2;

    /* Capability information is 2 bytes long */
    memcpy(pmadapter, &fixed_ie.capabilities, pcurrent_ptr, 2);
    PRINTM(MINFO, "InterpretIE: fixed_ie.capabilities=0x%X\n",
           fixed_ie.capabilities);
    fixed_ie.capabilities = wlan_le16_to_cpu(fixed_ie.capabilities);
    pcap_info = (IEEEtypes_CapInfo_t *) & fixed_ie.capabilities;
    memcpy(pmadapter, &pbss_entry->cap_info, pcap_info,
           sizeof(IEEEtypes_CapInfo_t));
    pcurrent_ptr += 2;
    bytes_left_for_current_beacon -= 2;

    /* Rest of the current buffer are IE's */
    PRINTM(MINFO, "InterpretIE: IELength for this AP = %d\n",
           bytes_left_for_current_beacon);

    HEXDUMP("InterpretIE: IE info", (t_u8 *) pcurrent_ptr,
            bytes_left_for_current_beacon);

    if (pcap_info->privacy) {
        PRINTM(MINFO, "InterpretIE: AP WEP enabled\n");
        pbss_entry->privacy = Wlan802_11PrivFilter8021xWEP;
    } else {
        pbss_entry->privacy = Wlan802_11PrivFilterAcceptAll;
    }

    if (pcap_info->ibss == 1) {
        pbss_entry->bss_mode = MLAN_BSS_MODE_IBSS;
    } else {
        pbss_entry->bss_mode = MLAN_BSS_MODE_INFRA;
    }

    if (pcap_info->spectrum_mgmt == 1) {
        PRINTM(MINFO, "InterpretIE: 11h- Spectrum Management "
               "capability bit found\n");
        pbss_entry->wlan_11h_bss_info.sensed_11h = 1;
    }

    /* Process variable IE */
    while (bytes_left_for_current_beacon >= 2) {
        element_id = (IEEEtypes_ElementId_e) (*((t_u8 *) pcurrent_ptr));
        element_len = *((t_u8 *) pcurrent_ptr + 1);
        total_ie_len = element_len + sizeof(IEEEtypes_Header_t);

        if (bytes_left_for_current_beacon < total_ie_len) {
            PRINTM(MERROR, "InterpretIE: Error in processing IE, "
                   "bytes left < IE length\n");
            bytes_left_for_current_beacon = 0;
            continue;
        }

        switch (element_id) {

        case SSID:
            if (element_len > MRVDRV_MAX_SSID_LENGTH) {
                bytes_left_for_current_beacon = 0;
                continue;
            }
            pbss_entry->ssid.ssid_len = element_len;
            memcpy(pmadapter, pbss_entry->ssid.ssid, (pcurrent_ptr + 2),
                   element_len);
            PRINTM(MINFO, "InterpretIE: ssid: %-32s\n", pbss_entry->ssid.ssid);
            break;

        case SUPPORTED_RATES:
            if (element_len > WLAN_SUPPORTED_RATES) {
                bytes_left_for_current_beacon = 0;
                continue;
            }
            memcpy(pmadapter, pbss_entry->data_rates, pcurrent_ptr + 2,
                   element_len);
            memcpy(pmadapter, pbss_entry->supported_rates, pcurrent_ptr + 2,
                   element_len);
            HEXDUMP("InterpretIE: SupportedRates:", pbss_entry->supported_rates,
                    element_len);
            rate_size = element_len;
            found_data_rate_ie = MTRUE;
            break;

        case FH_PARAM_SET:
            pfh_param_set = (IEEEtypes_FhParamSet_t *) pcurrent_ptr;
            pbss_entry->network_type_use = Wlan802_11FH;
            memcpy(pmadapter, &pbss_entry->phy_param_set.fh_param_set,
                   pfh_param_set, MIN(total_ie_len,
                                      sizeof(IEEEtypes_FhParamSet_t)));
            pbss_entry->phy_param_set.fh_param_set.len =
                MIN(element_len, (sizeof(IEEEtypes_FhParamSet_t)
                                  - sizeof(IEEEtypes_Header_t)));
            pbss_entry->phy_param_set.fh_param_set.dwell_time
                =
                wlan_le16_to_cpu(pbss_entry->phy_param_set.fh_param_set.
                                 dwell_time);
            break;

        case DS_PARAM_SET:
            pds_param_set = (IEEEtypes_DsParamSet_t *) pcurrent_ptr;

            pbss_entry->network_type_use = Wlan802_11DS;
            pbss_entry->channel = pds_param_set->current_chan;

            memcpy(pmadapter, &pbss_entry->phy_param_set.ds_param_set,
                   pds_param_set, MIN(total_ie_len,
                                      sizeof(IEEEtypes_DsParamSet_t)));
            pbss_entry->phy_param_set.ds_param_set.len =
                MIN(element_len, (sizeof(IEEEtypes_DsParamSet_t)
                                  - sizeof(IEEEtypes_Header_t)));
            break;

        case CF_PARAM_SET:
            pcf_param_set = (IEEEtypes_CfParamSet_t *) pcurrent_ptr;
            memcpy(pmadapter, &pbss_entry->ss_param_set.cf_param_set,
                   pcf_param_set, MIN(total_ie_len,
                                      sizeof(IEEEtypes_CfParamSet_t)));
            pbss_entry->ss_param_set.cf_param_set.len =
                MIN(element_len, (sizeof(IEEEtypes_CfParamSet_t)
                                  - sizeof(IEEEtypes_Header_t)));
            break;

        case IBSS_PARAM_SET:
            pibss_param_set = (IEEEtypes_IbssParamSet_t *) pcurrent_ptr;
            pbss_entry->atim_window =
                wlan_le16_to_cpu(pibss_param_set->atim_window);
            memcpy(pmadapter, &pbss_entry->ss_param_set.ibss_param_set,
                   pibss_param_set, MIN(total_ie_len,
                                        sizeof(IEEEtypes_IbssParamSet_t)));
            pbss_entry->ss_param_set.ibss_param_set.len =
                MIN(element_len, (sizeof(IEEEtypes_IbssParamSet_t)
                                  - sizeof(IEEEtypes_Header_t)));
            break;

            /* Handle Country Info IE */
        case COUNTRY_INFO:
            pcountry_info = (IEEEtypes_CountryInfoSet_t *) pcurrent_ptr;

            if (pcountry_info->len < sizeof(pcountry_info->country_code) ||
                (unsigned) (pcountry_info->len + 2) >
                sizeof(IEEEtypes_CountryInfoFullSet_t)) {
                PRINTM(MERROR,
                       "InterpretIE: 11D- Err "
                       "country_info len =%d min=%d max=%d\n",
                       pcountry_info->len, sizeof(pcountry_info->country_code),
                       sizeof(IEEEtypes_CountryInfoFullSet_t));
                LEAVE();
                return MLAN_STATUS_FAILURE;
            }

            memcpy(pmadapter, &pbss_entry->country_info,
                   pcountry_info, pcountry_info->len + 2);
            HEXDUMP("InterpretIE: 11D- country_info:",
                    (t_u8 *) pcountry_info, (t_u32) (pcountry_info->len + 2));
            break;

        case ERP_INFO:
            perp_info = (IEEEtypes_ERPInfo_t *) pcurrent_ptr;
            pbss_entry->erp_flags = perp_info->erp_flags;
            break;

        case POWER_CONSTRAINT:
        case POWER_CAPABILITY:
        case TPC_REPORT:
        case CHANNEL_SWITCH_ANN:
        case QUIET:
        case IBSS_DFS:
        case SUPPORTED_CHANNELS:
        case TPC_REQUEST:
            wlan_11h_process_bss_elem(pmadapter, &pbss_entry->wlan_11h_bss_info,
                                      pcurrent_ptr);
            break;
        case EXTENDED_SUPPORTED_RATES:
            /*
             * Only process extended supported rate
             * if data rate is already found.
             * Data rate IE should come before
             * extended supported rate IE
             */
            if (found_data_rate_ie) {
                if ((element_len + rate_size) > WLAN_SUPPORTED_RATES) {
                    bytes_to_copy = (WLAN_SUPPORTED_RATES - rate_size);
                } else {
                    bytes_to_copy = element_len;
                }

                prate = (t_u8 *) pbss_entry->data_rates;
                prate += rate_size;
                memcpy(pmadapter, prate, pcurrent_ptr + 2, bytes_to_copy);

                prate = (t_u8 *) pbss_entry->supported_rates;
                prate += rate_size;
                memcpy(pmadapter, prate, pcurrent_ptr + 2, bytes_to_copy);
            }
            HEXDUMP("InterpretIE: ExtSupportedRates:",
                    pbss_entry->supported_rates, element_len + rate_size);
            break;

        case VENDOR_SPECIFIC_221:
            pvendor_ie = (IEEEtypes_VendorSpecific_t *) pcurrent_ptr;

            if (!memcmp
                (pmadapter, pvendor_ie->vend_hdr.oui, wpa_oui,
                 sizeof(wpa_oui))) {
                pbss_entry->pwpa_ie =
                    (IEEEtypes_VendorSpecific_t *) pcurrent_ptr;
                pbss_entry->wpa_offset =
                    (t_u16) (pcurrent_ptr - pbss_entry->pbeacon_buf);
                HEXDUMP("InterpretIE: Resp WPA_IE",
                        (t_u8 *) pbss_entry->pwpa_ie,
                        ((*(pbss_entry->pwpa_ie)).vend_hdr.len +
                         sizeof(IEEEtypes_Header_t)));
            } else
                if (!memcmp
                    (pmadapter, pvendor_ie->vend_hdr.oui, wmm_oui,
                     sizeof(wmm_oui))) {
                if (total_ie_len == sizeof(IEEEtypes_WmmParameter_t)
                    || total_ie_len == sizeof(IEEEtypes_WmmInfo_t)) {

                    /*
                     * Only accept and copy the WMM IE if it matches
                     * the size expected for the WMM Info IE or the
                     * WMM Parameter IE.
                     */
                    memcpy(pmadapter, (t_u8 *) & pbss_entry->wmm_ie,
                           pcurrent_ptr, total_ie_len);
                    HEXDUMP("InterpretIE: Resp WMM_IE",
                            (t_u8 *) & pbss_entry->wmm_ie, total_ie_len);
                }
            }
            break;
        case RSN_IE:
            pbss_entry->prsn_ie = (IEEEtypes_Generic_t *) pcurrent_ptr;
            pbss_entry->rsn_offset =
                (t_u16) (pcurrent_ptr - pbss_entry->pbeacon_buf);
            HEXDUMP("InterpretIE: Resp RSN_IE", (t_u8 *) pbss_entry->prsn_ie,
                    (*(pbss_entry->prsn_ie)).ieee_hdr.len +
                    sizeof(IEEEtypes_Header_t));
            break;
        case WAPI_IE:
            pbss_entry->pwapi_ie = (IEEEtypes_Generic_t *) pcurrent_ptr;
            pbss_entry->wapi_offset =
                (t_u16) (pcurrent_ptr - pbss_entry->pbeacon_buf);
            HEXDUMP("InterpretIE: Resp WAPI_IE", (t_u8 *) pbss_entry->pwapi_ie,
                    (*(pbss_entry->pwapi_ie)).ieee_hdr.len +
                    sizeof(IEEEtypes_Header_t));
            break;
        case HT_CAPABILITY:
            pbss_entry->pht_cap = (IEEEtypes_HTCap_t *) pcurrent_ptr;
            pbss_entry->ht_cap_offset =
                (t_u16) (pcurrent_ptr - pbss_entry->pbeacon_buf);
            HEXDUMP("InterpretIE: Resp HTCAP_IE", (t_u8 *) pbss_entry->pht_cap,
                    (*(pbss_entry->pht_cap)).ieee_hdr.len +
                    sizeof(IEEEtypes_Header_t));
            break;
        case HT_OPERATION:
            pbss_entry->pht_info = (IEEEtypes_HTInfo_t *) pcurrent_ptr;
            pbss_entry->ht_info_offset =
                (t_u16) (pcurrent_ptr - pbss_entry->pbeacon_buf);
            HEXDUMP("InterpretIE: Resp HTINFO_IE",
                    (t_u8 *) pbss_entry->pht_info,
                    (*(pbss_entry->pht_info)).ieee_hdr.len +
                    sizeof(IEEEtypes_Header_t));
            break;
        case BSSCO_2040:
            pbss_entry->pbss_co_2040 = (IEEEtypes_2040BSSCo_t *) pcurrent_ptr;
            pbss_entry->bss_co_2040_offset =
                (t_u16) (pcurrent_ptr - pbss_entry->pbeacon_buf);
            HEXDUMP("InterpretIE: Resp 2040BSSCOEXISTANCE_IE",
                    (t_u8 *) pbss_entry->pbss_co_2040,
                    (*(pbss_entry->pbss_co_2040)).ieee_hdr.len +
                    sizeof(IEEEtypes_Header_t));
            break;
        case EXT_CAPABILITY:
            pbss_entry->pext_cap = (IEEEtypes_ExtCap_t *) pcurrent_ptr;
            pbss_entry->ext_cap_offset =
                (t_u16) (pcurrent_ptr - pbss_entry->pbeacon_buf);
            HEXDUMP("InterpretIE: Resp EXTCAP_IE",
                    (t_u8 *) pbss_entry->pext_cap,
                    (*(pbss_entry->pext_cap)).ieee_hdr.len +
                    sizeof(IEEEtypes_Header_t));
            break;
        case OVERLAPBSSSCANPARAM:
            pbss_entry->poverlap_bss_scan_param =
                (IEEEtypes_OverlapBSSScanParam_t *) pcurrent_ptr;
            pbss_entry->overlap_bss_offset =
                (t_u16) (pcurrent_ptr - pbss_entry->pbeacon_buf);
            HEXDUMP("InterpretIE: Resp HTCAP_IE",
                    (t_u8 *) pbss_entry->poverlap_bss_scan_param,
                    (*(pbss_entry->poverlap_bss_scan_param)).ieee_hdr.len +
                    sizeof(IEEEtypes_Header_t));
            break;
        }

        pcurrent_ptr += element_len + 2;

        /* Need to account for IE ID and IE Len */
        bytes_left_for_current_beacon -= (element_len + 2);

    }                           /* while (bytes_left_for_current_beacon > 2) */

    LEAVE();
    return ret;
}

/**
 *  @brief Adjust ie's position in BSSDescriptor_t
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param pbss_entry   A pointer to BSSDescriptor_t structure
 *
 *  @return           N/A
 */
static t_void
wlan_adjust_ie_in_bss_entry(IN mlan_private * pmpriv,
                            IN BSSDescriptor_t * pbss_entry)
{
    ENTER();
    if (pbss_entry->pbeacon_buf) {
        if (pbss_entry->pwpa_ie) {
            pbss_entry->pwpa_ie = (IEEEtypes_VendorSpecific_t *)
                (pbss_entry->pbeacon_buf + pbss_entry->wpa_offset);
        }
        if (pbss_entry->prsn_ie) {
            pbss_entry->prsn_ie = (IEEEtypes_Generic_t *)
                (pbss_entry->pbeacon_buf + pbss_entry->rsn_offset);
        }
        if (pbss_entry->pwapi_ie) {
            pbss_entry->pwapi_ie = (IEEEtypes_Generic_t *)
                (pbss_entry->pbeacon_buf + pbss_entry->wapi_offset);
        }
        if (pbss_entry->pht_cap) {
            pbss_entry->pht_cap = (IEEEtypes_HTCap_t *)
                (pbss_entry->pbeacon_buf + pbss_entry->ht_cap_offset);
        }
        if (pbss_entry->pht_info) {
            pbss_entry->pht_info = (IEEEtypes_HTInfo_t *)
                (pbss_entry->pbeacon_buf + pbss_entry->ht_info_offset);
        }
        if (pbss_entry->pbss_co_2040) {
            pbss_entry->pbss_co_2040 = (IEEEtypes_2040BSSCo_t *)
                (pbss_entry->pbeacon_buf + pbss_entry->bss_co_2040_offset);
        }
        if (pbss_entry->pext_cap) {
            pbss_entry->pext_cap = (IEEEtypes_ExtCap_t *)
                (pbss_entry->pbeacon_buf + pbss_entry->ext_cap_offset);
        }
        if (pbss_entry->poverlap_bss_scan_param) {
            pbss_entry->poverlap_bss_scan_param =
                (IEEEtypes_OverlapBSSScanParam_t *)
                (pbss_entry->pbeacon_buf + pbss_entry->overlap_bss_offset);
        }
    } else {
        pbss_entry->pwpa_ie = MNULL;
        pbss_entry->wpa_offset = 0;
        pbss_entry->prsn_ie = MNULL;
        pbss_entry->rsn_offset = 0;
        pbss_entry->pwapi_ie = MNULL;
        pbss_entry->wapi_offset = 0;
        pbss_entry->pht_cap = MNULL;
        pbss_entry->ht_cap_offset = 0;
        pbss_entry->pht_info = MNULL;
        pbss_entry->ht_info_offset = 0;
        pbss_entry->pbss_co_2040 = MNULL;
        pbss_entry->bss_co_2040_offset = 0;
        pbss_entry->pext_cap = MNULL;
        pbss_entry->ext_cap_offset = 0;
        pbss_entry->poverlap_bss_scan_param = MNULL;
        pbss_entry->overlap_bss_offset = 0;
    }
    LEAVE();
    return;
}

/**
 *  @brief Store a beacon or probe response for a BSS returned in the scan
 *
 *  Store a new scan response or an update for a previous scan response.  New
 *    entries need to verify that they do not exceed the total amount of
 *    memory allocated for the table.

 *  Replacement entries need to take into consideration the amount of space
 *    currently allocated for the beacon/probe response and adjust the entry
 *    as needed.
 *
 *  A small amount of extra pad (SCAN_BEACON_ENTRY_PAD) is generally reserved
 *    for an entry in case it is a beacon since a probe response for the
 *    network will by larger per the standard.  This helps to reduce the
 *    amount of memory copying to fit a new probe response into an entry
 *    already occupied by a network's previously stored beacon.
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param beacon_idx   Index in the scan table to store this entry; may be
 *                      replacing an older duplicate entry for this BSS
 *  @param num_of_ent   Number of entries currently in the table
 *  @param pnew_beacon  Pointer to the new beacon/probe response to save
 *
 *  @return           N/A
 */
static t_void
wlan_ret_802_11_scan_store_beacon(IN mlan_private * pmpriv,
                                  IN t_u32 beacon_idx,
                                  IN t_u32 num_of_ent,
                                  IN BSSDescriptor_t * pnew_beacon)
{
    mlan_adapter *pmadapter = pmpriv->adapter;
    t_u8 *pbcn_store;
    t_u32 new_bcn_size;
    t_u32 old_bcn_size;
    t_u32 bcn_space;
    t_u32 adj_idx;
    mlan_status ret = MLAN_STATUS_SUCCESS;
    t_u8 *tmp_buf;
    t_u16 bcn_size = 0;
    t_u32 bcn_offset = 0;

    ENTER();

    if (pmadapter->pscan_table[beacon_idx].pbeacon_buf) {

        new_bcn_size = pnew_beacon->beacon_buf_size;
        old_bcn_size = pmadapter->pscan_table[beacon_idx].beacon_buf_size;
        bcn_space = pmadapter->pscan_table[beacon_idx].beacon_buf_size_max;
        pbcn_store = pmadapter->pscan_table[beacon_idx].pbeacon_buf;

        /* Set the max to be the same as current entry unless changed below */
        pnew_beacon->beacon_buf_size_max = bcn_space;

        if (new_bcn_size == old_bcn_size) {
            /*
             * Beacon is the same size as the previous entry.
             *   Replace the previous contents with the scan result
             */
            memcpy(pmadapter, pbcn_store,
                   pnew_beacon->pbeacon_buf, pnew_beacon->beacon_buf_size);

        } else if (new_bcn_size <= bcn_space) {
            /*
             * New beacon size will fit in the amount of space
             *   we have previously allocated for it
             */

            /* Copy the new beacon buffer entry over the old one */
            memcpy(pmadapter, pbcn_store, pnew_beacon->pbeacon_buf,
                   new_bcn_size);

            /*
             *  If the old beacon size was less than the maximum
             *  we had allotted for the entry, and the new entry
             *  is even smaller, reset the max size to the old beacon
             *  entry and compress the storage space (leaving a new
             *  pad space of (old_bcn_size - new_bcn_size).
             */
            if (old_bcn_size < bcn_space && new_bcn_size <= old_bcn_size) {
                /*
                 * Old Beacon size is smaller than the allotted storage size.
                 *   Shrink the allotted storage space.
                 */
                PRINTM(MINFO, "AppControl: Smaller Duplicate Beacon (%d), "
                       "old = %d, new = %d, space = %d, left = %d\n",
                       beacon_idx, old_bcn_size, new_bcn_size, bcn_space,
                       (pmadapter->bcn_buf_size -
                        (pmadapter->pbcn_buf_end - pmadapter->bcn_buf)));

                /*
                 *  memmove (since the memory overlaps) the data
                 *  after the beacon we just stored to the end of
                 *  the current beacon.  This cleans up any unused
                 *  space the old larger beacon was using in the buffer
                 */
                memmove(pmadapter,
                        (void *) ((t_ptr) pbcn_store + (t_ptr) old_bcn_size),
                        (void *) ((t_ptr) pbcn_store + (t_ptr) bcn_space),
                        (t_u32) ((t_ptr) pmadapter->pbcn_buf_end -
                                 ((t_ptr) pbcn_store + (t_ptr) bcn_space)));

                /*
                 * Decrement the end pointer by the difference between
                 *  the old larger size and the new smaller size since
                 *  we are using less space due to the new beacon being
                 *  smaller
                 */
                pmadapter->pbcn_buf_end -= (bcn_space - old_bcn_size);

                /* Set the maximum storage size to the old beacon size */
                pnew_beacon->beacon_buf_size_max = old_bcn_size;

                /* Adjust beacon buffer pointers that are past the current */
                for (adj_idx = 0; adj_idx < num_of_ent; adj_idx++) {
                    if (pmadapter->pscan_table[adj_idx].pbeacon_buf >
                        pbcn_store) {
                        pmadapter->pscan_table[adj_idx].pbeacon_buf -=
                            (bcn_space - old_bcn_size);
                        wlan_adjust_ie_in_bss_entry(pmpriv,
                                                    &pmadapter->
                                                    pscan_table[adj_idx]);
                    }
                }
            }
        } else if (pmadapter->pbcn_buf_end + (new_bcn_size - bcn_space)
                   < (pmadapter->bcn_buf + pmadapter->bcn_buf_size)) {
            /*
             * Beacon is larger than space previously allocated (bcn_space)
             *   and there is enough space left in the beaconBuffer to store
             *   the additional data
             */
            PRINTM(MINFO, "AppControl: Larger Duplicate Beacon (%d), "
                   "old = %d, new = %d, space = %d, left = %d\n",
                   beacon_idx, old_bcn_size, new_bcn_size, bcn_space,
                   (pmadapter->bcn_buf_size -
                    (pmadapter->pbcn_buf_end - pmadapter->bcn_buf)));

            /*
             * memmove (since the memory overlaps) the data
             *  after the beacon we just stored to the end of
             *  the current beacon.  This moves the data for
             *  the beacons after this further in memory to
             *  make space for the new larger beacon we are
             *  about to copy in.
             */
            memmove(pmadapter,
                    (void *) ((t_ptr) pbcn_store + (t_ptr) new_bcn_size),
                    (void *) ((t_ptr) pbcn_store + (t_ptr) bcn_space),
                    (t_u32) ((t_ptr) pmadapter->pbcn_buf_end -
                             ((t_ptr) pbcn_store + (t_ptr) bcn_space)));

            /* Copy the new beacon buffer entry over the old one */
            memcpy(pmadapter, pbcn_store, pnew_beacon->pbeacon_buf,
                   new_bcn_size);

            /* Move the beacon end pointer by the amount of new beacon data we
               are adding */
            pmadapter->pbcn_buf_end += (new_bcn_size - bcn_space);

            /*
             * This entry is bigger than the allotted max space
             *  previously reserved.  Increase the max space to
             *  be equal to the new beacon size
             */
            pnew_beacon->beacon_buf_size_max = new_bcn_size;

            /* Adjust beacon buffer pointers that are past the current */
            for (adj_idx = 0; adj_idx < num_of_ent; adj_idx++) {
                if (pmadapter->pscan_table[adj_idx].pbeacon_buf > pbcn_store) {
                    pmadapter->pscan_table[adj_idx].pbeacon_buf
                        += (new_bcn_size - bcn_space);
                    wlan_adjust_ie_in_bss_entry(pmpriv,
                                                &pmadapter->
                                                pscan_table[adj_idx]);
                }
            }
        } else {
            /*
             * Beacon is larger than the previously allocated space, but
             *   there is not enough free space to store the additional data
             */
            PRINTM(MERROR,
                   "AppControl: Failed: Larger Duplicate Beacon (%d),"
                   " old = %d, new = %d, space = %d, left = %d\n",
                   beacon_idx, old_bcn_size, new_bcn_size, bcn_space,
                   (pmadapter->bcn_buf_size -
                    (pmadapter->pbcn_buf_end - pmadapter->bcn_buf)));

            /* Storage failure, keep old beacon intact */
            pnew_beacon->beacon_buf_size = old_bcn_size;
            if (pnew_beacon->pwpa_ie)
                pnew_beacon->wpa_offset =
                    pmadapter->pscan_table[beacon_idx].wpa_offset;
            if (pnew_beacon->prsn_ie)
                pnew_beacon->rsn_offset =
                    pmadapter->pscan_table[beacon_idx].rsn_offset;
            if (pnew_beacon->pwapi_ie)
                pnew_beacon->wapi_offset =
                    pmadapter->pscan_table[beacon_idx].wapi_offset;
            if (pnew_beacon->pht_cap)
                pnew_beacon->ht_cap_offset =
                    pmadapter->pscan_table[beacon_idx].ht_cap_offset;
            if (pnew_beacon->pht_info)
                pnew_beacon->ht_info_offset =
                    pmadapter->pscan_table[beacon_idx].ht_info_offset;
            if (pnew_beacon->pbss_co_2040)
                pnew_beacon->bss_co_2040_offset =
                    pmadapter->pscan_table[beacon_idx].bss_co_2040_offset;
            if (pnew_beacon->pext_cap)
                pnew_beacon->ext_cap_offset =
                    pmadapter->pscan_table[beacon_idx].ext_cap_offset;
            if (pnew_beacon->poverlap_bss_scan_param)
                pnew_beacon->overlap_bss_offset =
                    pmadapter->pscan_table[beacon_idx].overlap_bss_offset;
        }
        /* Point the new entry to its permanent storage space */
        pnew_beacon->pbeacon_buf = pbcn_store;
        wlan_adjust_ie_in_bss_entry(pmpriv, pnew_beacon);
    } else {
        if ((pmadapter->pbcn_buf_end + pnew_beacon->beacon_buf_size +
             SCAN_BEACON_ENTRY_PAD > (pmadapter->bcn_buf +
                                      pmadapter->bcn_buf_size)) &&
            (pmadapter->bcn_buf_size < MAX_SCAN_BEACON_BUFFER)) {
            /* no space for this entry, realloc bcn buffer */
            ret = pmadapter->callbacks.moal_malloc(pmadapter->pmoal_handle,
                                                   pmadapter->bcn_buf_size +
                                                   DEFAULT_SCAN_BEACON_BUFFER,
                                                   MLAN_MEM_DEF,
                                                   (t_u8 **) & tmp_buf);
            if ((ret == MLAN_STATUS_SUCCESS) && (tmp_buf)) {
                PRINTM(MCMND,
                       "Realloc Beacon buffer, old size=%d, new_size=%d\n",
                       pmadapter->bcn_buf_size,
                       pmadapter->bcn_buf_size + DEFAULT_SCAN_BEACON_BUFFER);
                bcn_size = pmadapter->pbcn_buf_end - pmadapter->bcn_buf;
                memcpy(pmadapter, tmp_buf, pmadapter->bcn_buf, bcn_size);
                /* Adjust beacon buffer pointers that are past the current */
                for (adj_idx = 0; adj_idx < num_of_ent; adj_idx++) {
                    bcn_offset =
                        pmadapter->pscan_table[adj_idx].pbeacon_buf -
                        pmadapter->bcn_buf;
                    pmadapter->pscan_table[adj_idx].pbeacon_buf =
                        tmp_buf + bcn_offset;
                    wlan_adjust_ie_in_bss_entry(pmpriv,
                                                &pmadapter->
                                                pscan_table[adj_idx]);
                }
                pmadapter->pbcn_buf_end = tmp_buf + bcn_size;
                pmadapter->callbacks.moal_mfree(pmadapter->pmoal_handle,
                                                (t_u8 *) pmadapter->bcn_buf);
                pmadapter->bcn_buf = tmp_buf;
                pmadapter->bcn_buf_size += DEFAULT_SCAN_BEACON_BUFFER;
            }
        }
        /*
         * No existing beacon data exists for this entry, check to see
         *   if we can fit it in the remaining space
         */
        if (pmadapter->pbcn_buf_end + pnew_beacon->beacon_buf_size +
            SCAN_BEACON_ENTRY_PAD < (pmadapter->bcn_buf +
                                     pmadapter->bcn_buf_size)) {

            /*
             * Copy the beacon buffer data from the local entry to the
             *   adapter dev struct buffer space used to store the raw
             *   beacon data for each entry in the scan table
             */
            memcpy(pmadapter, pmadapter->pbcn_buf_end, pnew_beacon->pbeacon_buf,
                   pnew_beacon->beacon_buf_size);

            /* Update the beacon ptr to point to the table save area */
            pnew_beacon->pbeacon_buf = pmadapter->pbcn_buf_end;
            pnew_beacon->beacon_buf_size_max = (pnew_beacon->beacon_buf_size
                                                + SCAN_BEACON_ENTRY_PAD);
            wlan_adjust_ie_in_bss_entry(pmpriv, pnew_beacon);

            /* Increment the end pointer by the size reserved */
            pmadapter->pbcn_buf_end += pnew_beacon->beacon_buf_size_max;

            PRINTM(MINFO, "AppControl: Beacon[%02d] sz=%03d,"
                   " used = %04d, left = %04d\n",
                   beacon_idx,
                   pnew_beacon->beacon_buf_size,
                   (pmadapter->pbcn_buf_end - pmadapter->bcn_buf),
                   (pmadapter->bcn_buf_size -
                    (pmadapter->pbcn_buf_end - pmadapter->bcn_buf)));
        } else {
            /*
             * No space for new beacon
             */
            PRINTM(MCMND, "AppControl: No space beacon (%d): "
                   "%02x:%02x:%02x:%02x:%02x:%02x; sz=%03d, left=%03d\n",
                   beacon_idx,
                   pnew_beacon->mac_address[0], pnew_beacon->mac_address[1],
                   pnew_beacon->mac_address[2], pnew_beacon->mac_address[3],
                   pnew_beacon->mac_address[4], pnew_beacon->mac_address[5],
                   pnew_beacon->beacon_buf_size,
                   (pmadapter->bcn_buf_size -
                    (pmadapter->pbcn_buf_end - pmadapter->bcn_buf)));

            /* Storage failure; clear storage records for this bcn */
            pnew_beacon->pbeacon_buf = MNULL;
            pnew_beacon->beacon_buf_size = 0;
            pnew_beacon->beacon_buf_size_max = 0;
            wlan_adjust_ie_in_bss_entry(pmpriv, pnew_beacon);
        }
    }

    LEAVE();
}

/**
 *  @brief Restore a beacon buffer of the current bss descriptor
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *
 *  @return             N/A
 */
static t_void
wlan_restore_curr_bcn(IN mlan_private * pmpriv)
{
    mlan_adapter *pmadapter = pmpriv->adapter;
    mlan_callbacks *pcb = (pmlan_callbacks) & pmadapter->callbacks;
    BSSDescriptor_t *pcurr_bss = &pmpriv->curr_bss_params.bss_descriptor;

    ENTER();

    if (pmpriv->pcurr_bcn_buf &&
        ((pmadapter->pbcn_buf_end + pmpriv->curr_bcn_size) <
         (pmadapter->bcn_buf + pmadapter->bcn_buf_size))) {

        pcb->moal_spin_lock(pmadapter->pmoal_handle, pmpriv->curr_bcn_buf_lock);

        /* restore the current beacon buffer */
        memcpy(pmadapter, pmadapter->pbcn_buf_end, pmpriv->pcurr_bcn_buf,
               pmpriv->curr_bcn_size);
        pcurr_bss->pbeacon_buf = pmadapter->pbcn_buf_end;
        pcurr_bss->beacon_buf_size = pmpriv->curr_bcn_size;
        pmadapter->pbcn_buf_end += pmpriv->curr_bcn_size;

        /* adjust the pointers in the current bss descriptor */
        if (pcurr_bss->pwpa_ie) {
            pcurr_bss->pwpa_ie = (IEEEtypes_VendorSpecific_t *)
                (pcurr_bss->pbeacon_buf + pcurr_bss->wpa_offset);
        }
        if (pcurr_bss->prsn_ie) {
            pcurr_bss->prsn_ie = (IEEEtypes_Generic_t *)
                (pcurr_bss->pbeacon_buf + pcurr_bss->rsn_offset);
        }
        if (pcurr_bss->pht_cap) {
            pcurr_bss->pht_cap = (IEEEtypes_HTCap_t *)
                (pcurr_bss->pbeacon_buf + pcurr_bss->ht_cap_offset);
        }

        if (pcurr_bss->pht_info) {
            pcurr_bss->pht_info = (IEEEtypes_HTInfo_t *)
                (pcurr_bss->pbeacon_buf + pcurr_bss->ht_info_offset);
        }

        if (pcurr_bss->pbss_co_2040) {
            pcurr_bss->pbss_co_2040 = (IEEEtypes_2040BSSCo_t *)
                (pcurr_bss->pbeacon_buf + pcurr_bss->bss_co_2040_offset);
        }

        if (pcurr_bss->pext_cap) {
            pcurr_bss->pext_cap = (IEEEtypes_ExtCap_t *)
                (pcurr_bss->pbeacon_buf + pcurr_bss->ext_cap_offset);
        }

        if (pcurr_bss->poverlap_bss_scan_param) {
            pcurr_bss->poverlap_bss_scan_param =
                (IEEEtypes_OverlapBSSScanParam_t *)
                (pcurr_bss->pbeacon_buf + pcurr_bss->overlap_bss_offset);
        }

        pcb->moal_spin_unlock(pmadapter->pmoal_handle,
                              pmpriv->curr_bcn_buf_lock);

        PRINTM(MINFO, "current beacon restored %d\n", pmpriv->curr_bcn_size);
    } else {
        PRINTM(MWARN, "curr_bcn_buf not saved or bcn_buf has no space\n");
    }

    LEAVE();
}

/**
 *  @brief Post process the scan table after a new scan command has completed
 *
 *  Inspect each entry of the scan table and try to find an entry that
 *    matches our current associated/joined network from the scan.  If
 *    one is found, update the stored copy of the BSSDescriptor for our
 *    current network.
 *
 *  Debug dump the current scan table contents if compiled accordingly.
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *
 *  @return             N/A
 */
static t_void
wlan_scan_process_results(IN mlan_private * pmpriv)
{
    mlan_adapter *pmadapter = pmpriv->adapter;
    t_s32 j;
    t_u32 i;

    ENTER();

    if (pmpriv->media_connected == MTRUE) {

        j = wlan_find_ssid_in_list(pmpriv,
                                   &pmpriv->curr_bss_params.bss_descriptor.ssid,
                                   pmpriv->curr_bss_params.bss_descriptor.
                                   mac_address, pmpriv->bss_mode);

        if (j >= 0) {
            pmadapter->callbacks.moal_spin_lock(pmadapter->pmoal_handle,
                                                pmpriv->curr_bcn_buf_lock);
            pmpriv->curr_bss_params.bss_descriptor.pwpa_ie = MNULL;
            pmpriv->curr_bss_params.bss_descriptor.wpa_offset = 0;
            pmpriv->curr_bss_params.bss_descriptor.prsn_ie = MNULL;
            pmpriv->curr_bss_params.bss_descriptor.rsn_offset = 0;
            pmpriv->curr_bss_params.bss_descriptor.pwapi_ie = MNULL;
            pmpriv->curr_bss_params.bss_descriptor.wapi_offset = 0;
            pmpriv->curr_bss_params.bss_descriptor.pht_cap = MNULL;
            pmpriv->curr_bss_params.bss_descriptor.ht_cap_offset = 0;
            pmpriv->curr_bss_params.bss_descriptor.pht_info = MNULL;
            pmpriv->curr_bss_params.bss_descriptor.ht_info_offset = 0;
            pmpriv->curr_bss_params.bss_descriptor.pbss_co_2040 = MNULL;
            pmpriv->curr_bss_params.bss_descriptor.bss_co_2040_offset = 0;
            pmpriv->curr_bss_params.bss_descriptor.pext_cap = MNULL;
            pmpriv->curr_bss_params.bss_descriptor.ext_cap_offset = 0;
            pmpriv->curr_bss_params.bss_descriptor.poverlap_bss_scan_param =
                MNULL;
            pmpriv->curr_bss_params.bss_descriptor.overlap_bss_offset = 0;
            pmpriv->curr_bss_params.bss_descriptor.pbeacon_buf = MNULL;
            pmpriv->curr_bss_params.bss_descriptor.beacon_buf_size = 0;
            pmpriv->curr_bss_params.bss_descriptor.beacon_buf_size_max = 0;

            PRINTM(MINFO, "Found current ssid/bssid in list @ index #%d\n", j);
            /* Make a copy of current BSSID descriptor */
            memcpy(pmadapter, &pmpriv->curr_bss_params.bss_descriptor,
                   &pmadapter->pscan_table[j],
                   sizeof(pmpriv->curr_bss_params.bss_descriptor));

            wlan_save_curr_bcn(pmpriv);
            pmadapter->callbacks.moal_spin_unlock(pmadapter->pmoal_handle,
                                                  pmpriv->curr_bcn_buf_lock);
        } else {
            wlan_restore_curr_bcn(pmpriv);
        }
    }

    for (i = 0; i < pmadapter->num_in_scan_table; i++)
        PRINTM(MINFO, "Scan:(%02d) %02x:%02x:%02x:%02x:%02x:%02x, "
               "RSSI[%03d], SSID[%s]\n",
               i,
               pmadapter->pscan_table[i].mac_address[0],
               pmadapter->pscan_table[i].mac_address[1],
               pmadapter->pscan_table[i].mac_address[2],
               pmadapter->pscan_table[i].mac_address[3],
               pmadapter->pscan_table[i].mac_address[4],
               pmadapter->pscan_table[i].mac_address[5],
               (t_s32) pmadapter->pscan_table[i].rssi,
               pmadapter->pscan_table[i].ssid.ssid);

    /*
     * Prepares domain info from scan table and downloads the
     *   domain info command to the FW.
     */
    wlan_11d_prepare_dnld_domain_info_cmd(pmpriv);

    LEAVE();
}

/**
 *  @brief Convert radio type scan parameter to a band config used in join cmd
 *
 *  @param radio_type Scan parameter indicating the radio used for a channel
 *                    in a scan command.
 *
 *  @return          Band type conversion of scanBand used in join/assoc cmds
 *
 */
static t_u8
radio_type_to_band(t_u8 radio_type)
{
    t_u8 ret_band;

    switch (radio_type) {
    case HostCmd_SCAN_RADIO_TYPE_A:
        ret_band = BAND_A;
        break;
    case HostCmd_SCAN_RADIO_TYPE_BG:
    default:
        ret_band = BAND_G;
        break;
    }

    return ret_band;
}

/**
 *  @brief Delete a specific indexed entry from the scan table.
 *
 *  Delete the scan table entry indexed by table_idx.  Compact the remaining
 *    entries and adjust any buffering of beacon/probe response data
 *    if needed.
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param table_idx    Scan table entry index to delete from the table
 *
 *  @return             N/A
 *
 *  @pre                table_idx must be an index to a valid entry
 */
static t_void
wlan_scan_delete_table_entry(IN mlan_private * pmpriv, IN t_s32 table_idx)
{
    mlan_adapter *pmadapter = pmpriv->adapter;
    t_u32 del_idx;
    t_u32 beacon_buf_adj;
    t_u8 *pbeacon_buf;

    ENTER();

    /*
     * Shift the saved beacon buffer data for the scan table back over the
     *   entry being removed.  Update the end of buffer pointer.  Save the
     *   deleted buffer allocation size for pointer adjustments for entries
     *   compacted after the deleted index.
     */
    beacon_buf_adj = pmadapter->pscan_table[table_idx].beacon_buf_size_max;

    PRINTM(MINFO, "Scan: Delete Entry %d, beacon buffer removal = %d bytes\n",
           table_idx, beacon_buf_adj);

    /* Check if the table entry had storage allocated for its beacon */
    if (beacon_buf_adj) {
        pbeacon_buf = pmadapter->pscan_table[table_idx].pbeacon_buf;

        /*
         * Remove the entry's buffer space, decrement the table end pointer
         *   by the amount we are removing
         */
        pmadapter->pbcn_buf_end -= beacon_buf_adj;

        PRINTM(MINFO,
               "Scan: Delete Entry %d, compact data: %p <- %p (sz = %d)\n",
               table_idx,
               pbeacon_buf,
               pbeacon_buf + beacon_buf_adj,
               pmadapter->pbcn_buf_end - pbeacon_buf);

        /*
         * Compact data storage.  Copy all data after the deleted entry's
         *   end address (pbeacon_buf + beacon_buf_adj) back to the original
         *   start address (pbeacon_buf).
         *
         * Scan table entries affected by the move will have their entry
         *   pointer adjusted below.
         *
         * Use memmove since the dest/src memory regions overlap.
         */
        memmove(pmadapter, pbeacon_buf,
                (void *) ((t_ptr) pbeacon_buf + (t_ptr) beacon_buf_adj),
                (t_u32) ((t_ptr) pmadapter->pbcn_buf_end -
                         (t_ptr) pbeacon_buf));
    }

    PRINTM(MINFO, "Scan: Delete Entry %d, num_in_scan_table = %d\n",
           table_idx, pmadapter->num_in_scan_table);

    /* Shift all of the entries after the table_idx back by one, compacting the
       table and removing the requested entry */
    for (del_idx = table_idx; (del_idx + 1) < pmadapter->num_in_scan_table;
         del_idx++) {
        /* Copy the next entry over this one */
        memcpy(pmadapter, pmadapter->pscan_table + del_idx,
               pmadapter->pscan_table + del_idx + 1, sizeof(BSSDescriptor_t));

        /*
         * Adjust this entry's pointer to its beacon buffer based on the
         *   removed/compacted entry from the deleted index.  Don't decrement
         *   if the buffer pointer is MNULL (no data stored for this entry).
         */
        if (pmadapter->pscan_table[del_idx].pbeacon_buf) {
            pmadapter->pscan_table[del_idx].pbeacon_buf -= beacon_buf_adj;
            if (pmadapter->pscan_table[del_idx].pwpa_ie) {
                pmadapter->pscan_table[del_idx].pwpa_ie =
                    (IEEEtypes_VendorSpecific_t *)
                    (pmadapter->pscan_table[del_idx].pbeacon_buf +
                     pmadapter->pscan_table[del_idx].wpa_offset);
            }
            if (pmadapter->pscan_table[del_idx].prsn_ie) {
                pmadapter->pscan_table[del_idx].prsn_ie =
                    (IEEEtypes_Generic_t *)
                    (pmadapter->pscan_table[del_idx].pbeacon_buf +
                     pmadapter->pscan_table[del_idx].rsn_offset);
            }
            if (pmadapter->pscan_table[del_idx].pwapi_ie) {
                pmadapter->pscan_table[del_idx].pwapi_ie =
                    (IEEEtypes_Generic_t *)
                    (pmadapter->pscan_table[del_idx].pbeacon_buf +
                     pmadapter->pscan_table[del_idx].wapi_offset);
            }
            if (pmadapter->pscan_table[del_idx].pht_cap) {
                pmadapter->pscan_table[del_idx].pht_cap =
                    (IEEEtypes_HTCap_t *) (pmadapter->pscan_table[del_idx].
                                           pbeacon_buf +
                                           pmadapter->pscan_table[del_idx].
                                           ht_cap_offset);
            }

            if (pmadapter->pscan_table[del_idx].pht_info) {
                pmadapter->pscan_table[del_idx].pht_info =
                    (IEEEtypes_HTInfo_t *) (pmadapter->pscan_table[del_idx].
                                            pbeacon_buf +
                                            pmadapter->pscan_table[del_idx].
                                            ht_info_offset);
            }
            if (pmadapter->pscan_table[del_idx].pbss_co_2040) {
                pmadapter->pscan_table[del_idx].pbss_co_2040 =
                    (IEEEtypes_2040BSSCo_t *) (pmadapter->pscan_table[del_idx].
                                               pbeacon_buf +
                                               pmadapter->pscan_table[del_idx].
                                               bss_co_2040_offset);
            }
            if (pmadapter->pscan_table[del_idx].pext_cap) {
                pmadapter->pscan_table[del_idx].pext_cap =
                    (IEEEtypes_ExtCap_t *) (pmadapter->pscan_table[del_idx].
                                            pbeacon_buf +
                                            pmadapter->pscan_table[del_idx].
                                            ext_cap_offset);
            }
            if (pmadapter->pscan_table[del_idx].poverlap_bss_scan_param) {
                pmadapter->pscan_table[del_idx].poverlap_bss_scan_param =
                    (IEEEtypes_OverlapBSSScanParam_t *) (pmadapter->
                                                         pscan_table[del_idx].
                                                         pbeacon_buf +
                                                         pmadapter->
                                                         pscan_table[del_idx].
                                                         overlap_bss_offset);
            }

        }
    }

    /* The last entry is invalid now that it has been deleted or moved back */
    memset(pmadapter, pmadapter->pscan_table + pmadapter->num_in_scan_table - 1,
           0x00, sizeof(BSSDescriptor_t));

    pmadapter->num_in_scan_table--;

    LEAVE();
}

/**
 *  @brief Delete all occurrences of a given SSID from the scan table
 *
 *  Iterate through the scan table and delete all entries that match a given
 *    SSID.  Compact the remaining scan table entries.
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param pdel_ssid    Pointer to an SSID to be used in deleting all
 *                        matching SSIDs from the scan table
 *
 *  @return             MLAN_STATUS_SUCCESS or MLAN_STATUS_FAILURE
 */
static mlan_status
wlan_scan_delete_ssid_table_entry(IN mlan_private * pmpriv,
                                  IN mlan_802_11_ssid * pdel_ssid)
{
    mlan_status ret = MLAN_STATUS_FAILURE;
    t_s32 table_idx;

    ENTER();

    PRINTM(MINFO, "Scan: Delete Ssid Entry: %-32s\n", pdel_ssid->ssid);

    /* If the requested SSID is found in the table, delete it.  Then keep
       searching the table for multiple entries for the SSID until no more are
       found */
    while ((table_idx = wlan_find_ssid_in_list(pmpriv,
                                               pdel_ssid,
                                               MNULL,
                                               MLAN_BSS_MODE_AUTO)) >= 0) {
        PRINTM(MINFO, "Scan: Delete SSID Entry: Found Idx = %d\n", table_idx);
        ret = MLAN_STATUS_SUCCESS;
        wlan_scan_delete_table_entry(pmpriv, table_idx);
    }

    LEAVE();
    return ret;
}

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

/**
 *  @brief Check if a scanned network compatible with the driver settings
 *
 *   WEP     WPA     WPA2    ad-hoc  encrypt                      Network
 * enabled enabled  enabled   AES     mode   Privacy  WPA  WPA2  Compatible
 *    0       0        0       0      NONE      0      0    0   yes No security
 *    0       1        0       0       x        1x     1    x   yes WPA (disable HT if no AES)
 *    0       0        1       0       x        1x     x    1   yes WPA2 (disable HT if no AES)
 *    0       0        0       1      NONE      1      0    0   yes Ad-hoc AES
 *    1       0        0       0      NONE      1      0    0   yes Static WEP (disable HT)
 *    0       0        0       0     !=NONE     1      0    0   yes Dynamic WEP
 *
 *  @param pmpriv  A pointer to mlan_private
 *  @param index   Index in scan table to check against current driver settings
 *  @param mode    Network mode: Infrastructure or IBSS
 *
 *  @return        Index in ScanTable, or negative value if error
 */
t_s32
wlan_is_network_compatible(IN mlan_private * pmpriv,
                           IN t_u32 index, IN t_u32 mode)
{
    mlan_adapter *pmadapter = pmpriv->adapter;
    BSSDescriptor_t *pbss_desc;

    ENTER();

    pbss_desc = &pmadapter->pscan_table[index];
    pbss_desc->disable_11n = MFALSE;

    /* Don't check for compatibility if roaming */
    if ((pmpriv->media_connected == MTRUE)
        && (pmpriv->bss_mode == MLAN_BSS_MODE_INFRA)
        && (pbss_desc->bss_mode == MLAN_BSS_MODE_INFRA)) {
        LEAVE();
        return index;
    }

    if (pbss_desc->wlan_11h_bss_info.chan_switch_ann.element_id ==
        CHANNEL_SWITCH_ANN) {
        PRINTM(MINFO, "Don't connect to AP with CHANNEL_SWITCH_ANN IE.\n");
        LEAVE();
        return -1;
    }

    if (pmpriv->wps.session_enable == MTRUE) {
        PRINTM(MINFO, "Return success directly in WPS period\n");
        LEAVE();
        return index;
    }

    if ((pbss_desc->bss_mode == mode) &&
        (pmpriv->sec_info.ewpa_enabled == MTRUE)) {
        if (((pbss_desc->pwpa_ie) &&
             ((*(pbss_desc->pwpa_ie)).vend_hdr.element_id == WPA_IE)) ||
            ((pbss_desc->prsn_ie) &&
             ((*(pbss_desc->prsn_ie)).ieee_hdr.element_id == RSN_IE))) {
            if (((pmpriv->adapter->config_bands & BAND_GN ||
                  pmpriv->adapter->config_bands & BAND_AN) &&
                 pbss_desc->pht_cap)
                && (pmpriv->bss_mode == MLAN_BSS_MODE_INFRA)
                && !is_wpa_oui_present(pmpriv->adapter, pbss_desc,
                                       CIPHER_SUITE_CCMP)
                && !is_rsn_oui_present(pmpriv->adapter, pbss_desc,
                                       CIPHER_SUITE_CCMP)) {

                if (is_wpa_oui_present
                    (pmpriv->adapter, pbss_desc, CIPHER_SUITE_TKIP)
                    || is_rsn_oui_present(pmpriv->adapter, pbss_desc,
                                          CIPHER_SUITE_TKIP)) {
                    PRINTM(MINFO,
                           "Disable 11n if AES is not supported by AP\n");
                    pbss_desc->disable_11n = MTRUE;
                } else {
                    LEAVE();
                    return -1;
                }
            }
            LEAVE();
            return index;
        } else {
            PRINTM(MINFO, "ewpa_enabled: Ignore none WPA/WPA2 AP\n");
            LEAVE();
            return -1;
        }
    }

    if (pmpriv->sec_info.wapi_enabled &&
        (pbss_desc->pwapi_ie &&
         ((*(pbss_desc->pwapi_ie)).ieee_hdr.element_id == WAPI_IE))) {
        PRINTM(MINFO, "Return success for WAPI AP\n");
        LEAVE();
        return index;
    }

    if (pbss_desc->bss_mode == mode) {
        if (pmpriv->sec_info.wep_status == Wlan802_11WEPDisabled
            && !pmpriv->sec_info.wpa_enabled
            && !pmpriv->sec_info.wpa2_enabled
            && ((!pbss_desc->pwpa_ie) ||
                ((*(pbss_desc->pwpa_ie)).vend_hdr.element_id != WPA_IE))
            && ((!pbss_desc->prsn_ie) ||
                ((*(pbss_desc->prsn_ie)).ieee_hdr.element_id != RSN_IE))
            && !pmpriv->adhoc_aes_enabled &&
            pmpriv->sec_info.encryption_mode == MLAN_ENCRYPTION_MODE_NONE &&
            !pbss_desc->privacy) {
            /* No security */
            LEAVE();
            return index;
        } else if (pmpriv->sec_info.wep_status == Wlan802_11WEPEnabled
                   && !pmpriv->sec_info.wpa_enabled
                   && !pmpriv->sec_info.wpa2_enabled
                   && !pmpriv->adhoc_aes_enabled && pbss_desc->privacy) {
            /* Static WEP enabled */
            PRINTM(MINFO, "Disable 11n in WEP mode\n");
            pbss_desc->disable_11n = MTRUE;
            LEAVE();
            return index;
        } else if (pmpriv->sec_info.wep_status == Wlan802_11WEPDisabled
                   && pmpriv->sec_info.wpa_enabled
                   && !pmpriv->sec_info.wpa2_enabled
                   && ((pbss_desc->pwpa_ie) &&
                       ((*(pbss_desc->pwpa_ie)).vend_hdr.element_id == WPA_IE))
                   && !pmpriv->adhoc_aes_enabled
                   /*
                    * Privacy bit may NOT be set in some APs like LinkSys WRT54G
                    * && pbss_desc->privacy
                    */
            ) {
            /* WPA enabled */
            PRINTM(MINFO,
                   "wlan_is_network_compatible() WPA: index=%d wpa_ie=%#x "
                   "rsn_ie=%#x WEP=%s WPA=%s WPA2=%s EncMode=%#x "
                   "privacy=%#x\n", index,
                   (pbss_desc->pwpa_ie) ? (*(pbss_desc->pwpa_ie)).vend_hdr.
                   element_id : 0,
                   (pbss_desc->prsn_ie) ? (*(pbss_desc->prsn_ie)).ieee_hdr.
                   element_id : 0,
                   (pmpriv->sec_info.wep_status ==
                    Wlan802_11WEPEnabled) ? "e" : "d",
                   (pmpriv->sec_info.wpa_enabled) ? "e" : "d",
                   (pmpriv->sec_info.wpa2_enabled) ? "e" : "d",
                   pmpriv->sec_info.encryption_mode, pbss_desc->privacy);
            if (((pmpriv->adapter->config_bands & BAND_GN ||
                  pmpriv->adapter->config_bands & BAND_AN) &&
                 pbss_desc->pht_cap)
                && (pmpriv->bss_mode == MLAN_BSS_MODE_INFRA)
                && !is_wpa_oui_present(pmpriv->adapter, pbss_desc,
                                       CIPHER_SUITE_CCMP)) {
                if (is_wpa_oui_present
                    (pmpriv->adapter, pbss_desc, CIPHER_SUITE_TKIP)) {
                    PRINTM(MINFO,
                           "Disable 11n if AES is not supported by AP\n");
                    pbss_desc->disable_11n = MTRUE;
                } else {
                    LEAVE();
                    return -1;
                }
            }
            LEAVE();
            return index;
        } else if (pmpriv->sec_info.wep_status == Wlan802_11WEPDisabled
                   && !pmpriv->sec_info.wpa_enabled
                   && pmpriv->sec_info.wpa2_enabled
                   && ((pbss_desc->prsn_ie) &&
                       ((*(pbss_desc->prsn_ie)).ieee_hdr.element_id == RSN_IE))
                   && !pmpriv->adhoc_aes_enabled
                   /*
                    * Privacy bit may NOT be set in some APs like LinkSys WRT54G
                    * && pbss_desc->privacy
                    */
            ) {
            /* WPA2 enabled */
            PRINTM(MINFO,
                   "wlan_is_network_compatible() WPA2: index=%d wpa_ie=%#x "
                   "rsn_ie=%#x WEP=%s WPA=%s WPA2=%s EncMode=%#x "
                   "privacy=%#x\n", index,
                   (pbss_desc->pwpa_ie) ? (*(pbss_desc->pwpa_ie)).vend_hdr.
                   element_id : 0,
                   (pbss_desc->prsn_ie) ? (*(pbss_desc->prsn_ie)).ieee_hdr.
                   element_id : 0,
                   (pmpriv->sec_info.wep_status ==
                    Wlan802_11WEPEnabled) ? "e" : "d",
                   (pmpriv->sec_info.wpa_enabled) ? "e" : "d",
                   (pmpriv->sec_info.wpa2_enabled) ? "e" : "d",
                   pmpriv->sec_info.encryption_mode, pbss_desc->privacy);
            if (((pmpriv->adapter->config_bands & BAND_GN ||
                  pmpriv->adapter->config_bands & BAND_AN) &&
                 pbss_desc->pht_cap)
                && (pmpriv->bss_mode == MLAN_BSS_MODE_INFRA)
                && !is_rsn_oui_present(pmpriv->adapter, pbss_desc,
                                       CIPHER_SUITE_CCMP)) {
                if (is_rsn_oui_present
                    (pmpriv->adapter, pbss_desc, CIPHER_SUITE_TKIP)) {
                    PRINTM(MINFO,
                           "Disable 11n if AES is not supported by AP\n");
                    pbss_desc->disable_11n = MTRUE;
                } else {
                    LEAVE();
                    return -1;
                }
            }
            LEAVE();
            return index;
        } else if (pmpriv->sec_info.wep_status == Wlan802_11WEPDisabled
                   && !pmpriv->sec_info.wpa_enabled
                   && !pmpriv->sec_info.wpa2_enabled
                   && ((!pbss_desc->pwpa_ie) ||
                       ((*(pbss_desc->pwpa_ie)).vend_hdr.element_id != WPA_IE))
                   && ((!pbss_desc->prsn_ie) ||
                       ((*(pbss_desc->prsn_ie)).ieee_hdr.element_id != RSN_IE))
                   && pmpriv->adhoc_aes_enabled &&
                   pmpriv->sec_info.encryption_mode == MLAN_ENCRYPTION_MODE_NONE
                   && pbss_desc->privacy) {
            /* Ad-hoc AES enabled */
            LEAVE();
            return index;
        } else if (pmpriv->sec_info.wep_status == Wlan802_11WEPDisabled
                   && !pmpriv->sec_info.wpa_enabled
                   && !pmpriv->sec_info.wpa2_enabled
                   && ((!pbss_desc->pwpa_ie) ||
                       ((*(pbss_desc->pwpa_ie)).vend_hdr.element_id != WPA_IE))
                   && ((!pbss_desc->prsn_ie) ||
                       ((*(pbss_desc->prsn_ie)).ieee_hdr.element_id != RSN_IE))
                   && !pmpriv->adhoc_aes_enabled &&
                   pmpriv->sec_info.encryption_mode != MLAN_ENCRYPTION_MODE_NONE
                   && pbss_desc->privacy) {
            /* Dynamic WEP enabled */
            PRINTM(MINFO, "wlan_is_network_compatible() dynamic WEP: index=%d "
                   "wpa_ie=%#x rsn_ie=%#x EncMode=%#x privacy=%#x\n",
                   index,
                   (pbss_desc->pwpa_ie) ? (*(pbss_desc->pwpa_ie)).vend_hdr.
                   element_id : 0,
                   (pbss_desc->prsn_ie) ? (*(pbss_desc->prsn_ie)).ieee_hdr.
                   element_id : 0, pmpriv->sec_info.encryption_mode,
                   pbss_desc->privacy);
            LEAVE();
            return index;
        }

        /* Security doesn't match */
        PRINTM(MINFO,
               "wlan_is_network_compatible() FAILED: index=%d wpa_ie=%#x "
               "rsn_ie=%#x WEP=%s WPA=%s WPA2=%s EncMode=%#x privacy=%#x\n",
               index,
               (pbss_desc->pwpa_ie) ? (*(pbss_desc->pwpa_ie)).vend_hdr.
               element_id : 0,
               (pbss_desc->prsn_ie) ? (*(pbss_desc->prsn_ie)).ieee_hdr.
               element_id : 0,
               (pmpriv->sec_info.wep_status ==
                Wlan802_11WEPEnabled) ? "e" : "d",
               (pmpriv->sec_info.wpa_enabled) ? "e" : "d",
               (pmpriv->sec_info.wpa2_enabled) ? "e" : "d",
               pmpriv->sec_info.encryption_mode, pbss_desc->privacy);
        LEAVE();
        return -1;
    }

    /* Mode doesn't match */
    LEAVE();
    return -1;
}

/**
 *  @brief Internal function used to flush the scan list
 *
 *  @param pmadapter    A pointer to mlan_adapter structure
 *
 *  @return             MLAN_STATUS_SUCCESS
 */
mlan_status
wlan_flush_scan_table(IN pmlan_adapter pmadapter)
{
    ENTER();

    PRINTM(MINFO, "Flushing scan table\n");

    memset(pmadapter, pmadapter->pscan_table, 0,
           (sizeof(BSSDescriptor_t) * MRVDRV_MAX_BSSID_LIST));
    pmadapter->num_in_scan_table = 0;

    memset(pmadapter, pmadapter->bcn_buf, 0, pmadapter->bcn_buf_size);
    pmadapter->pbcn_buf_end = pmadapter->bcn_buf;

    LEAVE();
    return MLAN_STATUS_SUCCESS;
}

/**
 *  @brief Internal function used to start a scan based on an input config
 *
 *  Use the input user scan configuration information when provided in
 *    order to send the appropriate scan commands to firmware to populate or
 *    update the internal driver scan table
 *
 *  @param pmpriv          A pointer to mlan_private structure
 *  @param pioctl_buf      A pointer to MLAN IOCTL Request buffer
 *  @param puser_scan_in   Pointer to the input configuration for the requested
 *                         scan.
 *
 *  @return              MLAN_STATUS_SUCCESS or < 0 if error
 */
mlan_status
wlan_scan_networks(IN mlan_private * pmpriv,
                   IN t_void * pioctl_buf,
                   IN const wlan_user_scan_cfg * puser_scan_in)
{
    mlan_status ret = MLAN_STATUS_SUCCESS;
    mlan_adapter *pmadapter = pmpriv->adapter;
    mlan_callbacks *pcb = (mlan_callbacks *) & pmadapter->callbacks;
    cmd_ctrl_node *pcmd_node = MNULL;
    pmlan_ioctl_req pioctl_req = (mlan_ioctl_req *) pioctl_buf;

    wlan_scan_cmd_config_tlv *pscan_cfg_out = MNULL;
    MrvlIEtypes_ChanListParamSet_t *pchan_list_out;
    t_u32 buf_size;
    ChanScanParamSet_t *pscan_chan_list;

    t_u8 keep_previous_scan;
    t_u8 filtered_scan;
    t_u8 scan_current_chan_only;
    t_u8 max_chan_per_scan;

    ENTER();

    ret =
        pcb->moal_malloc(pmadapter->pmoal_handle,
                         sizeof(wlan_scan_cmd_config_tlv), MLAN_MEM_DEF,
                         (t_u8 **) & pscan_cfg_out);
    if (ret != MLAN_STATUS_SUCCESS || !pscan_cfg_out) {
        PRINTM(MERROR, "Memory allocation for pscan_cfg_out failed!\n");
        if (pioctl_req)
            pioctl_req->status_code = MLAN_ERROR_NO_MEM;
        LEAVE();
        return MLAN_STATUS_FAILURE;
    }

    buf_size = sizeof(ChanScanParamSet_t) * WLAN_USER_SCAN_CHAN_MAX;
    ret =
        pcb->moal_malloc(pmadapter->pmoal_handle, buf_size, MLAN_MEM_DEF,
                         (t_u8 **) & pscan_chan_list);
    if (ret != MLAN_STATUS_SUCCESS || !pscan_chan_list) {
        PRINTM(MERROR, "Failed to allocate scan_chan_list\n");
        if (pscan_cfg_out)
            pcb->moal_mfree(pmadapter->pmoal_handle, (t_u8 *) pscan_cfg_out);
        if (pioctl_req)
            pioctl_req->status_code = MLAN_ERROR_NO_MEM;
        LEAVE();
        return MLAN_STATUS_FAILURE;
    }

    memset(pmadapter, pscan_chan_list, 0x00, buf_size);
    memset(pmadapter, pscan_cfg_out, 0x00, sizeof(wlan_scan_cmd_config_tlv));

    keep_previous_scan = MFALSE;

    ret = wlan_scan_setup_scan_config(pmpriv,
                                      puser_scan_in,
                                      &pscan_cfg_out->config,
                                      &pchan_list_out,
                                      pscan_chan_list,
                                      &max_chan_per_scan,
                                      &filtered_scan, &scan_current_chan_only);
    if (ret != MLAN_STATUS_SUCCESS) {

        PRINTM(MERROR, "Failed to setup scan config\n");
        if (pscan_cfg_out)
            pcb->moal_mfree(pmadapter->pmoal_handle, (t_u8 *) pscan_cfg_out);
        if (pscan_chan_list)
            pcb->moal_mfree(pmadapter->pmoal_handle, (t_u8 *) pscan_chan_list);
        if (pioctl_req)
            pioctl_req->status_code = MLAN_ERROR_INVALID_PARAMETER;
        LEAVE();
        return MLAN_STATUS_FAILURE;
    }

    if (puser_scan_in) {
        keep_previous_scan = puser_scan_in->keep_previous_scan;
    }

    if (keep_previous_scan == MFALSE) {
        memset(pmadapter, pmadapter->pscan_table, 0x00,
               sizeof(BSSDescriptor_t) * MRVDRV_MAX_BSSID_LIST);
        pmadapter->num_in_scan_table = 0;
        pmadapter->pbcn_buf_end = pmadapter->bcn_buf;
    }

    ret = wlan_scan_channel_list(pmpriv,
                                 pioctl_buf,
                                 max_chan_per_scan,
                                 filtered_scan,
                                 &pscan_cfg_out->config,
                                 pchan_list_out, pscan_chan_list);

    /* Get scan command from scan_pending_q and put to cmd_pending_q */
    if (ret == MLAN_STATUS_SUCCESS) {
        if (util_peek_list
            (pmadapter->pmoal_handle, &pmadapter->scan_pending_q,
             pcb->moal_spin_lock, pcb->moal_spin_unlock)) {
            pcmd_node =
                (cmd_ctrl_node *) util_dequeue_list(pmadapter->pmoal_handle,
                                                    &pmadapter->scan_pending_q,
                                                    pcb->moal_spin_lock,
                                                    pcb->moal_spin_unlock);
            pmadapter->pext_scan_ioctl_req = pioctl_req;
            wlan_request_cmd_lock(pmadapter);
            pmadapter->scan_processing = MTRUE;
            wlan_release_cmd_lock(pmadapter);
            wlan_insert_cmd_to_pending_q(pmadapter, pcmd_node, MTRUE);
        }
    }
    if (pscan_cfg_out)
        pcb->moal_mfree(pmadapter->pmoal_handle, (t_u8 *) pscan_cfg_out);

    if (pscan_chan_list)
        pcb->moal_mfree(pmadapter->pmoal_handle, (t_u8 *) pscan_chan_list);

    LEAVE();
    return ret;
}

/**
 *  @brief Prepare a scan command to be sent to the firmware
 *
 *  Use the wlan_scan_cmd_config sent to the command processing module in
 *   the wlan_prepare_cmd to configure a HostCmd_DS_802_11_SCAN command
 *   struct to send to firmware.
 *
 *  The fixed fields specifying the BSS type and BSSID filters as well as a
 *   variable number/length of TLVs are sent in the command to firmware.
 *
 *  @param pmpriv     A pointer to mlan_private structure
 *  @param pcmd       A pointer to HostCmd_DS_COMMAND structure to be sent to
 *                    firmware with the HostCmd_DS_801_11_SCAN structure
 *  @param pdata_buf  Void pointer cast of a wlan_scan_cmd_config struct used
 *                    to set the fields/TLVs for the command sent to firmware
 *
 *  @return           MLAN_STATUS_SUCCESS
 */
mlan_status
wlan_cmd_802_11_scan(IN mlan_private * pmpriv,
                     IN HostCmd_DS_COMMAND * pcmd, IN t_void * pdata_buf)
{
    HostCmd_DS_802_11_SCAN *pscan_cmd = &pcmd->params.scan;
    wlan_scan_cmd_config *pscan_cfg;

    ENTER();

    pscan_cfg = (wlan_scan_cmd_config *) pdata_buf;

    /* Set fixed field variables in scan command */
    pscan_cmd->bss_mode = pscan_cfg->bss_mode;
    memcpy(pmpriv->adapter, pscan_cmd->bssid, pscan_cfg->specific_bssid,
           sizeof(pscan_cmd->bssid));
    memcpy(pmpriv->adapter, pscan_cmd->tlv_buffer, pscan_cfg->tlv_buf,
           pscan_cfg->tlv_buf_len);

    pcmd->command = wlan_cpu_to_le16(HostCmd_CMD_802_11_SCAN);

    /* Size is equal to the sizeof(fixed portions) + the TLV len + header */
    pcmd->size = (t_u16) wlan_cpu_to_le16((t_u16) (sizeof(pscan_cmd->bss_mode)
                                                   + sizeof(pscan_cmd->bssid)
                                                   + pscan_cfg->tlv_buf_len
                                                   + S_DS_GEN));

    LEAVE();
    return MLAN_STATUS_SUCCESS;
}

/**
 *  @brief This function handles the command response of scan
 *
 *   The response buffer for the scan command has the following
 *      memory layout:
 *
 *     .-------------------------------------------------------------.
 *     |  Header (4 * sizeof(t_u16)):  Standard command response hdr |
 *     .-------------------------------------------------------------.
 *     |  BufSize (t_u16) : sizeof the BSS Description data          |
 *     .-------------------------------------------------------------.
 *     |  NumOfSet (t_u8) : Number of BSS Descs returned             |
 *     .-------------------------------------------------------------.
 *     |  BSSDescription data (variable, size given in BufSize)      |
 *     .-------------------------------------------------------------.
 *     |  TLV data (variable, size calculated using Header->Size,    |
 *     |            BufSize and sizeof the fixed fields above)       |
 *     .-------------------------------------------------------------.
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param resp         A pointer to HostCmd_DS_COMMAND
 *  @param pioctl_buf   A pointer to mlan_ioctl_req structure
 *
 *  @return             MLAN_STATUS_SUCCESS or MLAN_STATUS_FAILURE
 */
mlan_status
wlan_ret_802_11_scan(IN mlan_private * pmpriv,
                     IN HostCmd_DS_COMMAND * resp, IN t_void * pioctl_buf)
{
    mlan_status ret = MLAN_STATUS_SUCCESS;
    mlan_adapter *pmadapter = pmpriv->adapter;
    mlan_callbacks *pcb = MNULL;
    mlan_ioctl_req *pioctl_req = (mlan_ioctl_req *) pioctl_buf;
    cmd_ctrl_node *pcmd_node = MNULL;
    HostCmd_DS_802_11_SCAN_RSP *pscan_rsp = MNULL;
    BSSDescriptor_t *bss_new_entry = MNULL;
    MrvlIEtypes_Data_t *ptlv;
    MrvlIEtypes_TsfTimestamp_t *ptsf_tlv = MNULL;
    t_u8 *pbss_info;
    t_u32 scan_resp_size;
    t_u32 bytes_left;
    t_u32 num_in_table;
    t_u32 bss_idx;
    t_u32 idx;
    t_u32 tlv_buf_size;
    t_u64 tsf_val;
    chan_freq_power_t *cfp;
    MrvlIEtypes_ChanBandListParamSet_t *pchan_band_tlv = MNULL;
    ChanBandParamSet_t *pchan_band;
    t_u8 band;
    t_u8 is_bgscan_resp;
    t_u32 age_ts_usec;

    ENTER();
    pcb = (pmlan_callbacks) & pmadapter->callbacks;

    is_bgscan_resp = (resp->command == HostCmd_CMD_802_11_BG_SCAN_QUERY);
    if (is_bgscan_resp) {
        pscan_rsp = &resp->params.bg_scan_query_resp.scan_resp;
    } else {
        pscan_rsp = &resp->params.scan_resp;
    }

    if (pscan_rsp->number_of_sets > MRVDRV_MAX_BSSID_LIST) {
        PRINTM(MERROR, "SCAN_RESP: Invalid number of AP returned (%d)!!\n",
               pscan_rsp->number_of_sets);
        if (pioctl_req)
            pioctl_req->status_code = MLAN_ERROR_CMD_SCAN_FAIL;
        ret = MLAN_STATUS_FAILURE;
        goto done;
    }

    bytes_left = wlan_le16_to_cpu(pscan_rsp->bss_descript_size);
    PRINTM(MINFO, "SCAN_RESP: bss_descript_size %d\n", bytes_left);

    scan_resp_size = resp->size;

    PRINTM(MINFO, "SCAN_RESP: returned %d APs before parsing\n",
           pscan_rsp->number_of_sets);

    num_in_table = pmadapter->num_in_scan_table;
    pbss_info = pscan_rsp->bss_desc_and_tlv_buffer;

    /*
     * The size of the TLV buffer is equal to the entire command response
     *   size (scan_resp_size) minus the fixed fields (sizeof()'s), the
     *   BSS Descriptions (bss_descript_size as bytesLef) and the command
     *   response header (S_DS_GEN)
     */
    tlv_buf_size = scan_resp_size - (bytes_left
                                     + sizeof(pscan_rsp->bss_descript_size)
                                     + sizeof(pscan_rsp->number_of_sets)
                                     + S_DS_GEN);

    ptlv =
        (MrvlIEtypes_Data_t *) (pscan_rsp->bss_desc_and_tlv_buffer +
                                bytes_left);

    /* Search the TLV buffer space in the scan response for any valid TLVs */
    wlan_ret_802_11_scan_get_tlv_ptrs(pmadapter,
                                      ptlv,
                                      tlv_buf_size,
                                      TLV_TYPE_TSFTIMESTAMP,
                                      (MrvlIEtypes_Data_t **) & ptsf_tlv);

    /* Search the TLV buffer space in the scan response for any valid TLVs */
    wlan_ret_802_11_scan_get_tlv_ptrs(pmadapter,
                                      ptlv,
                                      tlv_buf_size,
                                      TLV_TYPE_CHANNELBANDLIST,
                                      (MrvlIEtypes_Data_t **) & pchan_band_tlv);

    /*
     *  Process each scan response returned (pscan_rsp->number_of_sets).  Save
     *    the information in the bss_new_entry and then insert into the
     *    driver scan table either as an update to an existing entry
     *    or as an addition at the end of the table
     */
    ret =
        pcb->moal_malloc(pmadapter->pmoal_handle, sizeof(BSSDescriptor_t),
                         MLAN_MEM_DEF, (t_u8 **) & bss_new_entry);

    if (ret != MLAN_STATUS_SUCCESS || !bss_new_entry) {
        PRINTM(MERROR, "Memory allocation for bss_new_entry failed!\n");
        if (pioctl_req)
            pioctl_req->status_code = MLAN_ERROR_NO_MEM;
        ret = MLAN_STATUS_FAILURE;
        goto done;
    }

    for (idx = 0; idx < pscan_rsp->number_of_sets && bytes_left; idx++) {
        /* Zero out the bss_new_entry we are about to store info in */
        memset(pmadapter, bss_new_entry, 0x00, sizeof(BSSDescriptor_t));

        /* Process the data fields and IEs returned for this BSS */
        if (wlan_interpret_bss_desc_with_ie(pmadapter,
                                            bss_new_entry,
                                            &pbss_info,
                                            &bytes_left) ==
            MLAN_STATUS_SUCCESS) {
            PRINTM(MINFO, "SCAN_RESP: BSSID = %02x:%02x:%02x:%02x:%02x:%02x\n",
                   bss_new_entry->mac_address[0], bss_new_entry->mac_address[1],
                   bss_new_entry->mac_address[2], bss_new_entry->mac_address[3],
                   bss_new_entry->mac_address[4],
                   bss_new_entry->mac_address[5]);
            /*
             * Search the scan table for the same bssid
             */
            for (bss_idx = 0; bss_idx < num_in_table; bss_idx++) {
                if (!memcmp(pmadapter, bss_new_entry->mac_address,
                            pmadapter->pscan_table[bss_idx].mac_address,
                            sizeof(bss_new_entry->mac_address))) {
                    /*
                     * If the SSID matches as well, it is a duplicate of
                     *   this entry.  Keep the bss_idx set to this
                     *   entry so we replace the old contents in the table
                     */
                    if ((bss_new_entry->ssid.ssid_len ==
                         pmadapter->pscan_table[bss_idx].ssid.ssid_len)
                        && (!memcmp(pmadapter, bss_new_entry->ssid.ssid,
                                    pmadapter->pscan_table[bss_idx].ssid.ssid,
                                    bss_new_entry->ssid.ssid_len))) {
                        PRINTM(MINFO, "SCAN_RESP: Duplicate of index: %d\n",
                               bss_idx);
                        break;
                    }
                }
            }
            /*
             * If the bss_idx is equal to the number of entries in the table,
             *   the new entry was not a duplicate; append it to the scan
             *   table
             */
            if (bss_idx == num_in_table) {
                /* Range check the bss_idx, keep it limited to the last entry */
                if (bss_idx == MRVDRV_MAX_BSSID_LIST) {
                    bss_idx--;
                } else {
                    num_in_table++;
                }
            }

            /*
             * Save the beacon/probe response returned for later application
             *   retrieval.  Duplicate beacon/probe responses are updated if
             *   possible
             */
            wlan_ret_802_11_scan_store_beacon(pmpriv,
                                              bss_idx,
                                              num_in_table, bss_new_entry);
            if (bss_new_entry->pbeacon_buf == MNULL) {
                PRINTM(MCMND, "No space for beacon, drop this entry\n");
                num_in_table--;
                continue;
            }
            /*
             * If the TSF TLV was appended to the scan results, save
             *   this entry's TSF value in the networkTSF field.  The
             *   networkTSF is the firmware's TSF value at the time the
             *   beacon or probe response was received.
             */
            if (ptsf_tlv) {
                memcpy(pmpriv->adapter, &tsf_val,
                       &ptsf_tlv->tsf_data[idx * TSF_DATA_SIZE],
                       sizeof(tsf_val));
                tsf_val = wlan_le64_to_cpu(tsf_val);
                memcpy(pmpriv->adapter, &bss_new_entry->network_tsf,
                       &tsf_val, sizeof(bss_new_entry->network_tsf));
            }
            band = BAND_G;
            if (pchan_band_tlv) {
                pchan_band = &pchan_band_tlv->chan_band_param[idx];
                band =
                    radio_type_to_band(pchan_band->
                                       radio_type & (MBIT(0) | MBIT(1)));
            }

            /* Save the band designation for this entry for use in join */
            bss_new_entry->bss_band = band;
            cfp =
                wlan_find_cfp_by_band_and_channel(pmadapter,
                                                  (t_u8) bss_new_entry->
                                                  bss_band,
                                                  (t_u16) bss_new_entry->
                                                  channel);

            if (cfp)
                bss_new_entry->freq = cfp->freq;
            else
                bss_new_entry->freq = 0;

            /* Copy the locally created bss_new_entry to the scan table */
            memcpy(pmadapter, &pmadapter->pscan_table[bss_idx],
                   bss_new_entry, sizeof(pmadapter->pscan_table[bss_idx]));

        } else {

            /* Error parsing/interpreting the scan response, skipped */
            PRINTM(MERROR,
                   "SCAN_RESP: wlan_interpret_bss_desc_with_ie returned error\n");
        }
    }

    PRINTM(MINFO, "SCAN_RESP: Scanned %2d APs, %d valid, %d total\n",
           pscan_rsp->number_of_sets,
           num_in_table - pmadapter->num_in_scan_table, num_in_table);

    /* Update the total number of BSSIDs in the scan table */
    pmadapter->num_in_scan_table = num_in_table;
    /* Update the age_in_second */
    pmadapter->callbacks.moal_get_system_time(pmadapter->pmoal_handle,
                                              &pmadapter->age_in_secs,
                                              &age_ts_usec);

    if (!util_peek_list
        (pmadapter->pmoal_handle, &pmadapter->scan_pending_q,
         pcb->moal_spin_lock, pcb->moal_spin_unlock)) {
        wlan_request_cmd_lock(pmadapter);
        pmadapter->scan_processing = MFALSE;
        wlan_release_cmd_lock(pmadapter);
        /*
         * Process the resulting scan table:
         *   - Remove any bad ssids
         *   - Update our current BSS information from scan data
         */
        wlan_scan_process_results(pmpriv);

        /* Need to indicate IOCTL complete */
        if (pioctl_req != MNULL) {
            pioctl_req->status_code = MLAN_ERROR_NO_ERROR;

            /* Indicate ioctl complete */
            pcb->moal_ioctl_complete(pmadapter->pmoal_handle,
                                     (pmlan_ioctl_req) pioctl_buf,
                                     MLAN_STATUS_SUCCESS);
        }
        wlan_recv_event(pmpriv, MLAN_EVENT_ID_DRV_SCAN_REPORT, MNULL);
    } else {
        /* If firmware not ready, do not issue any more scan commands */
        if (pmadapter->hw_status != WlanHardwareStatusReady) {
            /* Flush all pending scan commands */
            wlan_flush_scan_queue(pmadapter);
            /* Indicate IOCTL complete */
            if (pioctl_req != MNULL) {
                pioctl_req->status_code = MLAN_ERROR_FW_NOT_READY;

                /* Indicate ioctl complete */
                pcb->moal_ioctl_complete(pmadapter->pmoal_handle,
                                         (pmlan_ioctl_req) pioctl_buf,
                                         MLAN_STATUS_FAILURE);
            }
        } else {
            /* Get scan command from scan_pending_q and put to cmd_pending_q */
            pcmd_node =
                (cmd_ctrl_node *) util_dequeue_list(pmadapter->pmoal_handle,
                                                    &pmadapter->scan_pending_q,
                                                    pcb->moal_spin_lock,
                                                    pcb->moal_spin_unlock);

            wlan_insert_cmd_to_pending_q(pmadapter, pcmd_node, MTRUE);
        }
    }

  done:
    if (bss_new_entry)
        pcb->moal_mfree(pmadapter->pmoal_handle, (t_u8 *) bss_new_entry);

    LEAVE();
    return ret;
}

/**
 *  @brief Prepare an extended scan command to be sent to the firmware
 *
 *  Use the wlan_scan_cmd_config sent to the command processing module in
 *   the wlan_prepare_cmd to configure a HostCmd_DS_802_11_SCAN_EXT command
 *   struct to send to firmware.
 *
 *  @param pmpriv     A pointer to mlan_private structure
 *  @param pcmd       A pointer to HostCmd_DS_COMMAND structure to be sent to
 *                    firmware with the HostCmd_DS_802_11_SCAN_EXT structure
 *  @param pdata_buf  Void pointer cast of a wlan_scan_cmd_config struct used
 *                    to set the fields/TLVs for the command sent to firmware
 *
 *  @return           MLAN_STATUS_SUCCESS
 */
mlan_status
wlan_cmd_802_11_scan_ext(IN mlan_private * pmpriv,
                         IN HostCmd_DS_COMMAND * pcmd, IN t_void * pdata_buf)
{
    HostCmd_DS_802_11_SCAN_EXT *pext_scan_cmd = &pcmd->params.ext_scan;
    wlan_scan_cmd_config *pscan_cfg = MNULL;

    ENTER();

    pscan_cfg = (wlan_scan_cmd_config *) pdata_buf;

    memcpy(pmpriv->adapter, pext_scan_cmd->tlv_buffer,
           pscan_cfg->tlv_buf, pscan_cfg->tlv_buf_len);

    pcmd->command = wlan_cpu_to_le16(HostCmd_CMD_802_11_SCAN_EXT);

    /* Size is equal to the sizeof(fixed portions) + the TLV len + header */
    pcmd->size = wlan_cpu_to_le16((t_u16) (sizeof(pext_scan_cmd->reserved)
                                           + pscan_cfg->tlv_buf_len
                                           + S_DS_GEN));

    LEAVE();
    return MLAN_STATUS_SUCCESS;
}

/**
 *  @brief This function handles the command response of extended scan
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param resp         A pointer to HostCmd_DS_COMMAND
 *  @param pioctl_buf   A pointer to mlan_ioctl_req structure
 *
 *  @return             MLAN_STATUS_SUCCESS or MLAN_STATUS_FAILURE
 */
mlan_status
wlan_ret_802_11_scan_ext(IN mlan_private * pmpriv,
                         IN HostCmd_DS_COMMAND * resp, IN t_void * pioctl_buf)
{
    ENTER();

    PRINTM(MINFO, "EXT scan returns successfully\n");

    LEAVE();
    return MLAN_STATUS_SUCCESS;
}

/**
 *  @brief This function parse and store the extended scan results
 *
 *  @param pmpriv           A pointer to mlan_private structure
 *  @param number_of_sets   Number of BSS
 *  @param pscan_resp       A pointer to scan response buffer
 *  @param scan_resp_size   Size of scan response buffer
 *
 *  @return             MLAN_STATUS_SUCCESS or MLAN_STATUS_FAILURE
 */
static mlan_status
wlan_parse_ext_scan_result(IN mlan_private * pmpriv,
                           IN t_u8 number_of_sets,
                           IN t_u8 * pscan_resp, IN t_u16 scan_resp_size)
{
    mlan_status ret = MLAN_STATUS_SUCCESS;
    mlan_adapter *pmadapter = pmpriv->adapter;
    mlan_callbacks *pcb = MNULL;
    BSSDescriptor_t *bss_new_entry = MNULL;
    t_u8 *pbss_info;
    t_u32 bytes_left;
    t_u32 bytes_left_for_tlv;
    t_u32 num_in_table;
    t_u32 bss_idx;
    t_u32 idx;
    t_u64 tsf_val;
    chan_freq_power_t *cfp;
    t_u16 tlv_type, tlv_len;
    MrvlIEtypes_Data_t *ptlv = MNULL;
    MrvlIEtypes_Bss_Scan_Rsp_t *pscan_rsp_tlv = MNULL;
    MrvlIEtypes_Bss_Scan_Info_t *pscan_info_tlv = MNULL;
    t_u8 band;
    t_u32 age_ts_usec;

    ENTER();
    pcb = (pmlan_callbacks) & pmadapter->callbacks;

    if (number_of_sets > MRVDRV_MAX_BSSID_LIST) {
        PRINTM(MERROR, "EXT_SCAN: Invalid number of AP returned (%d)!!\n",
               number_of_sets);
        ret = MLAN_STATUS_FAILURE;
        goto done;
    }

    bytes_left = scan_resp_size;
    PRINTM(MINFO, "EXT_SCAN: bss_descript_size %d\n", scan_resp_size);
    PRINTM(MINFO, "EXT_SCAN: returned %d APs before parsing\n", number_of_sets);

    num_in_table = pmadapter->num_in_scan_table;
    ptlv = (MrvlIEtypes_Data_t *) pscan_resp;

    /*
     *  Process each scan response returned number_of_sets. Save
     *    the information in the bss_new_entry and then insert into the
     *    driver scan table either as an update to an existing entry
     *    or as an addition at the end of the table
     */
    ret =
        pcb->moal_malloc(pmadapter->pmoal_handle, sizeof(BSSDescriptor_t),
                         MLAN_MEM_DEF, (t_u8 **) & bss_new_entry);

    if (ret != MLAN_STATUS_SUCCESS || !bss_new_entry) {
        PRINTM(MERROR, "Memory allocation for bss_new_entry failed!\n");
        ret = MLAN_STATUS_FAILURE;
        goto done;
    }

    for (idx = 0; idx < number_of_sets && bytes_left >
         sizeof(MrvlIEtypesHeader_t); idx++) {
        tlv_type = wlan_le16_to_cpu(ptlv->header.type);
        tlv_len = wlan_le16_to_cpu(ptlv->header.len);
        if (bytes_left < sizeof(MrvlIEtypesHeader_t) + tlv_len) {
            PRINTM(MERROR, "EXT_SCAN: Error bytes left < TLV length\n");
            break;
        }
        pscan_rsp_tlv = MNULL;
        pscan_info_tlv = MNULL;
        bytes_left_for_tlv = bytes_left;
        /* BSS response TLV with beacon or probe response buffer at the initial
           position of each descriptor */
        if (tlv_type == TLV_TYPE_BSS_SCAN_RSP) {
            pbss_info = (t_u8 *) ptlv;
            pscan_rsp_tlv = (MrvlIEtypes_Bss_Scan_Rsp_t *) ptlv;
            ptlv = (MrvlIEtypes_Data_t *) (ptlv->data + tlv_len);
            bytes_left_for_tlv -= (tlv_len + sizeof(MrvlIEtypesHeader_t));
        } else
            break;

        /* Process variable TLV */
        while (bytes_left_for_tlv >= sizeof(MrvlIEtypesHeader_t) &&
               wlan_le16_to_cpu(ptlv->header.type) != TLV_TYPE_BSS_SCAN_RSP) {
            tlv_type = wlan_le16_to_cpu(ptlv->header.type);
            tlv_len = wlan_le16_to_cpu(ptlv->header.len);
            if (bytes_left_for_tlv < sizeof(MrvlIEtypesHeader_t) + tlv_len) {
                PRINTM(MERROR, "EXT_SCAN: Error in processing TLV, "
                       "bytes left < TLV length\n");
                pscan_rsp_tlv = MNULL;
                bytes_left_for_tlv = 0;
                continue;
            }
            switch (tlv_type) {
            case TLV_TYPE_BSS_SCAN_INFO:
                pscan_info_tlv = (MrvlIEtypes_Bss_Scan_Info_t *) ptlv;
                if (tlv_len != sizeof(MrvlIEtypes_Bss_Scan_Info_t) -
                    sizeof(MrvlIEtypesHeader_t)) {
                    bytes_left_for_tlv = 0;
                    continue;
                }
                break;
            default:
                break;
            }
            ptlv = (MrvlIEtypes_Data_t *) (ptlv->data + tlv_len);
            bytes_left -= (tlv_len + sizeof(MrvlIEtypesHeader_t));
            bytes_left_for_tlv -= (tlv_len + sizeof(MrvlIEtypesHeader_t));
        }
        /* No BSS response TLV */
        if (pscan_rsp_tlv == MNULL)
            break;

        /* Advance pointer to the beacon buffer length and update the bytes
           count so that the function wlan_interpret_bss_desc_with_ie() can
           handle the scan buffer withut any change */
        pbss_info += sizeof(t_u16);
        bytes_left -= sizeof(t_u16);

        /* Zero out the bss_new_entry we are about to store info in */
        memset(pmadapter, bss_new_entry, 0x00, sizeof(BSSDescriptor_t));

        /* Process the data fields and IEs returned for this BSS */
        if (wlan_interpret_bss_desc_with_ie(pmadapter,
                                            bss_new_entry,
                                            &pbss_info,
                                            &bytes_left) ==
            MLAN_STATUS_SUCCESS) {
            PRINTM(MINFO, "EXT_SCAN: BSSID = %02x:%02x:%02x:%02x:%02x:%02x\n",
                   bss_new_entry->mac_address[0], bss_new_entry->mac_address[1],
                   bss_new_entry->mac_address[2], bss_new_entry->mac_address[3],
                   bss_new_entry->mac_address[4],
                   bss_new_entry->mac_address[5]);
            /*
             * Search the scan table for the same bssid
             */
            for (bss_idx = 0; bss_idx < num_in_table; bss_idx++) {
                if (!memcmp(pmadapter, bss_new_entry->mac_address,
                            pmadapter->pscan_table[bss_idx].mac_address,
                            sizeof(bss_new_entry->mac_address))) {
                    /*
                     * If the SSID matches as well, it is a duplicate of
                     *   this entry.  Keep the bss_idx set to this
                     *   entry so we replace the old contents in the table
                     */
                    if ((bss_new_entry->ssid.ssid_len ==
                         pmadapter->pscan_table[bss_idx].ssid.ssid_len)
                        && (!memcmp(pmadapter, bss_new_entry->ssid.ssid,
                                    pmadapter->pscan_table[bss_idx].ssid.ssid,
                                    bss_new_entry->ssid.ssid_len))) {
                        PRINTM(MINFO, "EXT_SCAN: Duplicate of index: %d\n",
                               bss_idx);
                        break;
                    }
                }
            }
            /*
             * If the bss_idx is equal to the number of entries in the table,
             *   the new entry was not a duplicate; append it to the scan
             *   table
             */
            if (bss_idx == num_in_table) {
                /* Range check the bss_idx, keep it limited to the last entry */
                if (bss_idx == MRVDRV_MAX_BSSID_LIST) {
                    bss_idx--;
                } else {
                    num_in_table++;
                }
            }

            band = BAND_G;
            /*
             * If the BSS info TLV was appended to the scan results, save
             *   this entry's TSF value in the networkTSF field. The
             *   networkTSF is the firmware's TSF value at the time the
             *   beacon or probe response was received.
             */
            if (pscan_info_tlv) {
                /* RSSI is 2 byte long */
                bss_new_entry->rssi =
                    -(t_s32) (wlan_le16_to_cpu(pscan_info_tlv->rssi));
                PRINTM(MINFO, "EXT_SCAN: RSSI=%d\n", bss_new_entry->rssi);
                memcpy(pmpriv->adapter, &tsf_val, &pscan_info_tlv->tsf,
                       sizeof(tsf_val));
                tsf_val = wlan_le64_to_cpu(tsf_val);
                memcpy(pmpriv->adapter, &bss_new_entry->network_tsf,
                       &tsf_val, sizeof(bss_new_entry->network_tsf));
                band = radio_type_to_band(pscan_info_tlv->band);
            }
            /*
             * Save the beacon/probe response returned for later application
             *   retrieval. Duplicate beacon/probe responses are updated if
             *   possible
             */
            wlan_ret_802_11_scan_store_beacon(pmpriv,
                                              bss_idx,
                                              num_in_table, bss_new_entry);
            if (bss_new_entry->pbeacon_buf == MNULL) {
                PRINTM(MCMND, "No space for beacon, drop this entry\n");
                num_in_table--;
                continue;
            }
            /* Save the band designation for this entry for use in join */
            bss_new_entry->bss_band = band;
            cfp = wlan_find_cfp_by_band_and_channel(pmadapter,
                                                    (t_u8) bss_new_entry->
                                                    bss_band,
                                                    (t_u16) bss_new_entry->
                                                    channel);

            if (cfp)
                bss_new_entry->freq = cfp->freq;
            else
                bss_new_entry->freq = 0;

            /* Copy the locally created bss_new_entry to the scan table */
            memcpy(pmadapter, &pmadapter->pscan_table[bss_idx],
                   bss_new_entry, sizeof(pmadapter->pscan_table[bss_idx]));
        } else {
            /* Error parsing/interpreting the scan response, skipped */
            PRINTM(MERROR,
                   "EXT_SCAN: wlan_interpret_bss_desc_with_ie returned error\n");
        }
    }

    PRINTM(MINFO, "EXT_SCAN: Scanned %2d APs, %d valid, %d total\n",
           number_of_sets, num_in_table - pmadapter->num_in_scan_table,
           num_in_table);

    /* Update the total number of BSSIDs in the scan table */
    pmadapter->num_in_scan_table = num_in_table;
    /* Update the age_in_second */
    pmadapter->callbacks.moal_get_system_time(pmadapter->pmoal_handle,
                                              &pmadapter->age_in_secs,
                                              &age_ts_usec);

  done:
    if (bss_new_entry)
        pcb->moal_mfree(pmadapter->pmoal_handle, (t_u8 *) bss_new_entry);

    LEAVE();
    return ret;
}

/**
 *  @brief This function handles the event extended scan report
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param pmbuf        A pointer to mlan_buffer
 *
 *  @return             MLAN_STATUS_SUCCESS or MLAN_STATUS_FAILURE
 */
mlan_status
wlan_handle_event_ext_scan_report(IN mlan_private * pmpriv,
                                  IN mlan_buffer * pmbuf)
{
    mlan_adapter *pmadapter = pmpriv->adapter;
    mlan_callbacks *pcb = &pmadapter->callbacks;
    mlan_ioctl_req *pioctl_req = MNULL;
    cmd_ctrl_node *pcmd_node = MNULL;
    mlan_status ret = MLAN_STATUS_SUCCESS;

    mlan_event_scan_result *pevent_scan = (pmlan_event_scan_result)
        (pmbuf->pbuf + pmbuf->data_offset);
    t_u8 *ptlv = (pmbuf->pbuf + pmbuf->data_offset
                  + sizeof(mlan_event_scan_result));
    t_u16 tlv_buf_left = wlan_cpu_to_le16(pevent_scan->buf_size);

    DBG_HEXDUMP(MCMD_D, "EVENT EXT_SCAN", pmbuf->pbuf +
                pmbuf->data_offset, pmbuf->data_len);
    wlan_parse_ext_scan_result(pmpriv, pevent_scan->num_of_set,
                               ptlv, tlv_buf_left);
    if (!pevent_scan->more_event) {
        pioctl_req = pmadapter->pext_scan_ioctl_req;
        if (!util_peek_list(pmadapter->pmoal_handle,
                            &pmadapter->scan_pending_q, pcb->moal_spin_lock,
                            pcb->moal_spin_unlock)) {
            pmadapter->pext_scan_ioctl_req = MNULL;
            wlan_request_cmd_lock(pmadapter);
            pmadapter->scan_processing = MFALSE;
            wlan_release_cmd_lock(pmadapter);
            /*
             * Process the resulting scan table:
             *   - Remove any bad ssids
             *   - Update our current BSS information from scan data
             */
            wlan_scan_process_results(pmpriv);

            /* Need to indicate IOCTL complete */
            if (pioctl_req != MNULL) {
                pioctl_req->status_code = MLAN_ERROR_NO_ERROR;
                /* Indicate ioctl complete */
                pcb->moal_ioctl_complete(pmadapter->pmoal_handle,
                                         (pmlan_ioctl_req) pioctl_req,
                                         MLAN_STATUS_SUCCESS);
            }
            wlan_recv_event(pmpriv, MLAN_EVENT_ID_DRV_SCAN_REPORT, MNULL);
        } else {
            /* If firmware not ready, do not issue any more scan commands */
            if (pmadapter->hw_status != WlanHardwareStatusReady) {
                /* Flush all pending scan commands */
                wlan_flush_scan_queue(pmadapter);
                /* Indicate IOCTL complete */
                if (pioctl_req != MNULL) {
                    pioctl_req->status_code = MLAN_ERROR_FW_NOT_READY;

                    /* Indicate ioctl complete */
                    pcb->moal_ioctl_complete(pmadapter->pmoal_handle,
                                             (pmlan_ioctl_req) pioctl_req,
                                             MLAN_STATUS_FAILURE);
                }
            } else {
                /* Get scan command from scan_pending_q and put to
                   cmd_pending_q */
                pcmd_node =
                    (cmd_ctrl_node *) util_dequeue_list(pmadapter->pmoal_handle,
                                                        &pmadapter->
                                                        scan_pending_q,
                                                        pcb->moal_spin_lock,
                                                        pcb->moal_spin_unlock);
                wlan_insert_cmd_to_pending_q(pmadapter, pcmd_node, MTRUE);
            }
        }
    }

    LEAVE();
    return ret;
}

/**
 *  @brief This function prepares command of bg_scan_query.
 *
 *  @param pmpriv     A pointer to mlan_private structure
 *  @param pcmd       A pointer to HostCmd_DS_COMMAND structure
 *  @param pdata_buf  Void pointer cast of a wlan_scan_cmd_config struct used
 *                    to set the fields/TLVs for the command sent to firmware
 *
 *  @return           MLAN_STATUS_SUCCESS
 */
mlan_status
wlan_cmd_802_11_bg_scan_query(IN mlan_private * pmpriv,
                              IN HostCmd_DS_COMMAND * pcmd,
                              IN t_void * pdata_buf)
{
    HostCmd_DS_802_11_BG_SCAN_QUERY *bg_query = &pcmd->params.bg_scan_query;

    ENTER();

    pcmd->command = wlan_cpu_to_le16(HostCmd_CMD_802_11_BG_SCAN_QUERY);
    pcmd->size =
        wlan_cpu_to_le16(sizeof(HostCmd_DS_802_11_BG_SCAN_QUERY) + S_DS_GEN);

    bg_query->flush = MTRUE;

    LEAVE();
    return MLAN_STATUS_SUCCESS;
}

/**
 *  @brief Create a channel list for the driver to scan based on region info
 *
 *  Use the driver region/band information to construct a comprehensive list
 *    of channels to scan.  This routine is used for any scan that is not
 *    provided a specific channel list to scan.
 *
 *  @param pmpriv           A pointer to mlan_private structure
 *  @param pbg_scan_in      pointer to scan configuration parameters
 *  @param tlv_chan_list    A pointer to structure MrvlIEtypes_ChanListParamSet_t
 *
 *  @return                 channel number
 */
static t_u8
wlan_bgscan_create_channel_list(IN mlan_private * pmpriv,
                                IN const wlan_bgscan_cfg * pbg_scan_in,
                                MrvlIEtypes_ChanListParamSet_t * tlv_chan_list)
{
    mlan_adapter *pmadapter = pmpriv->adapter;
    region_chan_t *pscan_region;
    chan_freq_power_t *cfp;
    t_u32 region_idx;
    t_u32 chan_idx = 0;
    t_u32 next_chan;
    t_u8 scan_type;
    t_u8 radio_type;

    ENTER();

    for (region_idx = 0;
         region_idx < NELEMENTS(pmadapter->region_channel); region_idx++) {

        if (wlan_11d_is_enabled(pmpriv) && pmpriv->media_connected != MTRUE) {
            /* Scan all the supported chan for the first scan */
            if (!pmadapter->universal_channel[region_idx].valid)
                continue;
            pscan_region = &pmadapter->universal_channel[region_idx];
        } else {
            if (!pmadapter->region_channel[region_idx].valid)
                continue;
            pscan_region = &pmadapter->region_channel[region_idx];
        }

        if (pbg_scan_in && !pbg_scan_in->chan_list[0].chan_number &&
            pbg_scan_in->chan_list[0].radio_type & BAND_SPECIFIED) {
            radio_type = pbg_scan_in->chan_list[0].radio_type & ~BAND_SPECIFIED;
            if (!radio_type && (pscan_region->band != BAND_B) &&
                (pscan_region->band != BAND_G))
                continue;
            if (radio_type && (pscan_region->band != BAND_A))
                continue;
        }
        if (!wlan_is_band_compatible
            (pmpriv->config_bands | pmadapter->adhoc_start_band,
             pscan_region->band))
            continue;
        for (next_chan = 0;
             next_chan < pscan_region->num_cfp; next_chan++, chan_idx++) {
            if (chan_idx >= WLAN_BG_SCAN_CHAN_MAX)
                break;
            /* Set the default scan type to ACTIVE SCAN type, will later be
               changed to passive on a per channel basis if restricted by
               regulatory requirements (11d or 11h) */
            scan_type = MLAN_SCAN_TYPE_ACTIVE;
            cfp = pscan_region->pcfp + next_chan;
            if (scan_type == MLAN_SCAN_TYPE_ACTIVE
                && wlan_11d_is_enabled(pmpriv)) {
                scan_type = wlan_11d_get_scan_type(pmadapter,
                                                   pscan_region->band,
                                                   (t_u8) cfp->channel,
                                                   &pmadapter->
                                                   parsed_region_chan);
            }
            switch (pscan_region->band) {
            case BAND_A:
                tlv_chan_list->chan_scan_param[chan_idx].radio_type =
                    HostCmd_SCAN_RADIO_TYPE_A;
                if (!wlan_11d_is_enabled(pmpriv)) {
                    /* 11D not available... play it safe on DFS channels */
                    if (wlan_11h_radar_detect_required
                        (pmpriv, (t_u8) cfp->channel))
                        scan_type = MLAN_SCAN_TYPE_PASSIVE;
                }
                break;
            case BAND_B:
            case BAND_G:
            default:
                tlv_chan_list->chan_scan_param[chan_idx].radio_type =
                    HostCmd_SCAN_RADIO_TYPE_BG;
                break;
            }

            if (pbg_scan_in && pbg_scan_in->chan_list[0].scan_time) {
                tlv_chan_list->chan_scan_param[chan_idx].max_scan_time =
                    wlan_cpu_to_le16((t_u16) pbg_scan_in->chan_list[0].
                                     scan_time);
                tlv_chan_list->chan_scan_param[chan_idx].min_scan_time =
                    wlan_cpu_to_le16((t_u16) pbg_scan_in->chan_list[0].
                                     scan_time);
            } else if (scan_type == MLAN_SCAN_TYPE_PASSIVE) {
                tlv_chan_list->chan_scan_param[chan_idx].max_scan_time =
                    wlan_cpu_to_le16(pmadapter->passive_scan_time);
                tlv_chan_list->chan_scan_param[chan_idx].min_scan_time =
                    wlan_cpu_to_le16(pmadapter->passive_scan_time);
            } else {
                tlv_chan_list->chan_scan_param[chan_idx].max_scan_time =
                    wlan_cpu_to_le16(pmadapter->specific_scan_time);
                tlv_chan_list->chan_scan_param[chan_idx].min_scan_time =
                    wlan_cpu_to_le16(pmadapter->specific_scan_time);
            }

            if (scan_type == MLAN_SCAN_TYPE_PASSIVE) {
                tlv_chan_list->chan_scan_param[chan_idx].chan_scan_mode.
                    passive_scan = MTRUE;
            } else {
                tlv_chan_list->chan_scan_param[chan_idx].chan_scan_mode.
                    passive_scan = MFALSE;
            }

            tlv_chan_list->chan_scan_param[chan_idx].chan_number =
                (t_u8) cfp->channel;
            tlv_chan_list->chan_scan_param[chan_idx].chan_scan_mode.
                disable_chan_filt = MTRUE;
        }
    }

    LEAVE();
    return chan_idx;
}

/**
 *  @brief This function prepares command of bg_scan_config
 *
 *  @param pmpriv     A pointer to mlan_private structure
 *  @param pcmd       A pointer to HostCmd_DS_COMMAND structure
 *  @param pdata_buf  Void pointer cast of a wlan_scan_cmd_config struct used
 *                    to set the fields/TLVs for the command sent to firmware
 *
 *  @return           MLAN_STATUS_SUCCESS
 */
mlan_status
wlan_cmd_bgscan_config(IN mlan_private * pmpriv,
                       IN HostCmd_DS_COMMAND * pcmd, IN t_void * pdata_buf)
{
    mlan_adapter *pmadapter = pmpriv->adapter;
    HostCmd_DS_802_11_BG_SCAN_CONFIG *bg_scan = &pcmd->params.bg_scan_config;
    wlan_bgscan_cfg *bg_scan_in = (wlan_bgscan_cfg *) pdata_buf;
    t_u16 cmd_size = 0;
    MrvlIEtypes_NumProbes_t *pnum_probes_tlv = MNULL;
    MrvlIEtypes_BeaconLowRssiThreshold_t *rssi_tlv = MNULL;
    MrvlIEtypes_BeaconLowSnrThreshold_t *snr_tlv = MNULL;
    MrvlIEtypes_WildCardSsIdParamSet_t *pwildcard_ssid_tlv = MNULL;
    MrvlIEtypes_ChanListParamSet_t *tlv_chan_list = MNULL;
    t_u8 *tlv = MNULL;
    t_u16 num_probes = 0;
    t_u32 ssid_idx;
    t_u32 ssid_len = 0;
    t_u32 chan_idx;
    t_u32 chan_num;
    t_u8 radio_type;
    t_u16 scan_dur;
    t_u8 scan_type;

    ENTER();

    pcmd->command = wlan_cpu_to_le16(HostCmd_CMD_802_11_BG_SCAN_CONFIG);
    bg_scan->action = wlan_cpu_to_le16(bg_scan_in->action);
    bg_scan->enable = bg_scan_in->enable;
    bg_scan->bss_type = bg_scan_in->bss_type;
    cmd_size = sizeof(HostCmd_DS_802_11_BG_SCAN_CONFIG) + S_DS_GEN;
    if (bg_scan_in->chan_per_scan)
        bg_scan->chan_per_scan = bg_scan_in->chan_per_scan;
    else
        bg_scan->chan_per_scan = MRVDRV_MAX_CHANNELS_PER_SPECIFIC_SCAN;
    if (bg_scan_in->scan_interval)
        bg_scan->scan_interval = wlan_cpu_to_le32(bg_scan_in->scan_interval);
    else
        bg_scan->scan_interval = wlan_cpu_to_le32(DEFAULT_BGSCAN_INTERVAL);
    bg_scan->report_condition = wlan_cpu_to_le32(bg_scan_in->report_condition);

    if ((bg_scan_in->action == BG_SCAN_ACT_GET) ||
        (bg_scan_in->action == BG_SCAN_ACT_GET_PPS_UAPSD) || (!bg_scan->enable))
        goto done;

    tlv = (t_u8 *) bg_scan + sizeof(HostCmd_DS_802_11_BG_SCAN_CONFIG);
    num_probes = (bg_scan_in->num_probes ? bg_scan_in->num_probes :
                  pmadapter->scan_probes);
    if (num_probes) {
        pnum_probes_tlv = (MrvlIEtypes_NumProbes_t *) tlv;
        pnum_probes_tlv->header.type = wlan_cpu_to_le16(TLV_TYPE_NUMPROBES);
        pnum_probes_tlv->header.len =
            wlan_cpu_to_le16(sizeof(pnum_probes_tlv->num_probes));
        pnum_probes_tlv->num_probes = wlan_cpu_to_le16((t_u16) num_probes);
        tlv += sizeof(MrvlIEtypes_NumProbes_t);
        cmd_size += sizeof(MrvlIEtypes_NumProbes_t);
    }
    if (bg_scan_in->rssi_threshold) {
        rssi_tlv = (MrvlIEtypes_BeaconLowRssiThreshold_t *) tlv;
        rssi_tlv->header.type = wlan_cpu_to_le16(TLV_TYPE_RSSI_LOW);
        rssi_tlv->header.len =
            wlan_cpu_to_le16(sizeof(MrvlIEtypes_BeaconLowRssiThreshold_t) -
                             sizeof(MrvlIEtypesHeader_t));
        rssi_tlv->value = bg_scan_in->rssi_threshold;
        rssi_tlv->frequency = 0;
        tlv += sizeof(MrvlIEtypes_BeaconLowRssiThreshold_t);
        cmd_size += sizeof(MrvlIEtypes_BeaconLowRssiThreshold_t);
    }
    if (bg_scan_in->snr_threshold) {
        snr_tlv = (MrvlIEtypes_BeaconLowSnrThreshold_t *) tlv;
        snr_tlv->header.type = wlan_cpu_to_le16(TLV_TYPE_SNR_LOW);
        snr_tlv->header.len =
            wlan_cpu_to_le16(sizeof(MrvlIEtypes_BeaconLowSnrThreshold_t) -
                             sizeof(MrvlIEtypesHeader_t));
        snr_tlv->value = bg_scan_in->snr_threshold;
        snr_tlv->frequency = 0;
        tlv += sizeof(MrvlIEtypes_BeaconLowRssiThreshold_t);
        cmd_size += sizeof(MrvlIEtypes_BeaconLowRssiThreshold_t);
    }
    for (ssid_idx = 0; ((ssid_idx < NELEMENTS(bg_scan_in->ssid_list))
                        && (*bg_scan_in->ssid_list[ssid_idx].ssid ||
                            bg_scan_in->ssid_list[ssid_idx].max_len));
         ssid_idx++) {
        ssid_len = wlan_strlen((t_s8 *) bg_scan_in->ssid_list[ssid_idx].ssid);
        pwildcard_ssid_tlv = (MrvlIEtypes_WildCardSsIdParamSet_t *) tlv;
        pwildcard_ssid_tlv->header.type =
            wlan_cpu_to_le16(TLV_TYPE_WILDCARDSSID);
        pwildcard_ssid_tlv->header.len =
            (t_u16) (ssid_len + sizeof(pwildcard_ssid_tlv->max_ssid_length));
        pwildcard_ssid_tlv->max_ssid_length =
            bg_scan_in->ssid_list[ssid_idx].max_len;
        memcpy(pmadapter, pwildcard_ssid_tlv->ssid,
               bg_scan_in->ssid_list[ssid_idx].ssid, MIN(MLAN_MAX_SSID_LENGTH,
                                                         ssid_len));
        tlv +=
            sizeof(pwildcard_ssid_tlv->header) + pwildcard_ssid_tlv->header.len;
        cmd_size +=
            sizeof(pwildcard_ssid_tlv->header) + pwildcard_ssid_tlv->header.len;
        pwildcard_ssid_tlv->header.len =
            wlan_cpu_to_le16(pwildcard_ssid_tlv->header.len);
        PRINTM(MINFO, "Scan: ssid_list[%d]: %s, %d\n", ssid_idx,
               pwildcard_ssid_tlv->ssid, pwildcard_ssid_tlv->max_ssid_length);
    }
    if (bg_scan_in->chan_list[0].chan_number) {
        tlv_chan_list = (MrvlIEtypes_ChanListParamSet_t *) tlv;
        PRINTM(MINFO, "Scan: Using supplied channel list\n");
        chan_num = 0;
        for (chan_idx = 0; chan_idx < WLAN_BG_SCAN_CHAN_MAX
             && bg_scan_in->chan_list[chan_idx].chan_number; chan_idx++) {
            radio_type = bg_scan_in->chan_list[chan_idx].radio_type;
            if (!wlan_is_band_compatible
                (pmpriv->config_bands | pmadapter->adhoc_start_band,
                 radio_type_to_band(radio_type)))
                continue;
            scan_type = bg_scan_in->chan_list[chan_idx].scan_type;
            /* Prevent active scanning on a radar controlled channel */
            if (radio_type == HostCmd_SCAN_RADIO_TYPE_A) {
                if (wlan_11h_radar_detect_required
                    (pmpriv, bg_scan_in->chan_list[chan_idx].chan_number)) {
                    scan_type = MLAN_SCAN_TYPE_PASSIVE;
                }
            }
            tlv_chan_list->chan_scan_param[chan_num].chan_number =
                bg_scan_in->chan_list[chan_idx].chan_number;
            tlv_chan_list->chan_scan_param[chan_num].radio_type =
                bg_scan_in->chan_list[chan_idx].radio_type;

            if (scan_type == MLAN_SCAN_TYPE_PASSIVE) {
                tlv_chan_list->chan_scan_param[chan_num].chan_scan_mode.
                    passive_scan = MTRUE;
            } else {
                tlv_chan_list->chan_scan_param[chan_num].chan_scan_mode.
                    passive_scan = MFALSE;
            }
            if (bg_scan_in->chan_list[chan_idx].scan_time) {
                scan_dur = (t_u16) bg_scan_in->chan_list[chan_idx].scan_time;
            } else {
                if (scan_type == MLAN_SCAN_TYPE_PASSIVE) {
                    scan_dur = pmadapter->passive_scan_time;
                } else {
                    scan_dur = pmadapter->specific_scan_time;
                }
            }
            tlv_chan_list->chan_scan_param[chan_num].min_scan_time =
                wlan_cpu_to_le16(scan_dur);
            tlv_chan_list->chan_scan_param[chan_num].max_scan_time =
                wlan_cpu_to_le16(scan_dur);
            chan_num++;
        }
        tlv_chan_list->header.type = wlan_cpu_to_le16(TLV_TYPE_CHANLIST);
        tlv_chan_list->header.len =
            wlan_cpu_to_le16(sizeof(ChanScanParamSet_t) * chan_num);
        tlv +=
            sizeof(MrvlIEtypesHeader_t) + sizeof(ChanScanParamSet_t) * chan_num;
        cmd_size +=
            sizeof(MrvlIEtypesHeader_t) + sizeof(ChanScanParamSet_t) * chan_num;
    } else {
        tlv_chan_list = (MrvlIEtypes_ChanListParamSet_t *) tlv;
        chan_num =
            wlan_bgscan_create_channel_list(pmpriv, bg_scan_in, tlv_chan_list);
        tlv_chan_list->header.type = wlan_cpu_to_le16(TLV_TYPE_CHANLIST);
        tlv_chan_list->header.len =
            wlan_cpu_to_le16(sizeof(ChanScanParamSet_t) * chan_num);
        tlv +=
            sizeof(MrvlIEtypesHeader_t) + sizeof(ChanScanParamSet_t) * chan_num;
        cmd_size +=
            sizeof(MrvlIEtypesHeader_t) + sizeof(ChanScanParamSet_t) * chan_num;
    }
  done:
    pcmd->size = wlan_cpu_to_le16(cmd_size);
    LEAVE();
    return MLAN_STATUS_SUCCESS;
}

/**
 *  @brief This function handles the command response of extended scan
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param resp         A pointer to HostCmd_DS_COMMAND
 *  @param pioctl_buf   A pointer to mlan_ioctl_req structure
 *
 *  @return             MLAN_STATUS_SUCCESS or MLAN_STATUS_FAILURE
 */
mlan_status
wlan_ret_bgscan_config(IN mlan_private * pmpriv,
                       IN HostCmd_DS_COMMAND * resp,
                       IN mlan_ioctl_req * pioctl_buf)
{
    mlan_ds_scan *pscan = MNULL;
    HostCmd_DS_802_11_BG_SCAN_CONFIG *bg_scan = &resp->params.bg_scan_config;
    wlan_bgscan_cfg *bg_scan_out = MNULL;

    ENTER();
    if (pioctl_buf) {
        pscan = (mlan_ds_scan *) pioctl_buf->pbuf;
        bg_scan_out = (wlan_bgscan_cfg *) pscan->param.user_scan.scan_cfg_buf;
        bg_scan_out->action = wlan_le16_to_cpu(bg_scan->action);
        if ((bg_scan_out->action == BG_SCAN_ACT_GET) &&
            (bg_scan_out->action == BG_SCAN_ACT_GET_PPS_UAPSD)) {
            bg_scan_out->enable = bg_scan->enable;
            bg_scan_out->bss_type = bg_scan->bss_type;
            bg_scan_out->chan_per_scan = bg_scan->chan_per_scan;
            bg_scan_out->scan_interval =
                wlan_le32_to_cpu(bg_scan->scan_interval);
            bg_scan_out->report_condition =
                wlan_le32_to_cpu(bg_scan->report_condition);
            pioctl_buf->data_read_written =
                sizeof(mlan_ds_scan) + MLAN_SUB_COMMAND_SIZE;
        }
    }
    LEAVE();
    return MLAN_STATUS_SUCCESS;
}

/**
 *  @brief This function finds ssid in ssid list.
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param ssid         SSID to find in the list
 *  @param bssid        BSSID to qualify the SSID selection (if provided)
 *  @param mode         Network mode: Infrastructure or IBSS
 *
 *  @return             index in BSSID list or < 0 if error
 */
t_s32
wlan_find_ssid_in_list(IN mlan_private * pmpriv,
                       IN mlan_802_11_ssid * ssid,
                       IN t_u8 * bssid, IN t_u32 mode)
{
    mlan_adapter *pmadapter = pmpriv->adapter;
    t_s32 net = -1, j;
    t_u8 best_rssi = 0;
    t_u32 i;

    ENTER();
    PRINTM(MINFO, "Num of entries in scan table = %d\n",
           pmadapter->num_in_scan_table);

    /*
     * Loop through the table until the maximum is reached or until a match
     *   is found based on the bssid field comparison
     */
    for (i = 0;
         i < pmadapter->num_in_scan_table && (!bssid || (bssid && net < 0));
         i++) {
        if (!wlan_ssid_cmp(pmadapter, &pmadapter->pscan_table[i].ssid, ssid) &&
            (!bssid
             || !memcmp(pmadapter, pmadapter->pscan_table[i].mac_address, bssid,
                        MLAN_MAC_ADDR_LENGTH))) {

            if (((mode == MLAN_BSS_MODE_INFRA) &&
                 !wlan_is_band_compatible(pmadapter->config_bands,
                                          pmadapter->pscan_table[i].bss_band))
                ||
                (wlan_find_cfp_by_band_and_channel
                 (pmadapter, (t_u8) pmadapter->pscan_table[i].bss_band,
                  (t_u16) pmadapter->pscan_table[i].channel) == MNULL)) {
                continue;
            }

            switch (mode) {
            case MLAN_BSS_MODE_INFRA:
            case MLAN_BSS_MODE_IBSS:
                j = wlan_is_network_compatible(pmpriv, i, mode);

                if (j >= 0) {
                    if (SCAN_RSSI(pmadapter->pscan_table[i].rssi) > best_rssi) {
                        best_rssi = SCAN_RSSI(pmadapter->pscan_table[i].rssi);
                        net = i;
                    }
                } else {
                    if (net == -1) {
                        net = j;
                    }
                }
                break;
            case MLAN_BSS_MODE_AUTO:
            default:
                /*
                 * Do not check compatibility if the mode requested is
                 *   Auto/Unknown.  Allows generic find to work without
                 *   verifying against the Adapter security settings
                 */
                if (SCAN_RSSI(pmadapter->pscan_table[i].rssi) > best_rssi) {
                    best_rssi = SCAN_RSSI(pmadapter->pscan_table[i].rssi);
                    net = i;
                }
                break;
            }
        }
    }

    LEAVE();
    return net;
}

/**
 *  @brief This function finds a specific compatible BSSID in the scan list
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param bssid        BSSID to find in the scan list
 *  @param mode         Network mode: Infrastructure or IBSS
 *
 *  @return             index in BSSID list or < 0 if error
 */
t_s32
wlan_find_bssid_in_list(IN mlan_private * pmpriv,
                        IN t_u8 * bssid, IN t_u32 mode)
{
    mlan_adapter *pmadapter = pmpriv->adapter;
    t_s32 net = -1;
    t_u32 i;

    ENTER();

    if (!bssid) {
        LEAVE();
        return -1;
    }

    PRINTM(MINFO, "FindBSSID: Num of BSSIDs = %d\n",
           pmadapter->num_in_scan_table);

    /*
     * Look through the scan table for a compatible match. The ret return
     *   variable will be equal to the index in the scan table (greater
     *   than zero) if the network is compatible.  The loop will continue
     *   past a matched bssid that is not compatible in case there is an
     *   AP with multiple SSIDs assigned to the same BSSID
     */
    for (i = 0; net < 0 && i < pmadapter->num_in_scan_table; i++) {
        if (!memcmp
            (pmadapter, pmadapter->pscan_table[i].mac_address, bssid,
             MLAN_MAC_ADDR_LENGTH)) {
            if (((mode == MLAN_BSS_MODE_INFRA) &&
                 !wlan_is_band_compatible(pmadapter->config_bands,
                                          pmadapter->pscan_table[i].bss_band))
                ||
                (wlan_find_cfp_by_band_and_channel
                 (pmadapter, (t_u8) pmadapter->pscan_table[i].bss_band,
                  (t_u16) pmadapter->pscan_table[i].channel) == MNULL)) {
                continue;
            }
            switch (mode) {
            case MLAN_BSS_MODE_INFRA:
            case MLAN_BSS_MODE_IBSS:
                net = wlan_is_network_compatible(pmpriv, i, mode);
                break;
            default:
                net = i;
                break;
            }
        }
    }

    LEAVE();
    return net;
}

/**
 *  @brief Compare two SSIDs
 *
 *  @param pmadapter A pointer to mlan_adapter structure
 *  @param ssid1     A pointer to ssid to compare
 *  @param ssid2     A pointer to ssid to compare
 *
 *  @return         0--ssid is same, otherwise is different
 */
t_s32
wlan_ssid_cmp(IN pmlan_adapter pmadapter,
              IN mlan_802_11_ssid * ssid1, IN mlan_802_11_ssid * ssid2)
{
    ENTER();

    if (!ssid1 || !ssid2) {
        LEAVE();
        return -1;
    }

    if (ssid1->ssid_len != ssid2->ssid_len) {
        LEAVE();
        return -1;
    }

    LEAVE();
    return memcmp(pmadapter, ssid1->ssid, ssid2->ssid, ssid1->ssid_len);
}

/**
 *  @brief This function inserts scan command node to scan_pending_q.
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param pcmd_node    A pointer to cmd_ctrl_node structure
 *  @return             N/A
 */
t_void
wlan_queue_scan_cmd(IN mlan_private * pmpriv, IN cmd_ctrl_node * pcmd_node)
{
    mlan_adapter *pmadapter = pmpriv->adapter;

    ENTER();

    if (pcmd_node == MNULL)
        goto done;
    util_enqueue_list_tail(pmadapter->pmoal_handle, &pmadapter->scan_pending_q,
                           (pmlan_linked_list) pcmd_node,
                           pmadapter->callbacks.moal_spin_lock,
                           pmadapter->callbacks.moal_spin_unlock);

  done:
    LEAVE();
}

/**
 *  @brief Find the AP with specific ssid in the scan list
 *
 *  @param pmpriv               A pointer to mlan_private structure
 *  @param preq_ssid_bssid      A pointer to AP's ssid returned
 *
 *  @return                     MLAN_STATUS_SUCCESS--success, otherwise--fail
 */
mlan_status
wlan_find_best_network(IN mlan_private * pmpriv,
                       OUT mlan_ssid_bssid * preq_ssid_bssid)
{
    mlan_adapter *pmadapter = pmpriv->adapter;
    mlan_status ret = MLAN_STATUS_SUCCESS;
    BSSDescriptor_t *preq_bss;
    t_s32 i;

    ENTER();

    memset(pmadapter, preq_ssid_bssid, 0, sizeof(mlan_ssid_bssid));

    i = wlan_find_best_network_in_list(pmpriv);

    if (i >= 0) {
        preq_bss = &pmadapter->pscan_table[i];
        memcpy(pmadapter, &preq_ssid_bssid->ssid, &preq_bss->ssid,
               sizeof(mlan_802_11_ssid));
        memcpy(pmadapter, (t_u8 *) & preq_ssid_bssid->bssid,
               (t_u8 *) & preq_bss->mac_address, MLAN_MAC_ADDR_LENGTH);

        /* Make sure we are in the right mode */
        if (pmpriv->bss_mode == MLAN_BSS_MODE_AUTO)
            pmpriv->bss_mode = preq_bss->bss_mode;
    }

    if (!preq_ssid_bssid->ssid.ssid_len) {
        ret = MLAN_STATUS_FAILURE;
        goto done;
    }

    PRINTM(MINFO, "Best network found = [%s], "
           "[%02x:%02x:%02x:%02x:%02x:%02x]\n",
           preq_ssid_bssid->ssid.ssid,
           preq_ssid_bssid->bssid[0], preq_ssid_bssid->bssid[1],
           preq_ssid_bssid->bssid[2], preq_ssid_bssid->bssid[3],
           preq_ssid_bssid->bssid[4], preq_ssid_bssid->bssid[5]);

  done:
    LEAVE();
    return ret;
}

/**
 *  @brief Send a scan command for all available channels filtered on a spec
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param pioctl_buf   A pointer to MLAN IOCTL Request buffer
 *  @param preq_ssid    A pointer to AP's ssid returned
 *
 *  @return             MLAN_STATUS_SUCCESS--success, otherwise--fail
 */
mlan_status
wlan_scan_specific_ssid(IN mlan_private * pmpriv,
                        IN t_void * pioctl_buf, IN mlan_802_11_ssid * preq_ssid)
{
    mlan_status ret = MLAN_STATUS_SUCCESS;
    mlan_callbacks *pcb = (mlan_callbacks *) & pmpriv->adapter->callbacks;
    wlan_user_scan_cfg *pscan_cfg;
    pmlan_ioctl_req pioctl_req = (mlan_ioctl_req *) pioctl_buf;

    ENTER();

    if (!preq_ssid) {
        if (pioctl_req)
            pioctl_req->status_code = MLAN_ERROR_CMD_SCAN_FAIL;
        ret = MLAN_STATUS_FAILURE;
        goto done;
    }

    wlan_scan_delete_ssid_table_entry(pmpriv, preq_ssid);

    ret =
        pcb->moal_malloc(pmpriv->adapter->pmoal_handle,
                         sizeof(wlan_user_scan_cfg), MLAN_MEM_DEF,
                         (t_u8 **) & pscan_cfg);

    if (ret != MLAN_STATUS_SUCCESS || !pscan_cfg) {
        PRINTM(MERROR, "Memory allocation for pscan_cfg failed!\n");
        if (pioctl_req)
            pioctl_req->status_code = MLAN_ERROR_NO_MEM;
        ret = MLAN_STATUS_FAILURE;
        goto done;
    }

    memset(pmpriv->adapter, pscan_cfg, 0x00, sizeof(wlan_user_scan_cfg));

    memcpy(pmpriv->adapter, pscan_cfg->ssid_list[0].ssid,
           preq_ssid->ssid, preq_ssid->ssid_len);
    pscan_cfg->keep_previous_scan = MTRUE;

    ret = wlan_scan_networks(pmpriv, pioctl_buf, pscan_cfg);

    if (pscan_cfg)
        pcb->moal_mfree(pmpriv->adapter->pmoal_handle, (t_u8 *) pscan_cfg);

  done:
    LEAVE();
    return ret;
}

/**
 *  @brief Save a beacon buffer of the current bss descriptor
 *  Save the current beacon buffer to restore in the following cases that
 *  makes the bcn_buf not to contain the current ssid's beacon buffer.
 *    - the current ssid was not found somehow in the last scan.
 *    - the current ssid was the last entry of the scan table and overloaded.
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *
 *  @return             N/A
 */
t_void
wlan_save_curr_bcn(IN mlan_private * pmpriv)
{
    mlan_adapter *pmadapter = pmpriv->adapter;
    mlan_callbacks *pcb = (pmlan_callbacks) & pmadapter->callbacks;
    BSSDescriptor_t *pcurr_bss = &pmpriv->curr_bss_params.bss_descriptor;
    mlan_status ret = MLAN_STATUS_SUCCESS;

    ENTER();
    /* save the beacon buffer if it is not saved or updated */
    if ((pmpriv->pcurr_bcn_buf == MNULL) ||
        (pmpriv->curr_bcn_size != pcurr_bss->beacon_buf_size) ||
        (memcmp(pmpriv->adapter, pmpriv->pcurr_bcn_buf, pcurr_bss->pbeacon_buf,
                pcurr_bss->beacon_buf_size))) {

        if (pmpriv->pcurr_bcn_buf) {
            pcb->moal_mfree(pmadapter->pmoal_handle, pmpriv->pcurr_bcn_buf);
            pmpriv->pcurr_bcn_buf = MNULL;
        }
        pmpriv->curr_bcn_size = pcurr_bss->beacon_buf_size;

        if (pmpriv->curr_bcn_size) {
            ret =
                pcb->moal_malloc(pmadapter->pmoal_handle,
                                 pcurr_bss->beacon_buf_size, MLAN_MEM_DEF,
                                 &pmpriv->pcurr_bcn_buf);

            if ((ret == MLAN_STATUS_SUCCESS) && pmpriv->pcurr_bcn_buf) {
                memcpy(pmpriv->adapter, pmpriv->pcurr_bcn_buf,
                       pcurr_bss->pbeacon_buf, pcurr_bss->beacon_buf_size);
                PRINTM(MINFO, "current beacon saved %d\n",
                       pmpriv->curr_bcn_size);
            }
        }
    }
    LEAVE();
}

/**
 *  @brief Free a beacon buffer of the current bss descriptor
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *
 *  @return             N/A
 */
t_void
wlan_free_curr_bcn(IN mlan_private * pmpriv)
{
    mlan_adapter *pmadapter = pmpriv->adapter;
    mlan_callbacks *pcb = (pmlan_callbacks) & pmadapter->callbacks;

    ENTER();
    if (pmpriv->pcurr_bcn_buf) {
        pcb->moal_mfree(pmadapter->pmoal_handle, pmpriv->pcurr_bcn_buf);
        pmpriv->pcurr_bcn_buf = MNULL;
    }
    LEAVE();
}