VOID UfxDevice_Reset ( _In_ UFXDEVICE Device ) /*++ Routine Description: Cancels all transfers and disables non-zero endpoints in response to USB reset. Arguments: UfxDevice - UFXDEVICE object representing the device. --*/ { PUFXDEVICE_CONTEXT DeviceContext; PREGISTERS_CONTEXT RegistersContext; ULONG EpIndex; TraceEntry(); DeviceContext = UfxDeviceGetContext(Device); RegistersContext = DeviceGetRegistersContext(DeviceContext->FdoWdfDevice); // // Get EP0 back to setup stage. // TransferReset(WdfCollectionGetFirstItem(DeviceContext->Endpoints)); // // Disable all non-default endpoints // // // #### TODO: Insert code to disable non-default endpoints #### // // // End any transfers on non-default endpoints. // for (EpIndex = 1; EpIndex < WdfCollectionGetCount(DeviceContext->Endpoints); EpIndex++) { TransferReset(WdfCollectionGetItem(DeviceContext->Endpoints, EpIndex)); } TraceExit(); }
WDFCOMMONBUFFER PciDtfCommonBufferFind(IN PDEVICE_DATA DeviceData, IN int ID, IN BOOLEAN Remove) { WDFCOMMONBUFFER CommonBuffer = NULL; ULONG Index, Count; Count = WdfCollectionGetCount(DeviceData->CommonBuffers); for (Index = 0; Index < Count; Index++) { CommonBuffer = (WDFCOMMONBUFFER) WdfCollectionGetItem(DeviceData->CommonBuffers, Index); if (GetCommonBufferData(CommonBuffer)->ID == ID) { if (Remove) WdfCollectionRemoveItem (DeviceData->CommonBuffers, Index); break; } } return Index < Count ? CommonBuffer : NULL; }
NTSTATUS Registry_ReadAllDeviceKeys(__in PDEVICE_CONTEXT deviceContext) { WDFKEY hKey = NULL; NTSTATUS status = STATUS_INVALID_DEVICE_STATE; UNICODE_STRING valueName; WDF_OBJECT_ATTRIBUTES collectionAttributes; WDF_OBJECT_ATTRIBUTES stringAttributes; GUID guidTest = {0}; PAGED_CODE(); if (!deviceContext->WdfDevice) { USBERR("deviceContext->WdfDevice is NULL.\n"); return status; } // The driver sets the PLUGPLAY_REGKEY_DEVICE flag to open the Device // Parameters subkey under the device's hardware key, or it sets the // PLUGPLAY_REGKEY_DRIVER flag to open the driver's software key. If the // PLUGPLAY_REGKEY_CURRENT_HWPROFILE flag is also set, // WdfDeviceOpenRegistryKey opens the copy of the hardware or software key // that is in the current hardware profile. status = WdfDeviceOpenRegistryKey(deviceContext->WdfDevice, PLUGPLAY_REGKEY_DEVICE, KEY_READ, WDF_NO_OBJECT_ATTRIBUTES, &hKey); if (!NT_SUCCESS (status)) { USBERR("WdfDeviceOpenRegistryKey failed.\n"); hKey = NULL; return status; } ////////////////////////////////////////////////////////////////////////// // Read the device interface guids from the registry. // These are set in the inf files [Device_AddReg] group. // WDF_OBJECT_ATTRIBUTES_INIT(&collectionAttributes); collectionAttributes.ParentObject = deviceContext->WdfDevice; status = WdfCollectionCreate(&collectionAttributes, &deviceContext->DeviceRegSettings.DeviceInterfaceGUIDs); if (!NT_SUCCESS(status)) { USBERR("collection object could not be allocated. status=%Xh", status); goto Done; } WDF_OBJECT_ATTRIBUTES_INIT(&stringAttributes); stringAttributes.ParentObject = deviceContext->DeviceRegSettings.DeviceInterfaceGUIDs; RtlInitUnicodeString(&valueName, L"DeviceInterfaceGUIDs"); status = WdfRegistryQueryMultiString(hKey, &valueName, &stringAttributes, deviceContext->DeviceRegSettings.DeviceInterfaceGUIDs); if (NT_SUCCESS(status)) { ULONG guidCount = WdfCollectionGetCount(deviceContext->DeviceRegSettings.DeviceInterfaceGUIDs); ULONG guidIndex; WDFSTRING wdfGuidString; BOOLEAN removeGuidFromCollection; USBMSG("Found %u DeviceInterfaceGUIDs strings.", guidCount); for (guidIndex = 0; guidIndex < guidCount; guidIndex++) { removeGuidFromCollection = TRUE; wdfGuidString = (WDFSTRING)WdfCollectionGetItem(deviceContext->DeviceRegSettings.DeviceInterfaceGUIDs, guidIndex); status = GUIDFromWdfString(wdfGuidString, &guidTest); if (!NT_SUCCESS(status)) { USBERR("removing invalid DeviceInterfaceGUID string at index %u\n", guidIndex); } else { if (IsEqualGUID(&guidTest, &Libusb0DeviceGuid)) { USBWRN("libusb0 device DeviceInterfaceGUID found. skippng..\n"); } else if (IsEqualGUID(&guidTest, &Libusb0FilterGuid)) { USBWRN("libusb0 filter DeviceInterfaceGUID found. skippng..\n"); } else if (IsEqualGUID(&guidTest, &LibusbKDeviceGuid)) { USBWRN("libusbK default device DeviceInterfaceGUID found. skippng..\n"); } else { removeGuidFromCollection = FALSE; } } if (removeGuidFromCollection) { WdfCollectionRemoveItem(deviceContext->DeviceRegSettings.DeviceInterfaceGUIDs, guidIndex); guidIndex--; guidCount--; } } } else { USBWRN("DeviceInterfaceGUIDs registry key does not exist.\n" " Ensure the DeviceInterfaceGUIDs key has been properly set in the .inf and re-install the device.\n\n"); } if (WdfCollectionGetCount(deviceContext->DeviceRegSettings.DeviceInterfaceGUIDs) == 0) { status = AddDefaultDeviceInterfaceGUID(deviceContext); if (!NT_SUCCESS(status)) { goto Done; } } ////////////////////////////////////////////////////////////////////////// // Read the device power policy settings (if any). // These are set in the inf files [Device_AddReg] group. // GetDeviceRegSettingKey(DeviceIdleEnabled, FALSE); GetDeviceRegSettingKey(DeviceIdleIgnoreWakeEnable, FALSE); GetDeviceRegSettingKey(UserSetDeviceIdleEnabled, FALSE); GetDeviceRegSettingKey(DefaultIdleState, FALSE); GetDeviceRegSettingKey(DefaultIdleTimeout, 5000); GetDeviceRegSettingKey(SystemWakeEnabled, FALSE); status = STATUS_SUCCESS; Done: if (hKey) { WdfRegistryClose(hKey); hKey = NULL; } return status; }
VOID FilterEvtIoDeviceControl( IN WDFQUEUE Queue, IN WDFREQUEST Request, IN size_t OutputBufferLength, IN size_t InputBufferLength, IN ULONG IoControlCode ) /*++ Routine Description: This event is called when the framework receives IRP_MJ_DEVICE_CONTROL requests from the system. Arguments: Queue - Handle to the framework queue object that is associated with the I/O request. Request - Handle to a framework request object. OutputBufferLength - length of the request's output buffer, if an output buffer is available. InputBufferLength - length of the request's input buffer, if an input buffer is available. IoControlCode - the driver-defined or system-defined I/O control code (IOCTL) that is associated with the request. Return Value: VOID --*/ { ULONG i; ULONG noItems; WDFDEVICE hFilterDevice; PFILTER_EXTENSION filterExt; UNREFERENCED_PARAMETER(Queue); UNREFERENCED_PARAMETER(OutputBufferLength); UNREFERENCED_PARAMETER(InputBufferLength); UNREFERENCED_PARAMETER(IoControlCode); PAGED_CODE(); KdPrint(("Ioctl recieved into filter control object.\n")); WdfWaitLockAcquire(FilterDeviceCollectionLock, NULL); noItems = WdfCollectionGetCount(FilterDeviceCollection); for(i=0; i<noItems ; i++) { hFilterDevice = WdfCollectionGetItem(FilterDeviceCollection, i); filterExt = FilterGetData(hFilterDevice); KdPrint(("Serial No: %d\n", filterExt->SerialNo)); } WdfWaitLockRelease(FilterDeviceCollectionLock); WdfRequestCompleteWithInformation(Request, STATUS_SUCCESS, 0); }
VOID UfxDevice_EvtDeviceUsbStateChange ( _In_ UFXDEVICE UfxDevice, _In_ USBFN_DEVICE_STATE NewState ) /*++ Routine Description: EvtDeviceUsbStateChange handler for the UFXDEVICE object. Arguments: UfxDevice - UFXDEVICE object representing the device. NewState - The new device state. --*/ { NTSTATUS Status; PUFXDEVICE_CONTEXT Context; PCONTROLLER_CONTEXT ControllerContext; ULONG EpIndex; USBFN_DEVICE_STATE OldState; PAGED_CODE(); TraceEntry(); Context = UfxDeviceGetContext(UfxDevice); ControllerContext = DeviceGetControllerContext(Context->FdoWdfDevice); OldState = Context->UsbState; TraceInformation("New STATE: %d", NewState); Status = UfxDeviceStopOrResumeIdle(UfxDevice, NewState, Context->UsbPort); LOG_NT_MSG(Status, "Failed to stop or resume idle"); WdfWaitLockAcquire(ControllerContext->InitializeDefaultEndpointLock, NULL); if (ControllerContext->InitializeDefaultEndpoint == TRUE) { // // Reset endpoint 0. This is the last part of soft reset, which was postponed // until now, since we need to make sure EP0 is created by UFX. // DeviceInitializeDefaultEndpoint(Context->FdoWdfDevice); ControllerContext->InitializeDefaultEndpoint = FALSE; } WdfWaitLockRelease(ControllerContext->InitializeDefaultEndpointLock); if (NewState == UsbfnDeviceStateConfigured && OldState != UsbfnDeviceStateSuspended) { for (EpIndex = 1; EpIndex < WdfCollectionGetCount(Context->Endpoints); EpIndex++) { UfxEndpointConfigure(WdfCollectionGetItem(Context->Endpoints, EpIndex)); } // // #### TODO: Insert code to allow the controller to accept U1/U2, if supported #### // } if (NewState == UsbfnDeviceStateDetached) { KeSetEvent(&ControllerContext->DetachEvent, IO_NO_INCREMENT, FALSE); } UfxDeviceEventComplete(UfxDevice, STATUS_SUCCESS); TraceExit(); }