summaryrefslogtreecommitdiff
path: root/serial/serenum/enum.c
blob: 35ebe2ceb5e3995c2fde40a920dad4bd840561e0 (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
/*++
Copyright (c) 1997  Microsoft Corporation

Module Name:

    ENUM.C

Abstract:

    This module contains the enumeration code needed to figure out
    whether or not a device is attached to the serial port.  If there
    is one, it will obtain the PNP COM ID (if the device is PNP) and
    parse out the relevant fields.


Environment:

    kernel mode only

Notes:


--*/


#include "pch.h"

#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGESENM, SerenumValidateID)
#pragma alloc_text(PAGESENM, SerenumDoEnumProtocol)
#pragma alloc_text(PAGESENM, SerenumCheckForLegacyDevice)
#pragma alloc_text(PAGESENM, SerenumScanOtherIdForMouse)
#pragma alloc_text(PAGESENM, Serenum_ReenumerateDevices)
#pragma alloc_text(PAGESENM, Serenum_IoSyncReq)
#pragma alloc_text(PAGESENM, Serenum_IoSyncReqWithIrp)
#pragma alloc_text(PAGESENM, Serenum_IoSyncIoctlEx)
#pragma alloc_text(PAGESENM, Serenum_ReadSerialPort)
#pragma alloc_text(PAGESENM, Serenum_Wait)
#pragma alloc_text(PAGESENM, SerenumReleaseThreadReference)

//#pragma alloc_text (PAGE, Serenum_GetRegistryKeyValue)
#endif

#if !defined(__isascii)
#define __isascii(_c)   ( (unsigned)(_c) < 0x80 )
#endif // !defined(__isascii)

// disable warnings

#if _MSC_VER >= 1200
#pragma warning(push)
#endif

#pragma warning(disable:4127) // conditional expression is constant

void
SerenumScanOtherIdForMouse( 
    _In_reads_(BufLen)                  PCHAR PBuffer, 
    _In_ ULONG                           BufLen,
    _Outptr_result_buffer_maybenull_(*PmouseIdLen) PCHAR *PpMouseId,
    _Out_                                ULONG *PmouseIdLen                                            
    )
/*++

Routine Description:

    This routines a PnP packet for a mouse ID up to the first PnP delimiter
    (i.e, '(').

Arguments:

   PBuffer - Pointer to the buffer to scan

   BufLen - Length of the buffer in bytes

   PpMouseId - Pointer to the pointer to the mouse ID (this will be set
               to point to the location in the buffer where the mouse ID
               was found)

Return value:

    void

--*/
{
   PAGED_CODE();

   *PpMouseId = PBuffer;

   while (BufLen--) {
      if (**PpMouseId == 'M' || **PpMouseId == 'B') {
         *PmouseIdLen = BufLen+1;
         return;
      } else if (**PpMouseId == '(' || **PpMouseId == ('(' - 0x20)) {
         *PmouseIdLen = 0;
         *PpMouseId = NULL;
         return;
      }
      (*PpMouseId)++;
   }
   *PmouseIdLen = 0;
   *PpMouseId = NULL;
}

#if DBG
VOID SerenumHexDump(PUCHAR PBuf, ULONG NBytes)
/*++

Routine Description:

   Hex dump a buffer with NPerRow chars per row output

Arguments:

   PBuf - Pointer to the buffer to dump

   NBytes - Length of the buffer in bytes

Return value:

   VOID

--*/
{
   const ULONG NPerRow = 20;

   ULONG dmpi;
   ULONG col;
   UCHAR c;
   ULONG LoopCount = 1;
   ULONG dividend = NBytes / NPerRow;
   ULONG remainder = NBytes % NPerRow;
   ULONG nHexChars = NPerRow;
   ULONG nSpaces = 1;

   DbgPrint("SERENUM: Raw Data Packet on probe\n");

   if (remainder) {
      LoopCount++;
   }

   for (dmpi = 0; dmpi < (dividend + 1); dmpi++) {
      DbgPrint("-------: ");

      for (col = 0; col < nHexChars; col++) {
         DbgPrint("%02x ", (unsigned char)PBuf[dmpi * NPerRow + col]);
      }

      for (col = 0; col < nSpaces; col++) {
         DbgPrint(" ");
      }

      for (col = 0; col < nHexChars; col++){
         c = PBuf[dmpi * NPerRow + col];

         if (__isascii(c) && (c > ' ')){
            DbgPrint("%c", c);
            }else{
               DbgPrint(".");
            }
      }

      DbgPrint("\n");

      //
      // If this is the last one, then we have less that NPerRow to dump
      //

      if (dmpi == dividend) {
         if (remainder == 0) {
            //
            // This was an even multiple -- we're done
            //

            break; // for (dmpi)
         } else {
            nHexChars = remainder;
            nSpaces = NPerRow - nHexChars;
         }
      }
   }
}
#endif // DBG

NTSTATUS
SerenumDoEnumProtocol(
                                  PFDO_DEVICE_DATA PFdoData, 
    _Outptr_result_buffer_(*PNBytes)  PUCHAR *PpBuf, 
                                  PUSHORT PNBytes,
                                  PBOOLEAN PDSRMissing)
{
   IO_STATUS_BLOCK ioStatusBlock;
   ULONG i;
   ULONG bitMask;
   KEVENT event;
   KTIMER timer;
   NTSTATUS status;
   PUCHAR pReadBuf;
   USHORT nRead;
   PDEVICE_OBJECT pDevStack = PFdoData->TopOfStack;
   SERIAL_BAUD_RATE baudRate;
   SERIAL_LINE_CONTROL lineControl;

#if DBG
#define PERFCNT 1
#endif

#if defined(PERFCNT)
   LARGE_INTEGER perfFreq;
   LARGE_INTEGER stPerfCnt, endPerfCnt;
   LONG diff;
#endif

   LARGE_INTEGER DefaultWait;

   PAGED_CODE();

   KeInitializeEvent(&event, NotificationEvent, FALSE);
   KeInitializeTimer(&timer);

#if defined(PERFCNT)
   perfFreq.QuadPart = (LONGLONG) 0;
#endif
   DefaultWait.QuadPart = (LONGLONG) -(SERENUM_DEFAULT_WAIT);
   *PpBuf = NULL;
   pReadBuf = NULL;
   nRead = 0;
   *PDSRMissing = FALSE;

   LOGENTRY(LOG_ENUM, 'SDEP', PFdoData,  PpBuf, PDSRMissing);


   pReadBuf = ExAllocatePoolZero(NonPagedPoolNx, MAX_DEVNODE_NAME + sizeof(CHAR) ,SERENUM_POOL_TAG);
   
   if (pReadBuf == NULL) {
      status = STATUS_INSUFFICIENT_RESOURCES;
      LOGENTRY(LOG_ENUM, 'SDE1', PFdoData,  status, 0);
      goto ProtocolDone;
   }

   *(pReadBuf + MAX_DEVNODE_NAME) = 0; 

   //
   // Set DTR
   //

   Serenum_KdPrint(PFdoData, SER_DBG_SS_TRACE, ("Setting DTR...\n"));

   status = Serenum_IoSyncIoctl(IOCTL_SERIAL_SET_DTR, FALSE, pDevStack, &event);

   if (!NT_SUCCESS(status)) {
      LOGENTRY(LOG_ENUM, 'SDE2', PFdoData,  status, 0);
      goto ProtocolDone;
   }

   //
   // Clear RTS
   //
   Serenum_KdPrint(PFdoData, SER_DBG_SS_TRACE, ("Clearing RTS...\n"));

   status = Serenum_IoSyncIoctl(IOCTL_SERIAL_CLR_RTS, FALSE, pDevStack, &event);

   if (!NT_SUCCESS(status)) {
      LOGENTRY(LOG_ENUM, 'SDE3', PFdoData,  status, 0);
      goto ProtocolDone;
   }

   //
   // Wait for the default timeout period
   //

#if defined(PERFCNT)
   stPerfCnt = KeQueryPerformanceCounter(&perfFreq);
#endif

   status = Serenum_Wait(&timer, DefaultWait);

#if defined(PERFCNT)
   endPerfCnt = KeQueryPerformanceCounter(NULL);
   diff = (LONG)(endPerfCnt.QuadPart - stPerfCnt.QuadPart);
   diff *= 1000;
   diff /= (LONG)perfFreq.QuadPart;

   LOGENTRY(LOG_ENUM, 'SDT0', PFdoData, diff, 0);
#endif

   if (!NT_SUCCESS(status)) {
      Serenum_KdPrint(PFdoData, SER_DBG_SS_ERROR,
                      ("Timer failed with status %x\n", status ));
      LOGENTRY(LOG_ENUM, 'SDE4', PFdoData,  status, 0);
      goto ProtocolDone;
   }

   Serenum_KdPrint(PFdoData, SER_DBG_SS_TRACE, ("Checking DSR...\n"));

   status = Serenum_IoSyncIoctlEx(IOCTL_SERIAL_GET_MODEMSTATUS, FALSE,
                                  pDevStack, &event, NULL, 0, &bitMask,
                                  sizeof(ULONG));

   if (!NT_SUCCESS(status)) {
      LOGENTRY(LOG_ENUM, 'SDE5', PFdoData,  status, 0);
      goto ProtocolDone;
   }

   //
   // If DSR is not set, then a legacy device (like a mouse) may be attached --
   // they are not required to assert DSR when they are present and ready.
   //

   if ((SERIAL_DSR_STATE & bitMask) == 0) {
      Serenum_KdPrint (PFdoData, SER_DBG_SS_TRACE,
                       ("No PNP device available - DSR not set.\n"));
      *PDSRMissing = TRUE;
      LOGENTRY(LOG_ENUM, 'SDND', PFdoData,  0, 0);
   }

   //
   // Setup the serial port for 1200 bits/s, 7 data bits,
   // no parity, one stop bit
   //
   Serenum_KdPrint(PFdoData, SER_DBG_SS_TRACE, ("Setting baud rate to 1200..."
                                               "\n"));
   baudRate.BaudRate = 1200;
   status = Serenum_IoSyncIoctlEx(IOCTL_SERIAL_SET_BAUD_RATE, FALSE, pDevStack,
                                  &event, &baudRate, sizeof(SERIAL_BAUD_RATE),
                                  NULL, 0);
   if (!NT_SUCCESS(status)) {
      LOGENTRY(LOG_ENUM, 'SDE6', PFdoData,  status, 0);
      goto ProtocolDone;
   }

   Serenum_KdPrint(PFdoData, SER_DBG_SS_TRACE,
                   ("Setting the line control...\n"));

   lineControl.StopBits = STOP_BIT_1;
   lineControl.Parity = NO_PARITY;
   lineControl.WordLength = 7;

   status = Serenum_IoSyncIoctlEx(IOCTL_SERIAL_SET_LINE_CONTROL, FALSE,
                                  pDevStack, &event, &lineControl,
                                  sizeof(SERIAL_LINE_CONTROL), NULL, 0);

   if (!NT_SUCCESS(status)) {
      LOGENTRY(LOG_ENUM, 'SDE7', PFdoData,  status, 0);
      goto ProtocolDone;
   }


   //
   // loop twice
   // The first iteration is for reading the PNP ID string from modems
   // and mice.
   // The second iteration is for other devices.
   //
   for (i = 0; i < 2; i++) {
      //
      // Purge the buffers before reading
      //

      LOGENTRY(LOG_ENUM, 'SDEI', PFdoData,  i, 0);

      Serenum_KdPrint(PFdoData, SER_DBG_SS_TRACE, ("Purging all buffers...\n"));

      bitMask = SERIAL_PURGE_RXCLEAR;

      status = Serenum_IoSyncIoctlEx(IOCTL_SERIAL_PURGE, FALSE, pDevStack,
                                     &event, &bitMask, sizeof(ULONG), NULL, 0);

      if (!NT_SUCCESS(status)) {
         LOGENTRY(LOG_ENUM, 'SDE8', PFdoData,  status, 0);
         break;
      }

      //
      // Clear DTR
      //
      Serenum_KdPrint(PFdoData, SER_DBG_SS_TRACE, ("Clearing DTR...\n"));

      status = Serenum_IoSyncIoctl(IOCTL_SERIAL_CLR_DTR, FALSE, pDevStack,
                                   &event);

      if (!NT_SUCCESS(status)) {
         LOGENTRY(LOG_ENUM, 'SDE9', PFdoData,  status, 0);
         break;
      }

      //
      // Clear RTS
      //
      Serenum_KdPrint(PFdoData, SER_DBG_SS_TRACE, ("Clearing RTS...\n"));

      status = Serenum_IoSyncIoctl(IOCTL_SERIAL_CLR_RTS, FALSE, pDevStack,
                                   &event);

      if (!NT_SUCCESS(status)) {
         LOGENTRY(LOG_ENUM, 'SDEA', PFdoData,  status, 0);
         break;
      }

      //
      // Set a timer for 200 ms
      //

      Serenum_KdPrint(PFdoData, SER_DBG_SS_TRACE, ("Waiting...\n"));

#if defined(PERFCNT)
      stPerfCnt = KeQueryPerformanceCounter(&perfFreq);
#endif

      status = Serenum_Wait(&timer, DefaultWait);

      if (!NT_SUCCESS(status)) {
         Serenum_KdPrint(PFdoData, SER_DBG_SS_ERROR,
                         ("Timer failed with status %x\n", status ));
         LOGENTRY(LOG_ENUM, 'SDEB', PFdoData,  status, 0);
         break;
      }

      //
      // set DTR
      //

      Serenum_KdPrint(PFdoData, SER_DBG_SS_TRACE, ("Setting DTR...\n"));

      status = Serenum_IoSyncIoctl(IOCTL_SERIAL_SET_DTR, FALSE, pDevStack,
                                   &event);

      if (!NT_SUCCESS(status)) {
         LOGENTRY(LOG_ENUM, 'SDEC', PFdoData,  status, 0);
         break;
      }


#if defined(PERFCNT)
   endPerfCnt = KeQueryPerformanceCounter(NULL);
   diff = (LONG)(endPerfCnt.QuadPart - stPerfCnt.QuadPart);
   diff *= 1000;
   diff /= (LONG)perfFreq.QuadPart;

   LOGENTRY(LOG_ENUM, 'SDT1', PFdoData, diff, 0);
#endif

      //
      // First iteration is for modems
      // Therefore wait for 200 ms as per protocol for getting PNP string out
      //

      if (!i) {
         status = Serenum_Wait(&timer, DefaultWait);
         if (!NT_SUCCESS(status)) {
            Serenum_KdPrint (PFdoData, SER_DBG_SS_ERROR,
                             ("Timer failed with status %x\n", status ));
            LOGENTRY(LOG_ENUM, 'SDED', PFdoData,  status, 0);
            break;
         }
      }

      //
      // set RTS
      //

      Serenum_KdPrint(PFdoData, SER_DBG_SS_TRACE, ("Setting RTS...\n"));

      status = Serenum_IoSyncIoctl(IOCTL_SERIAL_SET_RTS, FALSE, pDevStack,
                                   &event);

      if (!NT_SUCCESS(status)) {
         LOGENTRY(LOG_ENUM, 'SDEF', PFdoData,  status, 0);
         break;
      }

      //
      // Read from the serial port
      //
      Serenum_KdPrint(PFdoData, SER_DBG_SS_TRACE,
                      ("Reading the serial port...\n"));

      Serenum_KdPrint(PFdoData, SER_DBG_SS_INFO, ("Address: %p\n", pReadBuf));

      nRead = 0;

#if DBG
      RtlFillMemory(pReadBuf, MAX_DEVNODE_NAME, 0xff);
#endif

      //
      // Flush the input buffer
      //

      status = Serenum_ReadSerialPort((PCHAR)pReadBuf, MAX_DEVNODE_NAME,
                                      SERENUM_SERIAL_READ_TIME, &nRead,
                                      &ioStatusBlock, PFdoData);

      switch (status) {
      case STATUS_TIMEOUT:
         if (nRead == 0) {
            Serenum_KdPrint(PFdoData, SER_DBG_SS_TRACE,
                      ("Timeout with no bytes read; continuing\n"));
            LOGENTRY(LOG_ENUM, 'SDEG', PFdoData,  status, 0);
            continue;
         }

         //
         // We timed out with data, so we use what we have
         //

         status = STATUS_SUCCESS;

         LOGENTRY(LOG_ENUM, 'SDEH', PFdoData,  status, 0);
         break;

      case STATUS_SUCCESS:
         Serenum_KdPrint(PFdoData, SER_DBG_SS_TRACE, ("Read succeeded\n"));
         LOGENTRY(LOG_ENUM, 'SDEJ', PFdoData,  status, 0);
         goto ProtocolDone;
         break;

      default:
         Serenum_KdPrint(PFdoData, SER_DBG_SS_TRACE, ("Read failed with 0x%x\n",
                                                       status));
         LOGENTRY(LOG_ENUM, 'SDEK', PFdoData,  status, 0);
         goto ProtocolDone;
         break;
      }

      //
      // If anything was read from the serial port, we're done!
      //

      if (nRead) {
         break;
      }
   }

ProtocolDone:;

   if (!NT_SUCCESS(status)) {
      if (pReadBuf != NULL) {
         ExFreePoolWithTag(pReadBuf,SERENUM_POOL_TAG);
         pReadBuf = NULL;
         return status;
      }
   }

   *PNBytes = nRead;
   *PpBuf = pReadBuf;
   LOGENTRY(LOG_ENUM, 'SDE0', PFdoData,  status, nRead);
#pragma warning(suppress: 26045)
   return status;
}

BOOLEAN
SerenumValidateID(IN PUNICODE_STRING PId)
/*++

Routine Description:

    This validates all the characters in a MULTI_SZ for a Pnp ID.

    Invalid characters are:
        c <  0x20 (' ')
        c >  0x7F
        c == 0x2C (',')

Arguments:

    PId - Pointer to a multi_sz containing the IDs

Return value:

    BOOLEAN -- TRUE if valid ID, FALSE otherwise

--*/
{
   WCHAR *cp;

   PAGED_CODE();

   //
   // Walk each string in the multisz and check for bad characters
   //

   cp = PId->Buffer;

   if (cp == NULL) {
      return TRUE;
   }

   do {
      while (*cp) {
         if ((*cp < L' ') || (*cp > L'\x7f') || (*cp == L',') ) {
            return FALSE;
         }

         cp++;
      }

      cp++;
   } while (*cp);

   return TRUE;
}

BOOLEAN
SerenumCheckForLegacyDevice(IN PFDO_DEVICE_DATA PFdoData, 
     _In_reads_(BufferLen) IN PCHAR PIdBuf,
                            IN ULONG BufferLen,
                            IN OUT PUNICODE_STRING PHardwareIDs,
                            IN OUT PUNICODE_STRING PCompIDs,
                            IN OUT PUNICODE_STRING PDeviceIDs)
/*++

Routine Description:

   This routine implements legacy mouse detection

Arguments:

    PFdoData      - pointer to the FDO's device-specific data
    PIdBuf        - Buffer of data returned from device
    BufferLen     - length of PIdBuf in bytes
    PHardwareIDs  - MULTI_SZ to return hardware ID's in
    PCompIDs      - MULTI_SZ to return compatible ID's in
    PDeviceIDs    - MULTI_SZ to return device ID's in

Return value:

    BOOLEAN -- TRUE if mouse detected, FALSE otherwise

--*/
{
   PCHAR mouseId = PIdBuf;
   ULONG mouseIdLen;
   BOOLEAN rval = FALSE;
   
   PAGED_CODE();

   SerenumScanOtherIdForMouse(PIdBuf, BufferLen, &mouseId, &mouseIdLen);

   if (mouseId != NULL) {
      //
      // A legacy device is attached to the serial port, since DSR was
      // not set when RTS was set.
      // If we find a mouse from the PIdBuf, copy the appropriate
      // strings into the hardwareIDs and compIDs manually.
      //
      if (*mouseId == 'M') {
         if ((mouseId - PIdBuf) > 1
             && mouseIdLen > 1 
             && mouseId[1] == '3' ) {
            Serenum_KdPrint(PFdoData, SER_DBG_SS_TRACE, ("*PNP0F08 mouse\n"));
            Serenum_InitMultiString(PFdoData, PHardwareIDs, "*PNP0F08", NULL);
            Serenum_InitMultiString(PFdoData, PCompIDs, "SERIAL_MOUSE", NULL);
            //
            // CIMEXCIMEX 04/28/1999 -
            //     Device ID's should be unique, at least as unique as the
            // hardware ID's. This ID should really be Serenum\\PNP0F08
            //
            Serenum_InitMultiString(PFdoData, PDeviceIDs, "Serenum\\Mouse",
                                    NULL);
            rval = TRUE;

         } else {
            Serenum_KdPrint(PFdoData, SER_DBG_SS_TRACE, ("*PNP0F01 mouse\n"));
            Serenum_InitMultiString(PFdoData, PHardwareIDs, "*PNP0F01", NULL);
            Serenum_InitMultiString(PFdoData, PCompIDs, "SERIAL_MOUSE", NULL);
            //
            // CIMEXCIMEX 04/28/1999 -
            //     Device ID's should be unique, at least as unique as the
            // hardware ID's. This ID should really be Serenum\\PNP0F01
            //
            Serenum_InitMultiString(PFdoData, PDeviceIDs, "Serenum\\Mouse",
                                    NULL);
            rval = TRUE;
         }
      } else if (*mouseId == 'B') {
         Serenum_KdPrint(PFdoData, SER_DBG_SS_TRACE, ("*PNP0F09 mouse\n"));
         Serenum_InitMultiString(PFdoData, PHardwareIDs, "*PNP0F09", NULL);
         Serenum_InitMultiString(PFdoData, PCompIDs, "*PNP0F0F", "SERIAL_MOUSE",
                                 NULL);
         //
         // CIMEXCIMEX 04/28/1999 -
         //     Device ID's should be unique, at least as unique as the
         // hardware ID's. This ID should really be Serenum\\PNP0F09
         //
         Serenum_InitMultiString(PFdoData, PDeviceIDs, "Serenum\\BallPoint",
                                 NULL);
         rval = TRUE;
      }

#if DBG
      if (rval) {
         Serenum_KdPrint(PFdoData, SER_DBG_SS_TRACE,
                         ("Buffers at 0x%p 0x%p 0x%p\n",
                          PHardwareIDs->Buffer, PCompIDs->Buffer,
                          PDeviceIDs->Buffer));
      }
#endif // DBG

   }

   return rval;
}

NTSTATUS
Serenum_ReenumerateDevices(IN PIRP Irp, IN PFDO_DEVICE_DATA PFdoData,
                           PBOOLEAN PSameDevice)
/*++

Routine Description:

    This enumerates the serenum bus which is represented by Fdo (a pointer
    to the device object representing the serial bus). It creates new PDOs
    for any new devices which have been discovered since the last enumeration

Arguments:

    PFdoData - Pointer to the fdo's device extension
                for the serial bus which needs to be enumerated
    Irp - Pointer to the Irp which was sent to reenumerate.

Return value:

    NTSTATUS

--*/
{
   NTSTATUS status;
   KEVENT event;
   KTIMER timer;

   UNICODE_STRING pdoUniName;
   PDEVICE_OBJECT pdo = PFdoData->NewPDO;
   PDEVICE_OBJECT pDevStack = PFdoData->TopOfStack;
   PPDO_DEVICE_DATA pdoData;
   UNICODE_STRING hardwareIDs;
   UNICODE_STRING compIDs;
   UNICODE_STRING deviceIDs;
   UNICODE_STRING devDesc;
   UNICODE_STRING serNo;
   UNICODE_STRING pnpRev;


   BOOLEAN DSRMissing = FALSE;
   BOOLEAN legacyDeviceFound = FALSE;

   USHORT nActual = 0;

   PCHAR pReadBuf = NULL;
   WCHAR pdoName[] = SERENUM_PDO_NAME_BASE;

   SERIAL_BASIC_SETTINGS basicSettings;
   BOOLEAN basicSettingsDone = FALSE;
   SERIAL_TIMEOUTS timeouts, newTimeouts;

   ULONG curTry = 0;
   BOOLEAN sameDevice = FALSE;

   PAGED_CODE();

   //
   // While enumeration is taking place, we can't allow a Create to come down
   // from an upper driver.  We use this semaphore to protect ourselves.
   //

   status = KeWaitForSingleObject(&PFdoData->CreateSemaphore, Executive,
                                  KernelMode, FALSE, NULL);

   if (!NT_SUCCESS(status)) {
      return status;
   }


   //
   // Initialization
   //

   RtlInitUnicodeString(&pdoUniName, pdoName);
   pdoName[((sizeof(pdoName)/sizeof(WCHAR)) - 2)] = L'0' + PFdoData->PdoIndex++;

   KeInitializeEvent(&event, NotificationEvent, FALSE);
   KeInitializeTimer(&timer);

   RtlInitUnicodeString(&hardwareIDs, NULL);
   RtlInitUnicodeString(&compIDs, NULL);
   RtlInitUnicodeString(&deviceIDs, NULL);
   RtlInitUnicodeString(&devDesc, NULL);
   RtlInitUnicodeString(&serNo, NULL);
   RtlInitUnicodeString(&pnpRev, NULL);

   //
   // If the current PDO should be marked missing, do so.
   //
   if (PFdoData->PDOForcedRemove && pdo != NULL) {
       Serenum_PDO_EnumMarkMissing(PFdoData, pdo->DeviceExtension);
       pdo = NULL;
   }

   //
   // Open the Serial port before sending Irps down
   // Use the Irp passed to us, and grab it on the way up.
   //

   Serenum_KdPrint(PFdoData, SER_DBG_SS_TRACE,
                   ("Opening the serial port...\n"));

   status = Serenum_IoSyncReqWithIrp(Irp, IRP_MJ_CREATE, &event, pDevStack);

   LOGENTRY(LOG_ENUM, 'SRRO', PFdoData, status, 0);

   //
   // If we cannot open the stack, odd's are we have a live and started PDO on
   // it. Since enumeration might interfere with running devices, we do not
   // adjust our list of children if we cannot open the stack.
   //
   if (!NT_SUCCESS(status)) {
      Serenum_KdPrint(PFdoData, SER_DBG_SS_TRACE,
                      ("Failed to open the serial port...\n"));
      KeReleaseSemaphore(&PFdoData->CreateSemaphore, IO_NO_INCREMENT, 1, FALSE);
      LOGENTRY(LOG_ENUM, 'SRR1', PFdoData, status, 0);
      return status;
   }

   //
   // Set up the COM port
   //

   Serenum_KdPrint(PFdoData, SER_DBG_SS_TRACE, ("Setting up port\n"));

   status = Serenum_IoSyncIoctlEx(IOCTL_SERIAL_INTERNAL_BASIC_SETTINGS, TRUE,
                                  pDevStack, &event, NULL, 0,
                                  &basicSettings, sizeof(basicSettings));

   if (NT_SUCCESS(status)) {
      basicSettingsDone = TRUE;
   } else {
      //
      // This "serial" driver doesn't support BASIC_SETTINGS so instead
      // we just set what we really need the old fashioned way
      //

      status = Serenum_IoSyncIoctlEx(IOCTL_SERIAL_GET_TIMEOUTS, FALSE, 
                                     pDevStack, &event,
                                     NULL, 0, &timeouts, sizeof(timeouts));
      
      if (!NT_SUCCESS(status)) {
         //
         // This should not happen because we are sending an Ioctl to Serial
         // but for robustness of the code we check the return status.
         //
         Serenum_KdPrint(PFdoData, SER_DBG_SS_TRACE,
                         ("Failed to get the serial timeouts...\n"));
         KeReleaseSemaphore(&PFdoData->CreateSemaphore, IO_NO_INCREMENT, 1, FALSE);
         return status;
      }

      RtlZeroMemory(&newTimeouts, sizeof(newTimeouts));

      Serenum_IoSyncIoctlEx(IOCTL_SERIAL_SET_TIMEOUTS, FALSE, pDevStack, &event,
                             &newTimeouts, sizeof(newTimeouts), NULL, 0);
   }


   //
   // Run the serial PnP device detection protocol; give it up to 3 tries
   //

   while (curTry <= 2) {
      if (pReadBuf) {
         ExFreePoolWithTag(pReadBuf,SERENUM_POOL_TAG);
         pReadBuf = NULL;
      }

      status = SerenumDoEnumProtocol(PFdoData, (PUCHAR*)&pReadBuf, &nActual,
                                     &DSRMissing);

      if (status == STATUS_SUCCESS) {
         break;
      }

      curTry++;
   }

   //
   // If DSR wasn't set any existing pdos will be eliminated
   //


   if (basicSettingsDone) {
      Serenum_KdPrint(PFdoData, SER_DBG_SS_TRACE,
                      ("Restoring basic settings\n"));

      Serenum_IoSyncIoctlEx(IOCTL_SERIAL_INTERNAL_RESTORE_SETTINGS, TRUE,
                            pDevStack, &event, &basicSettings,
                            sizeof(basicSettings), NULL, 0);
   } else {
      Serenum_IoSyncIoctlEx(IOCTL_SERIAL_SET_TIMEOUTS, FALSE, pDevStack, &event,
                             &timeouts, sizeof(timeouts), NULL, 0);
   }

   //
   // Cleanup and then Close
   //

   Serenum_KdPrint(PFdoData, SER_DBG_SS_TRACE,
                   ("Cleanup on the serial port...\n"));

   //
   // We ignore the status -- we have to finish closing
   //

   (void)Serenum_IoSyncReqWithIrp(Irp, IRP_MJ_CLEANUP, &event, pDevStack);

#if DBG
   if (!NT_SUCCESS(status)) {
      Serenum_KdPrint(PFdoData, SER_DBG_SS_ERROR,
                      ("Failed to cleanup the serial port...\n"));
      // don't return because we want to attempt to close!
   }
#endif

   //
   // Close the Serial port after everything is done
   //

   Serenum_KdPrint(PFdoData, SER_DBG_SS_TRACE,
                   ("Closing the serial port...\n"));

   //
   // We ignore the status -- we have to close!!
   //

   Serenum_IoSyncReqWithIrp(Irp, IRP_MJ_CLOSE, &event, pDevStack);

   LOGENTRY(LOG_ENUM, 'SRRC', PFdoData, 0, 0);

   //
   // Our status is that of the enumeration
   //

   if (!NT_SUCCESS(status)) {
      Serenum_KdPrint(PFdoData, SER_DBG_SS_ERROR,
                      ("Failed to enumerate the serial port...\n"));
      KeReleaseSemaphore(&PFdoData->CreateSemaphore, IO_NO_INCREMENT, 1, FALSE);
      if (pReadBuf != NULL) {
         ExFreePoolWithTag(pReadBuf,SERENUM_POOL_TAG);
      }
      LOGENTRY(LOG_ENUM, 'SRR2', PFdoData, status, 0);
      return status;
   }

   //
   // Check if anything was read, and if not, we're done
   //

   if (nActual == 0) {
      if (pReadBuf != NULL) {
         ExFreePoolWithTag(pReadBuf,SERENUM_POOL_TAG);
         pReadBuf = NULL;
      }

      if (pdo != NULL) {
         //
         // Something was there.  The device must have been unplugged.
         // Remove the PDO.
         //

         Serenum_PDO_EnumMarkMissing(PFdoData, pdo->DeviceExtension);
         pdo = NULL;
      }

      goto ExitReenumerate;
   }

   Serenum_KdPrint(PFdoData, SER_DBG_SS_TRACE,
                   ("Something was read from the serial port...\n"));



#if 0
   if (PFdoData->DebugLevel & SER_DBG_PNP_DUMP_PACKET) {
      SerenumHexDump(pReadBuf, nActual);
   }
#endif

   //
   // Determine from the result whether the current pdo (if we have one),
   // should be deleted.  If it's the same device, then keep it.  If it's a
   // different device or if the device is a legacy device, then create a
   // new pdo.
   //
   if (DSRMissing) {
      legacyDeviceFound
         = SerenumCheckForLegacyDevice(PFdoData, pReadBuf, nActual,
                                       &hardwareIDs, &compIDs, &deviceIDs);
   }

   if (!legacyDeviceFound) {
      //
      // No legacy device was found, so parse the data we got back
      // from the device.
      //

      status = Serenum_ParseData(PFdoData, pReadBuf, nActual, &hardwareIDs,
                                 &compIDs, &deviceIDs, &devDesc, &serNo, &pnpRev);

      //
      // Last chance:
      //
      // 1) DSR is present
      // 2) Not a PnP device
      //
      // There are some devices that are legacy but also assert DSR (e.g., the
      // gyropoint mouse).  Give it one last shot.
      //

      if (!DSRMissing && !NT_SUCCESS(status)) {


         //
         // CIMEXCIMEX Serenum_ParseData() isn't very tidy, so we
         // must clean up after them
         //

         SerenumFreeUnicodeString(&hardwareIDs);
         SerenumFreeUnicodeString(&compIDs);
         SerenumFreeUnicodeString(&deviceIDs);
         SerenumFreeUnicodeString(&devDesc);
         SerenumFreeUnicodeString(&serNo);
         SerenumFreeUnicodeString(&pnpRev);

         if (SerenumCheckForLegacyDevice(PFdoData, pReadBuf, nActual,
                                         &hardwareIDs, &compIDs, &deviceIDs)) {
            status = STATUS_SUCCESS;
         }
      }

      //
      // If the data can't be parsed and this isn't a legacy device, then
      // it is something we don't understand.  We bail out at this point
      //


      if (!NT_SUCCESS(status)) {
         Serenum_KdPrint(PFdoData, SER_DBG_SS_ERROR,
                         ("Failed to parse the data for the new device\n"));

         //
         // If there is a current PDO, remove it since we can't ID the
         // attached device.
         //

         if (pdo) {
            Serenum_PDO_EnumMarkMissing(PFdoData, pdo->DeviceExtension);
            pdo = NULL;
         }

         SerenumFreeUnicodeString(&hardwareIDs);
         SerenumFreeUnicodeString(&compIDs);
         SerenumFreeUnicodeString(&deviceIDs);
         SerenumFreeUnicodeString(&devDesc);
         SerenumFreeUnicodeString(&serNo);
         SerenumFreeUnicodeString(&pnpRev);


         ExFreePoolWithTag(pReadBuf,SERENUM_POOL_TAG);
         pReadBuf = NULL;

         goto ExitReenumerate;
      }
   }

   //
   // We're now finally able to free this read buffer.
   //

   if (pReadBuf != NULL) {
      ExFreePoolWithTag(pReadBuf,SERENUM_POOL_TAG);
   }

   //
   // Validate all the ID's -- if any are illegal,
   // then we fail the enumeration
   //

   if (!SerenumValidateID(&hardwareIDs) || !SerenumValidateID(&compIDs)
       || !SerenumValidateID(&deviceIDs)) {

      //
      // If a PDO already exists, mark it missing and get rid
      // of it since we don't know what is out there any longer
      //

      if (pdo) {
         Serenum_PDO_EnumMarkMissing(PFdoData, pdo->DeviceExtension);
         pdo = NULL;
      }

      SerenumFreeUnicodeString(&hardwareIDs);
      SerenumFreeUnicodeString(&compIDs);
      SerenumFreeUnicodeString(&deviceIDs);
      SerenumFreeUnicodeString(&devDesc);
      SerenumFreeUnicodeString(&serNo);
      SerenumFreeUnicodeString(&pnpRev);


      goto ExitReenumerate;
   }

   //
   // Check if the current device is the same as the one that we're
   // enumerating.  If so, we'll just keep the current pdo.
   //
   if (pdo) {
      pdoData = pdo->DeviceExtension;

      //
      // CIMEXCIMEX 04/28/1999 -
      //     We should be comparing device ID's here, but the above mentioned
      // bug must be fixed first. Note that even this code is broken as it
      // doesn't take into account that hardware/compID's are multiSz.
      //

      if (!(RtlEqualUnicodeString(&pdoData->HardwareIDs, &hardwareIDs, FALSE)
            && RtlEqualUnicodeString(&pdoData->CompIDs, &compIDs, FALSE))) {
         //
         // The ids are not the same, so get rid of this pdo and create a
         // new one so that the PNP system will query the ids and find a
         // new driver
         //
         Serenum_KdPrint(PFdoData, SER_DBG_SS_TRACE, ("Different device."
                                                     " Removing PDO %p\n",
                                                     pdo));
         Serenum_PDO_EnumMarkMissing(PFdoData, pdoData);
         pdo = NULL;
      } else {
         Serenum_KdPrint(PFdoData, SER_DBG_SS_TRACE,
                         ("Same device. Keeping current Pdo %p\n", pdo));
         sameDevice = TRUE;
      }
   }

   //
   // If there isn't a pdo, then create one!
   //
   if (!pdo) {
      //
      // Allocate a pdo
      //
      status = IoCreateDevice(PFdoData->Self->DriverObject,
                              sizeof(PDO_DEVICE_DATA), &pdoUniName,
                              FILE_DEVICE_UNKNOWN,
                              FILE_AUTOGENERATED_DEVICE_NAME, FALSE, &pdo);

      if (!NT_SUCCESS(status)) {

         Serenum_KdPrint(PFdoData, SER_DBG_SS_ERROR,
                         ("Create device failed\n"));
         KeReleaseSemaphore(&PFdoData->CreateSemaphore, IO_NO_INCREMENT, 1,
                            FALSE);
         return status;
      }

      Serenum_KdPrint(PFdoData, SER_DBG_SS_TRACE,
                      ("Created PDO on top of filter: %p\n",pdo));


      //
      // Initialize the rest of the device object
      //


      pdoData = pdo->DeviceExtension;

      //
      // Copy our temp buffers over to the DevExt
      //

      pdoData->HardwareIDs = hardwareIDs;
      pdoData->CompIDs = compIDs;
      pdoData->DeviceIDs = deviceIDs;
      pdoData->DevDesc = devDesc;
      pdoData->SerialNo = serNo;
      pdoData->PnPRev = pnpRev;

      Serenum_InitPDO(pdo, PFdoData);

   }

ExitReenumerate:;

   KeReleaseSemaphore(&PFdoData->CreateSemaphore, IO_NO_INCREMENT, 1, FALSE);

   *PSameDevice = sameDevice;

   return STATUS_SUCCESS;
}

void
Serenum_PDO_EnumMarkMissing(PFDO_DEVICE_DATA FdoData, PPDO_DEVICE_DATA PdoData)
/*++

Routine Description:
    Removes the attached pdo from the fdo's list of children.

    NOTE: THIS FUNCTION CAN ONLY BE CALLED DURING AN ENUMERATION. If called
          outside of enumeration, Serenum might delete it's PDO before PnP has
          been told the PDO is gone.

Arguments:
    FdoData - Pointer to the fdo's device extension
    PdoData - Pointer to the pdo's device extension

Return value:
    none

--*/
{
   KIRQL oldIrql;

   Serenum_KdPrint (FdoData, SER_DBG_SS_TRACE, ("Removing Pdo %p\n",
                                                 PdoData->Self));

   ASSERT(PdoData->Attached);

   KeAcquireSpinLock(&FdoData->EnumerationLock, &oldIrql);

   PdoData->Attached = FALSE;
   FdoData->NewPDO = NULL;
   FdoData->NewPdoData = NULL;
   FdoData->NewNumPDOs = 0;
   FdoData->NewPDOForcedRemove = FALSE;

   FdoData->EnumFlags |= SERENUM_ENUMFLAG_DIRTY;

   KeReleaseSpinLock(&FdoData->EnumerationLock, oldIrql);
}

NTSTATUS
Serenum_IoSyncReqWithIrp(PIRP PIrp, UCHAR MajorFunction, PKEVENT PEvent,
                         PDEVICE_OBJECT PDevObj )
/*++

Routine Description:
    Performs a synchronous IO request by waiting on the event object
    passed to it.  The IRP isn't deallocated after this call.

Arguments:
    PIrp - The IRP to be used for this request

    MajorFunction - The major function

    PEvent - An event used to wait for the IRP

    PDevObj - The object that we're performing the IO request upon

Return value:
    NTSTATUS

--*/
{
    PIO_STACK_LOCATION stack;
    NTSTATUS status;

    PAGED_CODE();
    
    stack = IoGetNextIrpStackLocation(PIrp);

    stack->MajorFunction = MajorFunction;

    KeClearEvent(PEvent);

    IoSetCompletionRoutine(PIrp, Serenum_EnumComplete, PEvent, TRUE,
                           TRUE, TRUE);

    status = Serenum_IoSyncReq(PDevObj, PIrp, PEvent);

    if (status == STATUS_SUCCESS) {
       status = PIrp->IoStatus.Status;
    }

    return status;
}

NTSTATUS
Serenum_IoSyncIoctlEx(ULONG Ioctl, BOOLEAN Internal, PDEVICE_OBJECT PDevObj,
                      PKEVENT PEvent, PVOID PInBuffer, ULONG InBufferLen,
                      PVOID POutBuffer, ULONG OutBufferLen)
/*++

Routine Description:
    Performs a synchronous IO control request by waiting on the event object
    passed to it.  The IRP is deallocated by the IO system when finished.

Return value:
    NTSTATUS

--*/
{
    PIRP pIrp;
    NTSTATUS status;
    IO_STATUS_BLOCK IoStatusBlock;

    PAGED_CODE();
    
    KeClearEvent(PEvent);

    // Allocate an IRP - No need to release
    // When the next-lower driver completes this IRP, the IO Mgr releases it.

    pIrp = IoBuildDeviceIoControlRequest(Ioctl, PDevObj, PInBuffer, InBufferLen,
                                         POutBuffer, OutBufferLen, Internal,
                                         PEvent, &IoStatusBlock);

    if (pIrp == NULL) {
        Serenum_KdPrint_Def (SER_DBG_SS_ERROR, ("Failed to allocate IRP\n"));
        return STATUS_INSUFFICIENT_RESOURCES;
    }

    status = Serenum_IoSyncReq(PDevObj, pIrp, PEvent);


    if (status == STATUS_SUCCESS) {
       status = IoStatusBlock.Status;
    }

    return status;
}


NTSTATUS
Serenum_IoSyncReq(PDEVICE_OBJECT PDevObj, IN PIRP PIrp, PKEVENT PEvent)
/*++

Routine Description:
    Performs a synchronous IO request by waiting on the event object
    passed to it.  The IRP is deallocated by the IO system when finished.

Return value:
    NTSTATUS

--*/
{
   NTSTATUS status;
   PLARGE_INTEGER Timeout=NULL;

   PAGED_CODE();
    
   status = IoCallDriver(PDevObj, PIrp);

   if (status == STATUS_PENDING) {
      // wait for it...
      __analysis_assume(Timeout!=NULL);
      status = KeWaitForSingleObject(PEvent, Executive, KernelMode, FALSE,
                                     Timeout);
   }

    return status;
}

NTSTATUS
Serenum_Wait(IN PKTIMER Timer, IN LARGE_INTEGER DueTime)
/*++

Routine Description:
    Performs a wait for the specified time.
    NB: Negative time is relative to the current time.  Positive time
    represents an absolute time to wait until.

Return value:
    NTSTATUS

--*/
{
   PAGED_CODE();
    
   if (KeSetTimer(Timer, DueTime, NULL)) {
      Serenum_KdPrint_Def(SER_DBG_SS_INFO, ("Timer already set: %p\n", Timer));
   }

   return KeWaitForSingleObject(Timer, Executive, KernelMode, FALSE, NULL);
}


NTSTATUS
Serenum_CompletionRoutine(
    PDEVICE_OBJECT   DeviceObject,
    PIRP             Irp,
    PVOID            Context
    )
/*++

Routine Description:

    The completion routine for plug & play irps that needs to be
    processed first by the lower drivers.

Arguments:

   DeviceObject - pointer to a device object.

   Irp - pointer to an I/O Request Packet.

   Context - pointer to an event object.

Return Value:

      NT status code

--*/
{
    PCOMMON_DEVICE_DATA commonData=(PCOMMON_DEVICE_DATA)DeviceObject->DeviceExtension;
    UNREFERENCED_PARAMETER(Context);    
    
    // the driver must mark the irp as pending if PendingReturned is set.
    if (Irp->PendingReturned == TRUE){
        IoMarkIrpPending(Irp);
    }
    // release the removelock
    IoReleaseRemoveLock(&commonData->RemoveLock, Irp); 
    return STATUS_CONTINUE_COMPLETION;
    
}




NTSTATUS
Serenum_EnumComplete (
    IN PDEVICE_OBJECT   DeviceObject,
    IN PIRP             Irp,
    IN PVOID            Context
    )
/*++
Routine Description:
    A completion routine for use when calling the lower device objects to
    which our bus (FDO) is attached.  It sets the event for the synchronous
    calls done.

--*/
{
    UNREFERENCED_PARAMETER(DeviceObject);
    UNREFERENCED_PARAMETER(Irp);

#pragma warning(suppress: 6387)
    KeSetEvent((PKEVENT)Context, IO_NO_INCREMENT, FALSE);
    // No special priority
    // No Wait

    return STATUS_MORE_PROCESSING_REQUIRED; // Keep this IRP
}


NTSTATUS
Serenum_ReadSerialPort(
  _Out_writes_(Buflen) OUT PCHAR PReadBuffer, IN USHORT Buflen,
                       IN ULONG Timeout, OUT PUSHORT nActual,
                       OUT PIO_STATUS_BLOCK PIoStatusBlock,
                       IN const PFDO_DEVICE_DATA FdoData)
{
    NTSTATUS status;
    PIRP pIrp;
    LARGE_INTEGER startingOffset;
    KEVENT event;
    SERIAL_TIMEOUTS timeouts;

    PAGED_CODE();
    
    startingOffset.QuadPart = (LONGLONG) 0;
    //
    // Set the proper timeouts for the read
    //

    timeouts.ReadIntervalTimeout = MAXULONG;
    timeouts.ReadTotalTimeoutMultiplier = MAXULONG;
    timeouts.ReadTotalTimeoutConstant = Timeout;
    timeouts.WriteTotalTimeoutMultiplier = 0;
    timeouts.WriteTotalTimeoutConstant = 0;

    KeInitializeEvent(&event, NotificationEvent, FALSE);

    status = Serenum_IoSyncIoctlEx(IOCTL_SERIAL_SET_TIMEOUTS, FALSE,
                                   FdoData->TopOfStack, &event, &timeouts,
                                   sizeof(timeouts), NULL, 0);

    if (!NT_SUCCESS(status)) {
       return status;
    }

    Serenum_KdPrint(FdoData, SER_DBG_SS_TRACE, ("Read pending...\n"));

    *nActual = 0;

    while (*nActual < Buflen) {
        KeClearEvent(&event);

        pIrp = IoBuildSynchronousFsdRequest(IRP_MJ_READ, FdoData->TopOfStack,
                                            PReadBuffer, 1, &startingOffset,
                                            &event, PIoStatusBlock);

        if (pIrp == NULL) {
            Serenum_KdPrint(FdoData, SER_DBG_SS_ERROR, ("Failed to allocate IRP"
                                                        "\n"));
            return STATUS_INSUFFICIENT_RESOURCES;
        }

        status = IoCallDriver(FdoData->TopOfStack, pIrp);

        if (status == STATUS_PENDING) {

            //
            // Wait for the IRP
            //

            status = KeWaitForSingleObject(&event, Executive, KernelMode,
                                           FALSE, NULL);

            if (status == STATUS_SUCCESS) {
               status = PIoStatusBlock->Status;
            }
        }

        if (!NT_SUCCESS(status) || status == STATUS_TIMEOUT) {
           Serenum_KdPrint (FdoData, SER_DBG_SS_ERROR,
                            ("IO Call failed with status %x\n", status));
           return status;
        }

        *nActual += (USHORT)PIoStatusBlock->Information;
        PReadBuffer += (USHORT)PIoStatusBlock->Information;
    }

    return status;
}


NTSTATUS
Serenum_GetRegistryKeyValue(
    _In_ HANDLE Handle,
    _In_reads_bytes_(KeyNameStringLength) PWCHAR KeyNameString,
    _In_ ULONG KeyNameStringLength,
    _Out_writes_bytes_to_(DataLength, *ActualLength) PVOID Data,
    _In_ ULONG DataLength,
    _Out_ PULONG ActualLength
    )
/*++

Routine Description:

    Reads a registry key value from an already opened registry key.

Arguments:

    Handle              Handle to the opened registry key

    KeyNameString       ANSI string to the desired key

    KeyNameStringLength Length of the KeyNameString

    Data                Buffer to place the key value in

    DataLength          Length of the data buffer

Return Value:

    STATUS_SUCCESS if all works, otherwise status of system call that
    went wrong.

--*/
{
    UNICODE_STRING              keyName;
    ULONG                       length;
    PKEY_VALUE_FULL_INFORMATION fullInfo = NULL;

    NTSTATUS                    ntStatus = STATUS_INSUFFICIENT_RESOURCES;

#pragma warning(suppress: 26035)
#pragma warning(suppress: 26018)
	RtlInitUnicodeString (&keyName, KeyNameString);

    length = sizeof(KEY_VALUE_FULL_INFORMATION) + KeyNameStringLength
      + DataLength;
    fullInfo = ExAllocatePoolZero(PagedPool, length,SERENUM_POOL_TAG);

    if (ActualLength != NULL) {
       *ActualLength = 0;
    }

    if (fullInfo) {
#pragma warning(suppress: 26018)
        ntStatus = ZwQueryValueKey(Handle, &keyName, KeyValueFullInformation,
                                   fullInfo, length, &length);

        if (NT_SUCCESS(ntStatus)) {
            //
            // If there is enough room in the data buffer, copy the output
            //

            if (DataLength >= fullInfo->DataLength) {
                RtlCopyMemory(Data, ((PUCHAR)fullInfo) + fullInfo->DataOffset,
                              fullInfo->DataLength);
                if (ActualLength != NULL) {
                   *ActualLength = fullInfo->DataLength;
                }
            }
        }

        ExFreePoolWithTag(fullInfo,SERENUM_POOL_TAG);
        fullInfo = NULL;
    }

    if (!NT_SUCCESS(ntStatus) && !NT_ERROR(ntStatus)) {
       if (ntStatus == STATUS_BUFFER_OVERFLOW) {
          ntStatus = STATUS_BUFFER_TOO_SMALL;
       } else {
          ntStatus = STATUS_UNSUCCESSFUL;
       }
    }
#pragma warning(suppress: 26045)
	return ntStatus;
}


VOID
SerenumWaitForEnumThreadTerminate(IN PFDO_DEVICE_DATA PFdoData)
{
    KIRQL oldIrql;
    PVOID pThreadObj;

    //
    // Take a reference under the lock so the thread can't disappear on us.
    //

    KeAcquireSpinLock(&PFdoData->EnumerationLock, &oldIrql);

    //
    // If the work item beat us, then the thread is done and we can
    // delete/stop/unload.  Otherwise, we have to wait.  We can use
    // the reference we stole to hold the object around.
    //

    if (PFdoData->ThreadObj != NULL) {
        pThreadObj = PFdoData->ThreadObj;
        PFdoData->ThreadObj = NULL;
        PFdoData->EnumFlags &= ~SERENUM_ENUMFLAG_PENDING;
    } else {
        pThreadObj = NULL;
    }

    KeReleaseSpinLock(&PFdoData->EnumerationLock, oldIrql);

    if (pThreadObj != NULL) {
        KeWaitForSingleObject(pThreadObj, Executive, KernelMode, FALSE, NULL);
        ObDereferenceObject(pThreadObj);
    }
}


IO_WORKITEM_ROUTINE
SerenumEnumThreadWorkItem;

VOID
SerenumEnumThreadWorkItem(IN PDEVICE_OBJECT PDevObj, IN PVOID PFdoData)
{
    PFDO_DEVICE_DATA pFdoData = (PFDO_DEVICE_DATA)PFdoData;
    KIRQL oldIrql;
    PVOID pThreadObj;
    PIO_WORKITEM pWorkItem;

    _Analysis_assume_(pFdoData != NULL); // // Not NULL when passed to IoQueueWorkItem()
    UNREFERENCED_PARAMETER(PDevObj);

    //
    // See if the delete/stop code beat us to the thread obj.
    // If not, we can derefence the thread.
    //

    KeAcquireSpinLock(&pFdoData->EnumerationLock, &oldIrql);

    if (pFdoData->ThreadObj != NULL) {
        pThreadObj = pFdoData->ThreadObj;
        pFdoData->ThreadObj = NULL;
        pFdoData->EnumFlags &= ~SERENUM_ENUMFLAG_PENDING;
    } else {
        pThreadObj = NULL;
    }

    pWorkItem = pFdoData->EnumWorkItem;
    pFdoData->EnumWorkItem = NULL;

    KeReleaseSpinLock(&pFdoData->EnumerationLock, oldIrql);

    if (pThreadObj != NULL) {
        ObDereferenceObject(pThreadObj);
    }

    IoFreeWorkItem(pWorkItem);
}


KSTART_ROUTINE
SerenumEnumThread;

VOID
SerenumEnumThread(IN PVOID PFdoData)
{
   PFDO_DEVICE_DATA pFdoData = (PFDO_DEVICE_DATA)PFdoData;
   PIRP pIrp = NULL;
   NTSTATUS status;
   KIRQL oldIrql;
   PKTHREAD pThread;
   BOOLEAN sameDevice = TRUE;

   pThread = KeGetCurrentThread();

   KeSetPriorityThread(pThread, HIGH_PRIORITY);

   pIrp = IoAllocateIrp(pFdoData->TopOfStack->StackSize + 1, FALSE);

   if (pIrp == NULL) {
      status = STATUS_INSUFFICIENT_RESOURCES;
      goto SerenumEnumThreadErrOut;
   }
   IoSetNextIrpStackLocation(pIrp);
   status = Serenum_ReenumerateDevices(pIrp, pFdoData, &sameDevice);

SerenumEnumThreadErrOut:

   if (pIrp != NULL) {
      IoFreeIrp(pIrp);
   }

   KeAcquireSpinLock(&pFdoData->EnumerationLock, &oldIrql);

   if ((status == STATUS_SUCCESS) && !sameDevice) {
      pFdoData->EnumFlags |= SERENUM_ENUMFLAG_DIRTY;
   }

   KeReleaseSpinLock(&pFdoData->EnumerationLock, oldIrql);

   if ((status == STATUS_SUCCESS) && !sameDevice) {
      IoInvalidateDeviceRelations(pFdoData->UnderlyingPDO, BusRelations);
   }

   //
   // Queue a work item to release the last reference if remove/stop
   // hasn't already.
   //

   IoQueueWorkItem(pFdoData->EnumWorkItem, SerenumEnumThreadWorkItem,
                   DelayedWorkQueue, pFdoData);

   PsTerminateSystemThread(STATUS_SUCCESS);
}


NTSTATUS
SerenumStartProtocolThread(IN PFDO_DEVICE_DATA PFdoData)
{
   NTSTATUS status;
   OBJECT_ATTRIBUTES objAttrib;
   HANDLE handle;
   PVOID tmpObj;
   KIRQL oldIrql;
   PIO_WORKITEM pWorkItem;

   InitializeObjectAttributes(&objAttrib, NULL, OBJ_KERNEL_HANDLE, NULL, NULL);

   pWorkItem = IoAllocateWorkItem(PFdoData->Self);

   if (pWorkItem == NULL) {
       return STATUS_INSUFFICIENT_RESOURCES;
   }

   PFdoData->EnumWorkItem = pWorkItem;

   status = PsCreateSystemThread(&handle, THREAD_ALL_ACCESS, NULL, NULL, NULL,
                                 SerenumEnumThread, PFdoData);

   if (NT_SUCCESS(status)) {

      ASSERT(PFdoData->ThreadObj == NULL);

      //
      // We do this merely to get an object pointer that the remove
      // code can wait on.
      //

      status = ObReferenceObjectByHandle(handle, THREAD_ALL_ACCESS, NULL,
                                         KernelMode, &tmpObj, NULL);


      KeAcquireSpinLock(&PFdoData->EnumerationLock, &oldIrql);

      if (NT_SUCCESS(status)) {
         PFdoData->ThreadObj = tmpObj;
         KeReleaseSpinLock(&PFdoData->EnumerationLock, oldIrql);
      } else {
         //
         // The thread may be done by now, so no one would need to
         // synchronize with it.
         //

         PFdoData->ThreadObj = NULL;
         PFdoData->EnumWorkItem = NULL;
         KeReleaseSpinLock(&PFdoData->EnumerationLock, oldIrql);

      }

      //
      // Close the handle so the only references possible are the ones
      // for the thread itself and the one either the work item or
      // remove will take care of
      //

      ZwClose(handle);
   } else {
       PFdoData->EnumWorkItem = NULL;
       IoFreeWorkItem(pWorkItem);
   }

   return status;
}


#if _MSC_VER >= 1200
#pragma warning(pop)
#endif

#pragma warning(default:4127) // conditional expression is constant