⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usbfndrv.cpp

📁 扬创yc2440-t2-dm9000 ce 5.0 bsp
💻 CPP
📖 第 1 页 / 共 3 页
字号:
        {
            sizeof(USB_ENDPOINT_DESCRIPTOR),// bLength
            USB_ENDPOINT_DESCRIPTOR_TYPE,   // bDescriptorType
            INTERRUPT_IN_ENDPOINT_ADDRESS,  // bEndpointAddress (endpoint 2, out)
            USB_ENDPOINT_TYPE_INTERRUPT,    // bmAttributes
            MAX_INTERRUPT_ENDPOINT_PACKET_SIZE,   // wMaxPacketSize
            0x20                            // bInterval (interrupt only)
        },
        NULL
    }
};

static UFN_INTERFACE                        g_HighSpeedInterface1 = {
    sizeof(UFN_INTERFACE),
    {
        sizeof(USB_INTERFACE_DESCRIPTOR),   // bLength
        USB_INTERFACE_DESCRIPTOR_TYPE,      // bDescriptorType
        0x00,                               // bInterfaceNumber    
        0x00,                               // bAlternateSetting

        dim(g_HighSpeedEndpoints),          // bNumEndpoints
        0xff,       // bInterfaceClass
        0xff,// bInterfaceSubClass
        0xff,             // bInterfaceProtocol
        0x00                                // iInterface    
    },
    NULL,                                   // extended
    0,
    g_HighSpeedEndpoints                    // endpoint array
};
static UFN_INTERFACE                        g_HighSpeedInterface2 = {
    sizeof(UFN_INTERFACE),
    {
        sizeof(USB_INTERFACE_DESCRIPTOR),   // bLength
        USB_INTERFACE_DESCRIPTOR_TYPE,      // bDescriptorType
        0x00,                               // bInterfaceNumber    
        0x00,                               // bAlternateSetting

        dim(g_HighSpeedEndpoints)-1,        // bNumEndpoints
        0xff,       // bInterfaceClass
        0xff,// bInterfaceSubClass
        0xff,             // bInterfaceProtocol
        0x00                                // iInterface    
    },
    NULL,                                   // extended
    0,
    g_HighSpeedEndpoints                    // endpoint array
};

static UFN_INTERFACE                        g_FullSpeedInterface1 = {
    sizeof(UFN_INTERFACE),
    {
        sizeof(USB_INTERFACE_DESCRIPTOR),   // bLength
        USB_INTERFACE_DESCRIPTOR_TYPE,      // bDescriptorType
        0x00,                               // bInterfaceNumber    
        0x00,                               // bAlternateSetting

        dim(g_FullSpeedEndpoints),          // bNumEndpoints
        0xff,       // bInterfaceClass
        0xff,// bInterfaceSubClass
        0xff,             // bInterfaceProtocol
        0x00                                // iInterface    
    },
    NULL,                                   // extended
    0,
    g_FullSpeedEndpoints                    // endpoint array
};

static UFN_INTERFACE                        g_FullSpeedInterface2 = {
    sizeof(UFN_INTERFACE),
    {
        sizeof(USB_INTERFACE_DESCRIPTOR),   // bLength
        USB_INTERFACE_DESCRIPTOR_TYPE,      // bDescriptorType
        0x00,                               // bInterfaceNumber    
        0x00,                               // bAlternateSetting

        dim(g_FullSpeedEndpoints)-1,          // bNumEndpoints
        0xff,       // bInterfaceClass
        0xff,// bInterfaceSubClass
        0xff,             // bInterfaceProtocol
        0x00                                // iInterface    
    },
    NULL,                                   // extended
    0,
    g_FullSpeedEndpoints                    // endpoint array
};

static UFN_CONFIGURATION                    g_HighSpeedConfig1 = {
    sizeof(UFN_CONFIGURATION),
    {
        sizeof(USB_CONFIGURATION_DESCRIPTOR),// bLength
        USB_CONFIGURATION_DESCRIPTOR_TYPE,  // bDescriptorType
        
        CB_CONFIG_DESCRIPTOR,               // wTotalLength
        0x01,                               // bNumInterfaces
        0x01,                               // bConfigurationValue
        0x00,                               // iConfiguration
        USB_CONFIG_RESERVED_ATTRIBUTE | USB_CONFIG_SELF_POWERED,            // bmAttributes
        0x00                                // MaxPower
    },
    NULL,
    0x01,                                   // number of interfaces
    &g_HighSpeedInterface1                  // interface array
};

static UFN_CONFIGURATION                    g_HighSpeedConfig2 = {
    sizeof(UFN_CONFIGURATION),
    {
        sizeof(USB_CONFIGURATION_DESCRIPTOR),// bLength
        USB_CONFIGURATION_DESCRIPTOR_TYPE,  // bDescriptorType
        
        CB_CONFIG_DESCRIPTOR- sizeof(USB_ENDPOINT_DESCRIPTOR), // wTotalLength
        0x01,                               // bNumInterfaces
        0x01,                               // bConfigurationValue
        0x00,                               // iConfiguration
        USB_CONFIG_RESERVED_ATTRIBUTE | USB_CONFIG_SELF_POWERED,            // bmAttributes
        0x00                                // MaxPower
    },
    NULL,
    0x01,                                   // number of interfaces
    &g_HighSpeedInterface2                  // interface array
};

static UFN_CONFIGURATION                    g_FullSpeedConfig1 = {
    sizeof(UFN_CONFIGURATION),
    {
        sizeof(USB_CONFIGURATION_DESCRIPTOR),// bLength
        USB_CONFIGURATION_DESCRIPTOR_TYPE,  // bDescriptorType
        
        CB_CONFIG_DESCRIPTOR,               // wTotalLength
        0x01,                               // bNumInterfaces
        0x01,                               // bConfigurationValue
        0x00,                               // iConfiguration
        USB_CONFIG_RESERVED_ATTRIBUTE | USB_CONFIG_SELF_POWERED,            // bmAttributes
        0x00                                // MaxPower
    },
    NULL,
    0x01,                                   // number of interfaces
    &g_FullSpeedInterface1                   // interface array
};

static UFN_CONFIGURATION                    g_FullSpeedConfig2 = {
    sizeof(UFN_CONFIGURATION),
    {
        sizeof(USB_CONFIGURATION_DESCRIPTOR),// bLength
        USB_CONFIGURATION_DESCRIPTOR_TYPE,  // bDescriptorType
        
        CB_CONFIG_DESCRIPTOR - sizeof(USB_ENDPOINT_DESCRIPTOR), // wTotalLength
        0x01,                               // bNumInterfaces
        0x01,                               // bConfigurationValue
        0x00,                               // iConfiguration
        USB_CONFIG_RESERVED_ATTRIBUTE | USB_CONFIG_SELF_POWERED,            // bmAttributes
        0x00                                // MaxPower
    },
    NULL,
    0x01,                                   // number of interfaces
    &g_FullSpeedInterface2                   // interface array
};


static UFN_CLIENT_REG_INFO g_RegInfo = { sizeof(UFN_CLIENT_REG_INFO) };

static LPCWSTR g_rgpszStrings0409[] = {
    g_RegInfo.szVendor, g_RegInfo.szProduct
};

static UFN_STRING_SET g_rgStringSets[] = {
    0x0409, g_rgpszStrings0409, dim(g_rgpszStrings0409)
};


CUsbFn::CUsbFn (LPCTSTR lpActivePath)
:   m_pUfnFuncs(&m_UfnFuncs)
{
    DWORD dwLen = _tcslen(lpActivePath);
    m_lpActivePath = new TCHAR[dwLen+1];
    if (m_lpActivePath) {
        _tcscpy(m_lpActivePath,lpActivePath);
        m_lpActivePath[dwLen]= 0;
    }
    m_hDevice = NULL;
    m_pvInterface = NULL;
    m_CurrentSpeed = BS_HIGH_SPEED;
}
#define COMLAYER_DRIVER_PATH TEXT("ComLayer")
BOOL CUsbFn::Init()
{
    if (m_lpActivePath &&
            UfnInitializeInterface(m_lpActivePath, &m_hDevice, &m_UfnFuncs, &m_pvInterface)== ERROR_SUCCESS) {
        PREFAST_ASSERT(m_hDevice!=NULL);
        PREFAST_ASSERT(m_pUfnFuncs!=NULL);
        CRegistryEdit driverKey(m_lpActivePath);
        if (driverKey.IsKeyOpened()) {
            DWORD dwRet = UfnGetRegistryInfo(m_lpActivePath, &g_RegInfo);
            if (dwRet == ERROR_SUCCESS) {
                g_HighSpeedDeviceDesc.idVendor = (USHORT) g_RegInfo.idVendor;
                g_HighSpeedDeviceDesc.idProduct = (USHORT) g_RegInfo.idProduct;
                g_HighSpeedDeviceDesc.bcdDevice = (USHORT) g_RegInfo.bcdDevice;

                g_FullSpeedDeviceDesc.idVendor = g_HighSpeedDeviceDesc.idVendor;
                g_FullSpeedDeviceDesc.idProduct = g_HighSpeedDeviceDesc.idProduct;
                g_FullSpeedDeviceDesc.bcdDevice = g_HighSpeedDeviceDesc.bcdDevice;
                
                // Register the descriptor tree with device controller
                m_fInterrupt = TRUE;
                dwRet = ERROR_GEN_FAILURE;
                
#ifdef USE_INTERRUPT_ENDPOINT
                dwRet = m_pUfnFuncs->lpRegisterDevice(m_hDevice,
                    &g_HighSpeedDeviceDesc, &g_HighSpeedConfig1, 
                    &g_FullSpeedDeviceDesc, &g_FullSpeedConfig1, 
                    g_rgStringSets, dim(g_rgStringSets));
#endif            
                if (dwRet != ERROR_SUCCESS) { // Try Second
                    m_fInterrupt = FALSE;
                    dwRet = m_pUfnFuncs->lpRegisterDevice(m_hDevice,
                        &g_HighSpeedDeviceDesc, &g_HighSpeedConfig2, 
                        &g_FullSpeedDeviceDesc, &g_FullSpeedConfig2, 
                        g_rgStringSets, dim(g_rgStringSets));
                }
                if (dwRet == ERROR_SUCCESS ) { // Do I need Initial Pipe Here?
                    dwRet = m_pUfnFuncs->lpStart(m_hDevice, DeviceNotifyStub, this,  &m_hDefaultPipe);

                }
            }
            return (dwRet == ERROR_SUCCESS );
        }
    }
    return FALSE;
}
CUsbFn::~CUsbFn()
{
    if (m_lpActivePath)
        delete m_lpActivePath;

    if (m_pvInterface)
        UfnDeinitializeInterface(m_pvInterface);
}

BOOL WINAPI CUsbFn::DeviceNotifyStub(PVOID   pvNotifyParameter, DWORD   dwMsg, DWORD   dwParam)
{
    PREFAST_ASSERT(pvNotifyParameter!=NULL);
    return ((CUsbFn *)pvNotifyParameter)->DeviceNotify(dwMsg,dwParam);
}
BOOL CUsbFn::DeviceNotify(DWORD dwMsg, DWORD dwParam)
{
    DEBUGMSG (ZONE_WRITE|ZONE_EVENTS,(TEXT("DeviceNotify (0x%x,0x%x)\r\n"),dwMsg, dwParam));
    switch(dwMsg) {
        case UFN_MSG_BUS_EVENTS: {
            // Ensure device is in running state
            DEBUGCHK(m_hDefaultPipe);

           switch(dwParam) {                
                case UFN_DETACH:
                    CableDetached();
                    break;
                case UFN_ATTACH:
                    break;
                case UFN_RESET: {
                    CableDetached();
                    break;
                }
             } 

           break;
        }
        
        case UFN_MSG_BUS_SPEED:
             m_CurrentSpeed = (UFN_BUS_SPEED) dwParam;
        break;

        case UFN_MSG_SETUP_PACKET:
        case UFN_MSG_PREPROCESSED_SETUP_PACKET: {
            HandleRequest(dwMsg,*(PUSB_DEVICE_REQUEST)dwParam);
            break;
        }

        case UFN_MSG_CONFIGURED: {
            if (dwParam == 0) {
                CableDetached();
            }
            else {
                CableAttached();
            }
            break;
        }
        
    }
    return TRUE;
}
void CUsbFn::HandleRequest(
    DWORD dwMsg,
    USB_DEVICE_REQUEST udr
    )
{
    CONTROL_RESPONSE response;

    if (dwMsg == UFN_MSG_PREPROCESSED_SETUP_PACKET) {
        response = CR_SUCCESS; // Don't respond since it was already handled.
        
        if ( udr.bmRequestType ==
            (USB_REQUEST_HOST_TO_DEVICE | USB_REQUEST_STANDARD | USB_REQUEST_FOR_ENDPOINT) ) {
            switch (udr.bRequest) {
                case USB_REQUEST_CLEAR_FEATURE:
                    // This may be needed in the future to terminate a transfer 
                    // in progress
                    HandleClearFeature(udr);
                    break;
            }
        }
    }
    else {
        DEBUGCHK(dwMsg == UFN_MSG_SETUP_PACKET);
        response = CR_STALL_DEFAULT_PIPE;

        if (udr.bmRequestType & USB_REQUEST_CLASS) {
            response = HandleClassRequest(udr);
        }
    }

    if (response == CR_STALL_DEFAULT_PIPE) {
        m_pUfnFuncs->lpStallPipe(m_hDevice, m_hDefaultPipe);
        m_pUfnFuncs->lpSendControlStatusHandshake(m_hDevice);
    }
    else if (response == CR_SUCCESS_SEND_CONTROL_HANDSHAKE) {
        m_pUfnFuncs->lpSendControlStatusHandshake(m_hDevice);
    }
}
CONTROL_RESPONSE CUsbFn::HandleClassRequest(
    USB_DEVICE_REQUEST udr
    )
{

    CONTROL_RESPONSE response = CR_STALL_DEFAULT_PIPE;
           
    if (udr.bmRequestType == 
            (USB_REQUEST_CLASS | USB_REQUEST_FOR_INTERFACE | USB_REQUEST_HOST_TO_DEVICE) ) { 
        if (udr.bRequest == SET_CONTROL_LINE_STATE) {
            /* Host is notifying us of control line state.
             * wValue contains bitmask
             * 0 - DTR
             * 1 - RTS
             */
            DEBUGMSG( ZONE_FUNCTION, (TEXT("SET_CONTROL_LINE_STATE %X\r\n"),
                 udr.wValue));
            DWORD dwModemStatus = 0;
            if (udr.wValue & USB_COMM_DTR)
              dwModemStatus |= (MS_DSR_ON|MS_RLSD_ON); // DTR active, set DSR/RLSD
            if (udr.wValue & USB_COMM_RTS) 
              dwModemStatus |= MS_CTS_ON;   // RTS active, set CTS
            ModemSignal(dwModemStatus);
            response = CR_SUCCESS_SEND_CONTROL_HANDSHAKE;
        }
    }
    else {
        RETAILMSG(1, (_T("Unrecognized Serial class bRequest -> 0x%x\r\n"), udr.bmRequestType));
        ASSERT(FALSE);
    }
    ASSERT(response == CR_SUCCESS_SEND_CONTROL_HANDSHAKE);
    return response;
}
void USBSerialFn::CableDetached()
{
    // This is last time to call this structure.
    m_HardwareLock.Lock();
    CloseBulkIn();
    CloseBulkOut();
    CloseInterruptIn();
    m_HardwareLock.Unlock();
    ModemSignal(0);
}
void USBSerialFn::CableAttached() 
{
    m_HardwareLock.Lock();
    DWORD dwBulkSize = (m_CurrentSpeed == BS_HIGH_SPEED?
        g_HighSpeedEndpoints[BULK_IN_DESCRIPTOR_INDEX].Descriptor.wMaxPacketSize:
        g_FullSpeedEndpoints[BULK_IN_DESCRIPTOR_INDEX].Descriptor.wMaxPacketSize);
    BYTE uEdptAddr  = (m_CurrentSpeed == BS_HIGH_SPEED?
        g_HighSpeedEndpoints[BULK_IN_DESCRIPTOR_INDEX].Descriptor.bEndpointAddress:
        g_FullSpeedEndpoints[BULK_IN_DESCRIPTOR_INDEX].Descriptor.bEndpointAddress);
    OpenBulkIn(m_hDevice, m_pUfnFuncs,uEdptAddr,FALSE, dwBulkSize, 0x1000, 2);
    
    dwBulkSize = (m_CurrentSpeed == BS_HIGH_SPEED?
        g_HighSpeedEndpoints[BULK_OUT_DESCRIPTOR_INDEX].Descriptor.wMaxPacketSize:
        g_FullSpeedEndpoints[BULK_OUT_DESCRIPTOR_INDEX].Descriptor.wMaxPacketSize);
    uEdptAddr  = (m_CurrentSpeed == BS_HIGH_SPEED?
        g_HighSpeedEndpoints[BULK_OUT_DESCRIPTOR_INDEX].Descriptor.bEndpointAddress:
        g_FullSpeedEndpoints[BULK_OUT_DESCRIPTOR_INDEX].Descriptor.bEndpointAddress);
    OpenBulkOut(m_hDevice, m_pUfnFuncs,uEdptAddr,TRUE,dwBulkSize,dwBulkSize, 4); // WCEUSBSH Bug. Only can usb MaxPacketSize
    
    if (m_fInterrupt) {
        DWORD dwInterruptSize = (m_CurrentSpeed == BS_HIGH_SPEED?
            g_HighSpeedEndpoints[INTERRUPT_IN_DESCRIPTOR_INDEX].Descriptor.wMaxPacketSize:
            g_FullSpeedEndpoints[INTERRUPT_IN_DESCRIPTOR_INDEX].Descriptor.wMaxPacketSize);
        uEdptAddr  = (m_CurrentSpeed == BS_HIGH_SPEED?
            g_HighSpeedEndpoints[INTERRUPT_IN_DESCRIPTOR_INDEX].Descriptor.bEndpointAddress:
            g_FullSpeedEndpoints[INTERRUPT_IN_DESCRIPTOR_INDEX].Descriptor.bEndpointAddress);
        OpenInterruptIn(m_hDevice, m_pUfnFuncs,uEdptAddr,FALSE,dwInterruptSize,8, 1);
    }
    m_HardwareLock.Unlock();
}
CSerialPDD * CreateSerialObject(LPTSTR lpActivePath, PVOID pMdd,PHWOBJ pHwObj, DWORD )
{
    CSerialPDD * pSerialPDD = new USBSerialFn(lpActivePath,pMdd, pHwObj);
    if (pSerialPDD && !pSerialPDD->Init()) {
        delete pSerialPDD;
        pSerialPDD = NULL;
    }
    return pSerialPDD;
}
void DeleteSerialObject(CSerialPDD * pSerialPDD)
{
    if (pSerialPDD)
        delete pSerialPDD;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -