/*++ Routine Description: In this callback, the driver does whatever is necessary to make the hardware ready to use. In the case of a USB device, this involves reading and selecting descriptors. --*/ NTSTATUS PSDrv_EvtDevicePrepareHardware(IN WDFDEVICE Device, IN WDFCMRESLIST ResourceList, IN WDFCMRESLIST ResourceListTranslated) { NTSTATUS status; PDEVICE_CONTEXT pDeviceContext; WDF_USB_DEVICE_INFORMATION info; UNREFERENCED_PARAMETER(ResourceList); UNREFERENCED_PARAMETER(ResourceListTranslated); PSDrv_DbgPrint(3, ("PSDrv_EvtDevicePrepareHardware - begins\n")); PAGED_CODE(); pDeviceContext = GetDeviceContext(Device); // Read the device descriptor, configuration descriptor and select the interface descriptors status = ReadAndSelectDescriptors(Device); if (!NT_SUCCESS(status)) { PSDrv_DbgPrint(1, ("ReadAndSelectDescriptors failed! (Status = %x)\n", status)); return status; } WDF_USB_DEVICE_INFORMATION_INIT(&info); // Retrieve USBD version information, port driver capabilities and device capabilities such as speed, power, etc. status = WdfUsbTargetDeviceRetrieveInformation(pDeviceContext->WdfUsbTargetDevice, &info); if (NT_SUCCESS(status)) { pDeviceContext->IsDeviceHighSpeed = (info.Traits & WDF_USB_DEVICE_TRAIT_AT_HIGH_SPEED) ? TRUE : FALSE; PSDrv_DbgPrint(3, ("DeviceIsHighSpeed: %s\n", pDeviceContext->IsDeviceHighSpeed ? "TRUE" : "FALSE")); } else { pDeviceContext->IsDeviceHighSpeed = FALSE; } PSDrv_DbgPrint(3, ("IsDeviceSelfPowered: %s\n", (info.Traits & WDF_USB_DEVICE_TRAIT_SELF_POWERED) ? "TRUE" : "FALSE")); pDeviceContext->WaitWakeEnable = info.Traits & WDF_USB_DEVICE_TRAIT_REMOTE_WAKE_CAPABLE; PSDrv_DbgPrint(3, ("IsDeviceRemoteWakeable: %s\n", (info.Traits & WDF_USB_DEVICE_TRAIT_REMOTE_WAKE_CAPABLE) ? "TRUE" : "FALSE")); // Enable wait-wake and idle timeout if the device supports it if(pDeviceContext->WaitWakeEnable) { status = SetPowerPolicy(Device); if (!NT_SUCCESS (status)) { PSDrv_DbgPrint(3, ("SetPowerPolicy failed! (Status = %x)\n", status)); return status; } } PSDrv_DbgPrint(3, ("PSDrv_EvtDevicePrepareHardware - ends\n")); return status; }
/////////////////////////////////////////////////////////////////////////////////////////////////// //功能设备对象构造函数,使用了Pdo参数,并向KPnpDevice传入GUID(应用程序访问设备是需要的) KadyUsbTestDevice::KadyUsbTestDevice(PDEVICE_OBJECT Pdo, ULONG Unit) : KadyUsbDvcName(NULL), KPnpDevice(Pdo, &GUID_DEVINTERFACE_KADYUSB340) { //判断构造函数是否正确 if (!NT_SUCCESS(m_ConstructorStatus)) { ASSERT(FALSE); return; } //初始化下层接口 m_Lower.Initialize(this, Pdo); //初始化USB接口(默认调用配置1的接口0) m_Interface.Initialize( m_Lower, //USB设备 0, //接口0 1, //配置1 0 //初始化接口 ); //初始化USB端点 EndPoint1In.Initialize(m_Lower, 0x81, 128); //端点1,输入端点,128个字节 EndPoint2Out.Initialize(m_Lower, 0x02, 256); //端点2,输出端点,256个字节 #if (_WDM_ && (WDM_MAJORVERSION > 1 ||((WDM_MAJORVERSION == 1) && (WDM_MINORVERSION >= 0x20)))) //初始化USB直接客户访问接口 if (STATUS_SUCCESS == m_BusIntf.Initialize(m_Lower.TopOfStack())) m_fBusIntfAvailable = TRUE; else m_fBusIntfAvailable = FALSE; #endif //将USB设备对象设置为下层设备 SetLowerDevice(&m_Lower); //调用标准的即插即用初始化 SetPnpPolicy(); //调用标准的电源初始化 SetPowerPolicy(); //加载注册表信息 LoadRegistryParameters(); }