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

📄 usbrndis.c

📁 6410BSP3
💻 C
📖 第 1 页 / 共 4 页
字号:
            pucData = m_pucUSBDescriptors;
            wLength = m_pucUSBDescriptors[0];
            break;

        case USB_CONFIGURATION_DESCRIPTOR_TYPE:
            USBDBGMSG(USBDBG_ZONE_VERBOSE, (
                L"UsbDbg:Setup request [USB_REQUEST_GET_DESCRIPTOR: "
                L"USB_CONFIGURATION_DESCRIPTOR_TYPE] Len:%d\r\n",
                pUdr->wLength
            ));
            pucData = (UCHAR *)m_configDesc1.pUsbConfigDescriptor;
            // return the full descriptor if host asked for it
            if (pUdr->wLength > m_configDesc1.pUsbConfigDescriptor->bLength)
                wLength = CONFIGDESC_LEN;
            else
                wLength = m_configDesc1.pUsbConfigDescriptor->bLength;
            break;

        case USB_STRING_DESCRIPTOR_TYPE:
            USBDBGMSG(USBDBG_ZONE_VERBOSE, (
                L"UsbDbg:Setup request [USB_REQUEST_GET_DESCRIPTOR: "
                L"USB_STRING_DESCRIPTOR_TYPE] Len:%d\r\n",
                pUdr->wLength));
            switch (LOBYTE(wType))
            {
                case 0x00:
                    pucData = (UCHAR *) &m_SupportedLang;
                    wLength = m_SupportedLang.bLength;
                    break;

                case 0x01:
                    {
                        USB_STRING* pManufacturer = NULL;
                        if (m_pddIfc.pfnIoctl(
                                USBDBG_PDD_IOCTL_MANUFACTURER_STRING,
                                NULL,
                                0,
                                (LPVOID) &pManufacturer,
                                sizeof(pManufacturer),
                                0))
                        {
                            pucData = (UCHAR *)pManufacturer;
                            wLength = pManufacturer->ucbLength;
                        }
                        else
                        {
                            USBDBGMSG(USBDBG_ZONE_VERBOSE, (
                                L"UsbDbg: Using default manufacturer"
                                L" string.\r\n"));
                            pucData = (UCHAR *) &m_Manufacturer;
                            wLength = m_Manufacturer.ucbLength;
                        }
                    }
                    break;

                case 0x02:
                    {
                        USB_STRING* pProductString = NULL;
                        if (m_pddIfc.pfnIoctl(
                                USBDBG_PDD_IOCTL_PRODUCT_STRING,
                                NULL,
                                0,
                                (LPVOID) &pProductString,
                                sizeof(pProductString),
                                0))
                        {
                            pucData = (UCHAR *)pProductString;
                            wLength = pProductString->ucbLength;
                        }
                        else
                        {
                            USBDBGMSG(USBDBG_ZONE_VERBOSE, (
                                L"UsbDbg:Using default product string.\r\n"
                            ));
                            pucData = (UCHAR *) &m_Product;
                            wLength = m_Product.ucbLength;
                        }
                    }
                    break;

                case 0x03:
                    {
                        USB_STRING* pucSerialNumString = NULL;
                        if (m_pddIfc.pfnIoctl(
                            USBDBG_PDD_IOCTL_SERIALNUM_STRING,
                            NULL,
                            0,
                            (LPVOID) &pucSerialNumString,
                            sizeof(pucSerialNumString),
                            0))
                        {
                            pucData = (UCHAR *)pucSerialNumString;
                            wLength = pucSerialNumString->ucbLength;
                        }
                        else
                        {
                            USBDBGMSG(USBDBG_ZONE_VERBOSE, (
                                L"UsbDbg:Using default implementation of "
                                L"UsbSerialNumber generation.\r\n"
                            ));
                            pucData = (UCHAR *) &m_UsbSerialNum;
                            wLength = m_UsbSerialNum.ucbLength;
                        }
                    }
                    break;

                default:
                    USBDBGMSG(USBDBG_ZONE_ERROR, (
                        L"ERROR!UsbDbg:*** Unknown STRING index %d\r\n", 
                        LOBYTE(wType)
                    ));
                    fRet = FALSE;
                    break;
            }
            break;

        default:
            USBDBGMSG(USBDBG_ZONE_ERROR, (
                L"ERROR!UsbDbg:*** Unknown GET_DESCRIPTOR request:0x%x\r\n",
                HIBYTE(wType)
            ));
            fRet = FALSE;
            break;
    }

    if (fRet)
    {
        fRet = (SendRecvData(CONTROL_ENDPT, ENDPT_DIR_TX, pucData, wLength, 0)
                == ERROR_SUCCESS);
        return fRet;
    }
    else
        return TRUE;    //status stage should still happen

}

static
BOOL
SendPendingRndisMsg(
    )
{
    DWORD dwRet;
    
    // send the pendig RNDIS message on EP0
    dwRet = SendRecvData(CONTROL_ENDPT,
              ENDPT_DIR_TX,
              m_rndisMsgToSend.pbData,
              m_rndisMsgToSend.cbData,
              0);
    m_rndisMsgToSend.fMsgSent = TRUE;

    if (dwRet == ERROR_SUCCESS)
    {
        USBDBGMSG(USBDBG_ZONE_VERBOSE, (
            L"usbdbg:Sent RNDIS Message on EP0 IN %d bytes\r\n",
            m_rndisMsgToSend.cbData));

        return TRUE;
    }
    else
        return FALSE;
        
}

//Process GetStatus request from host side
static
BOOL
ProcessGetStatus(
    USB_DEVICE_REQUEST* pUdr
    )
{
    UCHAR pucData[2] = {0};

    if(pUdr->bmRequestType == (USB_REQUEST_DEVICE_TO_HOST | USB_REQUEST_FOR_DEVICE))
    {
        //is this device selfpowered?
        if(((m_configDesc1.pUsbConfigDescriptor)->bmAttributes & USB_CONFIG_SELF_POWERED) != 0)
            pucData[0] |= USB_GETSTATUS_SELF_POWERED;
        //is this device configured as remote wakeup resource?
        if(((m_configDesc1.pUsbConfigDescriptor)->bmAttributes & USB_CONFIG_REMOTE_WAKEUP) != 0)
            pucData[0] |= USB_GETSTATUS_REMOTE_WAKEUP_ENABLED;
    }

    return (SendRecvData(CONTROL_ENDPT, ENDPT_DIR_TX, pucData, sizeof(pucData), 0) == ERROR_SUCCESS);

}

static
void
ProcessRndisMsg(
    PBYTE pbData,
    UINT32 cbData
    )
{
    //wait for the last processing to complete before processing the new pkt
    //

    static BOOL s_fProcessingRndisMsg = FALSE;
    static PBYTE s_pbData = NULL;
    static UINT32 s_cbData = 0;
    static BOOL s_fPendingRndisMsg = FALSE;
    
    if (s_fProcessingRndisMsg)
    {
        // remember the data for processing once last call to processrndismsg
        // returns
        s_pbData = pbData;
        s_cbData = cbData;
        s_fPendingRndisMsg = TRUE;
        return;
    }

    s_fPendingRndisMsg = FALSE;
    s_fProcessingRndisMsg = TRUE;
    RndisPdd_RecvdMsg(pbData,cbData);
    s_fProcessingRndisMsg = FALSE;

    while (s_fPendingRndisMsg)
    {
        s_fPendingRndisMsg = FALSE;
        s_fProcessingRndisMsg = TRUE;
        RndisPdd_RecvdMsg(s_pbData, s_cbData);
        s_fProcessingRndisMsg = FALSE;
    }

}

static
void
ProcessSetupPacket(
    USB_DEVICE_REQUEST* pUdr,
    BOOL* pfProcessRndisMsg
    )
{
    BOOL fSendRecvStatusACK = TRUE;
    DWORD dwSuccess;

    USBDBGMSG(USBDBG_ZONE_FUNC, (L"usbdbg: +ProcessSetupPacket\r\n"));

    *pfProcessRndisMsg = FALSE;
    
    // class/vendor request
    //
    if (pUdr->bmRequestType & (USB_REQUEST_CLASS | USB_REQUEST_VENDOR))
    {
        switch (pUdr->bRequest) {

            case SEND_ENCAPSULATED_COMMAND:
                USBDBGMSG(USBDBG_ZONE_VERBOSE, (
                    L"UsbDbg:Setup request [SEND_ENCAPSULATED_COMMAND]\r\n"
                    ));

                // read the rndis msg from PDD
                dwSuccess = SendRecvData(CONTROL_ENDPT,
                                          ENDPT_DIR_RX,
                                          m_ep0MsgRxBuffer,
                                          pUdr->wLength,
                                          0);

                fSendRecvStatusACK = (dwSuccess == ERROR_SUCCESS);
                if (dwSuccess == ERROR_SUCCESS)
                {
                    // This is a RNDIS message. Set flag to process it
                    *pfProcessRndisMsg = TRUE;
                }
                break;

            case GET_ENCAPSULATED_RESPONSE:
                USBDBGMSG(USBDBG_ZONE_VERBOSE, (
                    L"UsbDbg:Setup request [GET_ENCAPSULATED_RESPONSE]\r\n"
                ));
                fSendRecvStatusACK = SendPendingRndisMsg();
                break;

            default:
                USBDBGMSG(USBDBG_ZONE_ERROR, (
                    L"ERROR!UsbDbg:Unrecognized Setup request. "
                    L"bmRequestType=0x%x. bRequest=0x%x\r\n",
                    pUdr->bmRequestType,
                    pUdr->bRequest
                ));
                break;
        }
    }

    //a standard request
    //
    else
    {
        switch(pUdr->bRequest) {
            case USB_REQUEST_GET_STATUS:
                if((pUdr->bmRequestType ==  (USB_REQUEST_DEVICE_TO_HOST |USB_REQUEST_FOR_DEVICE)) 
                    ||(pUdr->bmRequestType ==  (USB_REQUEST_DEVICE_TO_HOST |USB_REQUEST_FOR_INTERFACE))  )
                {
                    fSendRecvStatusACK = ProcessGetStatus(pUdr);
                }
                else 
                {
                    USBDBGMSG(USBDBG_ZONE_WARN, (
                        L"WARN!UsbDbg:RequestType==0x%d, Unrecognzied"
                        L"or unsupported request\r\n", pUdr->bmRequestType
                        ));
                }
                break;

            case USB_REQUEST_CLEAR_FEATURE:
                if (pUdr->bmRequestType == 0x02)
                    USBDBGMSG(USBDBG_ZONE_WARN, (
                        L"WARN!UsbDbg:***RequestType==0x02\r\n"
                        ));
                break;

            case USB_REQUEST_SET_FEATURE:
                if (pUdr->bmRequestType == 0x02)
                    USBDBGMSG(USBDBG_ZONE_WARN, (
                        L"WARN!usbdbg:***RequestType==0x02\r\n"));
                break;

            case USB_REQUEST_SET_ADDRESS:
                USBDBGMSG(USBDBG_ZONE_VERBOSE, (
                    L"usbdbg:Recvd. USB_REQUEST_SET_ADDRESS.\r\n"));
                break;

            case USB_REQUEST_GET_DESCRIPTOR:
                fSendRecvStatusACK = SendDescriptor(pUdr);
                break;

            case USB_REQUEST_SET_DESCRIPTOR:
                break;

            case USB_REQUEST_GET_CONFIGURATION:
                break;

            case USB_REQUEST_SET_CONFIGURATION:
                break;

            case USB_REQUEST_GET_INTERFACE:
                break;

            case USB_REQUEST_SET_INTERFACE:
                break;

            case USB_REQUEST_SYNC_FRAME:
                break;

            default:
                USBDBGMSG(USBDBG_ZONE_WARN, (
                    L"WARN!usbdbg:***Unknown request 0x%x\r\n", pUdr->bRequest
                ));
        }
    }   // end of else for standard request

    if (fSendRecvStatusACK)
    {
        if (USBFN_IS_OUT_REQUESTTYPE(pUdr->bmRequestType))//Host->Device request
            SendControlStatusHandshake();
        else    // Device->Host request
            RecvControlStatusHandshake();
    }
    USBDBGMSG(USBDBG_ZONE_FUNC, (L"usbdbg: -ProcessSetupPacket\r\n"));

⌨️ 快捷键说明

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