📄 isodev.c
字号:
KeReleaseSpinLock(&DeviceExtension->IdleReqStateLock, oldIrql);
ntStatus = STATUS_INSUFFICIENT_RESOURCES;
}
SubmitIdleRequestIrp_Exit:
IsoUsb_DbgPrint(3, ("SubmitIdleRequest - ends\n"));
return ntStatus;
}
VOID
IdleNotificationCallback(
IN PDEVICE_EXTENSION DeviceExtension
)
/*++
Routine Description:
"A pointer to a callback function in your driver is passed down the stack with
this IOCTL, and it is this callback function that is called by USBHUB when it
safe for your device to power down."
"When the callback in your driver is called, all you really need to do is to
to first ensure that a WaitWake Irp has been submitted for your device, if
remote wake is possible for your device and then request a SetD2 (or DeviceWake)"
Arguments:
Return Value:
--*/
{
NTSTATUS ntStatus;
POWER_STATE powerState;
KEVENT irpCompletionEvent;
PIRP_COMPLETION_CONTEXT irpContext;
IsoUsb_DbgPrint(3, ("IdleNotificationCallback - begins\n"));
//
// Dont idle, if the device was just disconnected or being stopped
// i.e. return for the following DeviceState(s)
// NotStarted, Stopped, PendingStop, PendingRemove, SurpriseRemoved, Removed
//
if(DeviceExtension->DeviceState != Working) {
return;
}
//
// If there is not already a WW IRP pending, submit one now
//
if(DeviceExtension->WaitWakeEnable) {
IssueWaitWake(DeviceExtension);
}
//
// power down the device
//
irpContext = (PIRP_COMPLETION_CONTEXT)
ExAllocatePool(NonPagedPool,
sizeof(IRP_COMPLETION_CONTEXT));
if(!irpContext) {
IsoUsb_DbgPrint(1, ("Failed to alloc memory for irpContext\n"));
ntStatus = STATUS_INSUFFICIENT_RESOURCES;
}
else {
//
// increment the count. In the HoldIoRequestWorkerRoutine, the
// count is decremented twice (one for the system Irp and the
// other for the device Irp. An increment here compensates for
// the sytem irp..The decrement corresponding to this increment
// is in the completion function
//
IsoUsb_DbgPrint(3, ("IdleNotificationCallback::"));
IsoUsb_IoIncrement(DeviceExtension);
powerState.DeviceState = DeviceExtension->PowerDownLevel;
KeInitializeEvent(&irpCompletionEvent, NotificationEvent, FALSE);
irpContext->DeviceExtension = DeviceExtension;
irpContext->Event = &irpCompletionEvent;
ntStatus = PoRequestPowerIrp(
DeviceExtension->PhysicalDeviceObject,
IRP_MN_SET_POWER,
powerState,
(PREQUEST_POWER_COMPLETE) PoIrpCompletionFunc,
irpContext,
NULL);
if(STATUS_PENDING == ntStatus) {
IsoUsb_DbgPrint(3, ("IdleNotificationCallback::"
"waiting for the power irp to complete\n"));
KeWaitForSingleObject(&irpCompletionEvent,
Executive,
KernelMode,
FALSE,
NULL);
}
}
if(!NT_SUCCESS(ntStatus)) {
if(irpContext) {
ExFreePool(irpContext);
}
}
IsoUsb_DbgPrint(3, ("IdleNotificationCallback - ends\n"));
}
NTSTATUS
IdleNotificationRequestComplete(
IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp,
IN PDEVICE_EXTENSION DeviceExtension
)
/*++
Routine Description:
Completion routine for idle notification irp
Arguments:
DeviceObject - pointer to device object
Irp - I/O request packet
DeviceExtension - pointer to device extension
Return Value:
NT status value
--*/
{
NTSTATUS ntStatus;
POWER_STATE powerState;
KIRQL oldIrql;
PIRP idleIrp;
LARGE_INTEGER dueTime;
PUSB_IDLE_CALLBACK_INFO idleCallbackInfo;
IsoUsb_DbgPrint(3, ("IdleNotificationRequestCompete - begins\n"));
idleIrp = NULL;
//
// check the Irp status
//
ntStatus = Irp->IoStatus.Status;
if(!NT_SUCCESS(ntStatus) && ntStatus != STATUS_NOT_SUPPORTED) {
IsoUsb_DbgPrint(1, ("Idle irp completes with error::"));
switch(ntStatus) {
case STATUS_INVALID_DEVICE_REQUEST:
IsoUsb_DbgPrint(1, ("STATUS_INVALID_DEVICE_REQUEST\n"));
break;
case STATUS_CANCELLED:
IsoUsb_DbgPrint(1, ("STATUS_CANCELLED\n"));
break;
case STATUS_POWER_STATE_INVALID:
IsoUsb_DbgPrint(1, ("STATUS_POWER_STATE_INVALID\n"));
goto IdleNotificationRequestComplete_Exit;
case STATUS_DEVICE_BUSY:
IsoUsb_DbgPrint(1, ("STATUS_DEVICE_BUSY\n"));
break;
default:
IsoUsb_DbgPrint(1, ("default\n"));
break;
}
//
// if in error, issue a SetD0
//
if(PowerDeviceD0 != DeviceExtension->DevPower) {
IsoUsb_DbgPrint(3, ("IdleNotificationRequestComplete::"));
IsoUsb_IoIncrement(DeviceExtension);
powerState.DeviceState = PowerDeviceD0;
ntStatus = PoRequestPowerIrp(
DeviceExtension->PhysicalDeviceObject,
IRP_MN_SET_POWER,
powerState,
(PREQUEST_POWER_COMPLETE) PoIrpAsyncCompletionFunc,
DeviceExtension,
NULL);
if(!NT_SUCCESS(ntStatus)) {
IsoUsb_DbgPrint(1, ("PoRequestPowerIrp failed\n"));
}
}
}
IdleNotificationRequestComplete_Exit:
KeAcquireSpinLock(&DeviceExtension->IdleReqStateLock, &oldIrql);
idleCallbackInfo = DeviceExtension->IdleCallbackInfo;
DeviceExtension->IdleCallbackInfo = NULL;
idleIrp = (PIRP) InterlockedExchangePointer(
&DeviceExtension->PendingIdleIrp,
NULL);
InterlockedExchange(&DeviceExtension->IdleReqPend, 0);
KeReleaseSpinLock(&DeviceExtension->IdleReqStateLock, oldIrql);
if(idleCallbackInfo) {
ExFreePool(idleCallbackInfo);
}
//
// since we allocated the irp, we need to free it.
// return STATUS_MORE_PROCESSING_REQUIRED so that
// the kernel does not touch it.
//
if(idleIrp) {
IsoUsb_DbgPrint(3, ("completion routine has a valid irp and frees it\n"));
IoFreeIrp(Irp);
KeSetEvent(&DeviceExtension->NoIdleReqPendEvent,
IO_NO_INCREMENT,
FALSE);
}
else {
//
// The CancelSelectiveSuspend routine has grabbed the Irp from the device
// extension. Now the last one to decrement the FreeIdleIrpCount should
// free the irp.
//
if(0 == InterlockedDecrement(&DeviceExtension->FreeIdleIrpCount)) {
IsoUsb_DbgPrint(3, ("completion routine frees the irp\n"));
IoFreeIrp(Irp);
KeSetEvent(&DeviceExtension->NoIdleReqPendEvent,
IO_NO_INCREMENT,
FALSE);
}
}
if(DeviceExtension->SSEnable) {
IsoUsb_DbgPrint(3, ("Set the timer to fire DPCs\n"));
dueTime.QuadPart = -10000 * IDLE_INTERVAL; // 5000 ms
KeSetTimerEx(&DeviceExtension->Timer,
dueTime,
IDLE_INTERVAL, // 5000 ms
&DeviceExtension->DeferredProcCall);
IsoUsb_DbgPrint(3, ("IdleNotificationRequestCompete - ends\n"));
}
return STATUS_MORE_PROCESSING_REQUIRED;
}
VOID
CancelSelectSuspend(
IN PDEVICE_EXTENSION DeviceExtension
)
/*++
Routine Description:
This routine is invoked to cancel selective suspend request.
Arguments:
DeviceExtension - pointer to device extension
Return Value:
None.
--*/
{
PIRP irp;
KIRQL oldIrql;
irp = NULL;
IsoUsb_DbgPrint(3, ("CancelSelectSuspend - begins\n"));
KeAcquireSpinLock(&DeviceExtension->IdleReqStateLock, &oldIrql);
if(!CanDeviceSuspend(DeviceExtension))
{
IsoUsb_DbgPrint(3, ("Device is not idle\n"));
irp = (PIRP) InterlockedExchangePointer(
&DeviceExtension->PendingIdleIrp,
NULL);
}
KeReleaseSpinLock(&DeviceExtension->IdleReqStateLock, oldIrql);
//
// since we have a valid Irp ptr,
// we can call IoCancelIrp on it,
// without the fear of the irp
// being freed underneath us.
//
if(irp) {
//
// This routine has the irp pointer.
// It is safe to call IoCancelIrp because we know that
// the compleiton routine will not free this irp unless...
//
if(IoCancelIrp(irp)) {
IsoUsb_DbgPrint(3, ("IoCancelIrp returns TRUE\n"));
}
else {
IsoUsb_DbgPrint(3, ("IoCancelIrp returns FALSE\n"));
}
//
// ....we decrement the FreeIdleIrpCount from 2 to 1.
// if completion routine runs ahead of us, then this routine
// decrements the FreeIdleIrpCount from 1 to 0 and hence shall
// free the irp.
//
if(0 == InterlockedDecrement(&DeviceExtension->FreeIdleIrpCount)) {
IsoUsb_DbgPrint(3, ("CancelSelectSuspend frees the irp\n"));
IoFreeIrp(irp);
KeSetEvent(&DeviceExtension->NoIdleReqPendEvent,
IO_NO_INCREMENT,
FALSE);
}
}
IsoUsb_DbgPrint(3, ("CancelSelectSuspend - ends\n"));
return;
}
VOID
PoIrpCompletionFunc(
IN PDEVICE_OBJECT DeviceObject,
IN UCHAR MinorFunction,
IN POWER_STATE PowerState,
IN PVOID Context,
IN PIO_STATUS_BLOCK IoStatus
)
/*++
Routine Description:
Completion routine for power irp PoRequested in IdleNotification
RequestComplete routine.
Arguments:
DeviceObject - pointer to device object
MinorFunciton - minor function for the irp.
PowerState - irp power state
Context - context passed to the completion function
IoStatus - status block.
Return Value:
None
--*/
{
PIRP_COMPLETION_CONTEXT irpContext;
//
// initialize variables
//
irpContext = NULL;
if(Context) {
irpContext = (PIRP_COMPLETION_CONTEXT) Context;
}
//
// all we do is set the event and decrement the count
//
if(irpContext) {
KeSetEvent(irpContext->Event, 0, FALSE);
IsoUsb_DbgPrint(3, ("PoIrpCompletionFunc::"));
IsoUsb_IoDecrement(irpContext->DeviceExtension);
ExFreePool(irpContext);
}
return;
}
VOID
PoIrpAsyncCompletionFunc(
IN PDEVICE_OBJECT DeviceObject,
IN UCHAR MinorFunction,
IN POWER_STATE PowerState,
IN PVOID Context,
IN PIO_STATUS_BLOCK IoStatus
)
/*++
Routine Description:
Completion routine for PoRequest wait wake irp
Arguments:
DeviceObject - pointer to device object
MinorFunciton - minor function for the irp.
PowerState - irp power state
Context - context passed to the completion function
IoStatus - status block.
Return Value:
None
--*/
{
PDEVICE_EXTENSION DeviceExtension;
//
// initialize variables
//
DeviceExtension = (PDEVICE_EXTENSION) Context;
//
// all we do is decrement the count
//
IsoUsb_DbgPrint(3, ("PoIrpAsyncCompletionFunc::"));
IsoUsb_IoDecrement(DeviceExtension);
return;
}
VOID
WWIrpCompletionFunc(
IN PDEVICE_OBJECT DeviceObject,
IN UCHAR MinorFunction,
IN POWER_STATE PowerState,
IN PVOID Context,
IN PIO_STATUS_BLOCK IoStatus
)
/*++
Routine Description:
Completion routine for idle notification irp
Arguments:
Return Value:
--*/
{
PDEVICE_EXTENSION DeviceExtension;
//
// initialize variables
//
DeviceExtension = (PDEVICE_EXTENSION) Context;
//
// all we do is decrement the count
//
IsoUsb_DbgPrint(3, ("WWIrpCompletionFunc::"));
IsoUsb_IoDecrement(DeviceExtension);
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -