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
|
/*++
Copyright (C) Microsoft Corporation. All rights reserved.
Module Name:
cdrom.h
Abstract:
Main header file for cdrom.sys.
This contains structure and function declarations as well as constant values.
Author:
Environment:
kernel mode only
Notes:
Revision History:
--*/
#ifndef __CDROM_H__
#define __CDROM_H__
#pragma warning(push)
#pragma warning(disable:4201) // nonstandard extension used : nameless struct/union
#pragma warning(disable:4214) // nonstandard extension used : bit field types other than int
#pragma warning(disable:4152) // nonstandard extension, function/data pointer conversion in expression
#include "wdf.h"
#include "ntddmmc.h"
#include "ntddcdvd.h"
#include "ntddcdrm.h"
#include "ntdddisk.h"
#include "ntddtape.h"
#include "ntddscsi.h"
#include "ntddvol.h"
#include "specstrings.h"
#include "cdromp.h"
// Set component ID for DbgPrintEx calls
#ifndef DEBUG_COMP_ID
#define DEBUG_COMP_ID DPFLTR_CDROM_ID
#endif
// Include initguid.h so GUID_CONSOLE_DISPLAY_STATE is declared
#include <initguid.h>
// Include header file and setup GUID for tracing
#include <storswtr.h>
#define WPP_GUID_CDROM (A4196372, C3C4, 42d5, 87BF, 7EDB2E9BCC27)
#ifndef WPP_CONTROL_GUIDS
#define WPP_CONTROL_GUIDS WPP_CONTROL_GUIDS_NORMAL_FLAGS(WPP_GUID_CDROM)
#endif
// This prototype is needed because, although NTIFS.H is now shipping with
// the WDK, can't include both it and the other headers we already use.
_IRQL_requires_max_(DISPATCH_LEVEL)
NTKERNELAPI
BOOLEAN
PsIsThreadTerminating(
_In_ PETHREAD Thread
);
//
//
extern CDROM_SCAN_FOR_SPECIAL_INFO CdromHackItems[];
#define CDROM_HACK_DEC_RRD (0x00000001)
#define CDROM_HACK_FUJITSU_FMCD_10x (0x00000002)
//#define CDROM_HACK_HITACHI_1750 (0x00000004) -- obsolete
#define CDROM_HACK_HITACHI_GD_2000 (0x00000008)
#define CDROM_HACK_TOSHIBA_SD_W1101 (0x00000010)
//#define CDROM_HACK_TOSHIBA_XM_3xx (0x00000020) -- obsolete
//#define CDROM_HACK_NEC_CDDA (0x00000040) -- obsolete
//#define CDROM_HACK_PLEXTOR_CDDA (0x00000080) -- obsolete
#define CDROM_HACK_BAD_GET_CONFIG_SUPPORT (0x00000100)
//#define CDROM_HACK_FORCE_READ_CD_DETECTION (0x00000200) -- obsolete
//#define CDROM_HACK_READ_CD_SUPPORTED (0x00000400) -- obsolete
#define CDROM_HACK_BAD_TYPE_ONE_GET_CONFIG (0x00000800)
#define CDROM_HACK_BAD_VENDOR_PROFILES (0x00001000)
#define CDROM_HACK_MSFT_VIRTUAL_ODD (0x00002000)
#define CDROM_HACK_LOCKED_PAGES (0x80000000) // not a valid flag to save
#define CDROM_HACK_VALID_FLAGS (0x00003fff)
#define CDROM_HACK_INVALID_FLAGS (~CDROM_HACK_VALID_FLAGS)
// A 64k buffer to be written takes the following amount of time:
// 1x CD == 75 sectors/sec == 0.4266667 seconds == 4266667 100ns units
// 4x CD == 300 sectors/sec == 0.1066667 seconds == 1066667 100ns units
// 10x CD == 300 sectors/sec == 0.0426667 seconds == 426667 100ns units
// 1x DVD == 676 sectors/sec == 0.0473373 seconds == 473373 100ns units
// 16x DVD == 10,816 sectors/sec == 0.0029586 seconds == 29586 100ns units
// 1x HDDVD == 2,230 sectors/sec == 0.0143498 seconds == 143498 100ns units
#define WRITE_RETRY_DELAY_CD_1x ((LONGLONG)4266667)
#define WRITE_RETRY_DELAY_CD_4x ((LONGLONG)1066667)
#define WRITE_RETRY_DELAY_CD_10x ((LONGLONG) 426667)
#define WRITE_RETRY_DELAY_DVD_1x ((LONGLONG) 473373)
#define WRITE_RETRY_DELAY_DVD_4x ((LONGLONG) 118343)
#define WRITE_RETRY_DELAY_DVD_16x ((LONGLONG) 29586)
#define WRITE_RETRY_DELAY_HDDVD_1x ((LONGLONG) 143498)
//
#define MAXIMUM_RETRIES 4
#define CDROM_GET_CONFIGURATION_TIMEOUT (0x4)
#define CDROM_READ_DISC_INFORMATION_TIMEOUT (0x4)
#define CDROM_TEST_UNIT_READY_TIMEOUT (0x14)
#define CDROM_GET_PERFORMANCE_TIMEOUT (0x14)
#define CDROM_READ_CAPACITY_TIMEOUT (0x14)
#define START_UNIT_TIMEOUT (60 * 4)
// Used to detect the loss of the autorun irp.
#define MEDIA_CHANGE_TIMEOUT_TIME 300
// Indicates whether is is safe to send StartUnit commands
// to this device. It will only be off for some removeable devices.
#define DEV_SAFE_START_UNIT 0x00000004
// Indicates that the device is connected to a backup power supply
// and hence write-through and synch cache requests may be ignored
#define DEV_POWER_PROTECTED 0x00000010
// The following CDROM_SPECIAL_ flags are set in ScanForSpecialFlags
// in the Device Extension
// Never Spin Up/Down the drive (may not handle properly)
#define CDROM_SPECIAL_DISABLE_SPIN_DOWN 0x00000001
//#define CDROM_SPECIAL_DISABLE_SPIN_UP 0x00000002
// Don't bother to lock the queue when powering down
// (used mostly to send a quick stop to a cdrom to abort audio playback)
//#define CDROM_SPECIAL_NO_QUEUE_LOCK 0x00000008
// Disable write cache due to known bugs
#define CDROM_SPECIAL_DISABLE_WRITE_CACHE 0x00000010
// Used to indicate that this request shouldn't invoke any power type operations
// like spinning up the drive.
#define SRB_CLASS_FLAGS_LOW_PRIORITY 0x10000000
// Used to indicate that an SRB is the result of a paging operation.
#define SRB_CLASS_FLAGS_PAGING 0x40000000
typedef struct _ERROR_RECOVERY_DATA {
MODE_PARAMETER_HEADER Header;
MODE_PARAMETER_BLOCK BlockDescriptor;
MODE_READ_RECOVERY_PAGE ReadRecoveryPage;
} ERROR_RECOVERY_DATA, *PERROR_RECOVERY_DATA;
// A compile-time check of the 30,000 limit not overflowing ULONG size...
// Note that it is not expected that a release (FRE) driver will normally
// have such a large history, instead using the compression function.
#define CDROM_INTERPRET_SENSE_INFO2_MAXIMUM_HISTORY_COUNT 30000
C_ASSERT( (MAXULONG - sizeof(SRB_HISTORY)) / 30000 >= sizeof(SRB_HISTORY_ITEM) );
// Intended to reuse a defined IOCTL code that not seen in Optical stack and does not require input parameter.
// This fake IOCTL is used used for MCN process sync-ed with serial queue.
#define IOCTL_MCN_SYNC_FAKE_IOCTL IOCTL_DISK_UPDATE_DRIVE_SIZE
/*++////////////////////////////////////////////////////////////////////////////
PCDROM_ERROR_HANDLER()
Routine Description:
This routine is a callback into the driver to handle errors. The queue
shall not be unfrozen when this error handler is called, even though the
SRB flags may mark the queue as having been frozen due to this SRB.
Irql:
This routine will be called at KIRQL <= DISPATCH_LEVEL
Arguments:
DeviceObject is the device object the error occurred on.
Srb is the Srb that was being processed when the error occurred.
Status may be overwritten by the routine if it decides that the error
was benign, or otherwise wishes to change the returned status code
for this command
Retry may be overwritten to specify that this command should or should
not be retried (if the callee supports retrying commands)
Return Value:
status
--*/
struct _CDROM_DEVICE_EXTENSION; // *PCDROM_DEVICE_EXTENSION;
typedef struct _CDROM_DEVICE_EXTENSION
CDROM_DEVICE_EXTENSION,
*PCDROM_DEVICE_EXTENSION;
typedef
VOID
(*PCDROM_ERROR_HANDLER) (
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_ PSCSI_REQUEST_BLOCK Srb,
_Inout_ PNTSTATUS Status,
_Inout_ PBOOLEAN Retry
);
// CdRom driver extension
typedef struct _CDROM_DRIVER_EXTENSION {
ULONG Version;
PDRIVER_OBJECT DriverObject;
ULONG Flags;
} CDROM_DRIVER_EXTENSION, *PCDROM_DRIVER_EXTENSION;
#define CDROM_FLAG_WINPE_MODE 0x00000001
WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(CDROM_DRIVER_EXTENSION, DriverGetExtension)
#define CdromMmcUpdateComplete 0
#define CdromMmcUpdateRequired 1
#define CdromMmcUpdateStarted 2
typedef struct _CDROM_MMC_EXTENSION {
BOOLEAN IsMmc; // mmc device
BOOLEAN IsAACS; // aacs compatible device
BOOLEAN IsWriter; // the drive is a writer or not
BOOLEAN WriteAllowed; // currently allow write request or not
BOOLEAN IsCssDvd; // A CSS protected DVD or CPPM-protected DVDAudio media in Drive.
BOOLEAN StreamingReadSupported; // the drive supports streaming for reads
BOOLEAN StreamingWriteSupported; // the drive supports streaming for writes
LONG UpdateState;
// The feature number defines the level and form of
// validation that needs to be performed on Io requests
FEATURE_NUMBER ValidationSchema;
ULONG Blocking;
SCSI_REQUEST_BLOCK CapabilitiesSrb;
PIRP CapabilitiesIrp;
WDFREQUEST CapabilitiesRequest;
SENSE_DATA CapabilitiesSenseData;
_Field_size_bytes_(CapabilitiesBufferSize)
PGET_CONFIGURATION_HEADER CapabilitiesBuffer;
ULONG CapabilitiesBufferSize;
PMDL CapabilitiesMdl;
BOOLEAN ReadCdC2Pointers;
BOOLEAN ReadCdSubCode;
} CDROM_MMC_EXTENSION, *PCDROM_MMC_EXTENSION;
typedef struct _CDROM_SCRATCH_READ_WRITE_CONTEXT {
// Information about the data that we need to read/write
ULONG PacketsCount;
ULONG TransferedBytes;
ULONG EntireXferLen;
ULONG MaxLength;
PUCHAR DataBuffer;
LARGE_INTEGER StartingOffset;
BOOLEAN IsRead;
// A pointer to the SRB history item to be filled upon completion
PSRB_HISTORY_ITEM SrbHistoryItem;
} CDROM_SCRATCH_READ_WRITE_CONTEXT, *PCDROM_SCRATCH_READ_WRITE_CONTEXT;
// Many commands get double-buffered. Since the max
// transfer size is typically 64k, most of these requests
// can be handled with a single pre-allocated buffer.
typedef struct _CDROM_SCRATCH_CONTEXT {
_Field_range_(4*1024, 64*1024) ULONG ScratchBufferSize; // 0x1000..0x10000 (4k..64k)
_Field_size_bytes_(ScratchBufferSize) PVOID ScratchBuffer; // used to get data for clients
PMDL ScratchBufferMdl; // used to get data for clients
WDFREQUEST ScratchRequest;
PSCSI_REQUEST_BLOCK ScratchSrb;
PSENSE_DATA ScratchSense;
PSRB_HISTORY ScratchHistory;
// This MDL is used to performed the request whose required transfer size is bigger than adaptor's max.
PMDL PartialMdl;
BOOLEAN PartialMdlIsBuilt;
// For debugging, set/clear this field when using the scratch buffer/request
PVOID ScratchInUse;
PCSTR ScratchInUseFileName;
ULONG ScratchInUseLineNumber;
// Stuff for asynchronous retrying of the transfer.
ULONG NumRetries;
// Read Write context
CDROM_SCRATCH_READ_WRITE_CONTEXT ScratchReadWriteContext;
} CDROM_SCRATCH_CONTEXT, *PCDROM_SCRATCH_CONTEXT;
// Context structure for the IOCTL work item
typedef struct _CDROM_IOCTL_CONTEXT {
WDFREQUEST OriginalRequest;
} CDROM_IOCTL_CONTEXT, *PCDROM_IOCTL_CONTEXT;
// Context structure for the read/write work item
typedef struct _CDROM_READ_WRITE_CONTEXT {
WDFREQUEST OriginalRequest;
} CDROM_READ_WRITE_CONTEXT, *PCDROM_READ_WRITE_CONTEXT;
typedef struct _CDROM_DATA {
CDROM_MMC_EXTENSION Mmc;
// hack flags for ScanForSpecial routines
ULONG_PTR HackFlags;
// the error handling routines need to be per-device, not per-driver....
PCDROM_ERROR_HANDLER ErrorHandler;
// Indicates whether an audio play operation is currently being performed.
// Only thing this does is prevent reads and toc requests while playing audio.
BOOLEAN PlayActive;
// indicate we need to pick a default dvd region for the user if we can
ULONG PickDvdRegion;
// The well known name link for this device.
UNICODE_STRING WellKnownName;
// We need to distinguish between the two...
ULONG MaxPageAlignedTransferBytes;
ULONG MaxUnalignedTransferBytes;
// Indicates that this is a DEC RRD cdrom.
// This drive requires software to fix responses from the faulty firmware
BOOLEAN IsDecRrd;
// Storage for the error recovery page. This is used as the method
// to switch block sizes for some drive-specific error recovery routines.
// ERROR_RECOVERY_DATA recoveryData; //obsolete along with error process for TOSHIBA_XM_3xx
// Indicates that the device is in exclusive mode and only
// the requests from the exclusive owner will be processed.
WDFFILEOBJECT ExclusiveOwner;
// Caller name of the owner, if the device is in exclusive mode.
UCHAR CallerName[CDROM_EXCLUSIVE_CALLER_LENGTH];
// Indicates that the device speed should be set to
// default value on the next media change.
BOOLEAN RestoreDefaults;
// How long to wait between retries if a READ/WRITE irp
// gets a LWIP (2/4/7, 2/4/8)?
LONGLONG ReadWriteRetryDelay100nsUnits;
// Cached Device Type information. Maybe FILE_DEVICE_CD_ROM or FILE_DEVICE_DVD
// CommonExtension.DevInfo->DeviceType maybe FILE_DEVICE_CD_ROM when this field is FILE_DEVICE_DVD
DEVICE_TYPE DriveDeviceType;
_Field_size_bytes_(CachedInquiryDataByteCount)
PINQUIRYDATA CachedInquiryData;
ULONG CachedInquiryDataByteCount;
} CDROM_DATA, *PCDROM_DATA;
typedef struct _CDROM_POWER_OPTIONS {
ULONG PowerDown : 1;
ULONG LockQueue : 1;
ULONG HandleSpinDown : 1;
ULONG HandleSpinUp : 1;
ULONG Reserved : 27;
} CDROM_POWER_OPTIONS, *PCDROM_POWER_OPTIONS;
// this is a private enum, but must be kept here
// to properly compile size of CDROM_DEVICE_EXTENSION
typedef enum {
PowerDownDeviceInitial,
PowerDownDeviceLocked,
PowerDownDeviceQuiesced,
PowerDownDeviceFlushed,
PowerDownDeviceStopped,
PowerDownDeviceOff,
PowerDownDeviceUnlocked
} CDROM_POWER_DOWN_STATE;
// this is a private enum, but must be kept here
// to properly compile size of CDROM_DEVICE_EXTENSION
typedef enum {
PowerUpDeviceInitial,
PowerUpDeviceLocked,
PowerUpDeviceOn,
PowerUpDeviceStarted,
PowerUpDeviceUnlocked
} CDROM_POWER_UP_STATE;
// this is a private structure, but must be kept here
// to properly compile size of CDROM_DEVICE_EXTENSION
typedef struct _CDROM_POWER_CONTEXT {
BOOLEAN InUse;
LARGE_INTEGER StartTime;
LARGE_INTEGER Step1CompleteTime; // for SYNC CACHE in power down case
LARGE_INTEGER CompleteTime;
union {
CDROM_POWER_DOWN_STATE PowerDown;
CDROM_POWER_UP_STATE PowerUp; // currently not used.
} PowerChangeState;
CDROM_POWER_OPTIONS Options;
WDFREQUEST PowerRequest;
SCSI_REQUEST_BLOCK Srb;
SENSE_DATA SenseData;
ULONG RetryCount;
LONGLONG RetryIntervalIn100ns;
} CDROM_POWER_CONTEXT, *PCDROM_POWER_CONTEXT;
// device extension structure
typedef struct _CDROM_DEVICE_EXTENSION {
// Version control field
ULONG Version;
// structure size
ULONG Size;
// the structure is fully ready to use
BOOLEAN IsInitialized;
// the device is active
BOOLEAN IsActive;
// the device is surprise removed.
BOOLEAN SurpriseRemoved;
// Back pointer to device object
WDFDEVICE Device;
// Save IoTarget here, do not retrieve anytime.
WDFIOTARGET IoTarget;
// Additional WDF queue for serial I/O processing
WDFQUEUE SerialIOQueue;
// A separate queue for all the create file requests in sync with device
// removal
WDFQUEUE CreateQueue;
//Main timer of driver, will do Mcn work. (once per second)
WDFTIMER MainTimer;
// Pointer to the initialization data for this driver. This is more
// efficient than constantly getting the driver extension.
PCDROM_DRIVER_EXTENSION DriverExtension;
// WDM device information
PDEVICE_OBJECT DeviceObject;
// Pointer to the physical device object we attached to
PDEVICE_OBJECT LowerPdo;
// FILE_DEVICE_CD_ROM -- 2
DEVICE_TYPE DeviceType;
// The name of the object
UNICODE_STRING DeviceName;
// System device number
ULONG DeviceNumber;
// Values for the flags are below.
USHORT DeviceFlags;
// Flags for special behaviour required by different hardware,
// such as never spinning down or disabling advanced features such as write cache
ULONG ScanForSpecialFlags;
// Add default Srb Flags.
ULONG SrbFlags;
// Request timeout in seconds;
ULONG TimeOutValue;
//The SCSI address of the device.
SCSI_ADDRESS ScsiAddress;
// Buffer for drive parameters returned in IO device control.
DISK_GEOMETRY DiskGeometry;
// Log2 of sector size
UCHAR SectorShift;
// Length of partition in bytes
LARGE_INTEGER PartitionLength;
// Number of bytes before start of partition
LARGE_INTEGER StartingOffset;
// Interface name string returned by IoRegisterDeviceInterface.
UNICODE_STRING MountedDeviceInterfaceName;
// Device capabilities
PSTORAGE_DEVICE_DESCRIPTOR DeviceDescriptor;
// SCSI port driver capabilities
PSTORAGE_ADAPTER_DESCRIPTOR AdapterDescriptor;
// Device power properties
PDEVICE_POWER_DESCRIPTOR PowerDescriptor;
// Request Sense Buffer
PSENSE_DATA SenseData;
// Total number of SCSI protocol errors on the device.
ULONG ErrorCount;
// Lock count for removable media.
LONG LockCount;
LONG ProtectedLockCount;
LONG InternalLockCount;
KEVENT EjectSynchronizationEvent;
WDFWAITLOCK EjectSynchronizationLock;
// Indicates that the necessary data structures for media change
// detection have been initialized.
PMEDIA_CHANGE_DETECTION_INFO MediaChangeDetectionInfo;
// Contains necessary data structures for ZPODD support.
PZERO_POWER_ODD_INFO ZeroPowerODDInfo;
// File system context. Used for kernel-mode requests to disable autorun.
FILE_OBJECT_CONTEXT KernelModeMcnContext;
// Count of media changes. This field is only valid for the root partition
// (ie. if PhysicalDevice == NULL).
ULONG MediaChangeCount;
// Storage for a release queue request.
WDFSPINLOCK ReleaseQueueSpinLock;
WDFREQUEST ReleaseQueueRequest;
SCSI_REQUEST_BLOCK ReleaseQueueSrb;
WDFMEMORY ReleaseQueueInputMemory; //This is a wrapper of ReleaseQueueSrb
BOOLEAN ReleaseQueueNeeded;
BOOLEAN ReleaseQueueInProgress;
// Context structure for power operations. Since we can only have
// one D irp at any time in the stack we don't need to worry about
// allocating multiple of these structures.
CDROM_POWER_CONTEXT PowerContext;
BOOLEAN PowerDownInProgress;
#if (NTDDI_VERSION >= NTDDI_WIN8)
BOOLEAN IsVolumeOnlinePending;
WDFQUEUE ManualVolumeReadyQueue;
#endif
// Lock for Shutdown/Flush operations that need to stop/start the queue
WDFWAITLOCK ShutdownFlushWaitLock;
// device specific data area
CDROM_DATA DeviceAdditionalData;
// scratch buffer related fields.
CDROM_SCRATCH_CONTEXT ScratchContext;
// Hold new private data that only classpnp should modify
// in this structure.
PCDROM_PRIVATE_FDO_DATA PrivateFdoData;
// Work item for async reads and writes and its context
WDFWORKITEM ReadWriteWorkItem;
CDROM_READ_WRITE_CONTEXT ReadWriteWorkItemContext;
// Auxiliary WDF object for processing ioctl requests that need to go down
// to the port driver.
WDFWORKITEM IoctlWorkItem;
CDROM_IOCTL_CONTEXT IoctlWorkItemContext;
} CDROM_DEVICE_EXTENSION, *PCDROM_DEVICE_EXTENSION;
WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(CDROM_DEVICE_EXTENSION, DeviceGetExtension)
// a type definition for functions to be called after synchronization
typedef
NTSTATUS
SYNC_HANDLER (
_In_ WDFDEVICE Device,
_In_ WDFREQUEST Request
);
typedef SYNC_HANDLER *PSYNC_HANDLER ;
typedef struct _CDROM_REQUEST_CONTEXT {
PCDROM_DEVICE_EXTENSION DeviceExtension;
WDFREQUEST OriginalRequest;
LARGE_INTEGER TimeReceived;
LARGE_INTEGER TimeSentDownFirstTime;
LARGE_INTEGER TimeSentDownLasttTime;
ULONG RetriedCount;
// Used to send down an incoming IOCTL in the original context
BOOLEAN SyncRequired;
PKEVENT SyncEvent;
PSYNC_HANDLER SyncCallback;
//
// Used for READ/WRITE requests.
// The reason why kernel primitives are used for the spinlock and
// the timer instead of WDF object is there is a race condition
// between the cancel callback and the timer routine.
// Because of this, the timer and the spin lock need to be associated
// per request rather than using shared memory in the device extension
// and it is possible for the WDF object initialization to fail whereas
// the kernel primitives initialize provided memory. Initializing the
// kernel primitives will never fail. Since READ/WRITE is a critical
// code path, it is not desired for READs or WRITEs to fail due to
// an allocation failure that can be avoided and because it is not
// uncommon to see a failure during a READ or WRITE that may not
// occur upon a retry.
//
KSPIN_LOCK ReadWriteCancelSpinLock;
KTIMER ReadWriteTimer;
KDPC ReadWriteDpc;
BOOLEAN ReadWriteIsCompleted;
BOOLEAN ReadWriteRetryInitialized;
} CDROM_REQUEST_CONTEXT, *PCDROM_REQUEST_CONTEXT;
WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(CDROM_REQUEST_CONTEXT, RequestGetContext)
// Define context structure for asynchronous completions.
typedef struct _COMPLETION_CONTEXT {
WDFDEVICE Device;
SCSI_REQUEST_BLOCK Srb;
} COMPLETION_CONTEXT, *PCOMPLETION_CONTEXT;
//
#define SCSI_CDROM_TIMEOUT 10
#define SCSI_CHANGER_BONUS_TIMEOUT 10
//
// This value is used as the upper limit for all commands CDROM sends down
// to device, unless the default timeout value is overriden by registry value
// "TimeOutValue" and it is larger than this value.
//
#define SCSI_CDROM_OPC_TIMEOUT 260
#define HITACHI_MODE_DATA_SIZE 12
#define MODE_DATA_SIZE 64
#define RAW_SECTOR_SIZE 2352
#define COOKED_SECTOR_SIZE 2048
#define CDROM_SRB_LIST_SIZE 4
#define PLAY_ACTIVE(x) (x->DeviceAdditionalData.PlayActive)
#define MSF_TO_LBA(Minutes,Seconds,Frames) \
(ULONG)((60 * 75 * (Minutes)) + (75 * (Seconds)) + ((Frames) - 150))
// Sector types for READ_CD
#define ANY_SECTOR 0
#define CD_DA_SECTOR 1
#define YELLOW_MODE1_SECTOR 2
#define YELLOW_MODE2_SECTOR 3
#define FORM2_MODE1_SECTOR 4
#define FORM2_MODE2_SECTOR 5
#define MAX_COPY_PROTECT_AGID 4
#ifdef ExAllocatePool
#undef ExAllocatePool
#define ExAllocatePool #assert(FALSE)
#endif
// memory allocation tags
// Sc?? - Mass storage driver tags
// ScC<number> - Class driver misc allocations
// ScC? - CdRom
#define CDROM_TAG_AUTORUN_DISABLE 'ACcS' // "ScCA" - Autorun disable functionality
#define CDROM_TAG_MEDIA_CHANGE_DETECTION 'aCcS' // "ScCa" - Media change detection
#define CDROM_TAG_SCRATCH 'BCcS' // "ScSB" - Scratch buffer (usually 64k)
#define CDROM_TAG_GET_CONFIG 'CCcS' // "ScCC" - Ioctl GET_CONFIGURATION
#define CDROM_TAG_COMPLETION_CONTEXT 'cCcS' // "ScCc" - Context of completion routine
#define CDROM_TAG_DESCRIPTOR 'DCcS' // "ScCD" - Adaptor & Device descriptor buffer
#define CDROM_TAG_DISC_INFO 'dCcS' // "ScCd" - Disc information
#define CDROM_TAG_SYNC_EVENT 'eCcS' // "ScCe" - Request sync event
#define CDROM_TAG_FEATURE 'FCcS' // "ScCF" - Feature descriptor
#define CDROM_TAG_GESN 'GCcS' // "ScCG" - GESN buffer
#define CDROM_TAG_SENSE_INFO 'ICcS' // "ScCI" - Sense info buffers
#define CDROM_TAG_INQUIRY 'iCcS' // "ScCi" - Cached inquiry buffer
#define CDROM_TAG_MODE_DATA 'MCcS' // "ScCM" - Mode data buffer
#define CDROM_TAG_STREAM 'OCCS' // "SCCO" - Set stream buffer
#define CDROM_TAG_NOTIFICATION 'oCcS' // "ScCo" - Device Notification buffer
#define CDROM_TAG_PLAY_ACTIVE 'pCcS' // "ScCp" - Play active checks
#define CDROM_TAG_REGISTRY 'rCcS' // "ScCr" - Registry string
#define CDROM_TAG_SRB 'SCcS' // "ScCS" - Srb allocation
#define CDROM_TAG_STRINGS 'sCcS' // "ScCs" - Assorted string data
#define CDROM_TAG_UPDATE_CAP 'UCcS' // "ScCU" - Update capacity path
#define CDROM_TAG_ZERO_POWER_ODD 'ZCcS' // "ScCZ" - Zero Power ODD
#define DVD_TAG_READ_KEY 'uCcS' // "ScCu" - Read buffer for dvd key
#define DVD_TAG_RPC2_CHECK 'VCcS' // "ScCV" - Read buffer for dvd/rpc2 check
#define DVD_TAG_DVD_REGION 'vCcS' // "ScCv" - Read buffer for rpc2 check
#define DVD_TAG_SECURITY 'XCcS' // "ScCX" - Security descriptor
// registry keys and data entry names.
#define CDROM_SUBKEY_NAME (L"CdRom") // store new settings here
#define CDROM_READ_CD_NAME (L"ReadCD") // READ_CD support previously detected
#define CDROM_NON_MMC_DRIVE_NAME (L"NonMmc") // MMC commands hang
#define CDROM_TYPE_ONE_GET_CONFIG_NAME (L"NoTypeOneGetConfig") // Type One Get Config commands not supported
#define CDROM_NON_MMC_VENDOR_SPECIFIC_PROFILE (L"NonMmcVendorSpecificProfile") // GET_CONFIG returns vendor specific header
// profiles that are not per spec (length divisible by 4)
#define DVD_DEFAULT_REGION (L"DefaultDvdRegion") // this is init. by the dvd class installer
#define DVD_MAX_REGION 8
// AACS defines
#define AACS_MKB_PACK_SIZE 0x8000 // does not include header
//enumeration of device interfaces need to be registered.
typedef enum {
CdRomDeviceInterface = 0, // CdRomClassGuid
MountedDeviceInterface // MOUNTDEV_MOUNTED_DEVICE_GUID
} CDROM_DEVICE_INTERFACES, *PCDROM_DEVICE_INTERFACES;
typedef
VOID
(*PCDROM_SCAN_FOR_SPECIAL_HANDLER) (
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_ ULONG_PTR Data
);
// Rountines Definition
#define FREE_POOL(_PoolPtr) \
if (_PoolPtr != NULL) { \
ExFreePool(_PoolPtr); \
_PoolPtr = NULL; \
}
#define EXCLUSIVE_MODE(_CdData) (_CdData->ExclusiveOwner != NULL)
#define EXCLUSIVE_OWNER(_CdData, _FileObject) (_CdData->ExclusiveOwner == _FileObject)
#define IS_SCSIOP_READ(opCode) \
((opCode == SCSIOP_READ6) || \
(opCode == SCSIOP_READ) || \
(opCode == SCSIOP_READ12) || \
(opCode == SCSIOP_READ16))
#define IS_SCSIOP_WRITE(opCode) \
((opCode == SCSIOP_WRITE6) || \
(opCode == SCSIOP_WRITE) || \
(opCode == SCSIOP_WRITE12) || \
(opCode == SCSIOP_WRITE16))
#define IS_SCSIOP_READWRITE(opCode) (IS_SCSIOP_READ(opCode) || IS_SCSIOP_WRITE(opCode))
// Bit Flag Macros
#define SET_FLAG(Flags, Bit) ((Flags) |= (Bit))
#define CLEAR_FLAG(Flags, Bit) ((Flags) &= ~(Bit))
#define TEST_FLAG(Flags, Bit) (((Flags) & (Bit)) != 0)
__inline
BOOLEAN
ValidChar(UCHAR Ch)
{
if (((Ch >= '0') && (Ch <= '9')) ||
(((Ch|0x20) >= 'a') && ((Ch|0x20) <= 'z')) ||
(strchr(" .,:;_-", Ch) != NULL))
{
return TRUE;
}
return FALSE;
}
// could be #define, but this allows typechecking
__inline
BOOLEAN
PORT_ALLOCATED_SENSE(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_ PSCSI_REQUEST_BLOCK Srb
)
{
UNREFERENCED_PARAMETER(DeviceExtension);
return (BOOLEAN)((TEST_FLAG(Srb->SrbFlags, SRB_FLAGS_PORT_DRIVER_ALLOCSENSE) &&
TEST_FLAG(Srb->SrbFlags, SRB_FLAGS_FREE_SENSE_BUFFER))
);
}
__inline
VOID
FREE_PORT_ALLOCATED_SENSE_BUFFER(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_ PSCSI_REQUEST_BLOCK Srb
)
{
#ifndef DEBUG
UNREFERENCED_PARAMETER(DeviceExtension);
#endif
NT_ASSERT(TEST_FLAG(Srb->SrbFlags, SRB_FLAGS_PORT_DRIVER_ALLOCSENSE));
NT_ASSERT(TEST_FLAG(Srb->SrbFlags, SRB_FLAGS_FREE_SENSE_BUFFER));
NT_ASSERT(Srb->SenseInfoBuffer != DeviceExtension->SenseData);
ExFreePool(Srb->SenseInfoBuffer);
Srb->SenseInfoBuffer = NULL;
Srb->SenseInfoBufferLength = 0;
CLEAR_FLAG(Srb->SrbFlags, SRB_FLAGS_FREE_SENSE_BUFFER);
return;
}
// Standard driver entry function
DRIVER_INITIALIZE DriverEntry;
// Driver Event callbacks
EVT_WDF_DRIVER_DEVICE_ADD DriverEvtDeviceAdd;
EVT_WDF_OBJECT_CONTEXT_CLEANUP DriverEvtCleanup;
// Device Event callbacks
EVT_WDF_OBJECT_CONTEXT_CLEANUP DeviceEvtCleanup;
EVT_WDF_FILE_CLOSE DeviceEvtFileClose;
EVT_WDF_IO_IN_CALLER_CONTEXT DeviceEvtIoInCallerContext;
EVT_WDF_DEVICE_SELF_MANAGED_IO_INIT DeviceEvtSelfManagedIoInit;
EVT_WDF_DEVICE_SELF_MANAGED_IO_CLEANUP DeviceEvtSelfManagedIoCleanup;
EVT_WDF_DEVICE_D0_ENTRY DeviceEvtD0Entry;
EVT_WDF_DEVICE_D0_EXIT DeviceEvtD0Exit;
EVT_WDF_DEVICE_SURPRISE_REMOVAL DeviceEvtSurpriseRemoval;
// Create Queue Event callbacks
EVT_WDF_IO_QUEUE_IO_DEFAULT CreateQueueEvtIoDefault;
// Sequential Queue Event callbacks
// We do not use KMDF annotation for the following function, because it handles
// both read and write requests and there is no single annotation for that.
VOID
SequentialQueueEvtIoReadWrite(
_In_ WDFQUEUE Queue,
_In_ WDFREQUEST Request,
_In_ size_t Length
);
EVT_WDF_IO_QUEUE_IO_DEVICE_CONTROL SequentialQueueEvtIoDeviceControl;
EVT_WDF_IO_QUEUE_IO_CANCELED_ON_QUEUE SequentialQueueEvtCanceledOnQueue;
// Miscellaneous request callbacks
EVT_WDF_OBJECT_CONTEXT_CLEANUP RequestEvtCleanup;
EVT_WDFDEVICE_WDM_IRP_PREPROCESS RequestProcessShutdownFlush;
EVT_WDFDEVICE_WDM_IRP_PREPROCESS RequestProcessSetPower;
// helper functions
_IRQL_requires_max_(PASSIVE_LEVEL)
NTSTATUS
DeviceClaimRelease(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_ BOOLEAN Release
);
_IRQL_requires_max_(APC_LEVEL)
VOID
DeviceReleaseMcnResources(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension
);
_IRQL_requires_max_(PASSIVE_LEVEL)
NTSTATUS
DeviceRetrieveDescriptor(
_In_ WDFDEVICE Device,
_In_ PSTORAGE_PROPERTY_ID PropertyId,
_Outptr_ PSTORAGE_DESCRIPTOR_HEADER* Descriptor
);
_IRQL_requires_max_(PASSIVE_LEVEL)
VOID
DeviceRetrieveHackFlagsFromRegistry(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension
);
_IRQL_requires_max_(APC_LEVEL)
VOID
DeviceHackFlagsScan(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_ ULONG_PTR Data
);
_IRQL_requires_max_(APC_LEVEL)
BOOLEAN
StringsAreMatched(
_In_opt_z_ PCHAR StringToMatch,
_In_z_ PCHAR TargetString
);
_IRQL_requires_max_(PASSIVE_LEVEL)
VOID
DeviceGetParameter(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_opt_ PWSTR SubkeyName,
_In_ PWSTR ParameterName,
_Inout_ PULONG ParameterValue // also default value
);
_IRQL_requires_max_(PASSIVE_LEVEL)
NTSTATUS
DeviceSetParameter(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_opt_z_ PWSTR SubkeyName,
_In_ PWSTR ParameterName,
_In_ ULONG ParameterValue
);
_IRQL_requires_max_(PASSIVE_LEVEL)
ULONG
DeviceGetTimeOutValueFromRegistry();
_IRQL_requires_max_(APC_LEVEL)
VOID
ScanForSpecialHandler(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_ ULONG_PTR HackFlags
);
_IRQL_requires_max_(PASSIVE_LEVEL)
NTSTATUS
DeviceSendSrbSynchronously(
_In_ WDFDEVICE Device,
_In_ PSCSI_REQUEST_BLOCK Srb,
_In_opt_ PVOID BufferAddress,
_In_ ULONG BufferLength,
_In_ BOOLEAN WriteToDevice,
_In_opt_ WDFREQUEST OriginalRequest
);
BOOLEAN
RequestSenseInfoInterpret(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_ WDFREQUEST Request, // get IRP MJ code, IoControlCode from it (or attached original request)
_In_ PSCSI_REQUEST_BLOCK Srb,
_In_ ULONG RetriedCount,
_Out_ NTSTATUS* Status,
_Out_opt_ _Deref_out_range_(0, MAXIMUM_RETRY_FOR_SINGLE_IO_IN_100NS_UNITS)
LONGLONG* RetryIntervalIn100ns
);
VOID
RequestSetReceivedTime(
_In_ WDFREQUEST Request
);
VOID
RequestSetSentTime(
_In_ WDFREQUEST Request
);
VOID
RequestClearSendTime(
_In_ WDFREQUEST Request
);
BOOLEAN
RequestSenseInfoInterpretForScratchBuffer(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_ ULONG RetriedCount,
_Out_ NTSTATUS* Status,
_Out_ _Deref_out_range_(0, MAXIMUM_RETRY_FOR_SINGLE_IO_IN_100NS_UNITS)
LONGLONG* RetryIntervalIn100ns
);
VOID
DeviceSendNotification(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_ const GUID* Guid,
_In_ ULONG ExtraDataSize,
_In_opt_ PVOID ExtraData
);
VOID
DeviceSendStartUnit(
_In_ WDFDEVICE Device
);
EVT_WDF_REQUEST_COMPLETION_ROUTINE DeviceAsynchronousCompletion;
VOID
DeviceReleaseQueue(
_In_ WDFDEVICE Device
);
EVT_WDF_REQUEST_COMPLETION_ROUTINE DeviceReleaseQueueCompletion;
VOID
DevicePerfIncrementErrorCount(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension
);
_IRQL_requires_max_(PASSIVE_LEVEL)
NTSTATUS
DeviceCacheDeviceInquiryData(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension
);
_IRQL_requires_max_(APC_LEVEL)
NTSTATUS
DeviceCacheGetConfigurationData(
_In_ WDFDEVICE Device
);
_IRQL_requires_max_(APC_LEVEL)
PVOID
DeviceFindFeaturePage(
_In_reads_bytes_(Length) PGET_CONFIGURATION_HEADER FeatureBuffer,
_In_ ULONG const Length,
_In_ FEATURE_NUMBER const Feature
);
_IRQL_requires_max_(APC_LEVEL)
VOID
DevicePrintAllFeaturePages(
_In_reads_bytes_(Usable) PGET_CONFIGURATION_HEADER Buffer,
_In_ ULONG const Usable
);
_IRQL_requires_max_(APC_LEVEL)
NTSTATUS
DeviceSetRawReadInfo(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension
);
_IRQL_requires_max_(PASSIVE_LEVEL)
NTSTATUS
MediaReadCapacity(
_In_ WDFDEVICE Device
);
_IRQL_requires_max_(APC_LEVEL)
VOID
MediaReadCapacityDataInterpret(
_In_ WDFDEVICE Device,
_In_ PREAD_CAPACITY_DATA ReadCapacityBuffer
);
_IRQL_requires_max_(APC_LEVEL)
NTSTATUS
DeviceInitReleaseQueueContext(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension
);
_IRQL_requires_max_(APC_LEVEL)
NTSTATUS
DeviceInitPowerContext(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension
);
_IRQL_requires_max_(APC_LEVEL)
NTSTATUS
DeviceCreateWellKnownName(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension
);
_IRQL_requires_max_(APC_LEVEL)
NTSTATUS
DeviceInitializeDvd(
_In_ WDFDEVICE Device
);
_IRQL_requires_max_(APC_LEVEL)
VOID
DevicePickDvdRegion(
_In_ WDFDEVICE Device
);
_IRQL_requires_max_(PASSIVE_LEVEL)
NTSTATUS
DeviceRegisterInterface(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_ CDROM_DEVICE_INTERFACES InterfaceType
);
_IRQL_requires_max_(DISPATCH_LEVEL)
NTSTATUS
DeviceSendPowerDownProcessRequest(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_opt_ PFN_WDF_REQUEST_COMPLETION_ROUTINE CompletionRoutine,
_In_opt_ WDFCONTEXT Context
);
_IRQL_requires_max_(APC_LEVEL)
VOID
DeviceScanForSpecial(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_ CDROM_SCAN_FOR_SPECIAL_INFO DeviceList[],
_In_ PCDROM_SCAN_FOR_SPECIAL_HANDLER Function
);
_IRQL_requires_max_(PASSIVE_LEVEL)
NTSTATUS
DeviceInitializeHotplugInfo(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension
);
NTSTATUS
DeviceErrorHandlerForMmc(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_ PSCSI_REQUEST_BLOCK Srb,
_Inout_ PNTSTATUS Status,
_Inout_ PBOOLEAN Retry
);
NTSTATUS
DeviceErrorHandlerForHitachiGD2000(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_ PSCSI_REQUEST_BLOCK Srb,
_Inout_ PNTSTATUS Status,
_Inout_ PBOOLEAN Retry
);
EVT_WDF_WORKITEM DeviceRestoreDefaultSpeed;
_IRQL_requires_max_(PASSIVE_LEVEL)
NTSTATUS
DeviceInitializeMediaChangeDetection(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension
);
VOID
DeviceSetMediaChangeStateEx(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_ MEDIA_CHANGE_DETECTION_STATE NewState,
_Inout_opt_ PMEDIA_CHANGE_DETECTION_STATE OldState
);
_IRQL_requires_max_(APC_LEVEL)
VOID
DeviceSendDelayedMediaChangeNotifications(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension
);
NTSTATUS
RequestSynchronizeProcessWithSerialQueue(
_In_ WDFDEVICE Device,
_In_ WDFREQUEST Request
);
_IRQL_requires_max_(PASSIVE_LEVEL)
NTSTATUS
DeviceCleanupProtectedLocks(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_ PFILE_OBJECT_CONTEXT FileObjectContext
);
_IRQL_requires_max_(APC_LEVEL)
NTSTATUS
DeviceCleanupDisableMcn(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_ PFILE_OBJECT_CONTEXT FileObjectContext
);
_IRQL_requires_max_(APC_LEVEL)
NTSTATUS
DeviceUnlockExclusive(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_ WDFFILEOBJECT FileObject,
_In_ BOOLEAN IgnorePreviousMediaChanges
);
_IRQL_requires_max_(PASSIVE_LEVEL)
NTSTATUS
RequestProcessSerializedIoctl(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_ WDFREQUEST Request
);
NTSTATUS
RequestIsIoctlBlockedByExclusiveAccess(
_In_ WDFREQUEST Request,
_Out_ PBOOLEAN IsBlocked
);
_IRQL_requires_max_(APC_LEVEL)
NTSTATUS
DeviceSendRequestSynchronously(
_In_ WDFDEVICE Device,
_In_ WDFREQUEST Request,
_In_ BOOLEAN RequestFormated
);
VOID
DeviceSendIoctlAsynchronously(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_ ULONG IoControlCode,
_In_ PDEVICE_OBJECT TargetDeviceObject
);
IO_COMPLETION_ROUTINE RequestAsynchronousIrpCompletion;
// MMC Update functions
BOOLEAN
DeviceIsMmcUpdateRequired(
_In_ WDFDEVICE Device
);
// Helper functions
_IRQL_requires_max_(APC_LEVEL)
VOID
DeviceEnableMediaChangeDetection(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_Inout_ PFILE_OBJECT_CONTEXT FileObjectContext,
_In_ BOOLEAN IgnorePreviousMediaChanges
);
_IRQL_requires_max_(APC_LEVEL)
VOID
DeviceDisableMediaChangeDetection(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_Inout_ PFILE_OBJECT_CONTEXT FileObjectContext
);
NTSTATUS
RequestSetContextFields(
_In_ WDFREQUEST Request,
_In_ PSYNC_HANDLER Handler
);
_IRQL_requires_max_(APC_LEVEL)
BOOLEAN
DeviceIsPlayActive(
_In_ WDFDEVICE Device
);
NTSTATUS
RequestDuidGetDeviceIdProperty(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_ WDFREQUEST Request,
_In_ WDF_REQUEST_PARAMETERS RequestParameters,
_Out_ size_t * DataLength
);
NTSTATUS
RequestDuidGetDeviceProperty(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_ WDFREQUEST Request,
_In_ WDF_REQUEST_PARAMETERS RequestParameters,
_Out_ size_t * DataLength
);
_IRQL_requires_max_(APC_LEVEL)
ULONG
DeviceRetrieveModeSenseUsingScratch(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_reads_bytes_(Length) PCHAR ModeSenseBuffer,
_In_ ULONG Length,
_In_ UCHAR PageCode,
_In_ UCHAR PageControl
);
_IRQL_requires_max_(APC_LEVEL)
PVOID
ModeSenseFindSpecificPage(
_In_reads_bytes_(Length) PCHAR ModeSenseBuffer,
_In_ size_t Length,
_In_ UCHAR PageMode,
_In_ BOOLEAN Use6Byte
);
_IRQL_requires_max_(PASSIVE_LEVEL)
NTSTATUS
PerformEjectionControl(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_ WDFREQUEST Request,
_In_ MEDIA_LOCK_TYPE LockType,
_In_ BOOLEAN Lock
);
_IRQL_requires_max_(PASSIVE_LEVEL)
VOID
DeviceDisableMainTimer(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension
);
_IRQL_requires_max_(APC_LEVEL)
NTSTATUS
DeviceEnableMainTimer(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension
);
EVT_WDF_TIMER DeviceMainTimerTickHandler;
VOID
RequestSetupMcnSyncIrp(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension
);
_IRQL_requires_max_(APC_LEVEL)
NTSTATUS
RequestSetupMcnRequest(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_ BOOLEAN UseGesn
);
_IRQL_requires_max_(APC_LEVEL)
BOOLEAN
RequestSendMcnRequest(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension
);
_IRQL_requires_max_(APC_LEVEL)
BOOLEAN
RequestPostWorkMcnRequest(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension
);
_IRQL_requires_max_(DISPATCH_LEVEL)
NTSTATUS
PowerContextReuseRequest(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension
);
_IRQL_requires_max_(DISPATCH_LEVEL)
NTSTATUS
PowerContextBeginUse(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension
);
_IRQL_requires_max_(DISPATCH_LEVEL)
NTSTATUS
PowerContextEndUse(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension
);
POWER_SETTING_CALLBACK DevicePowerSettingCallback;
// Zero Power ODD functions
_IRQL_requires_max_(PASSIVE_LEVEL)
NTSTATUS
DeviceInitializeZPODD(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension
);
_IRQL_requires_max_(APC_LEVEL)
VOID
DeviceReleaseZPODDResources(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension
);
NTSTATUS
DeviceZPODDGetPowerupReason(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_Out_ PSTORAGE_IDLE_POWERUP_REASON PowerupReason
);
_IRQL_requires_max_(PASSIVE_LEVEL)
BOOLEAN
DeviceZPODDIsInHomePosition(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension
);
_IRQL_requires_max_(PASSIVE_LEVEL)
VOID
DeviceMarkActive(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_ BOOLEAN IsActive,
_In_ BOOLEAN SetIdleTimeout
);
// common routines for specific IOCTL process
_IRQL_requires_max_(APC_LEVEL)
NTSTATUS
DvdStartSessionReadKey(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_ ULONG IoControlCode,
_In_opt_ WDFREQUEST OriginalRequest,
_In_opt_ PVOID InputBuffer,
_In_ size_t InputBufferLength,
_In_ PVOID OutputBuffer,
_In_ size_t OutputBufferLength,
_Out_ size_t * DataLength
);
_IRQL_requires_max_(APC_LEVEL)
NTSTATUS
ReadDvdStructure(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_opt_ WDFREQUEST OriginalRequest,
_In_ PVOID InputBuffer,
_In_ size_t InputBufferLength,
_In_ PVOID OutputBuffer,
_In_ size_t OutputBufferLength,
_Out_ size_t * DataLength
);
_IRQL_requires_max_(APC_LEVEL)
NTSTATUS
ReadQChannel(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_opt_ WDFREQUEST OriginalRequest,
_In_ PVOID InputBuffer,
_In_ size_t InputBufferLength,
_In_ PVOID OutputBuffer,
_In_ size_t OutputBufferLength,
_Out_ size_t * DataLength
);
_IRQL_requires_max_(APC_LEVEL)
NTSTATUS
DvdSendKey(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_opt_ WDFREQUEST OriginalRequest,
_In_ PVOID InputBuffer,
_In_ size_t InputBufferLength,
_Out_ size_t * DataLength
);
#ifndef SIZEOF_ARRAY
#define SIZEOF_ARRAY(ar) (sizeof(ar)/sizeof((ar)[0]))
#endif // !defined(SIZEOF_ARRAY)
// 100us to seconds
#define UNIT_100NS_PER_SECOND (10*1000*1000)
#define SECONDS_TO_100NS_UNITS(x) (((LONGLONG)x) * UNIT_100NS_PER_SECOND)
//
// Bit Flag Macros
//
#define SET_FLAG(Flags, Bit) ((Flags) |= (Bit))
#define CLEAR_FLAG(Flags, Bit) ((Flags) &= ~(Bit))
#define TEST_FLAG(Flags, Bit) (((Flags) & (Bit)) != 0)
//
// neat little hacks to count number of bits set efficiently
//
__inline ULONG CountOfSetBitsUChar(UCHAR _X)
{ ULONG i = 0; while (_X) { _X &= _X - 1; i++; } return i; }
__inline ULONG CountOfSetBitsULong(ULONG _X)
{ ULONG i = 0; while (_X) { _X &= _X - 1; i++; } return i; }
__inline ULONG CountOfSetBitsULong32(ULONG32 _X)
{ ULONG i = 0; while (_X) { _X &= _X - 1; i++; } return i; }
__inline ULONG CountOfSetBitsULong64(ULONG64 _X)
{ ULONG i = 0; while (_X) { _X &= _X - 1; i++; } return i; }
__inline ULONG CountOfSetBitsUlongPtr(ULONG_PTR _X)
{ ULONG i = 0; while (_X) { _X &= _X - 1; i++; } return i; }
__inline
BOOLEAN
IsVolumeMounted(
_In_ PDEVICE_OBJECT DeviceObject
)
{
#pragma prefast(push)
#pragma prefast(disable: 28175, "there is no other way to check if there is volume mounted")
return (DeviceObject->Vpb != NULL) &&
((DeviceObject->Vpb->Flags & VPB_MOUNTED) != 0);
#pragma prefast(pop)
}
__inline _Ret_range_(0,MAXIMUM_RETRY_FOR_SINGLE_IO_IN_100NS_UNITS)
LONGLONG
ConvertSectorsPerSecondTo100nsUnitsFor64kWrite(
_In_range_(1,0xFFFFFFFF) ULONG SectorsPerSecond // zero would cause divide-by-zero
)
{
// 64k write
// ---------------- == time per 64k write
// sectors/second
//
// (32 / N) == seconds
// (32 / N) * (1,000,000,000) == nanoseconds
// (32 / N) * (1,000,000,000) / 100 == 100ns increments
// (32 * 10,000,000) / N == 100ns increments
//
// 320,000,000 / N == 100ns increments
// And this is safe to run in kernel-mode (no floats)
//
// this assert ensures that we _never_ can return a value
// larger than the maximum allowed.
C_ASSERT(320000000 < MAXIMUM_RETRY_FOR_SINGLE_IO_IN_100NS_UNITS);
return 320000000 / SectorsPerSecond;
}
__inline
UCHAR
RequestGetCurrentStackLocationFlags(
_In_ WDFREQUEST Request
)
{
PIRP irp = NULL;
PIO_STACK_LOCATION currentStack = NULL;
irp = WdfRequestWdmGetIrp(Request);
currentStack = IoGetCurrentIrpStackLocation(irp);
return currentStack->Flags;
}
__inline
ULONG
TimeOutValueGetCapValue(
_In_ ULONG TimeOutValue,
_In_ ULONG Times
)
{
ULONG value = 0;
if (TimeOutValue > SCSI_CDROM_OPC_TIMEOUT)
{
// if time out value is specified by user in registry, and is
// bigger than OPC time out, it should be big enough
value = TimeOutValue;
}
else
{
// otherwise, OPC time out value should be the upper limit
value = min(TimeOutValue * Times, SCSI_CDROM_OPC_TIMEOUT);
}
return value;
}
VOID
RequestCompletion(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_ WDFREQUEST Request,
_In_ NTSTATUS Status,
_In_ ULONG_PTR Information
);
NTSTATUS
RequestSend(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_ WDFREQUEST Request,
_In_ WDFIOTARGET IoTarget,
_In_ ULONG Flags,
_Out_opt_ PBOOLEAN RequestSent
);
EVT_WDF_REQUEST_COMPLETION_ROUTINE RequestDummyCompletionRoutine;
VOID
RequestProcessInternalDeviceControl(
_In_ WDFREQUEST Request,
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension
);
EVT_WDF_WORKITEM IoctlWorkItemRoutine;
EVT_WDF_WORKITEM ReadWriteWorkItemRoutine;
#pragma warning(pop) // un-sets any local warning changes
#endif // __CDROMP_H__
|