写个Windows环境的USB驱动代码
时间: 2024-03-05 10:52:33 浏览: 20
以下是一个简单的 Windows 环境下的 USB 驱动程序,使用 Windows Driver Framework (WDF) 实现:
```c
#include <ntddk.h>
#include <wdf.h>
#define MY_USB_DEVICE_VID 0x1234
#define MY_USB_DEVICE_PID 0x5678
DECLARE_CONST_UNICODE_STRING(device_name, L"\\Device\\MyUSBDevice");
DECLARE_CONST_UNICODE_STRING(symbolic_link_name, L"\\DosDevices\\MyUSBDevice");
NTSTATUS MyUsbEvtDeviceAdd(IN WDFDRIVER Driver, IN PWDFDEVICE_INIT DeviceInit) {
NTSTATUS status;
WDFDEVICE device;
PDEVICE_CONTEXT context;
UNREFERENCED_PARAMETER(Driver);
status = WdfDeviceCreate(&DeviceInit, WDF_NO_OBJECT_ATTRIBUTES, &device);
if (!NT_SUCCESS(status)) {
KdPrint(("MyUSBDevice: WdfDeviceCreate failed with status 0x%x.\n", status));
return status;
}
context = GetDeviceContext(device);
status = WdfDeviceCreateSymbolicLink(device, &symbolic_link_name);
if (!NT_SUCCESS(status)) {
KdPrint(("MyUSBDevice: WdfDeviceCreateSymbolicLink failed with status 0x%x.\n", status));
return status;
}
KdPrint(("MyUSBDevice: device created successfully.\n"));
return status;
}
NTSTATUS MyUsbEvtDevicePrepareHardware(IN WDFDEVICE Device, IN WDFCMRESLIST ResourcesRaw, IN WDFCMRESLIST ResourcesTranslated) {
NTSTATUS status;
UNREFERENCED_PARAMETER(ResourcesRaw);
UNREFERENCED_PARAMETER(ResourcesTranslated);
status = MyUsbDeviceInit(Device);
if (!NT_SUCCESS(status)) {
KdPrint(("MyUSBDevice: MyUsbDeviceInit failed with status 0x%x.\n", status));
return status;
}
KdPrint(("MyUSBDevice: device prepared successfully.\n"));
return status;
}
NTSTATUS MyUsbEvtDeviceReleaseHardware(IN WDFDEVICE Device, IN WDFCMRESLIST ResourcesTranslated) {
UNREFERENCED_PARAMETER(Device);
UNREFERENCED_PARAMETER(ResourcesTranslated);
MyUsbDeviceDeinit(Device);
KdPrint(("MyUSBDevice: device released successfully.\n"));
return STATUS_SUCCESS;
}
NTSTATUS MyUsbEvtDeviceD0Entry(IN WDFDEVICE Device, IN WDF_POWER_DEVICE_STATE PreviousState) {
UNREFERENCED_PARAMETER(PreviousState);
NTSTATUS status = MyUsbDeviceStart(Device);
if (!NT_SUCCESS(status)) {
KdPrint(("MyUSBDevice: MyUsbDeviceStart failed with status 0x%x.\n", status));
return status;
}
KdPrint(("MyUSBDevice: device D0 entry successful.\n"));
return STATUS_SUCCESS;
}
VOID MyUsbEvtDeviceD0Exit(IN WDFDEVICE Device, IN WDF_POWER_DEVICE_STATE TargetState) {
UNREFERENCED_PARAMETER(TargetState);
MyUsbDeviceStop(Device);
KdPrint(("MyUSBDevice: device D0 exit successful.\n"));
return;
}
NTSTATUS MyUsbEvtDeviceQueryStop(IN WDFDEVICE Device) {
UNREFERENCED_PARAMETER(Device);
KdPrint(("MyUSBDevice: device query stop.\n"));
return STATUS_SUCCESS;
}
VOID MyUsbEvtDeviceSurpriseRemove(IN WDFDEVICE Device) {
UNREFERENCED_PARAMETER(Device);
KdPrint(("MyUSBDevice: device surprise remove.\n"));
return;
}
NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath) {
NTSTATUS status;
WDF_DRIVER_CONFIG config;
PWDFDEVICE_INIT deviceInit = NULL;
KdPrint(("MyUSBDevice: driver entry.\n"));
WDF_DRIVER_CONFIG_INIT(&config, MyUsbEvtDeviceAdd);
status = WdfDriverCreate(DriverObject, RegistryPath, WDF_NO_OBJECT_ATTRIBUTES, &config, WDF_NO_HANDLE);
if (!NT_SUCCESS(status)) {
KdPrint(("MyUSBDevice: WdfDriverCreate failed with status 0x%x.\n", status));
return status;
}
deviceInit = WdfControlDeviceInitAllocate(config.DriverGlobals, &SDDL_DEVOBJ_SYS_ALL_ADM_RWX_WORLD_RW_RES_R);
if (deviceInit == NULL) {
KdPrint(("MyUSBDevice: WdfControlDeviceInitAllocate failed.\n"));
return STATUS_INSUFFICIENT_RESOURCES;
}
status = WdfDeviceInitAssignName(deviceInit, &device_name);
if (!NT_SUCCESS(status)) {
KdPrint(("MyUSBDevice: WdfDeviceInitAssignName failed with status 0x%x.\n", status));
return status;
}
status = WdfDeviceCreate(&deviceInit, WDF_NO_OBJECT_ATTRIBUTES, WDF_NO_HANDLE);
if (!NT_SUCCESS(status)) {
KdPrint(("MyUSBDevice: WdfDeviceCreate failed with status 0x%x.\n", status));
return status;
}
WdfControlFinishInitializing(WdfDeviceWdmGetDeviceObject(deviceInit));
KdPrint(("MyUSBDevice: driver loaded successfully.\n"));
return status;
}
NTSTATUS MyUsbDeviceInit(IN WDFDEVICE Device) {
NTSTATUS status;
WDF_USB_DEVICE_INFORMATION info;
PDEVICE_CONTEXT context;
context = GetDeviceContext(Device);
status = WdfUsbTargetDeviceCreate(Device, WDF_NO_OBJECT_ATTRIBUTES, &context->UsbDevice);
if (!NT_SUCCESS(status)) {
KdPrint(("MyUSBDevice: WdfUsbTargetDeviceCreate failed with status 0x%x.\n", status));
return status;
}
WDF_USB_DEVICE_INFORMATION_INIT(&info);
status = WdfUsbTargetDeviceRetrieveInformation(context->UsbDevice, &info);
if (!NT_SUCCESS(status)) {
KdPrint(("MyUSBDevice: WdfUsbTargetDeviceRetrieveInformation failed with status 0x%x.\n", status));
return status;
}
if (info.VendorId != MY_USB_DEVICE_VID || info.ProductId != MY_USB_DEVICE_PID) {
KdPrint(("MyUSBDevice: unsupported device connected.\n"));
return STATUS_DEVICE_CONFIGURATION_ERROR;
}
KdPrint(("MyUSBDevice: device initialized successfully.\n"));
return STATUS_SUCCESS;
}
VOID MyUsbDeviceDeinit(IN WDFDEVICE Device) {
PDEVICE_CONTEXT context;
context = GetDeviceContext(Device);
if (context->UsbDevice != NULL) {
WdfObjectDelete(context->UsbDevice);
context->UsbDevice = NULL;
}
KdPrint(("MyUSBDevice: device deinitialized successfully.\n"));
return;
}
NTSTATUS MyUsbDeviceStart(IN WDFDEVICE Device) {
PDEVICE_CONTEXT context;
context = GetDeviceContext(Device);
KdPrint(("MyUSBDevice: device started successfully.\n"));
return STATUS_SUCCESS;
}
VOID MyUsbDeviceStop(IN WDFDEVICE Device) {
UNREFERENCED_PARAMETER(Device);
KdPrint(("MyUSBDevice: device stopped successfully.\n"));
return;
}
```
该程序包含了 USB 驱动程序的各个事件处理函数,其中,`MyUsbEvtDeviceAdd` 在设备连接时调用,`MyUsbEvtDevicePrepareHardware` 在设备准备工作时调用,`MyUsbEvtDeviceReleaseHardware` 在设备释放时调用,`MyUsbEvtDeviceD0Entry` 在设备进入 D0 状态时调用,`MyUsbEvtDeviceD0Exit` 在设备退出 D0 状态时调用,`MyUsbEvtDeviceQueryStop` 处理设备停止请求,`MyUsbEvtDeviceSurpriseRemove` 处理设备意外移除事件。
在 `DriverEntry` 函数中,使用了 WDF 驱动框架的接口函数创建了设备对象和控制设备对象,并注册了设备事件处理函数。在设备事件处理函数中,实现了设备的初始化、启动、停止和释放等功能。