xx_focus
驱动中牛
驱动中牛
  • 注册日期2003-08-06
  • 最后登录2007-06-08
  • 粉丝0
  • 关注0
  • 积分96分
  • 威望10点
  • 贡献值0点
  • 好评度8点
  • 原创分0分
  • 专家分0分
阅读:1310回复:2

请教,两个 USB 管道读写函数问题(DS工具 )20分

楼主#
更多 发布于:2004-11-23 13:21
使用 DS 生成框架程序,用 DS 自带例题中的代码进行管道读写

操作系统 Windows 2K + SP4
硬件使用 bulk 方式

一、控制读。直接重新启动系统

NTSTATUS TESTDevice::TEST_IOCTL_800_Handler(KIrp I)
{
//NTSTATUS status = STATUS_SUCCESS;

           t << "Entering TESTDevice::TEST_IOCTL_800_Handler, " << I << EOL;
// TODO: Verify that the input parameters are correct
// If not, return STATUS_INVALID_PARAMETER

// TODO: Handle the the TEST_IOCTL_800 request, or
// defer the processing of the IRP (i.e. by queuing) and set
// status to STATUS_PENDING.

// TODO: Assuming that the request was handled here. Set I.Information
// to indicate how much data to copy back to the user.

// TODO: Check the incoming request.  Replace "FALSE" in the following
// line with a check that returns TRUE if the request is not valid.
    NTSTATUS status = STATUS_INSUFFICIENT_RESOURCES;

USB_COMPLETION_INFO* pCompInfo = new (NonPagedPool) USB_COMPLETION_INFO;

if (pCompInfo == NULL)
{
I.Information() = 0;
return I.PnpComplete(this, STATUS_INSUFFICIENT_RESOURCES);
}
// Declare a memory object
KMemory Mem(I.Mdl());

    ULONG dwTotalSize = I.ReadSize(CURRENT);
ULONG dwMaxSize = m_Pip1.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;
}

    ULONG dwBytesRead = 0;

    // Create an URB to do actual Bulk read from Pipe0
    PURB pUrb = m_Pip1.BuildBulkTransfer(
Mem,  // Where is data coming from?
dwTotalSize,  // How much data to read?
TRUE,         // direction (TRUE = IN)
NULL      // Link to next URB
);

if ( pUrb != NULL)
{
   // Submit the URB to our USB device, synchronously - say less is OK
pUrb->UrbBulkOrInterruptTransfer.TransferFlags =
(USBD_TRANSFER_DIRECTION_IN | USBD_SHORT_TRANSFER_OK);

pCompInfo->m_pClass = this;
pCompInfo->m_pUrb = pUrb;

        status = m_Pip1.SubmitUrb(I, pUrb, LinkTo(ReadComplete), pCompInfo, 0);
        //status = m_Pip1.SubmitUrb(pUrb, NULL, NULL);

        if ( NT_SUCCESS(status) )
        {
dwBytesRead = pUrb->UrbBulkOrInterruptTransfer.TransferBufferLength;
   }

delete pUrb;
pUrb = NULL;
}

delete pCompInfo;
pCompInfo = NULL;

    I.Information() = dwBytesRead;
    return I.PnpComplete(this, status, IO_NO_INCREMENT);
}

二、控制写,直接重新启动系统
NTSTATUS TESTDevice::TEST_IOCTL_801_Handler(KIrp I)
{
// NTSTATUS status = STATUS_SUCCESS;

t << "Entering TESTDevice::TEST_IOCTL_801_Handler, " << I << EOL;
// TODO: Verify that the input parameters are correct
// If not, return STATUS_INVALID_PARAMETER

// TODO: Handle the the TEST_IOCTL_801 request, or
// defer the processing of the IRP (i.e. by queuing) and set
// status to STATUS_PENDING.

// TODO: Assuming that the request was handled here. Set I.Information
// to indicate how much data to copy back to the user.
   NTSTATUS status = STATUS_INSUFFICIENT_RESOURCES;

USB_COMPLETION_INFO* pCompInfo = new (NonPagedPool) USB_COMPLETION_INFO;

if (pCompInfo == NULL)
{
I.Information() = 0;
return I.PnpComplete(this, STATUS_INSUFFICIENT_RESOURCES);
}

// Declare a memory object
KMemory Mem(I.Mdl());

    ULONG dwTotalSize = I.WriteSize(CURRENT);
ULONG dwMaxSize = m_Pip2.MaximumTransferSize();

// If the total requested write size is greater than the Maximum Transfer
// Size for the Pipe, request to write 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;
}

    ULONG dwBytesSent = 0;

    PURB pUrb = m_Pip2.BuildBulkTransfer(
Mem,          // Where is data coming from?
dwTotalSize,  // How much data to write?
FALSE,        // direction (FALSE = OUT)
NULL          // Link to next URB
);

    // Submit the URB to our USB device, synchronously
    if (pUrb != NULL)
    {
pCompInfo->m_pClass = this;
pCompInfo->m_pUrb = pUrb;
        status = m_Pip2.SubmitUrb(I, pUrb, LinkTo(WriteComplete), pCompInfo, 0);

        if ( NT_SUCCESS(status) )
        {
            dwBytesSent = pUrb->UrbBulkOrInterruptTransfer.TransferBufferLength;
        }

delete pUrb;
pUrb = NULL;
    }

delete pCompInfo;
pCompInfo = NULL;

    I.Information() = dwBytesSent;
    return I.PnpComplete(this, status, IO_NO_INCREMENT);
}

[编辑 -  11/23/04 by  xx_focus]
xx_focus
驱动中牛
驱动中牛
  • 注册日期2003-08-06
  • 最后登录2007-06-08
  • 粉丝0
  • 关注0
  • 积分96分
  • 威望10点
  • 贡献值0点
  • 好评度8点
  • 原创分0分
  • 专家分0分
沙发#
发布于:2004-11-23 15:19
函数 ReadComplete() 代码
NTSTATUS TESTDevice::ReadComplete(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 nBytesRead = 0;

if ( NT_SUCCESS(status) )
{
nBytesRead = pUrb->UrbBulkOrInterruptTransfer.TransferBufferLength;
if (nBytesRead > 0)
t << "Read() got " << nBytesRead<< " bytes from USB\n";
    }

// Deallocate Urb and context structure
delete pUrb;
delete pContext;

// set returned count
I.Information() = nBytesRead;

           //不知道这一句加在这里有什么意义?
// Plug and Play accounting
DecrementOutstandingRequestCount();

// allow IRP completion processing
return STATUS_SUCCESS;
}
xx_focus
驱动中牛
驱动中牛
  • 注册日期2003-08-06
  • 最后登录2007-06-08
  • 粉丝0
  • 关注0
  • 积分96分
  • 威望10点
  • 贡献值0点
  • 好评度8点
  • 原创分0分
  • 专家分0分
板凳#
发布于:2004-11-23 16:57
怎么没人回答吗?
游客

返回顶部