summaryrefslogtreecommitdiff
path: root/network/wlan/WDI/COMMON/CustomizedScan.c
blob: 50b788e8aa07b9868b89e3ce1074d7cd243ef6cd (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
#include "Mp_Precomp.h"

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

#include "CustomizedScan.h"

static const char *info_sig = "CUSTOM_SCAN";
static const char *req_sig = "CUSTOM_SCAN_REQ";

#define CUSTOM_SCAN_MAX_TIME_WAIT_SCAN_STARTED_MS 300
#define CUSTOM_SCAN_WAIT_LINK_DONE_MS 500

#define CUSTOM_SCAN_PROCESSING_REQ(__info) (NULL != (__info)->pCurReq && !TEST_FLAG((__info)->pCurReq->flag, CUSTOM_SCAN_REQ_FLAG_TO_BE_FREED))

#define CUSTOM_SCAN_REQ_FLAG_REQUEUED					BIT0
#define CUSTOM_SCAN_REQ_FLAG_RESCHEDULED				BIT2
#define CUSTOM_SCAN_REQ_FLAG_RESET						BIT3
#define CUSTOM_SCAN_REQ_FLAG_TO_BE_FREED				BIT4
#define CUSTOM_SCAN_REQ_FLAG_POTENTIAL_REPEATE_COUNT_UNDERFLOW BIT5
#define CUSTOM_SCAN_REQ_FLAG_STARTED					BIT6
#define CUSTOM_SCAN_REQ_FLAG_TERMINATE					BIT7
#define CUSTOM_SCAN_REQ_FLAG_FLUSH						BIT8

#define CUSTOM_SCAN_TERM_SCAN_BUF_MS					20
#define CUSTOM_SCAN_ELAPSE_TIME(__req) (PlatformGetCurrentTime() / 1000 - (__req)->issueTime)
#define CUSTOM_SCAN_ALLOW_TIME(__req) (\
				CUSTOM_SCAN_TERM_SCAN_BUF_MS < (__req)->timeBound\
				? (__req)->timeBound - CUSTOM_SCAN_TERM_SCAN_BUF_MS\
				: (__req)->timeBound\
				)
				
#define CUSTOM_SCAN_TIME_DUE(__req) (0 < (__req)->timeBound && CUSTOM_SCAN_ALLOW_TIME(__req) <= CUSTOM_SCAN_ELAPSE_TIME(__req))

#define CHNL_ENTRY_EXT_FLAG_CHNL_SHUT					BIT0

typedef struct _CHNL_ENTRY_EXT
{
	RT_LIST_ENTRY				list;
	
	// super
	RT_CHNL_LIST_ENTRY			super;

	// ext
	FRAME_BUF					*probeReq;
	u1Byte						dataRate;
	u4Byte						flag; // init in customscan_InitChnlEntryExt and reset when requeued
}CHNL_ENTRY_EXT;

typedef struct _CUSTOM_SCAN_REQ
{
	RT_LIST_ENTRY				list;

	const char					*sig;

	u4Byte						flag;

	// source type
	CUSTOM_SCAN_SRC_TYPE		type;
	const char					*typeInfo;

	// state
	CUSTOM_SCAN_STATE			state;
	u8Byte						stateTime;

	// force passive scan 
	BOOLEAN						bForcePassive;
	
	// probe req
	FRAME_BUF					probeReqBuf;
	u1Byte						probeReqRsvd[CUSTOMIZED_SCAN_MAX_FRAME_LEN];

	// channels to scan, cnt is 0 if default scan
	RT_LIST_ENTRY				chnlListQ;
	u4Byte						chnlListCnt;

	// chnl entry pool
	POOL						chnlEntryPool;
	CHNL_ENTRY_EXT				chnlEntryPoolRsvd[MAX_SCAN_CHANNEL_NUM * 2];

	// status callback
	VOID						*cbCtx;
	CUSTOM_SCAN_STATE_CB		cb;

	// dbg
	u8Byte						dbgComp;
	u4Byte						dbgLevel;

	// extended dwell time, reset after applied
	u2Byte						extDwellTime;

	// info of the channel we are on, n/a if default scan
	CHNL_ENTRY_EXT				*curChnl;

	// time bound, 0 if not bounded
	u4Byte						timeBound;

	// time stamps
	u8Byte						issueTime;

	// time in ms to delay before the worker process it
	u4Byte						delayStartMs;

	// time in ms to delay before repeat a req
	u4Byte						repeatIntermittentMs;

	// repeat count
	u4Byte						repeatCount;

	// on probe rsp cb
	CUSTOM_SCAN_ON_PROBE_RSP_CB	onProbeRspCb;
	VOID						*onProbeRspCtx;

}CUSTOM_SCAN_REQ;

typedef struct _CUSTOM_SCAN_INFO
{
	DECLARE_RT_OBJECT(_CUSTOM_SCAN_INFO);

	const char					*sig;

	// the corresp. adapter
	ADAPTER						*pAdapter;

	// request queue
	RT_LIST_ENTRY				reqQ;
	u4Byte						reqCnt;

	// request pool
	POOL						reqPool;
	CUSTOM_SCAN_REQ				reqPoolRsvd[CUSTOMIZED_SCAN_MAX_REQ_NUM];

	// the currently processing req
	CUSTOM_SCAN_REQ				*pCurReq;

	// dbg
	u8Byte						dbgComp;
	u4Byte						dbgLevel;

	// the worker thread
	RT_THREAD					worker;

	// temporary field for constructing scan list
	RT_CHNL_LIST_ENTRY			*scanChnlList[MAX_SCAN_CHANNEL_NUM];
	u1Byte						scanChnlListLen;

	BOOLEAN						bWorkerExited;
	
}CUSTOM_SCAN_INFO;

//-----------------------------------------------------------------------------
// Local
//-----------------------------------------------------------------------------

static
VOID
customscan_Lock(
	IN  CUSTOM_SCAN_INFO		*info
	)
{
	PlatformAcquireSpinLock(info->pAdapter, RT_CUSTOM_SCAN_SPINLOCK);
}

static
VOID
customscan_Unlock(
	IN  CUSTOM_SCAN_INFO		*info
	)
{
	PlatformReleaseSpinLock(info->pAdapter, RT_CUSTOM_SCAN_SPINLOCK);
}

static
VOID
customscan_SetReqState(
	IN  CUSTOM_SCAN_INFO		*info,
	IN  CUSTOM_SCAN_REQ			*req,
	IN  CUSTOM_SCAN_STATE		state
	)
{
	req->state = state;
	req->stateTime = PlatformGetCurrentTime();
	if(req->cb)
	{
		(req->cb)(state, req->cbCtx);
	}
}

static
VOID
customscan_InitChnlEntryExt(
	IN  CHNL_ENTRY_EXT			*pExt,
	IN  const RT_CHNL_LIST_ENTRY *pChnl,
	IN  FRAME_BUF				*probeReq,
	IN  u1Byte					dataRate
	)
{
	RTInitializeListHead(&pExt->list);
	pExt->super = *pChnl;
	pExt->probeReq = probeReq;
	pExt->dataRate = dataRate;
	pExt->flag = 0;

	return;
}

static
VOID
customscan_InitReq(
	IN  CUSTOM_SCAN_INFO		*info,
	IN  CUSTOM_SCAN_REQ			*req
	)
{
	PlatformZeroMemory(req, sizeof(*req));
	
	RTInitializeListHead(&req->list);
	req->sig = req_sig;
	req->flag = 0;
	req->type = CUSTOM_SCAN_SRC_TYPE_UNSPECIFIED;
	req->state = CUSTOM_SCAN_STATE_IDLE;
	FrameBuf_Init(sizeof(req->probeReqRsvd), 0, req->probeReqRsvd, &req->probeReqBuf);
	
	RTInitializeListHead(&req->chnlListQ);
	req->chnlListCnt = 0;

	Pool_Init(&req->chnlEntryPool, "chnl entry pool", sizeof(req->chnlEntryPoolRsvd), 
		req->chnlEntryPoolRsvd, sizeof(req->chnlEntryPoolRsvd[0]), 
		BIT45, DBG_LOUD);

	req->cbCtx = NULL;
	req->cb = NULL;

	req->dbgComp = BIT45;
	req->dbgLevel = DBG_LOUD;

	req->extDwellTime = 0;
	req->curChnl = NULL;

	req->repeatCount = 1;
	
	return;
}

static
VOID
customscan_DescructReq(
	IN  CUSTOM_SCAN_INFO		*info,
	IN  CUSTOM_SCAN_REQ			*req
	)
{
	while(RTIsListNotEmpty(&req->chnlListQ))
	{
		RT_LIST_ENTRY *pEntry = RTRemoveTailListWithCnt(&req->chnlListQ, &req->chnlListCnt);

		Pool_Release(&req->chnlEntryPool, pEntry);
	}

	return;
}

static
VOID
customscan_FreeReq(
	IN	CUSTOM_SCAN_INFO		*info,
	IN	CUSTOM_SCAN_REQ 		*req
	)
{
	RT_TRACE(COMP_SCAN, DBG_LOUD, ("freeing: %s\n", req->typeInfo));
	customscan_DescructReq(info, req);
	Pool_Release(&info->reqPool, req);

	Pool_Dump(&info->reqPool);

	return;
}

static
VOID
customscan_FreeCurReq(
	IN	CUSTOM_SCAN_INFO		*info
	)
{
	customscan_FreeReq(info, info->pCurReq);
	info->pCurReq = NULL;

	return;
}

static
VOID
customscan_FlushReqQ(
	IN  CUSTOM_SCAN_INFO		*info
	)
{
	RT_LIST_ENTRY				*pEntry = NULL;

	while(RTIsListNotEmpty(&info->reqQ))
	{
		pEntry = RTRemoveTailListWithCnt(&info->reqQ, &info->reqCnt);
		customscan_Unlock(info);
		customscan_SetReqState(info, (CUSTOM_SCAN_REQ *)pEntry, CUSTOM_SCAN_STATE_PRE_DESTROY); // so client has a chance to cleanup
		customscan_Lock(info);
		SET_FLAG(((CUSTOM_SCAN_REQ *)pEntry)->flag, CUSTOM_SCAN_REQ_FLAG_FLUSH);
		customscan_FreeReq(info, (CUSTOM_SCAN_REQ *)pEntry);
	}

	return;
}

static
RT_CHNL_LIST_ENTRY *
customscan_GetScanChnlInDrvChnlList(
	IN  RT_CHNL_LIST_ENTRY		**pChnlList,
	IN  u1Byte					chnlListLen,
	IN  u1Byte					chnl
	)
{
	u4Byte						itChnl = 0;
	
	for(itChnl = 0; itChnl < chnlListLen; itChnl++)
	{
		if(pChnlList[itChnl]->ChannelNum == chnl)
			return pChnlList[itChnl];
	}

	return NULL;
}

static
const char *
customscan_ScanStateTxt(
	IN  CUSTOM_SCAN_STATE		state
	)
{
	switch (state) 
	{
	case CUSTOM_SCAN_STATE_IDLE:
		return "idle";
	case CUSTOM_SCAN_STATE_WAITING:
		return "waiting";
	case CUSTOM_SCAN_STATE_STARTING:
		return "starting";
	case CUSTOM_SCAN_STATE_STARTED:
		return "started";
	case CUSTOM_SCAN_STATE_SW_CHNL:
		return "swChnl";
	case CUSTOM_SCAN_STATE_DWELL:
		return "dwell";
	case CUSTOM_SCAN_STATE_COMPLETED:
		return "completed";
	case CUSTOM_SCAN_STATE_PRE_DESTROY:
		return "pre-destroy";
	case CUSTOM_SCAN_STATE_DEFERRED:
		return "deferred";
	case CUSTOM_SCAN_STATE_TERMINATING:
		return "terminating";
	default:
		return "Unknown?!";
	}
}

static
const char *
customscan_SrcTypeTxt(
	IN  CUSTOM_SCAN_SRC_TYPE		type
	)
{
	switch (type) 
	{
	case CUSTOM_SCAN_SRC_TYPE_SYS:
		return "sys";
	case CUSTOM_SCAN_SRC_TYPE_BT:
		return "bt";
	case CUSTOM_SCAN_SRC_TYPE_P2P:
		return "p2p";
	case CUSTOM_SCAN_SRC_TYPE_UNSPECIFIED:
		return "unspecified";
	default:
		return "Unknown?!";
	}
}

static
VOID
customscan_DumpChnlExt(
	IN  const CHNL_ENTRY_EXT	*chnlExt,
	IN  u8Byte					dbgComp,
	IN  u4Byte					dbgLevel
	)
{
	RT_TRACE(COMP_SCAN, DBG_LOUD, ("chnl: %u, active: %u, duration: %u, flag: 0x%08X\n", 
		chnlExt->super.ChannelNum, 
		SCAN_ACTIVE == chnlExt->super.ScanType, 
		chnlExt->super.ScanPeriod,
		chnlExt->flag
		));
}

static
VOID
customscan_DumpReq(
	IN  const CUSTOM_SCAN_REQ	*req
	)
{
	RT_LIST_ENTRY				*pEntry = NULL;

	RT_TRACE(COMP_SCAN, DBG_LOUD, ("type: %s\n", customscan_SrcTypeTxt(req->type)));
	RT_TRACE(COMP_SCAN, DBG_LOUD, ("type info: %s\n", req->typeInfo));
	RT_TRACE(COMP_SCAN, DBG_LOUD, ("state: %s\n", customscan_ScanStateTxt(req->state)));
	RT_TRACE(COMP_SCAN, DBG_LOUD, ("sig: %s\n", req->sig));
	FrameBuf_Dump(&req->probeReqBuf, BIT45, DBG_LOUD, "ProbeReqBuf");
	RT_TRACE(COMP_SCAN, req->dbgLevel, ("chnl list:\n"));
	RtEntryListForEach(&req->chnlListQ, pEntry)
	{
		customscan_DumpChnlExt((CHNL_ENTRY_EXT *)pEntry, BIT45, DBG_LOUD);
	}
	RT_TRACE(COMP_SCAN, DBG_LOUD, ("timeBound: %u\n", req->timeBound));
	RT_TRACE(COMP_SCAN, DBG_LOUD, ("delayStartMs: %u\n", req->delayStartMs));
	RT_TRACE(COMP_SCAN, DBG_LOUD, ("repeatIntermittentMs: %u\n", req->repeatIntermittentMs));
	RT_TRACE(COMP_SCAN, DBG_LOUD, ("repeatCount: %u\n", req->repeatCount));
	RT_TRACE(COMP_SCAN, DBG_LOUD, ("cb: %p\n", req->cb));
	RT_TRACE(COMP_SCAN, DBG_LOUD, ("ctx: %p\n", req->cbCtx));

	RT_TRACE(COMP_SCAN, DBG_LOUD, ("flag: 0x%08X\n", req->flag));
	RT_TRACE(COMP_SCAN, DBG_LOUD, ("state: %s\n", customscan_ScanStateTxt(req->state)));
	RT_TRACE(COMP_SCAN, DBG_LOUD, ("time in state: %u\n", (u4Byte)(PlatformGetCurrentTime() - req->stateTime)));

	return;
}

static
VOID
customscan_DumpReqQ(
	IN  const CUSTOM_SCAN_INFO	*info
	)
{
	RT_LIST_ENTRY				*pEntry = NULL;

	RT_TRACE(COMP_SCAN, DBG_LOUD, ("reqQ: %u\n", info->reqCnt));
	
	RtEntryListForEach(&info->reqQ, pEntry)
	{
		RT_TRACE(COMP_SCAN, DBG_LOUD, ("req:\n"));
		customscan_DumpReq((CUSTOM_SCAN_REQ *)pEntry);
	}

	return;
}

static
VOID
customscan_DumpReqPool(
	IN  const CUSTOM_SCAN_INFO	*info
	)
{
	const CUSTOM_SCAN_REQ		*pEntry = NULL;
	size_t						poolSize = sizeof(info->reqPoolRsvd) / sizeof(info->reqPoolRsvd[0]);
	size_t						it = 0;

	// Prefast warning C6328: Size mismatch ignore
#pragma warning (disable: 6328)
	RT_TRACE(COMP_SCAN, DBG_LOUD, ("req pool size: %u\n", poolSize));

	for(it = 0; it < poolSize; it++)
	{
		RT_TRACE(COMP_SCAN, DBG_LOUD, ("req:\n"));
		pEntry = &info->reqPoolRsvd[it];
		customscan_DumpReq(pEntry);
	}

	return;
}

static
RT_STATUS
customscan_TerminateCurrentChnl(
	IN  CUSTOM_SCAN_INFO		*info
	)
{
	ADAPTER						*pAdapter = info->pAdapter;

	if(!CUSTOM_SCAN_PROCESSING_REQ(info))
		return RT_STATUS_INVALID_STATE;

	RT_TRACE(COMP_SCAN, DBG_LOUD, 
		("req: %s, state: %s\n", 
		info->pCurReq->typeInfo,
		customscan_ScanStateTxt(info->pCurReq->state)));
	
	if(CUSTOM_SCAN_STATE_DWELL == info->pCurReq->state)
	{
		RT_TRACE(COMP_SCAN, DBG_LOUD, ("terminating cur req on chnl\n"));

		customscan_Unlock(info);
		if(IS_DUAL_BAND_SUPPORT(pAdapter))
			pAdapter->MgntInfo.bDualModeScanStep = 2;
		PlatformCancelTimer(pAdapter, &pAdapter->MgntInfo.ScanTimer);
		PlatformSetTimer(pAdapter, &pAdapter->MgntInfo.ScanTimer, 0);
		customscan_Lock(info);
	}

	return RT_STATUS_SUCCESS;
}

static
BOOLEAN
customscan_FreeDanglingReq(
	IN	CUSTOM_SCAN_INFO		*info
	)
{
	BOOLEAN						bFreed = FALSE;

	do
	{
		if(!info->pCurReq)
			break;

		if(!TEST_FLAG(info->pCurReq->flag, CUSTOM_SCAN_REQ_FLAG_RESET))
			break;

		// the scan is reset, ScanComplete for the req may/may not entered
		// however if ScanComplete entered, we can't get here since the req wold have been freed
		RT_TRACE(COMP_SCAN, DBG_WARNING, ("freeing dangling req: %s got reset and ScanComplete not enter, free it and continue\n", info->pCurReq->typeInfo));
		customscan_Unlock(info);
		customscan_SetReqState(info, info->pCurReq, CUSTOM_SCAN_STATE_PRE_DESTROY);
		customscan_Lock(info);
		customscan_FreeCurReq(info);

		bFreed = TRUE;
	}while(FALSE);
	
	return bFreed;
}

static
RT_STATUS
customscan_TerminateCurrentReq(
	IN  CUSTOM_SCAN_INFO		*info,
	IN  BOOLEAN					bStopOnCurChnl
	)
{
	ADAPTER						*pAdapter = info->pAdapter;
	RT_CHNL_LIST_ENTRY 			*pChnlEntry = NULL;

	if(!CUSTOM_SCAN_PROCESSING_REQ(info))
		return RT_STATUS_INVALID_STATE;

	RT_TRACE(COMP_SCAN, DBG_LOUD, 
		("req: %s, state: %s, ScanStep: %u\n", 
			info->pCurReq->typeInfo,
			customscan_ScanStateTxt(info->pCurReq->state),
			pAdapter->MgntInfo.ScanStep
		)
	);
	
	if(CUSTOM_SCAN_STATE_SW_CHNL == info->pCurReq->state
		|| CUSTOM_SCAN_STATE_DWELL == info->pCurReq->state
		|| CUSTOM_SCAN_STATE_STARTED == info->pCurReq->state
		|| CUSTOM_SCAN_STATE_STARTING == info->pCurReq->state
		)
	{// doing scan callback
		while(RtActChannelList(pAdapter, RT_CHNL_LIST_ACTION_POP_SCAN_CHANNEL, NULL, &pChnlEntry)){}

		if(bStopOnCurChnl)
		{
			ADAPTER 			*pda = GetDefaultAdapter(pAdapter);
			ADAPTER 			*pla = pda;
			u1Byte				curChnl = RT_GetChannelNumber(pla);				
			EXTCHNL_OFFSET		hwBW40MOffset = EXTCHNL_OFFSET_NO_EXT;
			EXTCHNL_OFFSET		hwBW80MOffset = EXTCHNL_OFFSET_NO_EXT;
	
			pAdapter->HalFunc.GetHwRegHandler(pAdapter, HW_VAR_BW40MHZ_EXTCHNL, (pu1Byte)(&hwBW40MOffset));
			pAdapter->HalFunc.GetHwRegHandler(pAdapter, HW_VAR_BW80MHZ_EXTCHNL, (pu1Byte)(&hwBW80MOffset));

			while(pla)
			{
				pla->MgntInfo.SettingBeforeScan.ChannelNumber = curChnl;

				pda->MgntInfo.SettingBeforeScan.Ext20MHzChnlOffsetOf40MHz = hwBW40MOffset;
				pda->MgntInfo.SettingBeforeScan.Ext40MHzChnlOffsetOf80MHz = hwBW80MOffset;

				if(EXTCHNL_OFFSET_NO_EXT != hwBW80MOffset)
				{
					pda->MgntInfo.SettingBeforeScan.ChannelBandwidth = CHANNEL_WIDTH_80;
				}
				else if(EXTCHNL_OFFSET_NO_EXT != hwBW40MOffset)
				{
					pda->MgntInfo.SettingBeforeScan.ChannelBandwidth = CHANNEL_WIDTH_40;
				}
				else
				{
					pda->MgntInfo.SettingBeforeScan.ChannelBandwidth = CHANNEL_WIDTH_20;
				}
				
				pla->MgntInfo.SettingBeforeScan.CenterFrequencyIndex1 = CHNL_GetCenterFrequency(
							pda->MgntInfo.SettingBeforeScan.ChannelNumber,
							pda->MgntInfo.SettingBeforeScan.ChannelBandwidth,
							pda->MgntInfo.SettingBeforeScan.Ext20MHzChnlOffsetOf40MHz);			
				pla = GetNextExtAdapter(pla);
			}
		
			RT_TRACE(COMP_SCAN, DBG_LOUD, ("to stop on chnl: %u\n", curChnl));
		}

		customscan_SetReqState(info, info->pCurReq, CUSTOM_SCAN_STATE_TERMINATING);
		SET_FLAG(info->pCurReq->flag, CUSTOM_SCAN_REQ_FLAG_TERMINATE);

		RT_TRACE(COMP_SCAN, DBG_LOUD, ("cur req terminated\n"));

		customscan_Unlock(info);
		pAdapter->MgntInfo.ScanStep = 0;
		if(IS_DUAL_BAND_SUPPORT(pAdapter))
			pAdapter->MgntInfo.bDualModeScanStep = 2;
		PlatformCancelTimer(pAdapter, &pAdapter->MgntInfo.ScanTimer);
		PlatformSetTimer(pAdapter, &pAdapter->MgntInfo.ScanTimer, 0);
		customscan_Lock(info);
	}
	else if(CUSTOM_SCAN_STATE_COMPLETED == info->pCurReq->state)
	{// doing scan complete
	}
	else if(CUSTOM_SCAN_STATE_TERMINATING == info->pCurReq->state
		&& TEST_FLAG(info->pCurReq->flag, CUSTOM_SCAN_REQ_FLAG_RESET)
		)
	{
		// Previously in terminating state because of scan reset 
		// and now the req is to be terminated again (aborted by OS) 
		// before ScanComplete is invoked.
		RT_TRACE(COMP_SCAN, DBG_LOUD, ("%s(): req %s is in terminating state and reset flag set, free it\n", __FUNCTION__, info->pCurReq->typeInfo));
		customscan_FreeDanglingReq(info);
	}
	
	else
	{
		RT_ASSERT(TRUE, ("invalid req state: %s\n", customscan_ScanStateTxt(info->pCurReq->state)));
		return RT_STATUS_FAILURE;
	}

	return RT_STATUS_SUCCESS;
}

static
VOID
customscan_RequeueCurReq(
	IN  CUSTOM_SCAN_INFO		*info
	)
{
	size_t						it = 0;
	
	FunctionIn(COMP_SCAN);
	
	customscan_SetReqState(info, info->pCurReq, CUSTOM_SCAN_STATE_DEFERRED);
	SET_FLAG(info->pCurReq->flag, CUSTOM_SCAN_REQ_FLAG_REQUEUED);
	for(it = 0; 
		it < sizeof(info->pCurReq->chnlEntryPoolRsvd) / sizeof(info->pCurReq->chnlEntryPoolRsvd[0]); 
		it++
		)
	{
		info->pCurReq->chnlEntryPoolRsvd[it].flag = 0;
	}
	info->pCurReq->curChnl = NULL;
	
	RTInsertHeadListWithCnt(&info->reqQ, &info->pCurReq->list, &info->reqCnt);
	info->pCurReq = NULL;

	FunctionOut(COMP_SCAN);
}

static
BOOLEAN
customscan_LinkInProgress(
	IN  CUSTOM_SCAN_INFO		*info
	)
{
	BOOLEAN						bLinkInProgress = FALSE;
	ADAPTER 					*pla = NULL;

	for(pla = GetDefaultAdapter(info->pAdapter);
		NULL != pla;
		pla = GetNextExtAdapter(pla)
		)
	{
		if(MgntIsLinkInProgress(&pla->MgntInfo))
		{
			bLinkInProgress = TRUE;
			break;
		}
	}

	return bLinkInProgress;
}

static
BOOLEAN
customscan_InvokeMgntLinkReq(
	IN  CUSTOM_SCAN_INFO		*info,
	IN  BOOLEAN					bForcePassive
	)
{
	BOOLEAN						bScanStarted = FALSE;
	
	//
	// Formerly, system scan comes from N6CSet_DOT11_SCAN_REQUEST and may
	// be called directly down to ScanCallback if timeout specified is 0,
	// and if scan is rejected by MgntLinkRequest (or the function it calls)
	// N6CSet_DOT11_SCAN_REQUEST does not try to handle this.
	// So for N6CSet_DOT11_SCAN_REQUEST, it is safe to simply call 
	// MgntLinkRequest here and don't care about indicate scan complete at 
	// all.
	// 
	
	MgntLinkRequest(
			info->pAdapter,
			TRUE,		//bScanOnly
			bForcePassive ? FALSE : TRUE, //bActiveScan,
			FALSE,		//FilterHiddenAP // Asked by Netgear's Lancelot for 8187 should look like their damn wg111v1, 2005.02.01, by rcnjko.
			FALSE,		// Update parameters
			NULL,		//ssid2scan
			0,			//NetworkType,
			0,			//ChannelNumber,
			0,			//BcnPeriod,
			0,			//DtimPeriod,
			0,			//mCap,
			NULL,		//SuppRateSet,
			NULL		//yIbpm,
			);

	//
	// Note: the following code handles the cases scan is not actually started
	// 	and we need to free the custom scan req.
	// 
	// pCurReq may be NULL:
	// 	After MgntLinkRequest, pCurReq may have been freed by the scan time check
	// 	mechanism (pMgntInfo->bCheckScanTime). In this case, the req is safely 
	// 	freed (or requeued if repeat count is not 0) in CustomScan_ScanCompleteCb.
	//
	// MgntLinkRequest rescheduled:
	// 	If MgntLinkRequest is rescheduled via MgntLinkRequestReschedule, it 
	// 	will be called again when RF is turned on in InactivePsTimerCallback.
	// 	In this case, we leave the custom scan req as the cur req and it will 
	// 	be activated again when RF is on.
	//
	// If scan request is rejected by MgntLinkRequest non rescheduled cases or
	// ScanByTimer, we need to free the request here.
	// 
	// If scan request is rejected by ScanCallback (and note that the scan timer
	// is not set again when returned), such as MgntResetOrPnPInProgress, we are
	// not able to detect that here, and the state of the custom scan req is left
	// as starting and will be recycled by the worker if the state time exceeds 
	// CUSTOM_SCAN_MAX_TIME_WAIT_SCAN_STARTED_MS.
	//

	customscan_Lock(info);
	if(info->pCurReq)
	{
		if(CUSTOM_SCAN_STATE_STARTED <= info->pCurReq->state)
		{
			bScanStarted = TRUE;
		}
		else
		{// ScanByTimer or its callers reject the scan request
			if(TEST_FLAG(info->pCurReq->flag, CUSTOM_SCAN_REQ_FLAG_RESCHEDULED))
			{// MgntLinkRequest shall be called again when RF is turned on
				// in this case, scan is "scheduled" to be started when RF is on, so return true
				bScanStarted = TRUE;
			}
			else
			{// other cases such as hw/sw radio off
				RT_TRACE(COMP_SCAN, DBG_LOUD, ("scan did not started and not rescheduled\n"));				customscan_Unlock(info);
				customscan_SetReqState(info, info->pCurReq, CUSTOM_SCAN_STATE_PRE_DESTROY);
				customscan_Lock(info);
				customscan_FreeCurReq(info);
			}
		}
	}
	customscan_Unlock(info);

	return bScanStarted;
}

static
VOID
customscan_WorkerCb(
	IN  VOID 					*pCtx
	)
{
	RT_THREAD					*threadCtx = (RT_THREAD *)pCtx;
	CUSTOM_SCAN_INFO			*info = (CUSTOM_SCAN_INFO *)threadCtx->pContext;
	u4Byte						bDelayMs = 0;
	BOOLEAN						bForcePassive = FALSE;

	FunctionIn(COMP_SCAN);

	RT_ASSERT(pCtx, ("%s(): pCtx is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(info_sig == info->sig, ("%s(): invalid info\n", __FUNCTION__));

	if(MgntScanInProgress(&info->pAdapter->MgntInfo))
	{
		RT_TRACE(COMP_SCAN, DBG_LOUD, ("skip for there's ongoing req\n"));
		FunctionOut(COMP_SCAN);
		return;
	}

	if(customscan_LinkInProgress(info))
	{
		RT_TRACE(COMP_SCAN, DBG_LOUD, 
			("skip and wait %u ms for link in progress\n", 
			CUSTOM_SCAN_WAIT_LINK_DONE_MS));
		PlatformSleepUs(CUSTOM_SCAN_WAIT_LINK_DONE_MS * 1000);
		PlatformSetEventToTrigerThread(info->pAdapter, &info->worker);
		FunctionOut(COMP_SCAN);
		return;
	}

	customscan_Lock(info);

	if(info->pCurReq)
	{
		if(TEST_FLAG(info->pCurReq->flag, CUSTOM_SCAN_REQ_FLAG_RESET))
		{
			RT_TRACE(COMP_SCAN, DBG_LOUD, ("%s(): req %s is in terminating state and reset flag set, free it\n", __FUNCTION__, info->pCurReq->typeInfo));

			customscan_FreeDanglingReq(info);
			// to continue processing req in queue
		}
		else
		{
			if(CUSTOM_SCAN_STATE_STARTING == info->pCurReq->state)
			{
				if(CUSTOM_SCAN_MAX_TIME_WAIT_SCAN_STARTED_MS < PlatformGetCurrentTime() - info->pCurReq->stateTime)
				{
					RT_TRACE(COMP_SCAN, DBG_WARNING, ("requeue req: %s for failed to start\n", info->pCurReq->typeInfo));
					customscan_FreeCurReq(info);
				}
				customscan_Unlock(info);
				FunctionOut(COMP_SCAN);
				return;
			}

			if(CUSTOM_SCAN_STATE_PRE_DESTROY == info->pCurReq->state)
			{
				RT_TRACE(COMP_SCAN, DBG_WARNING, ("cur req: %s is in its pre destroy state\n", info->pCurReq->typeInfo));
				customscan_Unlock(info);
				FunctionOut(COMP_SCAN);
				return;
			}	
		}
	}

	// There's ongoing custom req when scan in progress is false
	if(info->pCurReq)
	{
		if(TEST_FLAG(info->pCurReq->flag, CUSTOM_SCAN_REQ_FLAG_TERMINATE))
		{// to wait until it is freed
			RT_TRACE(COMP_SCAN, DBG_LOUD, ("req %s is being terminated, not to start another one\n", info->pCurReq->typeInfo));
		}
		else
		{
			RT_ASSERT(NULL == info->pCurReq, 
				("%s(): there's ongoing custom req %s when scan in progress is false, state: %s\n", 
				__FUNCTION__, info->pCurReq->typeInfo, customscan_ScanStateTxt(info->pCurReq->state)));
		}

		customscan_Unlock(info);
		FunctionOut(COMP_SCAN);
		return;
	}
	
	// Check if any request queued
	if(!info->reqCnt)
	{
		customscan_Unlock(info);
		FunctionOut(COMP_SCAN);
		return;
	}

	// Make curReq ready so it is available in the callbacks
	info->pCurReq = (CUSTOM_SCAN_REQ *)RTRemoveHeadListWithCnt(&info->reqQ, &info->reqCnt);
	RT_ASSERT(CUSTOM_SCAN_STATE_WAITING == info->pCurReq->state || CUSTOM_SCAN_STATE_DEFERRED == info->pCurReq->state, 
		("%s(): invalid req state: %s\n", __FUNCTION__, customscan_ScanStateTxt(info->pCurReq->state)));
	customscan_SetReqState(info, info->pCurReq, CUSTOM_SCAN_STATE_STARTING);

	RT_TRACE(COMP_SCAN, DBG_LOUD, ("starting req: %s\n", info->pCurReq->typeInfo));

	if(!TEST_FLAG(info->pCurReq->flag, CUSTOM_SCAN_REQ_FLAG_REQUEUED))
	{
		if(info->pCurReq->delayStartMs)
			bDelayMs = info->pCurReq->delayStartMs;
	}
	else
	{
		if(info->pCurReq->repeatIntermittentMs)
			bDelayMs = info->pCurReq->repeatIntermittentMs;
	}

	bForcePassive = info->pCurReq->bForcePassive;
	
	customscan_Unlock(info);

	if(bDelayMs)
		PlatformSleepUs(bDelayMs * 1000);

	customscan_InvokeMgntLinkReq(info, bForcePassive);

	FunctionOut(COMP_SCAN);
	
	return;
}

static
VOID
customscan_PreWorkerExitCb(
	IN  VOID 					*pCtx
	)
{
	RT_THREAD					*threadCtx = (RT_THREAD *)pCtx;
	CUSTOM_SCAN_INFO			*info = (CUSTOM_SCAN_INFO *)threadCtx->pContext;

	RT_ASSERT(pCtx, ("%s(): pCtx is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(info_sig == info->sig, ("%s(): invalid info\n", __FUNCTION__));

	info->bWorkerExited = TRUE;
	
	return;
}

//-----------------------------------------------------------------------------
// Driver callback
//-----------------------------------------------------------------------------

//
// Description:
//		This function handles reset request when scan in progress is not set.
//
VOID
CustomScan_ResetReqNoScanCb(
	IN  VOID					*pInfo
	)
{
	CUSTOM_SCAN_INFO			*info = (CUSTOM_SCAN_INFO *)pInfo;
	ADAPTER						*pAdapter = info->pAdapter;

	RT_ASSERT(pInfo, ("%s(): pInfo is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(info_sig == info->sig, ("%s(): invalid info\n", __FUNCTION__));
	
	if(MgntScanInProgress(&pAdapter->MgntInfo))
	{
		return;
	}

	customscan_Lock(info);

	// since we are doing reset, flush all queued reqs
	customscan_FlushReqQ(info);
	
	if(CUSTOM_SCAN_PROCESSING_REQ(info) 
		&& CUSTOM_SCAN_STATE_STARTING == info->pCurReq->state
		&& TEST_FLAG(info->pCurReq->flag, CUSTOM_SCAN_REQ_FLAG_RESCHEDULED)
		)
	{
		// A rescheduled scan req still waiting for RF on, we free it here, otherwise:
		// 	* If there's a subsequent connect request, the return point would be changed to 
		//	  IPS_CALLBACK_JOIN_REQUEST and when subsequent scan is issued, CustomScan_IssueSysScan
		// 	  would think that the driver is doing custom scan and return media busy. In addition,
		//    JoinRequest may issue scan and that scan would be associated with the rescheduled 
		//    custom scan request and that is wrong.
		// *  If there's no connect request, the scan would continue when RF is on and that's not
		//    desiarble since we shall stop scan when receive a reset request.
		RT_TRACE(COMP_SCAN, DBG_WARNING, ("rescheduled scan %s got reset\n", info->pCurReq->typeInfo));
		customscan_Unlock(info);
		customscan_SetReqState(info, info->pCurReq, CUSTOM_SCAN_STATE_PRE_DESTROY);
		customscan_Lock(info);
		customscan_FreeCurReq(info);
	}
	
	customscan_Unlock(info);
	
	return;
}

VOID
CustomScan_ScanResetCb(
	IN  VOID					*pInfo
	)
{
	CUSTOM_SCAN_INFO			*info = (CUSTOM_SCAN_INFO *)pInfo;

	RT_ASSERT(pInfo, ("%s(): pInfo is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(info_sig == info->sig, ("%s(): invalid info\n", __FUNCTION__));


	customscan_Lock(info);

	RT_TRACE(COMP_SCAN, DBG_WARNING, ("scan got reset\n"));

	customscan_DumpReqQ(info);
	if(CUSTOM_SCAN_PROCESSING_REQ(info))
	{
		RT_TRACE(COMP_SCAN, DBG_WARNING, ("cur req:\n"));
		customscan_DumpReq(info->pCurReq);
	}
	
	do
	{
		// since we are doing reset, flush all queued reqs
		customscan_FlushReqQ(info);

		// mark cur req as reset
		if(CUSTOM_SCAN_PROCESSING_REQ(info))
		{
			SET_FLAG(info->pCurReq->flag, CUSTOM_SCAN_REQ_FLAG_RESET);
			
			if(CUSTOM_SCAN_STATE_STARTING == info->pCurReq->state
				|| CUSTOM_SCAN_STATE_STARTED == info->pCurReq->state
				|| CUSTOM_SCAN_STATE_SW_CHNL == info->pCurReq->state
				|| CUSTOM_SCAN_STATE_DWELL == info->pCurReq->state
				)
			{// can't guarantee that ScanComplete for this req will be invoked
				RT_TRACE(COMP_SCAN, DBG_LOUD, ("req %s got reset\n", info->pCurReq->typeInfo));
				customscan_SetReqState(info, info->pCurReq, CUSTOM_SCAN_STATE_TERMINATING);
			}
		}		
	}while(FALSE);
	
	customscan_Unlock(info);
	
	return;
}

VOID
CustomScan_ScanByTimerCb(
	IN  VOID					*pInfo
	)
{
	CUSTOM_SCAN_INFO			*info = (CUSTOM_SCAN_INFO *)pInfo;

	RT_ASSERT(pInfo, ("%s(): pInfo is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(info_sig == info->sig, ("%s(): invalid info\n", __FUNCTION__));


	customscan_Lock(info);

	do
	{
		if(!CUSTOM_SCAN_PROCESSING_REQ(info))
		{
			RT_TRACE(COMP_SCAN, DBG_LOUD, ("pCurReq is NULL!!! return\n"));
			break;
		}

		if(CUSTOM_SCAN_STATE_STARTING == info->pCurReq->state)
		{
			customscan_SetReqState(info, info->pCurReq, CUSTOM_SCAN_STATE_STARTED);
			SET_FLAG(info->pCurReq->flag, CUSTOM_SCAN_REQ_FLAG_STARTED);
			RT_TRACE(COMP_SCAN, DBG_LOUD, ("req issued: %s\n", info->pCurReq->typeInfo));
		}
	}while(FALSE);
	
	customscan_Unlock(info);
	
	return;
}

BOOLEAN
CustomScan_ConstructScanListCb(
	IN  VOID					*pInfo,
	IN  RT_CHANNEL_LIST			*pChnlList
	)
{
	CUSTOM_SCAN_INFO			*info = (CUSTOM_SCAN_INFO *)pInfo;
	u4Byte						itChnl = 0;
	RT_LIST_ENTRY				*pEntry = NULL;
	BOOLEAN						bSysScan = FALSE;

	RT_ASSERT(pInfo, ("%s(): pInfo is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(info_sig == info->sig, ("%s(): invalid info\n", __FUNCTION__));

	// Not to acquire lock because
	// 	1. it is not necessary since constructing scan list is already an exclusive action
	// 	2. it may cause deadlock  
	//customscan_Lock(info);

	do
	{
		if(!CUSTOM_SCAN_PROCESSING_REQ(info))
		{
			RT_TRACE(COMP_SCAN, DBG_LOUD, ("pCurReq is NULL!!! return\n"));
			bSysScan = TRUE;
			break;
		}

		// Make a copy of the channel list
		info->scanChnlListLen = pChnlList->ScanChannelListLength;
		for(itChnl = 0; itChnl < pChnlList->ScanChannelListLength; itChnl++)
			info->scanChnlList[itChnl] = pChnlList->ScanChannelList[itChnl];

		// Reset driver channel list
		pChnlList->ScanChannelListLength = 0;

		// Reconstruct the channel list
		RtEntryListForEach(&info->pCurReq->chnlListQ, pEntry)
		{
			CHNL_ENTRY_EXT			*chnl = (CHNL_ENTRY_EXT *)pEntry;
			RT_CHNL_LIST_ENTRY		*chnlEntry = NULL;

			chnlEntry = customscan_GetScanChnlInDrvChnlList(info->scanChnlList, info->scanChnlListLen, chnl->super.ChannelNum);

			if(!chnlEntry)
				continue;

			// if client specifies mix, follow driver
			if(SCAN_MIX == chnl->super.ScanType)
				chnl->super.ScanType = chnlEntry->ScanType;

			// if driver specifies passive, we shall use passive
			if(SCAN_PASSIVE == chnlEntry->ScanType)
				chnl->super.ScanType = SCAN_PASSIVE;

			if(0 == chnl->super.ScanPeriod)
				chnl->super.ScanPeriod = chnlEntry->ScanPeriod;

			chnl->super.MaxTxPwrDbm = chnlEntry->MaxTxPwrDbm;
			chnl->super.ExInfo = chnlEntry->ExInfo;

			if(pChnlList->ScanChannelListLength < MAX_SCAN_CHANNEL_NUM)
			{
				pChnlList->ScanChannelList[pChnlList->ScanChannelListLength++] = &chnl->super;
			}
			else
			{
				RT_TRACE(COMP_SCAN, DBG_WARNING, ("unable to insert ch %u for scan\n", chnl->super.ChannelNum));
			}
		}

		bSysScan = (CUSTOM_SCAN_SRC_TYPE_SYS == info->pCurReq->type);
		
	}while(FALSE);

	//customscan_Unlock(info);

	return bSysScan;
}

u2Byte
CustomScan_PreSwChnlCb(
	IN  VOID					*pInfo,
	IN  RT_CHNL_LIST_ENTRY		*chnl
	)
{
	u2Byte						extDwellTime = 0;
	CUSTOM_SCAN_INFO			*info = (CUSTOM_SCAN_INFO *)pInfo;
	
	RT_ASSERT(pInfo, ("%s(): pInfo is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(info_sig == info->sig, ("%s(): invalid info\n", __FUNCTION__));

	customscan_Lock(info);
	
	do
	{
		if(!CUSTOM_SCAN_PROCESSING_REQ(info))
		{
			RT_TRACE(COMP_SCAN, DBG_LOUD, ("pCurReq is NULL!!! return\n"));
			break;
		}

		if(info->pCurReq->extDwellTime)
		{
			extDwellTime = info->pCurReq->extDwellTime;
			info->pCurReq->extDwellTime = 0;
			RT_TRACE(COMP_SCAN, DBG_LOUD, ("dwell time extended to: %u\n", extDwellTime));
		}
		customscan_SetReqState(info, info->pCurReq, CUSTOM_SCAN_STATE_SW_CHNL);

		RT_TRACE(COMP_SCAN, DBG_LOUD, 
		("%s: to sw from: %u to %u\n", 
		info->pCurReq->typeInfo,
			info->pCurReq->curChnl ? info->pCurReq->curChnl->super.ChannelNum : 0, 
			chnl->ChannelNum));

		info->pCurReq->curChnl = (CHNL_ENTRY_EXT *)CONTAINING_RECORD(chnl, CHNL_ENTRY_EXT, super);
		
	}while(FALSE);
	customscan_Unlock(info);

	return extDwellTime;
}

VOID
CustomScan_OnChnlCb(
	IN  VOID					*pInfo,
	IN  RT_CHNL_LIST_ENTRY		*chnl
	)
{
	CUSTOM_SCAN_INFO			*info = (CUSTOM_SCAN_INFO *)pInfo;

	RT_ASSERT(pInfo, ("%s(): pInfo is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(info_sig == info->sig, ("%s(): invalid info\n", __FUNCTION__));

	customscan_Lock(info);	

	do
	{
		if(!CUSTOM_SCAN_PROCESSING_REQ(info))
		{
			RT_TRACE(COMP_SCAN, DBG_LOUD, ("pCurReq is NULL!!! return\n"));
			break;
		}

		RT_TRACE(COMP_SCAN, DBG_TRACE, ("on chnl for req %s: %u for %u ms\n", info->pCurReq->typeInfo, chnl->ChannelNum, chnl->ScanPeriod));

		customscan_SetReqState(info, info->pCurReq, CUSTOM_SCAN_STATE_DWELL);

		if(info->pCurReq->curChnl)
		{
			RT_TRACE(COMP_SCAN, DBG_LOUD, ("flag: %u\n", info->pCurReq->curChnl->flag));
		}
		
		if(CUSTOM_SCAN_TIME_DUE(info->pCurReq))
		{
			customscan_TerminateCurrentReq(info, FALSE);
		}
	}while(FALSE);
	
	customscan_Unlock(info);

	return;
}

BOOLEAN
CustomScan_PreSetDwellTimerCb(
	IN  VOID					*pInfo,
	IN  RT_CHNL_LIST_ENTRY		*chnl
	)
{
	CUSTOM_SCAN_INFO			*info = (CUSTOM_SCAN_INFO *)pInfo;
	BOOLEAN						bSetDwellTimer = TRUE;

	RT_ASSERT(pInfo, ("%s(): pInfo is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(info_sig == info->sig, ("%s(): invalid info\n", __FUNCTION__));

	customscan_Lock(info);	

	do
	{
		if(!CUSTOM_SCAN_PROCESSING_REQ(info))
		{
			RT_TRACE(COMP_SCAN, DBG_LOUD, ("pCurReq is NULL!!! return\n"));
			break;
		}

		if(info->pCurReq->curChnl && TEST_FLAG(info->pCurReq->curChnl->flag, CHNL_ENTRY_EXT_FLAG_CHNL_SHUT))
		{
			RT_TRACE(COMP_SCAN, DBG_LOUD, ("shutting cur chnl: %u, not to set dwell timer\n", info->pCurReq->curChnl->super.ChannelNum));
			bSetDwellTimer = FALSE;
		}

	}while(FALSE);
	
	customscan_Unlock(info);

	return bSetDwellTimer;
}

BOOLEAN
CustomScan_SendProbeCb(
	IN  VOID					*pInfo,
	IN  RT_CHNL_LIST_ENTRY		*chnl
	)
{
	CUSTOM_SCAN_INFO			*info = (CUSTOM_SCAN_INFO *)pInfo;
	CHNL_ENTRY_EXT				*chnlExt = NULL;
	ADAPTER						*pAdapter = NULL;

	RT_TCB						*pTcb = NULL;
	RT_TX_LOCAL_BUFFER 			*pBuf = NULL;

	BOOLEAN						bContinue = TRUE;
	BOOLEAN						bSendProbe = FALSE;

	RT_ASSERT(pInfo, ("%s(): pInfo is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(info_sig == info->sig, ("%s(): invalid info\n", __FUNCTION__));

	customscan_Lock(info);

	do
	{
		if(!CUSTOM_SCAN_PROCESSING_REQ(info))
		{
			RT_TRACE(COMP_SCAN, DBG_TRACE, ("pCurReq is NULL!!! return\n"));
			break;
		}

		RT_ASSERT((VOID *)info->pCurReq->chnlEntryPool.start <= (VOID *)chnl 
			&& (VOID *)chnl < (VOID *)info->pCurReq->chnlEntryPool.end, 
			("%s(): chnl is not the customized one\n", __FUNCTION__));

		chnlExt = (CHNL_ENTRY_EXT *)CONTAINING_RECORD(chnl, CHNL_ENTRY_EXT, super);
		pAdapter = info->pAdapter;

		if(!chnlExt->probeReq) // didn't specify probe to send, use default
		{
			RT_TRACE(COMP_SCAN, DBG_TRACE, ("chnlExt->probeReq is NULL!!! return\n"));
			break;
		}

		bContinue = FALSE;

		if(0 == FrameBuf_Length(chnlExt->probeReq)) // probe specified but length is 0, to send nothing
		{
			RT_TRACE(COMP_SCAN, DBG_LOUD, ("0 == FrameBuf_Length(chnlExt->probeReq)!!! return\n"));
			break;
		}

		bSendProbe = TRUE;
	}while(FALSE);

	customscan_Unlock(info);

	if(bSendProbe)
	{// send probe w/o having custom scan lock acquired to prevent deadlock
		PlatformAcquireSpinLock(info->pAdapter, RT_TX_SPINLOCK);

		if(MgntGetBuffer(pAdapter, &pTcb, &pBuf))
		{
			PlatformMoveMemory(pBuf->Buffer.VirtualAddress, 
				FrameBuf_MHead(chnlExt->probeReq), 
				FrameBuf_Length(chnlExt->probeReq));
			pTcb->PacketLength = FrameBuf_Length(chnlExt->probeReq);
				
			MgntSendPacket(pAdapter, pTcb, pBuf, pTcb->PacketLength, NORMAL_QUEUE, chnlExt->dataRate);
		}

		PlatformReleaseSpinLock(info->pAdapter, RT_TX_SPINLOCK);	
	}
	
	return bContinue;
}

BOOLEAN
CustomScan_DualBandScanCb(
	IN  VOID					*pInfo
	)
{
	CUSTOM_SCAN_INFO			*info = (CUSTOM_SCAN_INFO *)pInfo;
	BOOLEAN						terminated = FALSE;

	RT_ASSERT(pInfo, ("%s(): pInfo is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(info_sig == info->sig, ("%s(): invalid info\n", __FUNCTION__));


	customscan_Lock(info);

	do{
		if(!CUSTOM_SCAN_PROCESSING_REQ(info))
		{
			RT_TRACE(COMP_SCAN, DBG_LOUD, ("pCurReq is NULL!!! return\n"));
			break;
		}

		RT_TRACE(COMP_SCAN, DBG_LOUD, ("state: %s\n", customscan_ScanStateTxt(info->pCurReq->state)));
		
		if(CUSTOM_SCAN_STATE_TERMINATING == info->pCurReq->state)
		{
			RT_CHNL_LIST_ENTRY 		*pChnlEntry = NULL;		
			
			while(RtActChannelList(info->pAdapter, RT_CHNL_LIST_ACTION_POP_SCAN_CHANNEL, NULL, &pChnlEntry)){}
			terminated = TRUE;
		}

		if(CUSTOM_SCAN_TIME_DUE(info->pCurReq))
		{
			customscan_TerminateCurrentReq(info, FALSE);
		}
	}while(FALSE);
	
	customscan_Unlock(info);

	return terminated;
}

VOID
CustomScan_PreScanCompleteCb(
	IN  VOID					*pInfo
	)
{
	CUSTOM_SCAN_INFO			*info = (CUSTOM_SCAN_INFO *)pInfo;

	RT_ASSERT(pInfo, ("%s(): pInfo is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(info_sig == info->sig, ("%s(): invalid info\n", __FUNCTION__));

	RT_TRACE(COMP_SCAN, DBG_LOUD, ("\n"));

	if(!CUSTOM_SCAN_PROCESSING_REQ(info))
	{
		RT_TRACE(COMP_SCAN, DBG_LOUD, ("pCurReq is NULL!!! return\n"));
		return;
	}

	return;
}

VOID
CustomScan_ScanCompleteCb(
	IN  VOID					*pInfo
	)
{
	CUSTOM_SCAN_INFO			*info = (CUSTOM_SCAN_INFO *)pInfo;

	RT_ASSERT(pInfo, ("%s(): pInfo is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(info_sig == info->sig, ("%s(): invalid info\n", __FUNCTION__));

	RT_TRACE(COMP_SCAN, DBG_LOUD, ("\n"));

	customscan_Lock(info);

	if(CUSTOM_SCAN_PROCESSING_REQ(info))
	{// Note that scan spinlock is acquired
		if(info->pCurReq->repeatCount)
		{
			info->pCurReq->repeatCount--;

			RT_TRACE(COMP_SCAN, DBG_LOUD, 
				("repeatCount: %u, state: %s\n", 
				info->pCurReq->repeatCount, customscan_ScanStateTxt(info->pCurReq->state)));
			
			if(info->pCurReq->repeatCount
				&& CUSTOM_SCAN_STATE_TERMINATING != info->pCurReq->state
				)
			{
				customscan_RequeueCurReq(info);
			}
			else
			{
				RT_TRACE(COMP_SCAN, DBG_LOUD, ("sending completed event to: %s\n", info->pCurReq->typeInfo));
				customscan_SetReqState(info, info->pCurReq, CUSTOM_SCAN_STATE_COMPLETED);
			}
			RT_TRACE(COMP_SCAN, DBG_LOUD, ("scan complete: sw back to chnl: %u\n", info->pAdapter->MgntInfo.SettingBeforeScan.ChannelNumber));
		}
		else
		{
			RT_TRACE(COMP_SCAN, DBG_WARNING, 
				("current req's repeat count reaches 0 already and it is not freed and scan complete is invoked again\n"));
			SET_FLAG(info->pCurReq->flag, CUSTOM_SCAN_REQ_FLAG_POTENTIAL_REPEATE_COUNT_UNDERFLOW);
		}
	}

	customscan_Unlock(info);
	
	return;
}

VOID
CustomScan_ScanCompleteReturnCb(
	IN  VOID					*pInfo
	)
{
	CUSTOM_SCAN_INFO			*info = (CUSTOM_SCAN_INFO *)pInfo;
	BOOLEAN						bProcessingReq = FALSE;
	CUSTOM_SCAN_REQ				*curReq = NULL;

	RT_ASSERT(pInfo, ("%s(): pInfo is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(info_sig == info->sig, ("%s(): invalid info\n", __FUNCTION__));

	FunctionIn(COMP_SCAN);
	
	customscan_Lock(info);
	if(CUSTOM_SCAN_PROCESSING_REQ(info))
	{
		bProcessingReq = TRUE;
		curReq = info->pCurReq;
		SET_FLAG(info->pCurReq->flag, CUSTOM_SCAN_REQ_FLAG_TO_BE_FREED);
		RT_TRACE(COMP_SCAN, DBG_LOUD, ("going to free: %s\n", curReq->typeInfo));
	}
	customscan_Unlock(info);

	// Note that pCurReq may be changed by the worker after unlock,
	// this happens when direct OID is used

	if(bProcessingReq)
	{
		RT_TRACE(COMP_SCAN, DBG_LOUD, ("sending pre-destroy event to: %s\n", curReq->typeInfo));
		customscan_SetReqState(info, curReq, CUSTOM_SCAN_STATE_PRE_DESTROY);
	}

	customscan_Lock(info);

	if(bProcessingReq)
	{
		if(info->pCurReq == curReq)
			customscan_FreeCurReq(info);
		else
			customscan_FreeReq(info, curReq);
	}

	// Trigger thread when ScanComplete has done everything it needs to do
	if(info->reqCnt)
	{
		PlatformSetEventToTrigerThread(info->pAdapter, &info->worker);	
	}

	customscan_Unlock(info);
	
	FunctionOut(COMP_SCAN);
	
	return;
}

VOID
CustomScan_RescheduleCb(
	IN  VOID					*pInfo
	)
{
	CUSTOM_SCAN_INFO			*info = (CUSTOM_SCAN_INFO *)pInfo;

	RT_ASSERT(pInfo, ("%s(): pInfo is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(info_sig == info->sig, ("%s(): invalid info\n", __FUNCTION__));

	customscan_Lock(info);
	
	do
	{
		if(!CUSTOM_SCAN_PROCESSING_REQ(info))
		{
			RT_TRACE(COMP_SCAN, DBG_LOUD, ("pCurReq is NULL!!! return\n"));
			break;
		}

		RT_TRACE(COMP_SCAN, DBG_LOUD, ("\n"));


		SET_FLAG(info->pCurReq->flag, CUSTOM_SCAN_REQ_FLAG_RESCHEDULED);
	}while(FALSE);
	customscan_Unlock(info);
	
	return;
}

VOID
CustomScan_WatchDogCb(
	IN  VOID					*pInfo
	)
{
	CUSTOM_SCAN_INFO			*info = (CUSTOM_SCAN_INFO *)pInfo;

	if(NULL == info || info_sig != info->sig)
	{// not initialized yet
		return;
	}
	
	if(info->bWorkerExited)
	{
		info->bWorkerExited = FALSE;

		// Note that this shall be done in passive level
		PlatformSetEventTrigerThread(info->pAdapter, &info->worker, PASSIVE_LEVEL, pInfo);
	}
}

VOID
CustomScan_OnProbeRspCb(
	IN  VOID					*pInfo,
	IN	RT_RFD					*pRfd
	)
{
	CUSTOM_SCAN_INFO			*info = (CUSTOM_SCAN_INFO *)pInfo;

	RT_ASSERT(pInfo, ("%s(): pInfo is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(info_sig == info->sig, ("%s(): invalid info\n", __FUNCTION__));

	customscan_Lock(info);

	if(CUSTOM_SCAN_PROCESSING_REQ(info))
	{
		if(info->pCurReq->onProbeRspCb)
			info->pCurReq->onProbeRspCb(
				info->pCurReq->state,
				info->pCurReq->onProbeRspCtx,
				pRfd
				);
	}

	customscan_Unlock(info);
	
	return;
}

//-----------------------------------------------------------------------------
// Public
//-----------------------------------------------------------------------------
VOID *
CustomScan_AllocInfo(
	IN  ADAPTER					*pAdapter
	)
{
	CUSTOM_SCAN_INFO			*info = NULL;
	MGNT_INFO					*pMgntInfo = NULL;

	RT_ASSERT(pAdapter, ("%s(): pAdapter is NULL!!!\n", __FUNCTION__));

	FunctionIn(COMP_SCAN);

	pMgntInfo = &pAdapter->MgntInfo;
	pMgntInfo->pCustomScanInfo = NULL;
	
	if(RT_STATUS_SUCCESS != PlatformAllocateMemory(pAdapter, (PVOID *)&info, sizeof(*info)))
	{
		return NULL;
	}

	pMgntInfo->pCustomScanInfo = info;

	return info;
}

VOID
CustomScan_FreeInfo(
	IN  VOID					*pInfo
	)
{
	CUSTOM_SCAN_INFO			*info = (CUSTOM_SCAN_INFO *)pInfo;

	RT_ASSERT(pInfo, ("%s(): pInfo is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(info_sig == info->sig, ("%s(): invalid info\n", __FUNCTION__));

	PlatformFreeMemory(info, sizeof(*info));

	return;
}

BOOLEAN
CustomScan_InProgress(
	IN  VOID					*pInfo
	)
{
	CUSTOM_SCAN_INFO			*info = (CUSTOM_SCAN_INFO *)pInfo;

	RT_ASSERT(pInfo, ("%s(): pInfo is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(info_sig == info->sig, ("%s(): invalid info\n", __FUNCTION__));

	if(!CUSTOM_SCAN_PROCESSING_REQ(info))
		return FALSE;

	if(CUSTOM_SCAN_PROCESSING_REQ(info) && CUSTOM_SCAN_SRC_TYPE_SYS != info->pCurReq->type)
		return TRUE;

	return FALSE;
}

VOID
CustomScan_Init(
	IN  VOID					*pInfo,
	IN  ADAPTER					*pScanAdapter,
	IN  u8Byte					dbgComp,
	IN  u4Byte					dbgLevel
	)
{
	CUSTOM_SCAN_INFO			*info = (CUSTOM_SCAN_INFO *)pInfo;
	
	RT_ASSERT(pInfo, ("%s(): pInfo is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(pScanAdapter, ("%s(): pScanAdapter is NULL!!!\n", __FUNCTION__));

	FunctionIn(COMP_SCAN);

	PlatformZeroMemory(info, sizeof(*info));
 
	info->sig = info_sig;
	info->pAdapter = pScanAdapter;
	RTInitializeListHead(&info->reqQ);
	info->reqCnt = 0;
	Pool_Init(&info->reqPool, "req pool", sizeof(info->reqPoolRsvd), 
		info->reqPoolRsvd, sizeof(info->reqPoolRsvd[0]), 
		dbgComp, dbgLevel);
	info->pCurReq = NULL;
	info->dbgComp = dbgComp;
	info->dbgLevel = dbgLevel;

	PlatformInitializeSpinLock(pScanAdapter, RT_CUSTOM_SCAN_SPINLOCK);

	RT_TRACE(COMP_SCAN, DBG_LOUD, ("ctx: %p\n", pInfo));

	info->bWorkerExited = FALSE;

	FunctionOut(COMP_SCAN);

	return;
}

VOID
CustomScan_Start(
	IN  VOID					*pInfo
	)
{
	CUSTOM_SCAN_INFO			*info = (CUSTOM_SCAN_INFO *)pInfo;
	ADAPTER						*pScanAdapter = info->pAdapter;
	
	RT_ASSERT(pInfo, ("%s(): pInfo is NULL!!!\n", __FUNCTION__));

	FunctionIn(COMP_SCAN);

	PlatformInitializeThreadEx(pScanAdapter, &info->worker, customscan_WorkerCb, customscan_PreWorkerExitCb, "custom scan worker thread", TRUE, 1000, pInfo);
	PlatformSetEventTrigerThread(pScanAdapter, &info->worker, PASSIVE_LEVEL, pInfo);
	
	FunctionOut(COMP_SCAN);

	return;
}

VOID
CustomScan_Stop(
	IN  VOID					*pInfo
	)
{
	CUSTOM_SCAN_INFO			*info = (CUSTOM_SCAN_INFO *)pInfo;
	
	RT_ASSERT(pInfo, ("%s(): pInfo is NULL!!!\n", __FUNCTION__));

	FunctionIn(COMP_SCAN);

	// Free all requests in the queue
	// Note that at this time, scan spinlock has been freed already
	customscan_Lock(info);
	customscan_FlushReqQ(info);
	customscan_Unlock(info);

	// Terminate the worker thread
	PlatformWaitThreadEnd(info->pAdapter, &info->worker);
	PlatformCancelThread(info->pAdapter, &info->worker);
	PlatformReleaseThread(info->pAdapter, &info->worker);

	FunctionOut(COMP_SCAN);

	return;
}

VOID
CustomScan_Deinit(
	IN  VOID					*pInfo
	)
{
	CUSTOM_SCAN_INFO			*info = (CUSTOM_SCAN_INFO *)pInfo;
	
	RT_ASSERT(pInfo, ("%s(): pInfo is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(info_sig == info->sig, ("%s(): invalid info\n", __FUNCTION__));

	PlatformFreeSpinLock(info->pAdapter, RT_CUSTOM_SCAN_SPINLOCK);

	return;
}

VOID *
CustomScan_AllocReq(
	IN  VOID					*pInfo,
	IN  CUSTOM_SCAN_STATE_CB	cb,
	IN  VOID					*ctx
	)
{
	CUSTOM_SCAN_INFO			*info = (CUSTOM_SCAN_INFO *)pInfo;
	CUSTOM_SCAN_REQ				*req = NULL;

	RT_ASSERT(pInfo, ("%s(): pInfo is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(info_sig == info->sig, ("%s(): invalid info\n", __FUNCTION__));

	customscan_Lock(info);

	do
	{
		if(NULL == (req = (CUSTOM_SCAN_REQ *)Pool_Acquire(&info->reqPool)))
		{
			RT_TRACE(COMP_SCAN, DBG_WARNING, ("Failed to acquire req from pool\n"));
			customscan_DumpReqPool(info);
			break;
		}

		customscan_InitReq(info, req);
	}while(FALSE);

	customscan_Unlock(info);

	return req;
}

FRAME_BUF *
CustomScan_GetProbeReqBuf(
	IN  VOID					*scanReq
	)
{
	CUSTOM_SCAN_REQ				*req = (CUSTOM_SCAN_REQ *)scanReq;
	
	RT_ASSERT(scanReq, ("%s(): scanReq is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(req_sig == req->sig, ("%s(): invalid scanReq\n", __FUNCTION__));
	RT_ASSERT(CUSTOM_SCAN_STATE_IDLE == req->state, ("%s(): invalid req state: %s\n", __FUNCTION__, customscan_ScanStateTxt(req->state)));

	return &req->probeReqBuf;
}

VOID
CustomScan_SetupCbCtx(
	IN  VOID					*scanReq,
	IN  CUSTOM_SCAN_STATE_CB	cb,
	IN  VOID					*ctx
	)
{
	CUSTOM_SCAN_REQ				*req = (CUSTOM_SCAN_REQ *)scanReq;

	RT_ASSERT(scanReq, ("%s(): scanReq is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(req_sig == req->sig, ("%s(): invalid scanReq\n", __FUNCTION__));
	RT_ASSERT(CUSTOM_SCAN_STATE_IDLE == req->state, ("%s(): invalid req state: %s\n", __FUNCTION__, customscan_ScanStateTxt(req->state)));

	req->cb = cb;
	req->cbCtx = ctx;
	
	return;
}

RT_STATUS
CustomScan_TermReq(
	IN  VOID					*pInfo,
	IN  BOOLEAN					bStopOnCurChnl
	)
{
	RT_STATUS					status = RT_STATUS_SUCCESS;
	CUSTOM_SCAN_INFO			*info = (CUSTOM_SCAN_INFO *)pInfo;
	
	RT_ASSERT(pInfo, ("%s(): pInfo is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(info_sig == info->sig, ("%s(): invalid info\n", __FUNCTION__));

	customscan_Lock(info);

	RT_TRACE(COMP_SCAN, DBG_WARNING, ("to terminate scan\n"));
	customscan_DumpReqQ(info);
	if(CUSTOM_SCAN_PROCESSING_REQ(info))
	{
		RT_TRACE(COMP_SCAN, DBG_WARNING, ("cur req:\n"));
		customscan_DumpReq(info->pCurReq);
	}

	do
	{
		customscan_FlushReqQ(info);
		
		if(!CUSTOM_SCAN_PROCESSING_REQ(info))
			break;

		info->pAdapter->MgntInfo.bCompleteScan = TRUE;
		RT_TRACE(COMP_SCAN, DBG_LOUD, ("CustomScan_TermReq(): info bCompleteScan %d\n", info->pAdapter->MgntInfo.bCompleteScan));
				
		if(RT_STATUS_SUCCESS != (status = customscan_TerminateCurrentReq(info, bStopOnCurChnl)))
			break;
		
	}while(FALSE);
	
	customscan_Unlock(info);

	return status;
}

RT_STATUS
CustomScan_AddScanChnl(
	IN  VOID					*scanReq,
	IN  u1Byte					chnl,
	IN  u1Byte					count,
	IN  RT_SCAN_TYPE			scanType,
	IN  u2Byte					duration,
	IN  u1Byte					dataRate,
	IN  FRAME_BUF				*probeReqBuf
	)
{
	RT_STATUS					status = RT_STATUS_SUCCESS;
	CUSTOM_SCAN_REQ				*req = (CUSTOM_SCAN_REQ *)scanReq;
	RT_CHNL_LIST_ENTRY			chnlEntry;
	u4Byte						itChnl = 0;
	
	RT_ASSERT(scanReq, ("%s(): scanReq is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(req_sig == req->sig, ("%s(): invalid scanReq\n", __FUNCTION__));
	RT_ASSERT(CUSTOM_SCAN_STATE_IDLE == req->state, ("%s(): invalid req state: %s\n", __FUNCTION__, customscan_ScanStateTxt(req->state)));

	if(MAX_SCAN_CHANNEL_NUM <= req->chnlListCnt)
	{
		RT_TRACE(COMP_SCAN, DBG_WARNING, ("chnlListCnt (%u) exceeds MAX_SCAN_CHANNEL_NUM\n", req->chnlListCnt));
		return RT_STATUS_BUFFER_TOO_SHORT;
	}

	chnlEntry.ChannelNum = chnl;
	chnlEntry.ScanType = scanType;
	chnlEntry.ScanPeriod = duration;
	chnlEntry.MaxTxPwrDbm = UNSPECIFIED_PWR_DBM;
	chnlEntry.ExInfo = 0;

	for(itChnl = 0; itChnl < count; itChnl++)
	{
		CHNL_ENTRY_EXT			*pExt = (CHNL_ENTRY_EXT *)Pool_Acquire(&req->chnlEntryPool);
		
		if(!pExt)
		{
			RT_TRACE(COMP_SCAN, DBG_WARNING, ("Failed to acquire chnl ext from pool\n"));
			status = RT_STATUS_RESOURCE;
			break;
		}

		customscan_InitChnlEntryExt(pExt, &chnlEntry, probeReqBuf, dataRate);
		RTInsertTailListWithCnt(&req->chnlListQ, &pExt->list, &req->chnlListCnt);
	}

	return status;
}

RT_STATUS
CustomScan_AddChnlPlanChnls(
	IN  VOID					*scanReq,
	IN  const RT_CHANNEL_PLAN	*plan,
	IN  RT_SCAN_TYPE			scanType,
	IN  u2Byte					duration,
	IN  u1Byte					dataRate,
	IN  FRAME_BUF				*probeReqBuf
	)
{
	RT_STATUS					status = RT_STATUS_SUCCESS;
	u4Byte						itChnl = 0;

	RT_ASSERT(scanReq, ("%s(): scanReq is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(plan, ("%s(): plan is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(plan->Len || plan->Len2_4G || plan->Len5G, ("%s(): plan->Len and plan->Len2_4G and plan->Len5G are all 0\n", __FUNCTION__));

	if(plan->Len)
	{// old definition
		// add all channels
		for(itChnl = 0; itChnl < plan->Len; itChnl++)
		{
			CustomScan_AddScanChnl(
				scanReq,
				plan->Channel[itChnl],
				1,
				scanType,
				duration,
				dataRate,
				probeReqBuf
				);
		}	
	}
	else
	{// new definition, see DefaultChannelPlan
		// add 2.4g channels
		for(itChnl = 0; itChnl < plan->Len2_4G; itChnl++)
		{
			CustomScan_AddScanChnl(
				scanReq,
				plan->Channel2_4G[itChnl],
				1,
				scanType,
				duration,
				dataRate,
				probeReqBuf
				);
		}

		// add 5g channels
		for(itChnl = 0; itChnl < plan->Len5G; itChnl++)
		{
			CustomScan_AddScanChnl(
				scanReq,
				plan->Channel5G[itChnl],
				1,
				scanType,
				duration,
				dataRate,
				probeReqBuf
				);
		}	

	}

	return status;
}

RT_STATUS
CustomScan_SetTimeBound(
	IN  VOID					*scanReq,
	IN  u4Byte					timeBound
	)
{
	CUSTOM_SCAN_REQ				*req = (CUSTOM_SCAN_REQ *)scanReq;

	RT_ASSERT(scanReq, ("%s(): scanReq is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(req_sig == req->sig, ("%s(): invalid scanReq\n", __FUNCTION__));

	if(CUSTOM_SCAN_STATE_IDLE != req->state)
	{
		return RT_STATUS_INVALID_STATE;
	}

	req->timeBound = timeBound;

	return RT_STATUS_SUCCESS;
}

RT_STATUS
CustomScan_ForcePassiveScan(
	IN  VOID					*scanReq
	)
{
	CUSTOM_SCAN_REQ				*req = (CUSTOM_SCAN_REQ *)scanReq;

	RT_ASSERT(scanReq, ("%s(): scanReq is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(req_sig == req->sig, ("%s(): invalid scanReq\n", __FUNCTION__));

	if(CUSTOM_SCAN_STATE_IDLE != req->state)
	{
		return RT_STATUS_INVALID_STATE;
	}

	req->bForcePassive = TRUE;

	return RT_STATUS_SUCCESS;
}

RT_STATUS
CustomScan_SetDelayStart(
	IN  VOID					*scanReq,
	IN  u4Byte					delayMs
	)
{
	CUSTOM_SCAN_REQ				*req = (CUSTOM_SCAN_REQ *)scanReq;

	RT_ASSERT(scanReq, ("%s(): scanReq is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(req_sig == req->sig, ("%s(): invalid scanReq\n", __FUNCTION__));

	if(CUSTOM_SCAN_STATE_IDLE != req->state)
	{
		return RT_STATUS_INVALID_STATE;
	}

	req->delayStartMs = delayMs;

	return RT_STATUS_SUCCESS;
}

RT_STATUS
CustomScan_SetRepeatIntermittent(
	IN  VOID					*scanReq,
	IN  u4Byte					repeatIntermittentMs
	)
{
	CUSTOM_SCAN_REQ				*req = (CUSTOM_SCAN_REQ *)scanReq;

	RT_ASSERT(scanReq, ("%s(): scanReq is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(req_sig == req->sig, ("%s(): invalid scanReq\n", __FUNCTION__));

	if(CUSTOM_SCAN_STATE_IDLE != req->state)
	{
		return RT_STATUS_INVALID_STATE;
	}

	req->repeatIntermittentMs = repeatIntermittentMs;

	return RT_STATUS_SUCCESS;
}


RT_STATUS
CustomScan_SetRepeatCount(
	IN  VOID					*scanReq,
	IN  u4Byte					count
	)
{
	CUSTOM_SCAN_REQ				*req = (CUSTOM_SCAN_REQ *)scanReq;

	RT_ASSERT(scanReq, ("%s(): scanReq is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(req_sig == req->sig, ("%s(): invalid scanReq\n", __FUNCTION__));

	if(CUSTOM_SCAN_STATE_IDLE != req->state)
	{
		return RT_STATUS_INVALID_STATE;
	}

	req->repeatCount = count;

	return RT_STATUS_SUCCESS;
}

RT_STATUS
CustomScan_SetProbeRspCb(
	IN  VOID					*scanReq,
	IN  CUSTOM_SCAN_ON_PROBE_RSP_CB onProbeRspCb,
	IN  VOID					*ctx
	)
{
	CUSTOM_SCAN_REQ				*req = (CUSTOM_SCAN_REQ *)scanReq;

	RT_ASSERT(scanReq, ("%s(): scanReq is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(req_sig == req->sig, ("%s(): invalid scanReq\n", __FUNCTION__));

	if(CUSTOM_SCAN_STATE_IDLE != req->state)
	{
		return RT_STATUS_INVALID_STATE;
	}

	req->onProbeRspCb = onProbeRspCb;
	req->onProbeRspCtx = ctx;

	return RT_STATUS_SUCCESS;
}

u4Byte
CustomScan_NumAddedChnl(
	IN  VOID					*scanReq
	)
{
	CUSTOM_SCAN_REQ				*req = (CUSTOM_SCAN_REQ *)scanReq;
	
	RT_ASSERT(scanReq, ("%s(): scanReq is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(req_sig == req->sig, ("%s(): invalid scanReq\n", __FUNCTION__));

	return req->chnlListCnt;
}

RT_STATUS
CustomScan_IssueReq(
	IN  VOID					*pInfo,
	IN  VOID					*scanReq,
	IN  CUSTOM_SCAN_SRC_TYPE	type,
	IN  const char				*typeInfo
	)
{
	CUSTOM_SCAN_INFO			*info = (CUSTOM_SCAN_INFO *)pInfo;
	CUSTOM_SCAN_REQ				*req = (CUSTOM_SCAN_REQ *)scanReq;
	
	RT_ASSERT(pInfo, ("%s(): pInfo is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(info_sig == info->sig, ("%s(): invalid info\n", __FUNCTION__));
	RT_ASSERT(scanReq, ("%s(): scanReq is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(req_sig == req->sig, ("%s(): invalid scanReq\n", __FUNCTION__));
	RT_ASSERT(CUSTOM_SCAN_STATE_IDLE == req->state, ("%s(): invalid req state: %s\n", __FUNCTION__, customscan_ScanStateTxt(req->state)));
	RT_ASSERT(typeInfo, ("%s(): typeInfo is NULL!!!\n", __FUNCTION__));
	
	FunctionIn(COMP_SCAN);

	req->type = type;
	req->typeInfo = typeInfo;
	req->issueTime = PlatformGetCurrentTime() / 1000;

	if(0 == req->repeatCount)
	{
		customscan_Lock(info);
		customscan_FreeReq(info, req);
		customscan_Unlock(info);
		return RT_STATUS_SUCCESS;
	}
	
	customscan_Lock(info);
	if(CUSTOM_SCAN_SRC_TYPE_SYS == type)
	{// give system scan the highest priority
		RTInsertHeadListWithCnt(&info->reqQ, &req->list, &info->reqCnt);
	}
	else
	{
		RTInsertTailListWithCnt(&info->reqQ, &req->list, &info->reqCnt);
	}
	customscan_SetReqState(info, req, CUSTOM_SCAN_STATE_WAITING);

	customscan_DumpReq(req);
	
	customscan_Unlock(info);

	if(RT_DRIVER_STOP(info->pAdapter))
	{
		// In this case, the request has been inserted to the reqQ and will be freed automatically in CustomScan_Deinit
		return RT_STATUS_INVALID_STATE;
	}


	PlatformSetEventToTrigerThread(info->pAdapter, &info->worker);
	
	return RT_STATUS_SUCCESS;
}

RT_STATUS
CustomScan_IssueSysScan(
	IN  VOID					*pInfo,
	IN  BOOLEAN					bActiveScan
	)
{
	RT_STATUS					status = RT_STATUS_SUCCESS;
	CUSTOM_SCAN_INFO			*info = (CUSTOM_SCAN_INFO *)pInfo;
	CUSTOM_SCAN_REQ				*req = NULL;
	RT_CHANNEL_PLAN				*plan = NULL;
	
	
	RT_ASSERT(pInfo, ("%s(): pInfo is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(info_sig == info->sig, ("%s(): invalid info\n", __FUNCTION__));

	FunctionIn(COMP_SCAN);

	do
	{
		if(MgntScanInProgress(&info->pAdapter->MgntInfo))
		{
			status = RT_STATUS_MEDIA_BUSY;
			break;
		}
		
		if(NULL == (req = (CUSTOM_SCAN_REQ *)CustomScan_AllocReq(pInfo, NULL, NULL)))
		{
			status = RT_STATUS_RESOURCE;
			break;
		}

		if(CUSTOM_SCAN_PROCESSING_REQ(info) && TEST_FLAG(info->pCurReq->flag, CUSTOM_SCAN_REQ_FLAG_RESET))
		{
			RT_TRACE(COMP_SCAN, DBG_LOUD, ("%s(): req %s is in terminating state and reset flag set, free it\n", __FUNCTION__, info->pCurReq->typeInfo));
			customscan_Lock(info);
			customscan_FreeDanglingReq(info);
			customscan_Unlock(info);
		}

		// set to cur req ASAP
		if(!CUSTOM_SCAN_PROCESSING_REQ(info))
		{
			customscan_Lock(info);
			info->pCurReq = req;
			customscan_Unlock(info);
		}
		else
		{
			status = RT_STATUS_MEDIA_BUSY;
			break;
		}

		// prepare chnls
		RtActChannelList(info->pAdapter, RT_CHNL_LIST_ACTION_GET_CHANNEL_PLAN, &info->pAdapter->MgntInfo.ChannelPlan, &plan);
		CustomScan_AddChnlPlanChnls(req, plan, bActiveScan ? SCAN_ACTIVE : SCAN_PASSIVE, 0, 0, NULL);

		if(!bActiveScan)
		{
			CustomScan_ForcePassiveScan(req);
		}

		// store info
		req->type = CUSTOM_SCAN_SRC_TYPE_SYS;
		req->typeInfo = "sys";
		req->issueTime = PlatformGetCurrentTime() / 1000;
		
		// set state
		customscan_Lock(info);
		customscan_SetReqState(info, info->pCurReq, CUSTOM_SCAN_STATE_STARTING);
		customscan_Unlock(info);
		
		// issue scan
		if(!customscan_InvokeMgntLinkReq(info, !bActiveScan))
		{
			status = RT_STATUS_FAILURE;
			break;
		}
	}while(FALSE);

	// cleanup
	if(req && RT_STATUS_SUCCESS != status)
	{
		customscan_Lock(info);
		if(req == info->pCurReq)
			info->pCurReq = NULL;
		
		customscan_FreeReq(info, req);

		customscan_Unlock(info);
	}

	FunctionOut(COMP_SCAN);

	return status;
}

RT_STATUS
CustomScan_ExtendDwellTime(
	IN  VOID					*pInfo,
	IN  u2Byte					dwellTime
	)
{
	RT_STATUS					status = RT_STATUS_SUCCESS;
	CUSTOM_SCAN_INFO			*info = (CUSTOM_SCAN_INFO *)pInfo;
	
	RT_ASSERT(pInfo, ("%s(): pInfo is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(info_sig == info->sig, ("%s(): invalid info\n", __FUNCTION__));

	customscan_Lock(info);

	do
	{
		if(!CustomScan_InProgress(pInfo))
		{
			RT_TRACE(COMP_SCAN, DBG_WARNING, ("no custom scan in progress\n"));
			status = RT_STATUS_INVALID_STATE;
			break;
		}
		
		if(CUSTOM_SCAN_STATE_DWELL != info->pCurReq->state)
		{
			RT_TRACE(COMP_SCAN, DBG_WARNING, 
				("Failed to extend dwell time: state is: %s\n", customscan_ScanStateTxt(info->pCurReq->state)));
			status = RT_STATUS_INVALID_STATE;
			break;
		}
		
		info->pCurReq->extDwellTime = dwellTime;
	}while(FALSE);

	customscan_Unlock(info);
	
	return status;

}

RT_STATUS
CustomScan_ShutDwellTime(
	IN  VOID					*pInfo,
	IN  VOID					*req
	)
{
	RT_STATUS					status = RT_STATUS_SUCCESS;
	CUSTOM_SCAN_INFO			*info = (CUSTOM_SCAN_INFO *)pInfo;
	CUSTOM_SCAN_REQ				*reqIn = (CUSTOM_SCAN_REQ *)req;

	FunctionIn(COMP_SCAN);
	
	RT_ASSERT(pInfo, ("%s(): pInfo is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(info_sig == info->sig, ("%s(): invalid info\n", __FUNCTION__));

	do
	{
		if(!CustomScan_InProgress(pInfo))
		{
			RT_TRACE(COMP_SCAN, DBG_WARNING, ("no custom scan in progress\n"));
			status = RT_STATUS_INVALID_STATE;
			break;
		}

		if(reqIn != info->pCurReq)
		{
			RT_TRACE(COMP_SCAN, DBG_WARNING, ("not current req\n"));
			status = RT_STATUS_INVALID_STATE;
			break;
		}
		
		if(CUSTOM_SCAN_STATE_DWELL != reqIn->state)
		{
			RT_TRACE(COMP_SCAN, DBG_WARNING, 
				("Failed to shut dwell time: state is: %s\n", customscan_ScanStateTxt(reqIn->state)));
			status = RT_STATUS_INVALID_STATE;
			break;
		}
		
		RT_TRACE(COMP_SCAN, DBG_WARNING, 
				("shutting off scan req %s dwell time\n", reqIn->typeInfo));
		
		if(RT_STATUS_SUCCESS != (status = customscan_TerminateCurrentChnl(info)))
		{
			RT_TRACE(COMP_SCAN, DBG_WARNING, 
				("customscan_TerminateCurrentChnl failed: 0x%08X\n", status));
			break;
		}

		if(reqIn->curChnl)
		{
			SET_FLAG(reqIn->curChnl->flag, CHNL_ENTRY_EXT_FLAG_CHNL_SHUT);
		}
	}while(FALSE);

	FunctionOut(COMP_SCAN);
	
	return status;

}

const char *
CustomScan_ScanStateTxt(
	IN  CUSTOM_SCAN_STATE		state
	)
{
	return customscan_ScanStateTxt(state);
}

BOOLEAN
CustomScan_AcquireCurCtx(
	IN  VOID					*pInfo,
	IN  VOID					*scanReq,
	OUT VOID					**ppCtx
	)
{
	CUSTOM_SCAN_INFO			*info = (CUSTOM_SCAN_INFO *)pInfo;
	CUSTOM_SCAN_REQ				*req = (CUSTOM_SCAN_REQ *)scanReq;
	BOOLEAN						bCurReq = FALSE;

	RT_ASSERT(pInfo, ("%s(): pInfo is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(info_sig == info->sig, ("%s(): invalid info\n", __FUNCTION__));
	RT_ASSERT(scanReq, ("%s(): scanReq is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(req_sig == req->sig, ("%s(): invalid scanReq\n", __FUNCTION__));

	customscan_Lock(info);

	do
	{
		if(req != info->pCurReq)
		{
			break;
		}

		bCurReq = TRUE;
		*ppCtx = req->cbCtx;
	}while(FALSE);

	if(!bCurReq)
	{
		customscan_Unlock(info);
	}

	return bCurReq;
}

VOID
CustomScan_ReleaseCurCtx(	
	IN  VOID					*pInfo
	)
{
	CUSTOM_SCAN_INFO			*info = (CUSTOM_SCAN_INFO *)pInfo;

	RT_ASSERT(pInfo, ("%s(): pInfo is NULL!!!\n", __FUNCTION__));
	RT_ASSERT(info_sig == info->sig, ("%s(): invalid info\n", __FUNCTION__));
	
	customscan_Unlock(info);
}