Skip to content

Commit

Permalink
Driver: Added handler for IRP_MJ_CLEANUP
Browse files Browse the repository at this point in the history
When application ends, it unplugs all devices that where created by the application.
No need to unplug manually.
  • Loading branch information
shauleiz committed Apr 19, 2016
1 parent 9659b1c commit 1f6379b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
65 changes: 64 additions & 1 deletion ScpVBus/bus/busenum.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ NPAGED_LOOKASIDE_LIST g_LookAside;
#pragma alloc_text(INIT, DriverEntry)
#pragma alloc_text(PAGE, Bus_DriverUnload)
#pragma alloc_text(PAGE, Bus_CreateClose)
#pragma alloc_text(PAGE, Bus_CleanUp)
#pragma alloc_text(PAGE, Bus_DispatchSystemControl)
#endif

Expand Down Expand Up @@ -50,7 +51,8 @@ NTSTATUS DriverEntry(__in PDRIVER_OBJECT DriverObject, __in PUNICODE_STRING Regi
DriverObject->MajorFunction[IRP_MJ_POWER] = Bus_Power;
DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = Bus_IoCtl;
DriverObject->MajorFunction[IRP_MJ_INTERNAL_DEVICE_CONTROL] = Bus_Internal_IoCtl;
DriverObject->MajorFunction[IRP_MJ_SYSTEM_CONTROL] = Bus_DispatchSystemControl;
DriverObject->MajorFunction[IRP_MJ_SYSTEM_CONTROL] = Bus_DispatchSystemControl;
DriverObject->MajorFunction[IRP_MJ_CLEANUP] = Bus_CleanUp;

DriverObject->DriverUnload = Bus_DriverUnload;
DriverObject->DriverExtension->AddDevice = Bus_AddDevice;
Expand Down Expand Up @@ -194,6 +196,67 @@ NTSTATUS Bus_CreateClose(PDEVICE_OBJECT DeviceObject, PIRP Irp)
return status;
}

NTSTATUS Bus_CleanUp(PDEVICE_OBJECT DeviceObject, PIRP Irp)
{
NTSTATUS status = STATUS_SUCCESS;
DWORD CurrentProcId = 0, CreateProcId = 0;
BOOLEAN IsFDO, found=FALSE;
PCOMMON_DEVICE_DATA commonData;
PPDO_DEVICE_DATA pdoData;
PFDO_DEVICE_DATA fdoData;
PLIST_ENTRY entry;

PAGED_CODE();
Bus_KdPrint(("Cleanup \n"));

// Check if this is FDO
commonData = (PCOMMON_DEVICE_DATA)DeviceObject->DeviceExtension;
IsFDO = commonData->IsFDO;

// If this is FDO start releasing PDOs (if Process is the same one as the one created the PDO)
if (IsFDO)
{
// Get the FDO data and acquire the mutex
fdoData = (PFDO_DEVICE_DATA)DeviceObject->DeviceExtension;
ExAcquireFastMutex(&fdoData->Mutex);

// Get the ID of the current process
CurrentProcId = CURRENT_PROCESS_ID();

// Loop on all PDOs (devices) belonging to this FDO
for (entry = fdoData->ListOfPDOs.Flink; entry != &fdoData->ListOfPDOs ; entry = entry->Flink)
{
// Get the PDO data and from it extract the ID of the process that created the PDO
pdoData = CONTAINING_RECORD(entry, PDO_DEVICE_DATA, Link);
CreateProcId = pdoData->CallingProcessId;

// If this proces is the same process that created the PDO
// Then mark the PDO for removal
if (CreateProcId == CurrentProcId)
{
Bus_KdPrint(("Plugged out %d\n", pdoData->SerialNo));
pdoData->CallingProcessId = 0;
pdoData->Present = FALSE;
found = TRUE;
}
}

ExReleaseFastMutex(&fdoData->Mutex);

// If there was at least one matching PDO then invalidate device structure
if (found)
IoInvalidateDeviceRelations(fdoData->UnderlyingPDO, BusRelations);
}

// Complete Request
Irp->IoStatus.Information = 0;
Irp->IoStatus.Status = status;
IoCompleteRequest(Irp, IO_NO_INCREMENT);

return status;

}

///-------------------------------------------------------------------------------------------------
/// <summary> DispatchDeviceControl routine. </summary>
///
Expand Down
3 changes: 3 additions & 0 deletions ScpVBus/bus/busenum.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ __drv_dispatchType(IRP_MJ_CREATE)
__drv_dispatchType(IRP_MJ_CLOSE)
DRIVER_DISPATCH Bus_CreateClose;

__drv_dispatchType(IRP_MJ_CLEANUP)
DRIVER_DISPATCH Bus_CleanUp;

__drv_dispatchType(IRP_MJ_DEVICE_CONTROL)
DRIVER_DISPATCH Bus_IoCtl;

Expand Down
2 changes: 1 addition & 1 deletion XOutput/Tester/Tester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "stdafx.h"
#include "..\XOutput.h"

#pragma comment(lib, "XOutput1_2")
#pragma comment(lib, "XOutput1_1")


int main()
Expand Down

0 comments on commit 1f6379b

Please sign in to comment.