void I2CClose( _In_ PDEVICE_CONTEXT DeviceContext ) /*++ Routine Description: This routine closes a handle to the I2C controller. Arguments: DeviceContext - a pointer to the device context. --*/ { TRACE_FUNC_ENTRY(TRACE_I2C); PAGED_CODE(); if (DeviceContext->I2CIoTarget != WDF_NO_HANDLE) { TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_I2C, "Closing handle to I2C target"); WdfIoTargetClose(DeviceContext->I2CIoTarget); } TRACE_FUNC_EXIT(TRACE_I2C); }
NTSTATUS SampleDrvEvtDeviceD0Entry ( _In_ WDFDEVICE Device, _In_ WDF_POWER_DEVICE_STATE PreviousPowerState ) /*++ Routine Description: This routine is invoked by the framework to program the device to goto D0, which is the working state. The framework invokes callback every time the hardware needs to be (re-)initialized. This includes after IRP_MN_START_DEVICE, IRP_MN_CANCEL_STOP_DEVICE, IRP_MN_CANCEL_REMOVE_DEVICE, and IRP_MN_SET_POWER-D0. N.B. This function is not marked pageable because this function is in the device power up path. When a function is marked pagable and the code section is paged out, it will generate a page fault which could impact the fast resume behavior because the client driver will have to wait until the system drivers can service this page fault. Arguments: Device - Supplies a handle to the framework device object. PreviousPowerState - WDF_POWER_DEVICE_STATE-typed enumerator that identifies the device power state that the device was in before this transition to D0. Return Value: NTSTATUS code. A failure here will indicate a fatal error and cause the framework to tear down the stack. --*/ { BYTE Data; NTSTATUS Status; WDFIOTARGET ReadTarget; WDFIOTARGET WriteTarget; PSAMPLE_DRV_DEVICE_EXTENSION SampleDrvExtension; UNICODE_STRING ReadString; WCHAR ReadStringBuffer[100]; UNICODE_STRING WriteString; WCHAR WriteStringBuffer[100]; UNREFERENCED_PARAMETER(PreviousPowerState); ReadTarget = NULL; WriteTarget = NULL; SampleDrvExtension = SampleDrvGetDeviceExtension(Device); // // For demonstration purporses, the sample device consumes two IO resources, // the first of which will be used for input, and the second for output. // RtlInitEmptyUnicodeString(&ReadString, ReadStringBuffer, sizeof(ReadStringBuffer)); RtlInitEmptyUnicodeString(&WriteString, WriteStringBuffer, sizeof(WriteStringBuffer)); // // Construct full-path string for GPIO read operation // Status = RESOURCE_HUB_CREATE_PATH_FROM_ID(&ReadString, SampleDrvExtension->ConnectionIds[0].LowPart, SampleDrvExtension->ConnectionIds[0].HighPart); if (!NT_SUCCESS(Status)) { goto Cleanup; } // // Construct full-path string for GPIO write operation // Status = RESOURCE_HUB_CREATE_PATH_FROM_ID(&WriteString, SampleDrvExtension->ConnectionIds[1].LowPart, SampleDrvExtension->ConnectionIds[1].HighPart); if (!NT_SUCCESS(Status)) { goto Cleanup; } // // Perform the read operation // Data = 0x0; Status = TestReadWrite(Device, &ReadString, TRUE, &Data, sizeof(Data), &ReadTarget); if (!NT_SUCCESS(Status)) { goto Cleanup; } // // Perform the write operation // Status = TestReadWrite(Device, &WriteString, FALSE, &Data, sizeof(Data), &WriteTarget); if (!NT_SUCCESS(Status)) { goto Cleanup; } Cleanup: if (ReadTarget != NULL) { WdfIoTargetClose(ReadTarget); WdfObjectDelete(ReadTarget); } if (WriteTarget != NULL) { WdfIoTargetClose(WriteTarget); WdfObjectDelete(WriteTarget); } return Status; }