📄 motod12device.cpp
字号:
return I.PnpComplete(this, STATUS_INVALID_PARAMETER);
}
// Always ok to write 0 elements.
if (I.WriteSize() == 0)
{
I.Information() = 0;
return I.PnpComplete(this, STATUS_SUCCESS);
}
ULONG dwTotalSize = I.WriteSize(CURRENT);
ULONG dwMaxSize = m_Endpoint2OUT.MaximumTransferSize();
// If the total requested read size is greater than the Maximum Transfer
// Size for the Pipe, request to read only the Maximum Transfer Size since
// the bus driver will fail an URB with a TransferBufferLength of greater
// than the Maximum Transfer Size.
if (dwTotalSize > dwMaxSize)
{
ASSERT(dwMaxSize);
dwTotalSize = dwMaxSize;
}
// Declare a memory object
KMemory Mem(I.Mdl());
// Allocate a new context structure for Irp completion
USB_COMPLETION_INFO* pCompInfo = new (NonPagedPool) USB_COMPLETION_INFO;
if (pCompInfo == NULL)
{
I.Information() = 0;
return I.PnpComplete(this, STATUS_INSUFFICIENT_RESOURCES);
}
// TODO: Select the correct pipe to write to
// Create an URB to do actual Bulk write to the pipe
PURB pUrb = m_Endpoint2OUT.BuildBulkTransfer(
Mem, // Where is data coming from?
dwTotalSize, // How much data to read?
FALSE, // direction (FALSE = OUT)
NULL // Link to next URB
);
if (pUrb == NULL)
{
delete pCompInfo;
I.Information() = 0;
return I.PnpComplete(this, STATUS_INSUFFICIENT_RESOURCES);
}
// Initialize context structure
pCompInfo->m_pClass = this;
pCompInfo->m_pUrb = pUrb;
// Submit the URB to our USB device
NTSTATUS status;
status = m_Endpoint2OUT.SubmitUrb(I, pUrb, LinkTo(WriteComplete), pCompInfo, 0);
return status;
}
////////////////////////////////////////////////////////////////////////
// MotoD12Device::WriteComplete
//
// Routine Description:
// Completion Handler for IRP_MJ_WRITE
//
// Parameters:
// I - IRP just completed by USB
// pContext - Context structure containing pointer to Urb
//
// Return Value:
// NTSTATUS STATUS_SUCCESS
//
// Comments:
// This routine is called when USBD completes the write request
//
NTSTATUS MotoD12Device::WriteComplete(KIrp I, USB_COMPLETION_INFO* pContext)
{
// Normal completion routine code to propagate pending flag
if (I->PendingReturned)
{
I.MarkPending();
}
NTSTATUS status = I.Status();
PURB pUrb = pContext->m_pUrb;
ULONG nBytesWritten = 0;
if ( NT_SUCCESS(status) )
{
nBytesWritten = pUrb->UrbBulkOrInterruptTransfer.TransferBufferLength;
if (nBytesWritten > 0)
t << "Wrote " << nBytesWritten << " bytes to USB\n";
}
// Deallocate Urb and context structure
delete pUrb;
delete pContext;
// set returned count
I.Information() = nBytesWritten;
// Plug and Play accounting
DecrementOutstandingRequestCount();
// allow IRP completion processing
return STATUS_SUCCESS;
}
////////////////////////////////////////////////////////////////////////
// MotoD12Device::DeviceControl
//
// Routine Description:
// Handler for IRP_MJ_DEVICE_CONTROL
//
// Parameters:
// I - Current IRP
//
// Return Value:
// None
//
// Comments:
// This routine is the first handler for Device Control requests.
// The KPnpDevice class handles restricting IRP flow
// if the device is stopping or being removed.
//
NTSTATUS MotoD12Device::DeviceControl(KIrp I)
{
NTSTATUS status;
t << "Entering MotoD12Device::Device Control, " << I << EOL;
switch (I.IoctlCode())
{
case MOTOD12_IOCTL_START:
status = MOTOD12_IOCTL_START_Handler(I);
break;
case MOTOD12_IOCTL_STOP:
status = MOTOD12_IOCTL_STOP_Handler(I);
break;
case MOTOD12_IOCTL_READ:
status = MOTOD12_IOCTL_READ_Handler(I);
break;
case MOTOD12_IOCTL_TIME:
status = MOTOD12_IOCTL_TIME_Handler(I);
break;
default:
// Unrecognized IOCTL request
status = STATUS_INVALID_PARAMETER;
break;
}
// If the IRP's IOCTL handler deferred processing using some driver
// specific scheme, the status variable is set to STATUS_PENDING.
// In this case we simply return that status, and the IRP will be
// completed later. Otherwise, complete the IRP using the status
// returned by the IOCTL handler.
if (status == STATUS_PENDING)
{
return status;
}
else
{
return I.PnpComplete(this, status);
}
}
////////////////////////////////////////////////////////////////////////
// MotoD12Device::MOTOD12_IOCTL_START_Handler
//
// Routine Description:
// Handler for IO Control Code MOTOD12_IOCTL_START
//
// Parameters:
// I - IRP containing IOCTL request
//
// Return Value:
// NTSTATUS - Status code indicating success or failure
//
// Comments:
// This routine implements the MOTOD12_IOCTL_START function.
// This routine runs at passive level.
//
NTSTATUS MotoD12Device::MOTOD12_IOCTL_START_Handler(KIrp I)
{
NTSTATUS status = STATUS_INVALID_PARAMETER;
t << "Entering MotoD12Device::MOTOD12_IOCTL_START_Handler, " << I << EOL;
__try
{
// TODO: Verify that the input parameters are correct
// If not, return STATUS_INVALID_PARAMETER
if(I.IoctlOutputBufferSize() || I.IoctlInputBufferSize() || I.IoctlBuffer())
__leave;
// TODO: Handle the the ZBUARD_IOCTL_LED_FLASH request, or
// defer the processing of the IRP (i.e. by queuing) and set
// status to STATUS_PENDING.
PURB pUrb = m_Lower.BuildVendorRequest(
NULL, // transfer buffer
0, // transfer buffer size
0, // request reserved bits
0, // request
0 // Value
);
// transmit
status = m_Lower.SubmitUrb(pUrb, NULL, NULL, 5000L);
}
__finally
{
// TODO: Assuming that the request was handled here. Set I.Information
// to indicate how much data to copy back to the user.
I.Information() = 0;
I.Status() = status;
}
return status;
}
////////////////////////////////////////////////////////////////////////
// MotoD12Device::MOTOD12_IOCTL_STOP_Handler
//
// Routine Description:
// Handler for IO Control Code MOTOD12_IOCTL_STOP
//
// Parameters:
// I - IRP containing IOCTL request
//
// Return Value:
// NTSTATUS - Status code indicating success or failure
//
// Comments:
// This routine implements the MOTOD12_IOCTL_STOP function.
// This routine runs at passive level.
//
NTSTATUS MotoD12Device::MOTOD12_IOCTL_STOP_Handler(KIrp I)
{
NTSTATUS status = STATUS_INVALID_PARAMETER;
t << "Entering MotoD12Device::MOTOD12_IOCTL_STOP_Handler, " << I << EOL;
__try
{
// TODO: Verify that the input parameters are correct
// If not, return STATUS_INVALID_PARAMETER
if(I.IoctlOutputBufferSize() || I.IoctlInputBufferSize() || I.IoctlBuffer())
__leave;
// TODO: Handle the the ZBUARD_IOCTL_LED_FLASH request, or
// defer the processing of the IRP (i.e. by queuing) and set
// status to STATUS_PENDING.
PURB pUrb = m_Lower.BuildVendorRequest(
NULL, // transfer buffer
0, // transfer buffer size
0, // request reserved bits
1, // request
0 // Value
);
// transmit
status = m_Lower.SubmitUrb(pUrb, NULL, NULL, 5000L);
}
__finally
{
// TODO: Assuming that the request was handled here. Set I.Information
// to indicate how much data to copy back to the user.
I.Information() = 0;
I.Status() = status;
}
return status;
}
////////////////////////////////////////////////////////////////////////
// MotoD12Device::MOTOD12_IOCTL_READ_Handler
//
// Routine Description:
// Handler for IO Control Code MOTOD12_IOCTL_READ
//
// Parameters:
// I - IRP containing IOCTL request
//
// Return Value:
// NTSTATUS - Status code indicating success or failure
//
// Comments:
// This routine implements the MOTOD12_IOCTL_READ function.
// This routine runs at passive level.
//
NTSTATUS MotoD12Device::MOTOD12_IOCTL_READ_Handler(KIrp I)
{
NTSTATUS status = STATUS_INVALID_PARAMETER;
t << "Entering MotoD12Device::MOTOD12_IOCTL_READ_Handler, " << I << EOL;
__try
{
// TODO: Verify that the input parameters are correct
// If not, return STATUS_INVALID_PARAMETER
if(I.IoctlOutputBufferSize() || I.IoctlInputBufferSize() || I.IoctlBuffer())
__leave;
// TODO: Handle the the ZBUARD_IOCTL_LED_FLASH request, or
// defer the processing of the IRP (i.e. by queuing) and set
// status to STATUS_PENDING.
PURB pUrb = m_Lower.BuildVendorRequest(
NULL, // transfer buffer
0, // transfer buffer size
0, // request reserved bits
2, // request
0 // Value
);
// transmit
status = m_Lower.SubmitUrb(pUrb, NULL, NULL, 5000L);
}
__finally
{
// TODO: Assuming that the request was handled here. Set I.Information
// to indicate how much data to copy back to the user.
I.Information() = 0;
I.Status() = status;
}
return status;
}
////////////////////////////////////////////////////////////////////////
// MotoD12Device::MOTOD12_IOCTL_TIME_Handler
//
// Routine Description:
// Handler for IO Control Code MOTOD12_IOCTL_TIME
//
// Parameters:
// I - IRP containing IOCTL request
//
// Return Value:
// NTSTATUS - Status code indicating success or failure
//
// Comments:
// This routine implements the MOTOD12_IOCTL_TIME function.
// This routine runs at passive level.
//
NTSTATUS MotoD12Device::MOTOD12_IOCTL_TIME_Handler(KIrp I)
{
NTSTATUS status = STATUS_SUCCESS;
t << "Entering MotoD12Device::MOTOD12_IOCTL_TIME_Handler, " << I << EOL;
__try
{
// TODO: Verify that the input parameters are correct
// If not, return STATUS_INVALID_PARAMETER
if(I.IoctlOutputBufferSize() || I.IoctlInputBufferSize() || I.IoctlBuffer())
__leave;
// TODO: Handle the the ZBUARD_IOCTL_LED_FLASH request, or
// defer the processing of the IRP (i.e. by queuing) and set
// status to STATUS_PENDING.
PURB pUrb = m_Lower.BuildVendorRequest(
NULL, // transfer buffer
0, // transfer buffer size
0, // request reserved bits
3, // request
0 // Value
);
// transmit
status = m_Lower.SubmitUrb(pUrb, NULL, NULL, 5000L);
}
__finally
{
// TODO: Assuming that the request was handled here. Set I.Information
// to indicate how much data to copy back to the user.
I.Information() = 0;
I.Status() = status;
}
return status;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -