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

📄 cpipe.cpp

📁 Intel PXA270 Wince5.0 BSP
💻 CPP
📖 第 1 页 / 共 5 页
字号:

        pTD->paNextTd.td = pPrev;
        pPrev = pTD;
        pTD = pNext;
    }

    // At this point, the list is connected by virtaddrs in forward chronological
    // order of completion. pTail is the va of the most recently completed TD
    // and pPrev is the va of the oldest completed TD.
    //
    // Just in case we took two interrupts before the CFDT thread got a chance
    // to run, let's append our new list to the end of any pre-existing one.
    // We need to steal the list head in order to do that.
    P_TD pOldQueueHead = (P_TD) InterlockedExchange((LPLONG)&m_pDoneHead, 0L);
    P_TD *ppOldQueueTail = &pOldQueueHead;
    while (*ppOldQueueTail)
        ppOldQueueTail = &(*ppOldQueueTail)->paNextTd.td;
    *ppOldQueueTail = pPrev;

    // Now put the new queue back in place. Ignore the old value because we
    // already know that it was zero.
#ifdef DEBUG
    DWORD old = InterlockedExchange((LPLONG)&m_pDoneHead, (LONG)pOldQueueHead);
    DEBUGCHK( old == 0 );
#else
    (void) InterlockedExchange((LPLONG)&m_pDoneHead, (LONG)pOldQueueHead);
#endif

    // Tell the CFDT thread that it has more work to do.
    SetEvent( m_hCheckForDoneTransfersEvent );
}

// ******************************************************************
// Scope: public 
CPipe::CPipe( IN const LPCUSB_ENDPOINT_DESCRIPTOR lpEndpointDescriptor,
              IN const BOOL fIsLowSpeed,
              IN const UCHAR bDeviceAddress )
//
// Purpose: constructor for CPipe
//
// Parameters: lpEndpointDescriptor - pointer to endpoint descriptor for
//                                    this pipe (assumed non-NULL)
//
//             fIsLowSpeed - indicates if this pipe is low speed
//
// Returns: Nothing.
//
// Notes: Most of the work associated with setting up the pipe
//        should be done via OpenPipe. The constructor actually
//        does very minimal work.
//
//        Do not modify static variables here!!!!!!!!!!!
// ******************************************************************
: m_usbEndpointDescriptor( *lpEndpointDescriptor )
, m_fIsLowSpeed( !!fIsLowSpeed ) // want to ensure m_fIsLowSpeed is 0 or 1
, m_fIsHalted( FALSE )
, m_fTransferInProgress( FALSE )
, m_pLastTransfer ( NULL )
, m_bEndpointAddress( lpEndpointDescriptor->bEndpointAddress )
, m_private_address( bDeviceAddress )
, m_bBusAddress( m_private_address )
, m_pED( NULL )
{
    DEBUGMSG( ZONE_PIPE && ZONE_VERBOSE, (TEXT("+CPipe::CPipe\n")) );
    // CPipe::Initialize should already have been called by now
    // to set up the schedule and init static variables
    DEBUGCHK( m_debug_fInitializeAlreadyCalled );

    InitializeCriticalSection( &m_csPipeLock );

    DEBUGMSG( ZONE_PIPE && ZONE_VERBOSE, (TEXT("-CPipe::CPipe\n")) );
}

// ******************************************************************
// Scope: public virtual 
CPipe::~CPipe( )
//
// Purpose: Destructor for CPipe
//
// Parameters: None
//
// Returns: Nothing.
//
// Notes:   Most of the work associated with destroying the Pipe
//          should be done via ClosePipe
//
//          Do not delete static variables here!!!!!!!!!!!
// ******************************************************************
{
    DEBUGMSG( ZONE_PIPE && ZONE_VERBOSE, (TEXT("+CPipe::~CPipe\n")) );
    // transfers should be aborted or closed before deleting object
    DEBUGCHK( m_fTransferInProgress == FALSE );
    DeleteCriticalSection( &m_csPipeLock );
    DEBUGMSG( ZONE_PIPE && ZONE_VERBOSE, (TEXT("-CPipe::~CPipe\n")) );
}

// ******************************************************************               
// Scope: public 
HCD_REQUEST_STATUS CPipe::IssueTransfer( 
                                    IN const UCHAR address,
                                    IN LPTRANSFER_NOTIFY_ROUTINE const lpStartAddress,
                                    IN LPVOID const lpvNotifyParameter,
                                    IN const DWORD dwFlags,
                                    IN LPCVOID const lpvControlHeader,
                                    IN const DWORD dwStartingFrame,
                                    IN const DWORD dwFrames,
                                    IN LPCDWORD const aLengths,
                                    IN const DWORD dwBufferSize,     
                                    IN_OUT LPVOID const lpvClientBuffer,
                                    IN const ULONG paBuffer,
                                    IN LPCVOID const lpvCancelId,
                                    OUT LPDWORD const adwIsochErrors,
                                    OUT LPDWORD const adwIsochLengths,
                                    OUT LPBOOL const lpfComplete,
                                    OUT LPDWORD const lpdwBytesTransferred,
                                    OUT LPDWORD const lpdwError )
//
// Purpose: Issue a Transfer on this pipe
//
// Parameters: address - USB address to send transfer to
//
//             OTHER PARAMS - see comment in CUhcd::IssueTransfer
//
// Returns: requestOK if transfer issued ok, else requestFailed
//
// Notes:   
// ******************************************************************
{
    DEBUGMSG( ZONE_TRANSFER, (TEXT("+CPipe(%s)::IssueTransfer, address = %d\n"), GetPipeType(), address) );

    STransfer *pTransfer;
    HCD_REQUEST_STATUS  status = requestFailed;

    EnterCriticalSection( &m_csPipeLock );
    if ( m_fTransferInProgress ) {
        pTransfer = new STransfer;
        if (pTransfer == NULL) {
            DEBUGMSG( ZONE_ERROR, (TEXT("-CPipe(%s)::IssueTransfer cannot allocate transfer params!")
                                   TEXT(" returning Failed\n"), GetPipeType() ) );
            LeaveCriticalSection( &m_csPipeLock );
            return requestFailed;
        }
    } else
        pTransfer = &m_transfer;

    if (m_bBusAddress == 0) {
        if (address != 0) {
            // Following assertion is not required by USB but if it's not true then
            // there are synchronization issues between here and ScheduleTransfer.
            DEBUGCHK( m_fTransferInProgress == FALSE );
            // This is the non-const private member to which m_bBusAddress is a reference:
            m_private_address = address;
        }
    } else
        DEBUGCHK( m_bBusAddress == address );

#ifdef DEBUG
    memset( pTransfer, GARBAGE, sizeof( *pTransfer ) );
#endif // DEBUG
    // IssueTransfer params
    pTransfer->address = m_bBusAddress;
    pTransfer->lpfnCallback = lpStartAddress;
    pTransfer->lpvCallbackParameter = lpvNotifyParameter;
    pTransfer->dwFlags = dwFlags;
    pTransfer->lpvControlHeader = lpvControlHeader;
    pTransfer->dwStartingFrame = dwStartingFrame;
    pTransfer->dwFrames = dwFrames;
    pTransfer->aLengths = aLengths;
    pTransfer->dwBufferSize = dwBufferSize;
    pTransfer->lpvClientBuffer = lpvClientBuffer;
    pTransfer->paClientBuffer = paBuffer;
    pTransfer->lpvCancelId = lpvCancelId;
    pTransfer->adwIsochErrors = adwIsochErrors;
    pTransfer->adwIsochLengths = adwIsochLengths;
    pTransfer->lpfComplete = lpfComplete;
    pTransfer->lpdwBytesTransferred = lpdwBytesTransferred;
    pTransfer->lpdwError = lpdwError;
    // non IssueTransfer params
    pTransfer->rPipe = this;
    pTransfer->vaTDList.td = NULL;
    pTransfer->numTDsInList = 0;
    pTransfer->numTDsComplete = 0;
    pTransfer->vaActualBuffer = PUCHAR( pTransfer->lpvClientBuffer );
    pTransfer->paActualBuffer = pTransfer->paClientBuffer;
    pTransfer->dwCurrentPermissions = GetCurrentPermissions();
    pTransfer->lpNextTransfer = NULL;

    // We must allocate the control header memory here so that cleanup works later.
    if (pTransfer->lpvControlHeader != NULL) {
        PUCHAR pSetup;
        // This must be a control transfer. It is asserted elsewhere,
        // but the worst case is we needlessly allocate some physmem.
        if ( !m_pCPhysMem->AllocateMemory(
                                   DEBUG_PARAM( TEXT("IssueTransfer SETUP Buffer"), )
                                   sizeof(USB_DEVICE_REQUEST),
                                   &pSetup,
                                   GetMemoryAllocationFlags() ) ) {
            DEBUGMSG( ZONE_WARNING, (TEXT("CPipe(%s)::IssueTransfer - no memory for SETUP buffer\n"), GetPipeType() ) );
            pTransfer->lpvControlHeader = NULL;
            pTransfer->paControlHeader = 0;
            goto doneIssueTransfer;
        }
        pTransfer->paControlHeader = m_pCPhysMem->VaToPa( pSetup );
        DEBUGCHK( pSetup != NULL && pTransfer->paControlHeader != 0 );
    #ifdef DEBUG
        m_debug_ControlExtraMemoryAllocated += sizeof( USB_DEVICE_REQUEST );
        DEBUGMSG( ZONE_TRANSFER, (TEXT("CPipe(Control)::IssueTransfer - alloc 1 control header, total bytes = %d\n"), m_debug_ControlExtraMemoryAllocated ));
    #endif // DEBUG

        __try {
            memcpy( pSetup, PUCHAR( pTransfer->lpvControlHeader ), sizeof(USB_DEVICE_REQUEST) );
            pTransfer->lpvControlHeader = pSetup;
        } __except( EXCEPTION_EXECUTE_HANDLER ) {
            // bad lpvControlHeader
            pTransfer->lpvControlHeader = pSetup;  // so we can free what we allocated
            goto doneIssueTransfer;
        }
    }

    if ( AreTransferParametersValid(pTransfer) ) {
        __try { // initializing transfer status parameters
            *pTransfer->lpfComplete = FALSE;
            *pTransfer->lpdwBytesTransferred = 0;
            *pTransfer->lpdwError = USB_NOT_COMPLETE_ERROR;
        } __except( EXCEPTION_EXECUTE_HANDLER ) {
              goto doneIssueTransfer;
        }
    } else {
        DEBUGCHK( 0 ); // either we're checking params wrong,
                       // or the caller is passing bad arguments
        goto doneIssueTransfer;
    }

    // allocate Transfer Descriptors 
    DEBUGCHK( pTransfer->numTDsInList == 0 &&
              pTransfer->vaTDList.td == NULL );
    pTransfer->numTDsInList = GetNumTDsNeeded(pTransfer);
    DEBUGCHK( pTransfer->numTDsInList > 0 );
    if ( !m_pCPhysMem->AllocateMemory(
                            DEBUG_PARAM( TEXT("IssueTransfer TDs"), )
                            pTransfer->numTDsInList * GetTdSize(),
                            (PUCHAR *) &pTransfer->vaTDList.td,
                            GetMemoryAllocationFlags() ) ) {
        DEBUGMSG( ZONE_WARNING, (TEXT("CPipe(%s)::IssueTransfer - no memory for TD list\n"), GetPipeType() ) );
        pTransfer->numTDsInList = 0;
        pTransfer->vaTDList.td = NULL;
        goto doneIssueTransfer;
    }
#ifdef DEBUG
    m_debug_TDMemoryAllocated += pTransfer->numTDsInList * GetTdSize(),
    DEBUGMSG( ZONE_TD, (TEXT("CPipe(%s)::IssueTransfer - alloc %d TDs, total bytes = %d\n"), GetPipeType(), pTransfer->numTDsInList, m_debug_TDMemoryAllocated ) );
#endif // DEBUG
    DEBUGCHK( (pTransfer->vaTDList.phys & 0x1F) == 0 ); // isoch TDs must have 32B alignment

#ifdef DEBUG
    if ( pTransfer->dwFlags & USB_IN_TRANSFER ) {
        // I am leaving this in for two reasons:
        //  1. The memset ought to work even on zero bytes to NULL.
        //  2. Why would anyone really want to do a zero length IN?
        DEBUGCHK( pTransfer->dwBufferSize > 0 &&
                  pTransfer->lpvClientBuffer != NULL );
        __try { // IN buffer, trash it
            memset( PUCHAR( pTransfer->lpvClientBuffer ), GARBAGE, pTransfer->dwBufferSize );
        } __except( EXCEPTION_EXECUTE_HANDLER ) {
        }
    }
#endif // DEBUG

    // allocate data buffer if needed
    DEBUGCHK( pTransfer->vaActualBuffer == PUCHAR( pTransfer->lpvClientBuffer ) &&
              pTransfer->paActualBuffer == pTransfer->paClientBuffer );

    if ( pTransfer->dwBufferSize > 0 &&
         pTransfer->paClientBuffer == 0 ) { 

        DEBUGCHK( pTransfer->lpvClientBuffer != NULL );

        // ok, there's data on this transfer and the client
        // did not specify a physical address for the
        // buffer. So, we need to allocate our own.

        if ( !m_pCPhysMem->AllocateMemory(
                                   DEBUG_PARAM( TEXT("IssueTransfer Buffer"), )
                                   pTransfer->dwBufferSize,
                                   &pTransfer->vaActualBuffer, 
                                   GetMemoryAllocationFlags() ) ) {
            DEBUGMSG( ZONE_WARNING, (TEXT("CPipe(%s)::IssueTransfer - no memory for TD buffer\n"), GetPipeType() ) );
            pTransfer->vaActualBuffer = NULL;
            pTransfer->paActualBuffer = 0;
            goto doneIssueTransfer;
        }
        pTransfer->paActualBuffer = m_pCPhysMem->VaToPa( pTransfer->vaActualBuffer );
        DEBUGCHK( pTransfer->vaActualBuffer != NULL &&
                  pTransfer->paActualBuffer != 0 );
#ifdef DEBUG
        m_debug_BufferMemoryAllocated += pTransfer->dwBufferSize;
        DEBUGMSG( ZONE_TD, (TEXT("CPipe(%s)::IssueTransfer - alloc buffer of %d, total bytes = %d\n"), GetPipeType(), pTransfer->dwBufferSize, m_debug_BufferMemoryAllocated ) );
#endif // DEBUG

        if ( !(pTransfer->dwFlags & USB_IN_TRANSFER) ) {
            __try { // copying client buffer for OUT transfer
                memcpy( pTransfer->vaActualBuffer, PUCHAR( pTransfer->lpvClientBuffer ), pTransfer->dwBufferSize );

⌨️ 快捷键说明

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