标题:
UsbStorage驱动开发(一)[原创]
[打印本页]
作者:
maingao
时间:
2005-8-19 11:23
标题:
UsbStorage驱动开发(一)[原创]
前段时间发现有网友在开发驱动的过程中遇到问题,所以今天我整理了一下我在写驱动中的一点心得。希望大家指导!
win2k的UsbStorage不需要驱动程序就能运行,但是如果我们的设备有特殊功能,像前几天有网友问的那种要显示自已的标号的。那么UsbStor.sys就不能满足我们的需求。当然如果要实现自己的功能,我们也只需要UsbStor.sys的基础上修改就行了!
USB磁盘驱动程序是一个BUS DRIVER,它的主要入口如下:
NTSTATUS
DriverEntry(
IN PDRIVER_OBJECT DriverObject,
IN PUNICODE_STRING pRegistryPath
)
{
#if DBG
DbgPrint("Enter UsbStore DriverEntry!\n");
#endif
DriverObject->DriverExtension->AddDevice = UsbStore_AddDevice;
DriverObject->DriverUnload = UsbStore_Unload;
DriverObject->DriverStartIo = UsbStore_StartIo;
DriverObject->MajorFunction[IRP_MJ_CREATE] =
DriverObject->MajorFunction[IRP_MJ_CLOSE] = UsbStore_CreateClose;
DriverObject->MajorFunction[IRP_MJ_READ] =
DriverObject->MajorFunction[IRP_MJ_WRITE] = UsbStore_ReadWrite;
DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = UsbStore_DeviceIoControl;
DriverObject->MajorFunction[IRP_MJ_SCSI] = UsbStore_Scsi;
DriverObject->MajorFunction[IRP_MJ_PNP] = UsbStore_Pnp;
DriverObject->MajorFunction[IRP_MJ_SYSTEM_CONTROL] = UsbStore_SystemControl;
DriverObject->MajorFunction[IRP_MJ_POWER] = UsbStore_Power;
return STATUS_SUCCESS;
}
USB STORAGE DRIVER对设备的主要操作MajorFunction是IRP_MJ_SCSI,而不是IRP_MJ_READ 以及IRP_MJ_WRITE 。
USB STORAGE DRIVER的上层程序是CLASS DRIVER。CLASS DRIVER对上层的USB磁盘的读写等操作转换成IRP_MJ_SCSI,USB STORAGE DRIVER根据USB CBI规范和BULK ONLY规范将上层DRIVER传送来的IRP_MJ_SCSI的SRB(SCSI_REQUEST_BLOCK)的相关部分传递给设备。
UsbStore_AddDevice的功能是建立USB STORAGE DRIVER的FDO。
NTSTATUS
UsbStore_AddDevice(
IN PDRIVER_OBJECT DriverObject,
IN PDEVICE_OBJECT PhysicalDeviceObject
)
{
NTSTATUS status;
PUSB_DEVICE_EXTENSION pUsb_DeviceExtension;
PDEVICE_OBJECT USbStore_FileDeviceObject = NULL;
KEVENT Event;
#if DBG
DbgPrint("Enter AddDevice Dispatch!\n");
#endif
status = IoCreateDevice(
DriverObject,
sizeof(USB_DEVICE_EXTENSION),
NULL,
FILE_DEVICE_BUS_EXTENDER,
FILE_AUTOGENERATED_DEVICE_NAME ,
FALSE,
&USbStore_FileDeviceObject
);
if(!NT_SUCCESS(status))
{
#if DBG
DbgPrint("IoCreateDevice Failed!\n");
#endif
return status;
}
pUsb_DeviceExtension = (PUSB_DEVICE_EXTENSION)USbStore_FileDeviceObject->DeviceExtension;
RtlZeroMemory(pUsb_DeviceExtension,sizeof(USB_DEVICE_EXTENSION));
pUsb_DeviceExtension->DeviceType = USBSTORE_FDO;
pUsb_DeviceExtension->UsbFileDeviceObject = USbStore_FileDeviceObject;
pUsb_DeviceExtension->UsbPhysicalDeviceObject = PhysicalDeviceObject;
pUsb_DeviceExtension->SystemPowerState = PowerSystemWorking;
pUsb_DeviceExtension->CurrentPowerState = PowerDeviceD0;
pUsb_DeviceExtension->UsbStore_TopOfStackDeviceObject =
IoAttachDeviceToDeviceStack(
USbStore_FileDeviceObject,
PhysicalDeviceObject
);
KeInitializeEvent(
&Event,
SynchronizationEvent,
FALSE
);
pUsb_DeviceExtension->Usb_Event = Event;
UsbStore_GetDriverFlags(USbStore_FileDeviceObject);
USbStore_FileDeviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
USbStore_FileDeviceObject->Flags |= DO_DIRECT_IO;
USbStore_FileDeviceObject->Flags |= DO_POWER_PAGABLE;
return STATUS_SUCCESS;
}
//UsbStore_GetDriverFlags
NTSTATUS
UsbStore_GetDriverFlags(
IN PDEVICE_OBJECT UsbFdo
)
{
PUSB_DEVICE_EXTENSION pUsb_DeviceExtension;
NTSTATUS status;
HANDLE RegHandle;
RTL_QUERY_REGISTRY_TABLE RegTable;
ULONG Value = 0;
RtlZeroMemory(&RegTable,sizeof(RTL_QUERY_REGISTRY_TABLE));
pUsb_DeviceExtension = UsbFdo->DeviceExtension;
status = IoOpenDeviceRegistryKey(
pUsb_DeviceExtension->UsbPhysicalDeviceObject,
PLUGPLAY_REGKEY_DRIVER,
STANDARD_RIGHTS_ALL,
&RegHandle
);
if(NT_SUCCESS(status))
{
RegTable.Flags = RTL_QUERY_REGISTRY_DIRECT;
RegTable.Name = L"DriverFlags";
RegTable.EntryContext = &Value;
RegTable.DefaultType = REG_DWORD;
RegTable.DefaultData = &Value;
RegTable.DefaultLength = sizeof(ULONG);
status = RtlQueryRegistryValues(
RTL_REGISTRY_HANDLE,
RegHandle,
&RegTable,
NULL,
NULL
);
if(!NT_SUCCESS(status))
{
#if DBG
DbgPrint("RtlQueryRegistryValues Failed!\n");
#endif
return status;
}
ZwClose(RegHandle);
}
if(Value>=4)
Value =0;
pUsb_DeviceExtension->DriverFlags = Value;
return status;
}
在这段代码中,
status = IoCreateDevice(
DriverObject,
sizeof(USB_DEVICE_EXTENSION),
NULL,
FILE_DEVICE_BUS_EXTENDER,
FILE_AUTOGENERATED_DEVICE_NAME ,
FALSE,
&USbStore_FileDeviceObject
);
负责建立一个USB STORAGE DRIVER的FDO,请各位注意,设备的类型(DEVICE TYPE)是FILE_DEVICE_BUS_EXTENDER。
取注册表的一段代码,主要负责是取出DRIVER FLAGS,它用于标记设备是满足CBI规范还是BULK ONLY规范。
(未完待续)
作者:
liu1234
时间:
2005-8-21 08:52
鼎
鼎鼎
鼎鼎鼎
鼎鼎鼎鼎
鼎鼎鼎鼎鼎
鼎鼎鼎鼎鼎鼎
鼎鼎鼎鼎鼎鼎鼎
鼎鼎鼎鼎鼎鼎鼎鼎
作者:
liu1234
时间:
2005-8-23 20:34
鼎
鼎鼎
鼎鼎鼎
鼎鼎鼎鼎
鼎鼎鼎鼎鼎
鼎鼎鼎鼎鼎鼎
鼎鼎鼎鼎鼎鼎鼎
鼎鼎鼎鼎鼎鼎鼎鼎
作者:
maingao
时间:
2005-8-24 16:37
由于本版的限制发帖长度,所以不得不将些帖分成若干段,给各位带来不便,请见谅!
作者:
eeizrg
时间:
2005-8-30 15:42
标题:
顶
鼎
鼎鼎
鼎鼎鼎
鼎鼎鼎鼎
鼎鼎鼎鼎鼎
鼎鼎鼎鼎鼎鼎
鼎鼎鼎鼎鼎鼎鼎
鼎鼎鼎鼎鼎鼎鼎鼎
企盼您的续集!
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0