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
|
/*--
Copyright (C) Microsoft Corporation. All rights reserved.
Module Name:
pnppower.c
Abstract:
Functions to handle PnP and Power IRPs.
Environment:
kernel mode only
Notes:
optical devices do not need to issue SPIN UP when power up.
The device itself should SPIN UP to process commands.
Revision History:
--*/
#include "ntddk.h"
#include "wdfcore.h"
#include "cdrom.h"
#include "ioctl.h"
#include "scratch.h"
#include "mmc.h"
#ifdef DEBUG_USE_WPP
#include "pnppower.tmh"
#endif
NTSTATUS
DeviceScratchSyncCache(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension
);
NTSTATUS
DeviceScratchPreventMediaRemoval(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_ BOOLEAN Prevent
);
NTSTATUS
RequestIssueShutdownFlush(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_ PIRP Irp
);
IO_COMPLETION_ROUTINE RequestProcessPowerIrpCompletion;
EVT_WDF_REQUEST_COMPLETION_ROUTINE RequestUnlockQueueCompletion;
#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, DevicePowerSettingCallback)
#endif
#pragma warning(push)
#pragma warning(disable:4152) // nonstandard extension, function/data pointer conversion in expression
#pragma warning(disable:28118) // Dispatch routines for IRP_MJ_SHUTDOWN, IRP_MJ_FLUSH_BUFFERS occur at PASSIVE_LEVEL.
// WDF defines EVT_WDFDEVICE_WDM_IRP_PREPROCESS with _IRQL_requires_max_(DISPATCH_LEVEL),
// triggering a false positive for this warning.
NTSTATUS
RequestProcessShutdownFlush(
WDFDEVICE Device,
PIRP Irp
)
/*++
Routine Description:
process IRP: IRP_MJ_SHUTDOWN, IRP_MJ_FLUSH_BUFFERS
Arguments:
Device - device object
Irp - the irp
Return Value:
NTSTATUS
--*/
{
NTSTATUS status = STATUS_SUCCESS;
PIO_STACK_LOCATION currentStack = NULL;
PCDROM_DEVICE_EXTENSION deviceExtension = DeviceGetExtension(Device);
//add trace info
// acquire the shutdown/flush lock
WdfWaitLockAcquire(deviceExtension->ShutdownFlushWaitLock, NULL);
currentStack = IoGetCurrentIrpStackLocation(Irp);
// finish all current requests
WdfIoQueueStopSynchronously(deviceExtension->SerialIOQueue);
// sync cache
if (NT_SUCCESS(status))
{
// safe to use scratch srb to send the request.
status = DeviceScratchSyncCache(deviceExtension);
}
// For SHUTDOWN, allow media removal.
if (NT_SUCCESS(status))
{
if (currentStack->MajorFunction == IRP_MJ_SHUTDOWN)
{
// safe to use scratch srb to send the request.
status = DeviceScratchPreventMediaRemoval(deviceExtension, FALSE);
}
}
// Use original IRP, send SRB_FUNCTION_SHUTDOWN or SRB_FUNCTION_FLUSH (no retry)
if (NT_SUCCESS(status))
{
status = RequestIssueShutdownFlush(deviceExtension, Irp);
}
// restart queue to allow processing further requests.
WdfIoQueueStart(deviceExtension->SerialIOQueue);
// release the shutdown/flush lock
WdfWaitLockRelease(deviceExtension->ShutdownFlushWaitLock);
// 6. complete the irp
Irp->IoStatus.Status = status;
IoCompleteRequest(Irp, 0);
return status;
}
NTSTATUS
RequestProcessSetPower(
WDFDEVICE Device,
PIRP Irp
)
/*++
Routine Description:
process IRP: IRP_MJ_POWER
Arguments:
Device - device object
Irp - the irp
Return Value:
NTSTATUS
--*/
{
PCDROM_DEVICE_EXTENSION deviceExtension = DeviceGetExtension(Device);
PIO_STACK_LOCATION currentStack;
NTSTATUS status;
BOOLEAN IrpMarkedPending = FALSE;
currentStack = IoGetCurrentIrpStackLocation(Irp);
if ((currentStack->Parameters.Power.Type == DevicePowerState) &&
(currentStack->Parameters.Power.State.DeviceState != PowerDeviceD0))
{
// We need to unlock the device queue in D3 postprocessing.
IoCopyCurrentIrpStackLocationToNext(Irp);
IoSetCompletionRoutine(Irp,
RequestProcessPowerIrpCompletion,
deviceExtension,
TRUE,
TRUE,
TRUE);
// Mark the Irp pending as we'll defer the I/O completion.
IoMarkIrpPending(Irp);
IrpMarkedPending = TRUE;
}
else {
IoSkipCurrentIrpStackLocation(Irp);
}
#pragma warning(push)
#pragma warning(disable: 28193) // OACR will complain that the status variable is not examined.
//
// Deliver the IRP back to the framework.
//
status = WdfDeviceWdmDispatchPreprocessedIrp(Device, Irp);
if (IrpMarkedPending)
{
UNREFERENCED_PARAMETER(status);
return STATUS_PENDING;
}
#pragma warning(pop)
return status;
}
// use scratch SRB to issue SYNC CACHE command.
NTSTATUS
DeviceScratchSyncCache(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension
)
/*++
Routine Description:
use scratch buffer to send SYNC CACHE command
Arguments:
DeviceExtension - device context
Return Value:
NTSTATUS
--*/
{
NTSTATUS status = STATUS_SUCCESS;
ULONG transferSize = 0;
CDB cdb;
ScratchBuffer_BeginUse(DeviceExtension);
RtlZeroMemory(&cdb, sizeof(CDB));
// Set up the CDB
cdb.SYNCHRONIZE_CACHE10.OperationCode = SCSIOP_SYNCHRONIZE_CACHE;
//srb->QueueTag = SP_UNTAGGED;
//srb->QueueAction = SRB_SIMPLE_TAG_REQUEST;
status = ScratchBuffer_ExecuteCdbEx(DeviceExtension, NULL, transferSize, FALSE, &cdb, 10, TimeOutValueGetCapValue(DeviceExtension->TimeOutValue, 4));
ScratchBuffer_EndUse(DeviceExtension);
return status;
}
NTSTATUS
DeviceScratchPreventMediaRemoval(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_ BOOLEAN Prevent
)
/*++
Routine Description:
use scratch SRB to issue ALLOW/PREVENT MEDIA REMOVAL command.
Arguments:
DeviceExtension - device context
Prevent - TRUE (prevent media removal); FALSE (allow media removal)
Return Value:
NTSTATUS
--*/
{
NTSTATUS status = STATUS_SUCCESS;
ULONG transferSize = 0;
CDB cdb;
ScratchBuffer_BeginUse(DeviceExtension);
RtlZeroMemory(&cdb, sizeof(CDB));
// Set up the CDB
cdb.MEDIA_REMOVAL.OperationCode = SCSIOP_MEDIUM_REMOVAL;
cdb.MEDIA_REMOVAL.Prevent = Prevent;
//srb->QueueTag = SP_UNTAGGED;
//srb->QueueAction = SRB_SIMPLE_TAG_REQUEST;
status = ScratchBuffer_ExecuteCdb(DeviceExtension, NULL, transferSize, FALSE, &cdb, 6);
ScratchBuffer_EndUse(DeviceExtension);
return status;
}
NTSTATUS
RequestIssueShutdownFlush(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_ PIRP Irp
)
/*++
Routine Description:
issue SRB function Flush/Shutdown command.
Arguments:
DeviceExtension - device context
Irp - the irp
Return Value:
NTSTATUS
--*/
{
NTSTATUS status = STATUS_SUCCESS;
PSCSI_REQUEST_BLOCK srb = DeviceExtension->ScratchContext.ScratchSrb;
PIO_STACK_LOCATION currentStack = NULL;
ULONG transferSize = 0;
BOOLEAN shouldRetry = TRUE;
ULONG timesAlreadyRetried = 0;
LONGLONG retryIn100nsUnits = 0;
currentStack = IoGetCurrentIrpStackLocation(Irp);
ScratchBuffer_BeginUse(DeviceExtension);
// no retry needed.
{
ScratchBuffer_SetupSrb(DeviceExtension, NULL, transferSize, FALSE);
// Set up the SRB/CDB
srb->QueueTag = SP_UNTAGGED;
srb->QueueAction = SRB_SIMPLE_TAG_REQUEST;
srb->TimeOutValue = TimeOutValueGetCapValue(DeviceExtension->TimeOutValue, 4);
srb->CdbLength = 0;
if (currentStack->MajorFunction == IRP_MJ_SHUTDOWN)
{
srb->Function = SRB_FUNCTION_SHUTDOWN;
}
else
{
srb->Function = SRB_FUNCTION_FLUSH;
}
ScratchBuffer_SendSrb(DeviceExtension, TRUE, NULL);
shouldRetry = RequestSenseInfoInterpretForScratchBuffer(DeviceExtension,
timesAlreadyRetried,
&status,
&retryIn100nsUnits);
UNREFERENCED_PARAMETER(shouldRetry); //defensive coding, avoid PREFAST warning.
UNREFERENCED_PARAMETER(status); //defensive coding, avoid PREFAST warning.
// retrieve the real status from the request.
status = WdfRequestGetStatus(DeviceExtension->ScratchContext.ScratchRequest);
}
ScratchBuffer_EndUse(DeviceExtension);
return status;
}
VOID
RequestUnlockQueueCompletion (
_In_ WDFREQUEST Request,
_In_ WDFIOTARGET Target,
_In_ PWDF_REQUEST_COMPLETION_PARAMS Params,
_In_ WDFCONTEXT Context
)
{
PIRP Irp = Context;
WDFDEVICE device = WdfIoTargetGetDevice(Target);
PCDROM_DEVICE_EXTENSION deviceExtension = DeviceGetExtension(device);
UNREFERENCED_PARAMETER(Request);
UNREFERENCED_PARAMETER(Params);
deviceExtension->PowerContext.Options.LockQueue = FALSE;
PowerContextEndUse(deviceExtension);
// Complete the original power irp
IoCompleteRequest( Irp, IO_NO_INCREMENT );
}
NTSTATUS
RequestProcessPowerIrpCompletion(
_In_ PDEVICE_OBJECT DeviceObject,
_In_ PIRP Irp,
_In_reads_opt_(_Inexpressible_("varies")) PVOID Context
)
/*++
Routine Description:
Free the Irp.
Arguments:
DeviceObject - device that the completion routine fires on.
Irp - The irp to be completed.
Context - IRP context
Return Value:
NTSTATUS
--*/
{
PCDROM_DEVICE_EXTENSION deviceExtension = Context;
PIO_STACK_LOCATION currentStack;
UNREFERENCED_PARAMETER(DeviceObject);
if (Irp->PendingReturned)
{
IoMarkIrpPending( Irp );
}
currentStack = IoGetCurrentIrpStackLocation(Irp);
NT_ASSERT(currentStack->Parameters.Power.Type == DevicePowerState);
NT_ASSERT(currentStack->Parameters.Power.State.DeviceState != PowerDeviceD0);
_Analysis_assume_(deviceExtension != NULL);
deviceExtension->PowerContext.PowerChangeState.PowerDown++;
// Step 5. UNLOCK QUEUE
if (deviceExtension->PowerContext.Options.LockQueue)
{
(VOID)DeviceSendPowerDownProcessRequest(deviceExtension,
RequestUnlockQueueCompletion,
Irp);
// Let the completion routine complete the Irp
return STATUS_MORE_PROCESSING_REQUIRED;
}
// Release the power context if it wasn't already done as part of D0Exit handling
if (deviceExtension->PowerContext.InUse)
{
PowerContextEndUse(deviceExtension);
}
return STATUS_CONTINUE_COMPLETION;
}
_IRQL_requires_max_(DISPATCH_LEVEL)
NTSTATUS
PowerContextReuseRequest(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension
)
/*++
Routine Description:
reset fields for the request.
Arguments:
DeviceExtension - device context
Return Value:
NTSTATUS
--*/
{
NTSTATUS status = STATUS_SUCCESS;
WDF_REQUEST_REUSE_PARAMS reuseParams;
PIRP irp = NULL;
RtlZeroMemory(&(DeviceExtension->PowerContext.SenseData), sizeof(DeviceExtension->PowerContext.SenseData));
RtlZeroMemory(&(DeviceExtension->PowerContext.Srb), sizeof(DeviceExtension->PowerContext.Srb));
irp = WdfRequestWdmGetIrp(DeviceExtension->PowerContext.PowerRequest);
// Re-use the previously created PowerRequest object and format it
WDF_REQUEST_REUSE_PARAMS_INIT(&reuseParams, WDF_REQUEST_REUSE_NO_FLAGS, STATUS_NOT_SUPPORTED);
status = WdfRequestReuse(DeviceExtension->PowerContext.PowerRequest, &reuseParams);
if (NT_SUCCESS(status))
{
// This request was preformated during initialization so this call should never fail.
status = WdfIoTargetFormatRequestForInternalIoctlOthers(DeviceExtension->IoTarget,
DeviceExtension->PowerContext.PowerRequest,
IOCTL_SCSI_EXECUTE_IN,
NULL, NULL,
NULL, NULL,
NULL, NULL);
if (!NT_SUCCESS(status))
{
TracePrint((TRACE_LEVEL_ERROR, TRACE_FLAG_GENERAL,
"PowerContextReuseRequest: WdfIoTargetFormatRequestForInternalIoctlOthers failed, %!STATUS!\n",
status));
}
}
// Do some basic initialization of the PowerRequest, the rest will be done by the caller
// of this function
if (NT_SUCCESS(status))
{
PIO_STACK_LOCATION nextStack = NULL;
nextStack = IoGetNextIrpStackLocation(irp);
nextStack->MajorFunction = IRP_MJ_SCSI;
nextStack->Parameters.Scsi.Srb = &(DeviceExtension->PowerContext.Srb);
DeviceExtension->PowerContext.Srb.Length = sizeof(SCSI_REQUEST_BLOCK);
DeviceExtension->PowerContext.Srb.OriginalRequest = irp;
DeviceExtension->PowerContext.Srb.SenseInfoBuffer = &(DeviceExtension->PowerContext.SenseData);
DeviceExtension->PowerContext.Srb.SenseInfoBufferLength = SENSE_BUFFER_SIZE;
}
return status;
}
_IRQL_requires_max_(DISPATCH_LEVEL)
NTSTATUS
PowerContextBeginUse(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension
)
/*++
Routine Description:
initialize fields in power context
Arguments:
DeviceExtension - device context
Return Value:
NTSTATUS
--*/
{
NTSTATUS status = STATUS_SUCCESS;
NT_ASSERT(!DeviceExtension->PowerContext.InUse);
DeviceExtension->PowerContext.InUse = TRUE;
DeviceExtension->PowerContext.RetryCount = MAXIMUM_RETRIES;
DeviceExtension->PowerContext.RetryIntervalIn100ns = 0;
KeQueryTickCount(&DeviceExtension->PowerContext.StartTime);
RtlZeroMemory(&(DeviceExtension->PowerContext.Options), sizeof(DeviceExtension->PowerContext.Options));
RtlZeroMemory(&(DeviceExtension->PowerContext.PowerChangeState), sizeof(DeviceExtension->PowerContext.PowerChangeState));
status = PowerContextReuseRequest(DeviceExtension);
RequestClearSendTime(DeviceExtension->PowerContext.PowerRequest);
return status;
}
_IRQL_requires_max_(DISPATCH_LEVEL)
NTSTATUS
PowerContextEndUse(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension
)
/*++
Routine Description:
inidate that power context using is finished.
Arguments:
DeviceExtension - device context
Return Value:
NTSTATUS
--*/
{
NT_ASSERT(DeviceExtension->PowerContext.InUse);
DeviceExtension->PowerContext.InUse = FALSE;
KeQueryTickCount(&DeviceExtension->PowerContext.CompleteTime);
return STATUS_SUCCESS;
}
_Function_class_(POWER_SETTING_CALLBACK)
_IRQL_requires_same_
NTSTATUS
DevicePowerSettingCallback(
_In_ LPCGUID SettingGuid,
_In_reads_bytes_(ValueLength) PVOID Value,
_In_ ULONG ValueLength,
_Inout_opt_ PVOID Context
)
/*++
Description:
This function is the callback for power setting notifications (registered
when ClasspGetD3IdleTimeout() is called for the first time).
Currently, this function is used to get the disk idle timeout value from
the system power settings.
This function is guaranteed to be called at PASSIVE_LEVEL.
Arguments:
SettingGuid - The power setting GUID.
Value - Pointer to the power setting value.
ValueLength - Size of the Value buffer.
Context - The FDO's device extension.
Return Value:
STATUS_SUCCESS
--*/
{
PCDROM_DEVICE_EXTENSION DeviceExtension = Context;
MONITOR_DISPLAY_STATE DisplayState;
PAGED_CODE();
if (IsEqualGUID(SettingGuid, &GUID_CONSOLE_DISPLAY_STATE)) {
if ((ValueLength == sizeof(ULONG)) && (Value != NULL)) {
DisplayState = *((PULONG)Value);
_Analysis_assume_(DeviceExtension != NULL);
//
// Power setting callbacks are asynchronous so make sure the device
// is completely initialized before taking any actions.
//
if (DeviceExtension->IsInitialized) {
//
// If monitor is off, change media change requests to not keep device active.
// This allows the devices to go to sleep if there are no other active requests.
//
if (DisplayState == PowerMonitorOff) {
//
// Mark the device inactive so that it can enter a low power state.
//
DeviceMarkActive(DeviceExtension, FALSE, TRUE);
SET_FLAG(DeviceExtension->MediaChangeDetectionInfo->SrbFlags, SRB_FLAGS_NO_KEEP_AWAKE);
}
else
{
CLEAR_FLAG(DeviceExtension->MediaChangeDetectionInfo->SrbFlags, SRB_FLAGS_NO_KEEP_AWAKE);
DeviceMarkActive(DeviceExtension, TRUE, TRUE);
}
}
}
}
return STATUS_SUCCESS;
}
#pragma warning(pop) // un-sets any local warning changes
|