原创 usb多重接口设备开发——4 驱动

2008-7-28 22:57 3077 6 6 分类: MCU/ 嵌入式
// 0号接口——dfu
// Generated by DriverWizard version DriverStudio 3.1.0 (Build 1722)
// Requires Compuware's DriverWorks classes

GUID xDFUDevice_Guid = xDFUDevice_CLASS_GUID;

xDFUDevice::xDFUDevice(PDEVICE_OBJECT Pdo, ULONG Unit) :
    KPnpDevice(Pdo, &xDFUDevice_Guid)
{
    // Check constructor status
    if ( ! NT_SUCCESS(m_ConstructorStatus) )
    {
        return;
    }

    m_Unit = Unit;
    m_Lower.Initialize(this, Pdo);
    m_Interface.Initialize(
                    m_Lower, //KUsbLowerDevice
                    0,         //InterfaceNumber:接口号,dfu设备作为0号接口
                    1,         //ConfigurationValue
                    0         //Initial Interface Alternate Setting
                    );

    SetLowerDevice(&m_Lower);
    SetPnpPolicy();
    SetPowerPolicy();
}

// 厂商请求和类请求合并处理
// 参考DS自带例程中ezusb.sys( Cypress的第一版驱动)
NTSTATUS xDFUDevice::VendorOrClassRequest(KIrp I)
{
    NTSTATUS status = STATUS_SUCCESS;

    //Check parameters
    if(I.IoctlInputBufferSize() != sizeof(VENDOR_OR_CLASS_REQUEST_CONTROL))
    {
        return STATUS_INVALID_PARAMETER;
    }

    PVENDOR_OR_CLASS_REQUEST_CONTROL p =
        reinterpret_cast<PVENDOR_OR_CLASS_REQUEST_CONTROL>( I.IoctlBuffer() );

    USHORT nUrbFunction = 0;

    // Determine the URB function based on 'RequestType' and 'Recepient' passed
    // down from user mode
    switch(((p->requestType) << 2) | (p->recepient ))
    {
        case 0x04:
            nUrbFunction = URB_FUNCTION_CLASS_DEVICE;
            break;
        case 0x05:
            nUrbFunction = URB_FUNCTION_CLASS_INTERFACE;
            break;
        case 0x06:
            nUrbFunction = URB_FUNCTION_CLASS_ENDPOINT;
            break;
        case 0x07:
            nUrbFunction = URB_FUNCTION_CLASS_OTHER;
            break;
        case 0x08:
            nUrbFunction = URB_FUNCTION_VENDOR_DEVICE;
            break;
        case 0x09:
            nUrbFunction = URB_FUNCTION_VENDOR_INTERFACE;
            break;
        case 0x0A:
            nUrbFunction = URB_FUNCTION_VENDOR_ENDPOINT;
            break;
        case 0x0B:
            nUrbFunction = URB_FUNCTION_VENDOR_OTHER;
            break;
        default:
            return STATUS_INVALID_PARAMETER;
            break;
    }

    KMemory M(I.Mdl());
    PURB pUrb = m_Lower.BuildVendorRequest(
                            M,
                            I.IoctlOutputBufferSize(),
                            p->requestTypeReservedBits,
                            p->request,
                            p->value,
                            p->direction,
                            TRUE,
                            NULL,
                            p->index,
                            nUrbFunction
                            );

    if (pUrb)
    {
        status = m_Lower.SubmitUrb(pUrb);
    }
    else
    {
        status = STATUS_INSUFFICIENT_RESOURCES;
    }

    if( NT_SUCCESS(status) )
    {
        I.Information() = 
           pUrb->UrbControlVendorClassRequest.TransferBufferLength;
    }

    delete pUrb;
    return status;
}

其它部分,采用网上流行的DS使用指南中的方法

// 1号接口——xAPI
xAPIDevice::xAPIDevice(PDEVICE_OBJECT Pdo, ULONG Unit) :
    KPnpDevice(Pdo, &xAPIDevice_CLASS_GUID)
{
    // Check constructor status
    if ( ! NT_SUCCESS(m_ConstructorStatus) )
    {
        return;
    }

    // Remember our unit number
    m_Unit = Unit;

    // Initialize the lower device
    m_Lower.Initialize(this, Pdo);
    m_Interface.Initialize(
                    m_Lower, //KUsbLowerDevice
                    1,         //InterfaceNumber
                    1,         //ConfigurationValue
                    0         //Initial Interface Alternate Setting
                    );
    // Initialize each Pipe object
    m_EP1InBulk.Initialize( m_Lower, 0x81, 65535);
    m_EP2OutBulk.Initialize(m_Lower, 0x02, 65535);

    // Inform the base class of the lower edge device object
    SetLowerDevice(&m_Lower);
    SetPnpPolicy();
    SetPowerPolicy();
}
其它与0号接口类似。

本例中,dfu设备不需要0号以外端点;事实上:可以将设备端的不同端点定义为不同接口,
在设备插上是,就会出现多个usb设备。

本例的开发环境,
win2003ddk sp1(xp)
DriverStudio 3.1.0 (Build 1722)
PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
我要评论
0
6
关闭 站长推荐上一条 /1 下一条