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

📄 crconfig.c

📁 TI公司的USB键盘例子程序,包括驱动,固件,应用程序等.
💻 C
📖 第 1 页 / 共 2 页
字号:
  PUSB_INTERFACE_DESCRIPTOR pInterfaceDesc;
  PUSBD_INTERFACE_INFORMATION pInterfaceInfo;


    pCryptExt = (PUSB_CRYPT_EXT)pDeviceObj->DeviceExtension;
    pConfigDescriptor = pCryptExt->pConfigDesc;

    lNumInterfaces = pConfigDescriptor->bNumInterfaces;


    // allocate mem for interface list, add one to last entry
    // to indicate end of list - NULL termination of sorts
    pInterfaceList = ExAllocatePool(NonPagedPool, 
                                    sizeof(USBD_INTERFACE_LIST_ENTRY) * 
                                    (lNumInterfaces + 1));

    if(pInterfaceList == NULL) 
    {
        USB_TRACE("SetupInterfaces() Failed to allocate memory\n");
        return STATUS_INSUFFICIENT_RESOURCES;
    }

    // save off beginning of interface lis
    pBegList = pInterfaceList;


    // cycle through interfaces in configuration
    // descriptor and get for our private list
    for(lIFNumber = 0; lIFNumber < lNumInterfaces; lIFNumber++)
    {
        pInterfaceDesc = USBD_ParseConfigurationDescriptorEx(
                                            pConfigDescriptor, 
                                            pConfigDescriptor,
                                            lIFNumber,
                                            0, -1, -1, -1);

        if(pInterfaceDesc) 
        {
            pInterfaceList->InterfaceDescriptor = pInterfaceDesc;
            pInterfaceList->Interface = NULL;
            pInterfaceList++;
        }

    }  // end for()

    // add NULL to end to termiante list
    pInterfaceList->InterfaceDescriptor = NULL;
    pInterfaceList->Interface = NULL;

    // now create descriptor which will request USB host
    // driver to use interface
    //
    pUrb = USBD_CreateConfigurationRequestEx(pConfigDescriptor, pBegList);

    if(pUrb == NULL)
    {
        ExFreePool(pBegList);
        return STATUS_INSUFFICIENT_RESOURCES;
    }


    // get pointer to first interface -- 2 pipes
    pInterfaceInfo = &pUrb->UrbSelectConfiguration.Interface;

    for(lCount = 0; lCount < (long)pInterfaceInfo->NumberOfPipes; lCount++) 
    {
        //
        // perform pipe initialization here
        // set the transfer size and any pipe flags we use
        // USBD sets the rest of the Interface struct members
        //
        pInterfaceInfo->Pipes[lCount].MaximumTransferSize = 
                            USBD_DEFAULT_MAXIMUM_TRANSFER_SIZE;
    }

    // now setup second interface - 3 pipes
    // get pointer to second interface
    (PCHAR)pInterfaceInfo = (PCHAR)pInterfaceInfo + pInterfaceInfo->Length;

    for(lCount = 0; lCount < (long)pInterfaceInfo->NumberOfPipes; lCount++) 
    {
        //
        // perform pipe initialization here
        // set the transfer size and any pipe flags we use
        // USBD sets the rest of the Interface struct members
        //
        pInterfaceInfo->Pipes[lCount].MaximumTransferSize = 
                            USBD_DEFAULT_MAXIMUM_TRANSFER_SIZE;
    }

    // call the USB bus driver to request these interfaces
    Status = SendUSBRequest(pDeviceObj, pUrb, TRUE);

    if(!NT_SUCCESS(Status)) 
    {
        ExFreePool(pUrb);
        ExFreePool(pBegList);
        return Status;
    }


    // save first interface info
    pInterfaceInfo = &pUrb->UrbSelectConfiguration.Interface;

    do {

        pCryptExt->EncryptionInfo.FunctionType = CRYPT_FUNCTION_ENCRYPTION;

        Status = SaveInterfaceInfo(pInterfaceInfo, &pCryptExt->pEncryptIFInfo,
                                   &pCryptExt->EncryptionInfo);

        if(!NT_SUCCESS(Status))
            break;

        // save second interface info
        (PCHAR)pInterfaceInfo = (PCHAR)pInterfaceInfo + pInterfaceInfo->Length;

        pCryptExt->DecryptionInfo.FunctionType = CRYPT_FUNCTION_DECRYPTION;

        Status = SaveInterfaceInfo(pInterfaceInfo, &pCryptExt->pDecryptIFInfo, 
                                   &pCryptExt->DecryptionInfo);

        if(!NT_SUCCESS(Status))
            break;

    } while(FALSE);



    // output interface info, for debugging purposes
    DumpInterfaceInfo(pDeviceObj, pCryptExt->pEncryptIFInfo);
    DumpInterfaceInfo(pDeviceObj, pCryptExt->pDecryptIFInfo);

    // all done, free memory
    ExFreePool(pUrb);
    ExFreePool(pBegList);

    return Status;
}




// =============== SaveInterfaceInfo() ==============
//
//  Desc: Save's off interface and pipe context info.
//
//  Returns: STATUS_SUCCESS or NT failure status
//
//
NTSTATUS SaveInterfaceInfo(IN PUSBD_INTERFACE_INFORMATION pInterfaceInfo,
                           IN PUSBD_INTERFACE_INFORMATION *pSaveUsbIF,
                           IN PCRYPT_HANDLE_INFO pHandleInfo)
{
  long lCnt;
  
    // save interface info in device extension
    *pSaveUsbIF = ExAllocatePool(NonPagedPool, pInterfaceInfo->Length);

    if(*pSaveUsbIF == NULL)
    {
        USB_TRACE("SaveInterfaceInfo() Memory alloc for failed\n");
        return STATUS_INSUFFICIENT_RESOURCES;
    }


    // save off interfaces into our device extension
    RtlCopyMemory((*pSaveUsbIF), pInterfaceInfo,  pInterfaceInfo->Length);

     // sanity check, the first pipe should be output from 
     // from device, the high bit in the endpoint address
     // should be set
     ASSERT(pInterfaceInfo->Pipes[0].EndpointAddress  & 0x80);

     ASSERT( !(pInterfaceInfo->Pipes[1].EndpointAddress  & 0x80) );


    // now save off the pipe info, depending on type of IF
    switch(pHandleInfo->FunctionType)
    {
        case CRYPT_FUNCTION_ENCRYPTION:


             pHandleInfo->OutputPipe   = pInterfaceInfo->Pipes[0].PipeHandle;
             pHandleInfo->InputPipe    = pInterfaceInfo->Pipes[1].PipeHandle;
             pHandleInfo->ProgressPipe = NULL;
             break;

        case CRYPT_FUNCTION_DECRYPTION:

             pHandleInfo->OutputPipe   = pInterfaceInfo->Pipes[0].PipeHandle;
             pHandleInfo->InputPipe    = pInterfaceInfo->Pipes[1].PipeHandle;
             pHandleInfo->ProgressPipe = pInterfaceInfo->Pipes[2].PipeHandle;
             break;

        default:

            ASSERT(FALSE);
            break;
    }


    return STATUS_SUCCESS;
}
                       
                       

// =============== DumpInterfaceInfo() ==============
//
//  For debugging purposes, dump the interface info
//
//
//
void DumpInterfaceInfo(IN PDEVICE_OBJECT pDeviceObject,
                       IN PUSBD_INTERFACE_INFORMATION pUsbIF)
{
  PUSB_CRYPT_EXT pCryptExt;
  int nIndex;

    pCryptExt = (PUSB_CRYPT_EXT)pDeviceObject->DeviceExtension;


    //
    // Dump interface info 
    //

    // dump Interface Info
    USB_TRACE("\n---- Interface Info-----\n");
    USB_TRACE1("Number of Pipes: 0x%x\n", pUsbIF->NumberOfPipes);
    USB_TRACE1("Interface Number: 0x%x\n", pUsbIF->InterfaceNumber);


    USB_TRACE3("Class, subclass, protocol 0x%x 0x%x 0x%x\n",
                         pUsbIF->Class,
                         pUsbIF->SubClass,
                         pUsbIF->Protocol);

    // dump pipe info
    USB_TRACE("---- Pipe Info-----\n");

    for(nIndex=0; nIndex < (int)pUsbIF->NumberOfPipes; nIndex++) 
    {
        USB_TRACE1("PipeType 0x%x\n", pUsbIF->Pipes[nIndex].PipeType);

        USB_TRACE1("EndpointAddress 0x%x\n", 
                             pUsbIF->Pipes[nIndex].EndpointAddress);

        USB_TRACE1("MaxPacketSize 0x%x\n", 
                            pUsbIF->Pipes[nIndex].MaximumPacketSize);

        USB_TRACE1("Interval 0x%x\n", 
                             pUsbIF->Pipes[nIndex].Interval);

        USB_TRACE1("Handle 0x%x\n", 
                             pUsbIF->Pipes[nIndex].PipeHandle);

        USB_TRACE1("MaximumTransferSize 0x%x\n", 
                            pUsbIF->Pipes[nIndex].MaximumTransferSize);
    }

}


⌨️ 快捷键说明

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