コード例 #1
0
VOID
ToastMon_EvtIoTargetRemoveComplete(
    WDFIOTARGET IoTarget
)
/*++

Routine Description:

    Called when the Target device is removed ( either the target
    received IRP_MN_REMOVE_DEVICE or IRP_MN_SURPRISE_REMOVAL)

Arguments:

    IoTarget -

Return Value:


--*/
{
    PDEVICE_EXTENSION      deviceExtension;
    PTARGET_DEVICE_INFO    targetDeviceInfo = NULL;

    KdPrint((("Device Removal (remove complete) Notification\n")));

    PAGED_CODE();

    targetDeviceInfo = GetTargetDeviceInfo(IoTarget);
    deviceExtension = targetDeviceInfo->DeviceExtension;

    //
    // Stop the timer 
    //
    WdfTimerStop(targetDeviceInfo->TimerForPostingRequests, TRUE);

    //
    // Remove the target device from the collection and set Opened to FALSE to match
    // the state change (in the case of a surprise removal of the target, 
    // ToastMon_EvtIoTargetQueryRemove is not called so Opened is still TRUE).
    //
    WdfWaitLockAcquire(deviceExtension->TargetDeviceCollectionLock, NULL);

    WdfCollectionRemove(deviceExtension->TargetDeviceCollection, IoTarget);
    targetDeviceInfo->Opened = FALSE;
     
    WdfWaitLockRelease(deviceExtension->TargetDeviceCollectionLock);

    //
    // Finally delete the target.
    //
    WdfObjectDelete(IoTarget);

    return;

}
コード例 #2
0
NTSTATUS
ToastMon_EvtIoTargetQueryRemove(
    WDFIOTARGET IoTarget
)
/*++

Routine Description:

    Called when the Target device receives IRP_MN_QUERY_REMOVE.
    This happens when somebody disables, ejects or uninstalls the target
    device driver in usermode. Here close the handle to the
    target device. If the system fails to remove the device for
    some reason, you will get RemoveCancelled callback where
    you can reopen and continue to interact with the target device.

Arguments:

    IoTarget -

Return Value:


--*/
{
    PTARGET_DEVICE_INFO         targetDeviceInfo = NULL;
    WDFWAITLOCK                 targetDeviceCollectionLock;

    PAGED_CODE();

    targetDeviceInfo = GetTargetDeviceInfo(IoTarget);

    KdPrint((("Device Removal (query remove) Notification\n")));

    //
    // Stop the timer 
    //

    WdfTimerStop(targetDeviceInfo->TimerForPostingRequests, TRUE);

    targetDeviceCollectionLock = targetDeviceInfo->DeviceExtension->TargetDeviceCollectionLock;

    //
    // The target is being query removed, set Opened to FALSE to match this state change.
    //
    WdfWaitLockAcquire(targetDeviceCollectionLock, NULL);
    targetDeviceInfo->Opened = FALSE;
    WdfWaitLockRelease(targetDeviceCollectionLock);

    WdfIoTargetCloseForQueryRemove(IoTarget);

    return STATUS_SUCCESS;

}
コード例 #3
0
VOID
Toastmon_EvtTimerPostRequests(
    IN WDFTIMER Timer
    )
/*++

Routine Description:

    Passive timer event to post read and write reqeuests.

Return Value:

    None

--*/
{
    PTARGET_DEVICE_INFO       targetInfo;
    
    WDFIOTARGET         ioTarget = GetTimerContext(Timer)->IoTarget;

    targetInfo = GetTargetDeviceInfo(ioTarget);

    //
    // Even though this routine and the completion routine check the 
    // ReadRequest/WriteRequest field outside a lock, no harm is done. 
    // Depending on how far the completion-routine has run, timer 
    // may miss an opportunity to post a request. Even if we use a lock, 
    // this race condition will still exist. 
    //

    //
    // Send a read request to the target device
    //
    if(targetInfo->ReadRequest) {
        ToastMon_PostReadRequests(ioTarget);
    }

    //
    // Send a write request to the target device
    //
    if(targetInfo->WriteRequest) {
        ToastMon_PostWriteRequests(ioTarget);
    }

    //
    // Restart the passive timer.
    //
    WdfTimerStart(targetInfo->TimerForPostingRequests,
                                      WDF_REL_TIMEOUT_IN_SEC(1));

    return;
}
コード例 #4
0
ファイル: toastmon.c プロジェクト: kcrazy/winekit
VOID
ToastMon_EvtIoTargetRemoveCanceled(
    WDFIOTARGET IoTarget
    )
/*++

Routine Description:

    Called when the Target device received IRP_MN_CANCEL_REMOVE.
    This happens if another app or driver talking to the target
    device doesn't close handle or veto query-remove notification.

Arguments:

    IoTarget -

Return Value:


--*/
{
    PTARGET_DEVICE_INFO         targetDeviceInfo = NULL;
    WDF_IO_TARGET_OPEN_PARAMS   openParams;
    NTSTATUS status;

    PAGED_CODE();

    KdPrint((("Device Removal (remove cancelled) Notification\n")));

    targetDeviceInfo = GetTargetDeviceInfo(IoTarget);

    //
    // Reopen the Target.
    //
    WDF_IO_TARGET_OPEN_PARAMS_INIT_REOPEN(&openParams);

    status = WdfIoTargetOpen(IoTarget, &openParams);

    if (!NT_SUCCESS(status)) {
        KdPrint(("WdfIoTargetOpen failed 0x%x\n", status));
        WdfObjectDelete(IoTarget);
        return;
    }

    //
    // Restart the timer.
    //
    WdfTimerStart(targetDeviceInfo->TimerForPostingRequests,
                                        WDF_REL_TIMEOUT_IN_SEC(1));

}
コード例 #5
0
ファイル: toastmon.c プロジェクト: kcrazy/winekit
VOID
Toastmon_WriteRequestCompletionRoutine(
    IN WDFREQUEST                  Request,
    IN WDFIOTARGET                 Target,
    PWDF_REQUEST_COMPLETION_PARAMS CompletionParams,
    IN WDFCONTEXT                  Context
    )
/*++

Routine Description:

    Completion Routine

Arguments:

    CompletionParams - Contains the results of the transfer such as
                                    IoStatus, Length, Buffer, etc.

    Context - context value specified in the WdfRequestSetCompletionRoutine

Return Value:

    VOID

--*/
{
    WDF_REQUEST_REUSE_PARAMS    params;
    PTARGET_DEVICE_INFO       targetInfo;
    NTSTATUS status;

    UNREFERENCED_PARAMETER(Context);

    targetInfo = GetTargetDeviceInfo(Target);
    
    //
    // Delete the memory object because we create a new one every time we post
    // the request. For perf reason, it would be better to preallocate even memory object.
    // Also for driver created requests, do not call WdfRequestRetrieve functions to get
    // the buffers. WdfRequestRetrieveBuffer functions can be called only for requests
    // delivered by the queue.
    //
    WdfObjectDelete(CompletionParams->Parameters.Write.Buffer);

    //
    // Scrub the request for reuse.
    //
    WDF_REQUEST_REUSE_PARAMS_INIT(&params, WDF_REQUEST_REUSE_NO_FLAGS, STATUS_SUCCESS);

    status = WdfRequestReuse(Request, &params);
    ASSERT(NT_SUCCESS(status));

    //
    // RequestReuse zero all the values in structure pointed by CompletionParams.
    // So you must get all the information from completion params before
    // calling RequestReuse.
    //

    targetInfo->WriteRequest = Request;

    //
    // Don't repost the request in the completion routine because it may lead to recursion
    // if the driver below completes the request synchronously.
    //    
    return;
}
コード例 #6
0
ファイル: toastmon.c プロジェクト: kcrazy/winekit
NTSTATUS
ToastMon_PostWriteRequests(
    IN WDFIOTARGET IoTarget
    )
/*++

Routine Description:

    Called by the timer callback to send a write request to the target device.

Return Value:

    NT Status code - only failure expected is STATUS_INSUFFICIENT_RESOURCES

--*/
{

    WDFREQUEST                  request;
    NTSTATUS                    status;
    PTARGET_DEVICE_INFO       targetInfo;
    WDFMEMORY               memory;
    WDF_OBJECT_ATTRIBUTES       attributes;

    targetInfo = GetTargetDeviceInfo(IoTarget);

    request = targetInfo->WriteRequest;

    //
    // Allocate memory for write. Ideally I should have allocated the memory upfront along
    // with the request because the sizeof the memory buffer is constant.
    // But for demonstration, I have choosen to allocate a memory
    // object everytime I send a request down and delete it when the request
    // is completed.
    //
    WDF_OBJECT_ATTRIBUTES_INIT(&attributes);
    status = WdfMemoryCreate(
                        &attributes,
                        NonPagedPool,
                        DRIVER_TAG,
                        WRITE_BUF_SIZE,
                        &memory,
                        NULL); // buffer pointer

    if (!NT_SUCCESS(status)) {
        KdPrint(("WdfMemoryCreate failed 0x%x\n", status));
        return status;
    }

    status = WdfIoTargetFormatRequestForWrite(
                                IoTarget,
                                request,
                                memory,
                                NULL, // Buffer offset
                                NULL); // OutputBufferOffset
   if (!NT_SUCCESS(status)) {
        KdPrint(("WdfIoTargetFormatRequestForWrite failed 0x%x\n", status));
        return status;
    }

    WdfRequestSetCompletionRoutine(request,
                   Toastmon_WriteRequestCompletionRoutine,
                   targetInfo);


    //
    // Clear the WriteRequest field in the context to avoid
    // being reposted even before the reqeuest completes.
    // This will be reset in the complete routine when the request completes.
    //
    targetInfo->WriteRequest = NULL;

    if(WdfRequestSend(request, IoTarget, WDF_NO_SEND_OPTIONS) == FALSE) {
        status = WdfRequestGetStatus(request);
        KdPrint(("WdfRequestSend failed 0x%x\n", status));
        targetInfo->WriteRequest = request;
    }
    return status;
}
コード例 #7
0
ファイル: toastmon.c プロジェクト: kcrazy/winekit
NTSTATUS
Toastmon_OpenDevice(
    WDFDEVICE Device,
    PUNICODE_STRING SymbolicLink,
    WDFIOTARGET *Target
    )
/*++

Routine Description:

    Open the I/O target and preallocate any resources required
    to communicate with the target device.

Arguments:

Return Value:

    NTSTATUS

--*/
{
    NTSTATUS                    status = STATUS_SUCCESS;
    PTARGET_DEVICE_INFO         targetDeviceInfo = NULL;
    WDF_IO_TARGET_OPEN_PARAMS   openParams;
    WDFIOTARGET                 ioTarget;
    WDF_OBJECT_ATTRIBUTES       attributes;
    PDEVICE_EXTENSION           deviceExtension = GetDeviceExtension(Device);
    WDF_TIMER_CONFIG            wdfTimerConfig;
    

    WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&attributes, TARGET_DEVICE_INFO);

    status = WdfIoTargetCreate(deviceExtension->WdfDevice,
                            &attributes,
                            &ioTarget);
    if (!NT_SUCCESS(status)) {
        KdPrint(("WdfIoTargetCreate failed 0x%x\n", status));
        return status;
    }

    targetDeviceInfo = GetTargetDeviceInfo(ioTarget);
    targetDeviceInfo->DeviceExtension = deviceExtension;

    //
    // Warning: It's not recommended to open the targetdevice
    // from a pnp notification callback routine, because if
    // the target device initiates any kind of PnP action as
    // a result of this open, the PnP manager could deadlock.
    // You should queue a workitem to do that.
    // For example, SWENUM devices in conjunction with KS
    // initiate an enumeration of a device when you do the
    // open on the device interface.
    // We can open the target device here because we know the
    // toaster function driver doesn't trigger any pnp action.
    //

    WDF_IO_TARGET_OPEN_PARAMS_INIT_OPEN_BY_NAME(
        &openParams,
        SymbolicLink,
        STANDARD_RIGHTS_ALL);

    openParams.ShareAccess = FILE_SHARE_WRITE | FILE_SHARE_READ;
    //
    // Framework provides default action for all of these if you don't register
    // these callbacks -it will close the handle to the target when the device is
    // being query-removed and reopen it if the query-remove fails.
    // In this sample, we use a periodic timers to post requests to the target.
    // So we need to register these callbacks so that we can start and stop
    // the timer when the state of the target device changes. Since we are
    // registering these callbacks, we are now responsbile for closing and
    // reopening the target.
    //
    openParams.EvtIoTargetQueryRemove = ToastMon_EvtIoTargetQueryRemove;
    openParams.EvtIoTargetRemoveCanceled = ToastMon_EvtIoTargetRemoveCanceled;
    openParams.EvtIoTargetRemoveComplete = ToastMon_EvtIoTargetRemoveComplete;


    status = WdfIoTargetOpen(ioTarget, &openParams);

    if (!NT_SUCCESS(status)) {
        KdPrint(("WdfIoTargetOpen failed with status 0x%x\n", status));
        WdfObjectDelete(ioTarget);
        return status;
    }
   
    KdPrint(("Target Device 0x%x, PDO 0x%x, Fileobject 0x%x, Filehandle 0x%x\n",
                        WdfIoTargetWdmGetTargetDeviceObject(ioTarget),
                        WdfIoTargetWdmGetTargetPhysicalDevice(ioTarget),
                        WdfIoTargetWdmGetTargetFileObject(ioTarget),
                        WdfIoTargetWdmGetTargetFileHandle(ioTarget)));

    //
    // Create two requests - one for read and one for write.
    //
    WDF_OBJECT_ATTRIBUTES_INIT(&attributes);
    attributes.ParentObject = ioTarget;

    status = WdfRequestCreate(&attributes,
                              ioTarget,
                              &targetDeviceInfo->ReadRequest);

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

    WDF_OBJECT_ATTRIBUTES_INIT(&attributes);
    attributes.ParentObject = ioTarget;

    status = WdfRequestCreate(&attributes,
                            ioTarget,
                            &targetDeviceInfo->WriteRequest);

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

    //
    // Create a passive timer to post requests to the I/O target.
    //
    WDF_TIMER_CONFIG_INIT(&wdfTimerConfig,
                          Toastmon_EvtTimerPostRequests);
     
    WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&attributes, TIMER_CONTEXT);

    //
    // Make IoTarget as parent of the timer to prevent the ioTarget
    // from deleted until the dpc has runto completion.
    //
    attributes.ParentObject = ioTarget;

    //
    // By specifying WdfExecutionLevelPassive the framework will invoke
    // the timer callback Toastmon_EvtTimerPostRequests at PASSIVE_LEVEL.
    //
    attributes.ExecutionLevel = WdfExecutionLevelPassive;

    //
    // Setting the AutomaticSerialization to FALSE prevents
    // WdfTimerCreate to fail if the parent device object's 
    // execution level is set to WdfExecutionLevelPassive.
    //
    wdfTimerConfig.AutomaticSerialization = FALSE;
    
    status = WdfTimerCreate(&wdfTimerConfig,
                       &attributes,
                       &targetDeviceInfo->TimerForPostingRequests
                       );

    if(!NT_SUCCESS(status)) {
        KdPrint(("WdfTimerCreate failed 0x%x\n", status));
        WdfObjectDelete(ioTarget);
        return status;
    }

    GetTimerContext(targetDeviceInfo->TimerForPostingRequests)->IoTarget = ioTarget;

    //
    // Start the passive timer. The first timer will be queued after 1ms  interval and
    // after that it will be requeued in the timer callback function. 
    // The value of 1 ms (lowest timer resoltion allowed on NT) is chosen here so 
    // that timer would fire right away.
    //
    WdfTimerStart(targetDeviceInfo->TimerForPostingRequests,
                                        WDF_REL_TIMEOUT_IN_MS(1));

    *Target = ioTarget;

    return status;

}
コード例 #8
0
VOID
ToastMon_EvtIoTargetRemoveCanceled(
    WDFIOTARGET IoTarget
    )
/*++

Routine Description:

    Called when the Target device received IRP_MN_CANCEL_REMOVE.
    This happens if another app or driver talking to the target
    device doesn't close handle or veto query-remove notification.

Arguments:

    IoTarget -

Return Value:


--*/
{
    PTARGET_DEVICE_INFO         targetDeviceInfo = NULL;
    WDFWAITLOCK                 targetDeviceCollectionLock;
    WDF_IO_TARGET_OPEN_PARAMS   openParams;
    NTSTATUS status;

    PAGED_CODE();

    KdPrint((("Device Removal (remove cancelled) Notification\n")));

    targetDeviceInfo = GetTargetDeviceInfo(IoTarget);

    //
    // Reopen the Target.
    //
    WDF_IO_TARGET_OPEN_PARAMS_INIT_REOPEN(&openParams);

    status = WdfIoTargetOpen(IoTarget, &openParams);

    if (!NT_SUCCESS(status)) {
        KdPrint(("WdfIoTargetOpen failed 0x%x\n", status));
        WdfObjectDelete(IoTarget);
        return;
    }

    targetDeviceCollectionLock = targetDeviceInfo->DeviceExtension->TargetDeviceCollectionLock;

    //
    // The query remove has failed and the target has been successfully reopened. Set Opened
    // back to TRUE to reflect the state change.
    //
    WdfWaitLockAcquire(targetDeviceCollectionLock, NULL);
    targetDeviceInfo->Opened = TRUE;
    WdfWaitLockRelease(targetDeviceCollectionLock);


    //
    // Restart the timer.
    //
    WdfTimerStart(targetDeviceInfo->TimerForPostingRequests,
                                        WDF_REL_TIMEOUT_IN_SEC(1));

}