📄 computer00usbdevice.cpp
字号:
// Can't fail this IRP
return STATUS_SUCCESS;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// Computer00USBDevice::OnQueryCapabilities
// Handler for IRP_MJ_PNP subfcn IRP_MN_QUERY_CAPABILITIES.
// The system calls this to query the device capabilities.
// The Bus driver fills in the device capabilities structure.
// This method is usually only overridden to alter the
// device capabilities reported by the bus driver.
//
// Arguments:
// IN I
// the query capabilities IRP
//
// Return Value:
// NTSTATUS
//
NTSTATUS Computer00USBDevice::OnQueryCapabilities(KIrp I)
{
T.Trace(TraceInfo, __FUNCTION__"++. IRP %p\n", I);
NTSTATUS status = STATUS_SUCCESS;
I.CopyParametersDown();
I.SetCompletionRoutine(LinkTo(OnQueryCapabilitiesComplete), this, TRUE, TRUE, TRUE);
status = m_Lower.PnpCall(this, I);
T.Trace(TraceInfo, __FUNCTION__"--. IRP %p, STATUS %x\n", I, status);
return status;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// Computer00USBDevice::OnQueryCapabilitiesComplete
// Completion routine for IRP_MJ_PNP subfcn IRP_MN_QUERY_CAPABILITIES.
// The system calls OnQueryCapabilities to query the device capabilities.
// The Bus driver fills in the device capabilities structure.
// This method is called when the IRP is completed to alter the
// device capabilities reported by the bus driver.
//
// Arguments:
// IN I
// the query capabilities IRP
//
// Return Value:
// NTSTATUS
//
NTSTATUS Computer00USBDevice::OnQueryCapabilitiesComplete(KIrp I)
{
T.Trace(TraceInfo, __FUNCTION__"++.");
NTSTATUS status = I.Status();
if (I->PendingReturned)
I.MarkPending();
if (NT_SUCCESS(status))
{
// TODO: Adjust device capabilities structure as required.
I.DeviceCapabilities()->SurpriseRemovalOK = TRUE;
}
T.Trace(TraceInfo, __FUNCTION__"--.");
return status;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// Computer00USBDevice::TestBusInterface
// This routine uses the USB direct client interface to
// query information.
//
// Arguments:
// none
//
// Return Value:
// none
//
void Computer00USBDevice::TestBusInterface()
{
#if (_WDM_ && (WDM_MAJORVERSION > 1 ||((WDM_MAJORVERSION == 1) && (WDM_MINORVERSION >= 0x20))))
if (m_fBusIntfAvailable)
{
T.Trace(TraceInfo, "USB Bus Interface Version: %u\n", m_BusIntf.Version());
T.Trace(TraceInfo, "High Speed Device: %S\n", (m_BusIntf.IsDeviceHighSpeed()?"TRUE":"FALSE"));
USBD_VERSION_INFORMATION UsbVerInfo;
RtlZeroMemory(&UsbVerInfo, sizeof(USBD_VERSION_INFORMATION));
ULONG HcdCapabilities = 0;
m_BusIntf.GetUSBDIVersion(&UsbVerInfo, &HcdCapabilities);
T.Trace(TraceInfo, "USBDI_Version: %u\n", UsbVerInfo.USBDI_Version);
T.Trace(TraceInfo, "Supported_USB_Version: %u\n", UsbVerInfo.Supported_USB_Version);
ULONG TotalBW, ConsumedBW;
NTSTATUS Status = m_BusIntf.GetBandwidth(&TotalBW,&ConsumedBW);
if (STATUS_SUCCESS == Status)
{
T.Trace(TraceInfo, "Total Bandwidth: %u\n", TotalBW);
T.Trace(TraceInfo, "Consumed Bandwidth: %u\n", ConsumedBW);
}
PWSTR HcName = NULL;
Status = m_BusIntf.GetControllerName(HcName);
if (STATUS_SUCCESS == Status && HcName)
{
T.Trace(TraceInfo, "HC Name: %s\n", HcName);
delete HcName;
}
ULONG CurrentFrame;
m_BusIntf.QueryBusTime(&CurrentFrame);
T.Trace(TraceInfo, "Current Frame: %u\n", CurrentFrame);
}
#endif
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// Computer00USBDevice::OnDevicePowerUp
// Handler for IRP_MJ_POWER subfcn IRP_MN_SET_POWER
// for a request to go to power on state from low power state
// This function was called by the framework from the completion
// routine of the IRP_MJ_POWER dispatch handler in KPnpDevice.
// The bus driver has completed the IRP and this driver can now
// access the hardware device.
// This routine runs at DISPATCH_LEVEL.
//
// Arguments:
// IN I
// the power IRP
//
// Return Value:
// NTSTATUS
//
NTSTATUS Computer00USBDevice::OnDevicePowerUp(KIrp I)
{
T.Trace(TraceInfo, __FUNCTION__"++. IRP %p\n", I);
NTSTATUS status = STATUS_SUCCESS;
// TODO: Add device-specific code to:
// Restore any context to the hardware device that
// was saved during the handling of a power down request.
// See the OnDeviceSleep function.
// Do NOT complete this IRP.
// The base class will handle completion of the IRP
T.Trace(TraceInfo, __FUNCTION__"--. IRP %p, STATUS %x\n", I, status);
return status;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// Computer00USBDevice::OnDeviceSleep
// Handler for IRP_MJ_POWER subfcn IRP_MN_SET_POWER
// for a request to go to a low power state from a high power state
// This function was called by the framework from the IRP_MJ_POWER
// dispatch handler in KPnpDevice prior to forwarding to the PDO.
// The hardware has yet to be powered down and this driver can now
// access the hardware device.
// This routine runs at PASSIVE_LEVEL.
//
// Arguments:
// IN I
// the power IRP
//
// Return Value:
// NTSTATUS
//
NTSTATUS Computer00USBDevice::OnDeviceSleep(KIrp I)
{
T.Trace(TraceInfo, __FUNCTION__"++. IRP %p\n", I);
NTSTATUS status = STATUS_SUCCESS;
// TODO: Add device-specific code to:
// Save any context to the hardware device that will be required
// during a power up request. See the OnDevicePowerUp function.
// Do NOT complete this IRP. The base class handles forwarding
// this IRP to the PDO.
T.Trace(TraceInfo, __FUNCTION__"--. IRP %p, STATUS %x\n", I, status);
return status;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// Computer00USBDevice::Create
// Dispatch routine for IRP_MJ_CREATE requests.
//
// Arguments:
// IN I
// the create IRP
//
// Return Value:
// NTSTATUS
//
NTSTATUS Computer00USBDevice::Create(KIrp I)
{
T.Trace(TraceInfo, __FUNCTION__"++. IRP %p\n", I);
// TODO: For any IRP, to display the contents of the IRP
// in a formatted way, use the KTrace << operator:
// T << I;
NTSTATUS status = STATUS_SUCCESS;
// For devices that expose an interface instead of symbolic
// link, DO_EXCLUSIVE flag is not used by the I/O manager.
// So, exclusivity must be enforced here.
if (m_OpenCounter.Test() > 0)
{
status = STATUS_SHARING_VIOLATION;
I.Information() = 0;
I.PnpComplete(this, status);
T.Trace(TraceInfo, __FUNCTION__"--. IRP %p, STATUS %x\n", I, status);
return status;
}
// TODO: At this point, perform custom processing for IRP_MJ_CREATE
// Generally a create IRP is targeted at our FDO, so its not needed
// to pass it down to the PDO.
I.Information() = 0;
I.PnpComplete(this, status);
T.Trace(TraceInfo, __FUNCTION__"--. IRP %p, STATUS %x\n", I, status);
return status;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// Computer00USBDevice::Close
// Dispatch routine for IRP_MJ_CLOSE requests.
//
// Arguments:
// IN I
// the close IRP
//
// Return Value:
// NTSTATUS
//
NTSTATUS Computer00USBDevice::Close(KIrp I)
{
T.Trace(TraceInfo, __FUNCTION__"++. IRP %p\n", I);
NTSTATUS status = STATUS_SUCCESS;
// TODO: At this point, perform custom processing for IRP_MJ_CLOSE
// Generally a close IRP is targeted at our FDO, so we don't need
// to pass it down to the PDO.
I.Information() = 0;
I.PnpComplete(this, status);
T.Trace(TraceInfo, __FUNCTION__"--. IRP %p, STATUS %x\n", I, status);
return status;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// Computer00USBDevice::Read
// Dispatch routine for IRP_MJ_READ requests.
//
// Arguments:
// IN I
// the read IRP
//
// Return Value:
// NTSTATUS
//
NTSTATUS Computer00USBDevice::Read(KIrp I)
{
T.Trace(TraceInfo, __FUNCTION__"++. IRP %p\n", I);
NTSTATUS status = STATUS_SUCCESS;
I.Information() = 0;
// TODO: Validate the parameters of the IRP. Replace "FALSE"
// in the following line with error checking code that
// evaulates to TRUE if the request is not valid.
if (I.Mdl()==NULL) //如果输入缓冲区无效
{
status = STATUS_INVALID_PARAMETER; //返回无效参数
I.PnpComplete(status); //完成该IRP
T.Trace(TraceWarning, __FUNCTION__"--. IRP %p, STATUS %x\n", I, status);
return status;
}
// Always ok to read 0 elements
if (I.ReadSize() == 0)
{
I.PnpComplete(this, status);
T.Trace(TraceInfo, __FUNCTION__"--. IRP %p, STATUS %x\n", I, status);
return status;
}
KMemory Mem(I.Mdl()); // Declare a memory object
// Get a pointer to the caller's buffer. Note that this
// routine is safe on all platforms.
// PUCHAR pBuffer = (PUCHAR) Mem.MapToSystemSpace();
ULONG readSize = I.ReadSize();
ULONG bytesRead = 0;
//创建一个URB,用来发送数据到端点2
PURB pUrb=m_Endpoint2In.BuildBulkTransfer( Mem, //direct方式时,使用KMemory
readSize, //读数据的数据字节数
TRUE, //TURE表示读数据
NULL, //连接下一个传输的URB,这里没有,置为NULL
TRUE, //表示设备传输的字节数可以少于指定的字节数
NULL); //指向一个已经存在的URB。置为NULL,分配一个新的URB
if(pUrb==NULL) //如果分配失败
{
status=STATUS_INSUFFICIENT_RESOURCES; //返回资源不足
}
else
{
status=m_Endpoint2In.SubmitUrb(pUrb,NULL,this,1000); //提交URB,并设置1s超时
I.Information() = pUrb->UrbBulkOrInterruptTransfer.TransferBufferLength; //实际读到的数据字节数
delete pUrb; //删除刚刚分配的URB
}
I.PnpComplete(this, status);
T.Trace(TraceInfo, __FUNCTION__"--. IRP %p, STATUS %x\n", I, status);
return status;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// Computer00USBDevice::Write
// Dispatch routine for IRP_MJ_WRITE requests.
//
// Arguments:
// IN I
// the write IRP
//
// Return Value:
// NTSTATUS
//参看Read函数
NTSTATUS Computer00USBDevice::Write(KIrp I)
{
T.Trace(TraceInfo, __FUNCTION__"++. IRP %p\n", I);
NTSTATUS status = STATUS_SUCCESS;
I.Information() = 0;
// TODO: Validate the parameters of the IRP. Replace "FALSE"
// in the following line with error checking code that
// evaulates to TRUE if the request is not valid.
if (I.Mdl()==NULL)
{
status = STATUS_INVALID_PARAMETER;
I.PnpComplete(status);
T.Trace(TraceWarning, __FUNCTION__"--. IRP %p, STATUS %x\n", I, status);
return status;
}
// Always ok to write 0 elements
if (I.WriteSize() == 0)
{
I.PnpComplete(this, status);
T.Trace(TraceInfo, __FUNCTION__"--. IRP %p, STATUS %x\n", I, status);
return status;
}
KMemory Mem(I.Mdl()); // Declare a memory object
// Get a pointer to the caller's buffer. Note that this
// routine is safe on all platforms.
// PUCHAR pBuffer = (PUCHAR) Mem.MapToSystemSpace();
ULONG writeSize = I.WriteSize();
ULONG bytesSent = 0;
PURB pUrb=m_Endpoint2Out.BuildBulkTransfer( Mem,//direct方式时,使用KMemory
writeSize, //读数据的数据字节数
FALSE, //FALSE表示输出数据
NULL, //连接下一个传输的URB,这里没有,置为NULL
FALSE, //表示设备传输的字节数不可以少于指定的字节数
NULL); //指向一个已经存在的URB。置为NULL,分配一个新的URB
if(pUrb==NULL) //如果分配失败
{
status=STATUS_INSUFFICIENT_RESOURCES; //返回资源不足
}
else
{
status=m_Endpoint2Out.SubmitUrb(pUrb,NULL,this,1000); //提交URB,并设置1s超时
I.Information() = pUrb->UrbBulkOrInterruptTransfer.TransferBufferLength; //实际读到的数据字节数
delete pUrb; //删除刚刚分配的URB
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -