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

📄 chw.cpp

📁 Intel PXA270 Wince5.0 BSP
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Module Name:
//     CHW.cpp
// Abstract:
//     This file implements the UHCI specific register routines
//
// Notes:
//
//

#include "chw.hpp"
#include "cpipe.hpp"


#pragma warning (4 : 4100)      // temporary, will clean later. JEFFRO.

// BPC DEBUG
#ifdef PLAT_LUBBOCK
extern "C" EnableSA1111DMA();
extern "C" DisableSA1111DMA();
extern "C" DisableSA1111VLIO();
extern "C" EnableCotullaFLIO( int nCS, unsigned short usRT, unsigned short usRBW, unsigned short usRDF, unsigned short usRDN, unsigned short usRRR, unsigned short usRBuff );
#endif

// definitions for static variables
volatile HcRegisters *CHW::m_portBase = 0;
volatile HCCA        *CHW::m_pHCCA = 0;

CRITICAL_SECTION    CHW::m_csFrameCounter;
WORD                CHW::m_wFrameHigh = 0;

DWORD               CHW::m_dwSysIntr = 0;
HANDLE              CHW::m_hUsbInterruptEvent = NULL;
HANDLE              CHW::m_hUsbInterruptThread = NULL;
BOOL                CHW::m_fUsbInterruptThreadClosing = FALSE;

LONG                CHW::m_fFrameLengthIsBeingAdjusted = FALSE;
LONG                CHW::m_fStopAdjustingFrameLength = FALSE;
HANDLE              CHW::m_hAdjustDoneCallbackEvent = NULL;
USHORT              CHW::m_uNewFrameLength = 0;

PDWORD CHW::m_pControlHead = 0;
PDWORD CHW::m_pBulkHead = 0;
PDWORD CHW::m_pInterruptTable = 0;

extern BOOL g_fPowerUpFlag;

// ******************************************************************
BOOL CHW::Initialize( IN const REGISTER portBase,
                      IN const DWORD dwSysIntr,
                      IN CPhysMem *pCPhysMem,
                      IN LPVOID pvUhcdPddObject )
//
// Purpose: Reset and Configure the Host Controller with the schedule.
//
// Parameters: portBase - base address for host controller registers
//
//             dwSysIntr - system interrupt number to use for USB
//                         interrupts from host controller
//
//             frameListPhysAddr - physical address of frame list index
//                                 maintained by CPipe class
//
//             pvUhcdPddObject - PDD specific structure used during suspend/resume
//
// Returns: TRUE if initialization succeeded, else FALSE
//
// Notes: This function is only called from the CUhcd::Initialize routine.
//
//        This function is static
// ******************************************************************
{
    DEBUGMSG( ZONE_INIT, (TEXT("+CHW::Initialize base=0x%x, intr=0x%x\n"), portBase, dwSysIntr));

    InitializeCriticalSection( &m_csFrameCounter );

    DEBUGCHK( m_wFrameHigh == 0 );

    m_portBase = (volatile HcRegisters *) portBase;
    m_dwSysIntr = dwSysIntr;

    if ( m_portBase == 0 ) {
        DEBUGMSG( ZONE_ERROR, (TEXT("-CHW::Initialize - zero Register Base\n")));
        return FALSE;
    }

    // set up the Host Controller Communications Area
    if (pCPhysMem->AllocateSpecialMemory(sizeof(HCCA), (PUCHAR *) &m_pHCCA) == FALSE) {
        DEBUGMSG(ZONE_ERROR, (TEXT("-CHW::Initialize, cannot allocate HCCA!!\n")));
        return FALSE;
    }
    memset((PUCHAR)m_pHCCA, 0, sizeof(HCCA));

    // Reset the HC hardware
    DEBUGMSG(ZONE_INIT && ZONE_REGISTERS, (TEXT("CHW::Initialize - signalling H/W reset\n")));
    m_portBase->HcCommandStatus.HCR = 1;
    while (m_portBase->HcCommandStatus.HCR == 1)
        ; // wait for the reset operation to complete (max 10us)
    m_portBase->HcControl.HCFS = HcRegisters::HcControl::HCFS_RESET;

    m_portBase->HcHCCA = pCPhysMem->VaToPa((PUCHAR) m_pHCCA);  // locate the HCCA

    // These calculations are from the OHCI 1.0a spec, section 5.4
    // The 210 is the maximum amount of USB protocol overhead per frame, measured in bit-times.
    m_portBase->HcFmInterval.FSMPS = (m_portBase->HcFmInterval.FI - 210) * 6 / 7;
    m_portBase->HcPeriodicStart.PS = m_portBase->HcFmInterval.FI * 9 / 10;

    m_portBase->HcControl.CBSR = 3; // 4:1 Control/Bulk ratio
    DEBUGMSG(ZONE_INIT && ZONE_REGISTERS, (TEXT("CHW::Initialize - end H/W reset\n")));

    DEBUGMSG(ZONE_INIT && ZONE_REGISTERS && ZONE_VERBOSE, (TEXT("CHW::Initialize - initing schedule\n")));
    m_wFrameHigh = 0;           // reset the frame counter
    DEBUGMSG(ZONE_INIT && ZONE_REGISTERS && ZONE_VERBOSE, (TEXT("CHW::Initialize - \n") ));

    // Re-initialize the public constants now that we know the portBase
    // This'll be done properly - in a constructor - in the future.
    m_pControlHead = const_cast<PDWORD>(&m_portBase->HcControlHeadED);
    m_pBulkHead = const_cast<PDWORD>(&m_portBase->HcBulkHeadED);
    m_pInterruptTable = const_cast<PDWORD>(m_pHCCA->HccaInterruptTable);

    // m_hUsbInterrupt - Auto Reset, and Initial State = non-signaled
    DEBUGCHK( m_hUsbInterruptEvent == NULL );
    m_hUsbInterruptEvent = CreateEvent( NULL, FALSE, FALSE, NULL );
    if ( m_hUsbInterruptEvent == NULL ) {
        DEBUGMSG(ZONE_ERROR, (TEXT("-CHW::Initialize. Error creating USBInterrupt event\n")));
        return FALSE;
    }

    InterruptDisable( m_dwSysIntr ); // Just to make sure this is really ours.
    // Initialize Interrupt. When interrupt id # m_sysIntr is triggered,
    // m_hUsbInterruptEvent will be signaled. Last 2 params must be NULL
    if ( !InterruptInitialize( m_dwSysIntr, m_hUsbInterruptEvent, NULL, NULL) ) {
        DEBUGMSG(ZONE_ERROR, (TEXT("-CHW::Initialize. Error on InterruptInitialize\r\n")));
        return FALSE;
    }

    // Apply power to all root hub ports, just like UHCI.
	// this root hub port configuration stuff belongs somewhere else...

	// set up the over-current indication monitoring scheme: 
	// in this case, monitor over-current indicators collectively, not on a per port basis.
	m_portBase->HcRhDescriptorA.NOCP = 1;	// allow over-current indication monitoring
	m_portBase->HcRhDescriptorA.OCPM = 0;	// report over-current condition collectively, eg. not on a per port basis.

	// set up the port power enabling scheme:
	// in this case, allow power switching, but only on a global basis,
	// eg. all ports respond to the SetGlobalPower (set Status,LPSC=1) or ClrGlobalPower (set Status.LPS=1) commands
    m_portBase->HcRhDescriptorA.NPS = 1;	// this mode is for having the ports powered any time the USB OHC is powered
//	m_portBase->HcRhDescriptorA.NPS = 0;	// this mode, NoPowerSwitch = 0, (eg. ALLOW power to be switched), is for having the ports powered when Status.LPSC is set to 1.
	m_portBase->HcRhDescriptorA.PSM = 0;	// this mode, PowerSwitchMode = 0, eg. Global, is for having the ports powered when Status.LPSC is set to 1.
    m_portBase->HcRhStatus.LPSC = 1;		// power the ports. writing to the Status.LPSC bit is the SetGlobalPower command
    
    // Start up our IST - the parameter passed to the thread
    // is unused for now
    DEBUGCHK( m_hUsbInterruptThread == NULL &&
              m_fUsbInterruptThreadClosing == FALSE );
    m_hUsbInterruptThread = CreateThread( 0, 0, UsbInterruptThreadStub, pvUhcdPddObject, 0, NULL );
    if ( m_hUsbInterruptThread == NULL ) {
        DEBUGMSG(ZONE_ERROR, (TEXT("-CHW::Initialize. Error creating IST\n")));
        return FALSE;
    }

    // Enable interrupts
    m_portBase->HcInterruptEnable.SO  = 1; // scheduling overrun, added by bpc
    m_portBase->HcInterruptEnable.WDH = 1; // writeback done head was written
    m_portBase->HcInterruptEnable.UE  = 1; // unrecoverable error
    // bpc: temp disable this: m_portBase->HcInterruptEnable.FNO = 1; // frame number overflow
    m_portBase->HcInterruptEnable.RHSC= 1; // root hub status change
    m_portBase->HcInterruptEnable.MIE = 1; // master interrupt enable

    DEBUGMSG( ZONE_INIT, (TEXT("-CHW::Initialize, success!\n")));
    return TRUE;
}

// ******************************************************************
void CHW::DeInitialize( void )
//
// Purpose: Delete any resources associated with static members
//
// Parameters: none
//
// Returns: nothing
//
// Notes: This function is only called from the ~CUhcd() routine.
//
//        This function is static
// ******************************************************************
{
    m_fUsbInterruptThreadClosing = TRUE; // tell USBInterruptThread that we are closing
    // tell adjustment thread (if it exists) to close
    InterlockedExchange( &m_fStopAdjustingFrameLength, TRUE );

    if ( m_fFrameLengthIsBeingAdjusted ) {
        Sleep( 20 ); // give adjustment thread time to close
        DEBUGCHK( !m_fFrameLengthIsBeingAdjusted );
    }
    // m_hAdjustDoneCallbackEvent <- don't need to do anything to this
    // m_uNewFrameLength <- don't need to do anything to this

    // Wake up the interrupt thread and give it time to die.
    if ( m_hUsbInterruptEvent ) {
        SetEvent(m_hUsbInterruptEvent);
        if ( m_hUsbInterruptThread ) {
            DWORD dwWaitReturn = WaitForSingleObject(m_hUsbInterruptThread, 1000);
            if ( dwWaitReturn != WAIT_OBJECT_0 ) {
                DEBUGCHK( 0 );
                TerminateThread(m_hUsbInterruptThread, DWORD(-1));
            }
            CloseHandle(m_hUsbInterruptThread);
            m_hUsbInterruptThread = NULL;
        }
        // we have to close our interrupt before closing the event!
        InterruptDisable( m_dwSysIntr );

        CloseHandle(m_hUsbInterruptEvent);
        m_hUsbInterruptEvent = NULL;
    } else {
        InterruptDisable( m_dwSysIntr );
    }

    DeleteCriticalSection( &m_csFrameCounter );

    m_fUsbInterruptThreadClosing = FALSE;
    m_wFrameHigh = 0;
}

// ******************************************************************
void CHW::EnterOperationalState( void )
//
// Purpose: Signal the host controller to start processing the schedule
//
// Parameters: None
//
// Returns: Nothing.
//
// Notes: This function is only called from the CUhcd::Initialize routine.
//        It assumes that CPipe::Initialize and CHW::Initialize
//        have already been called.
//
//        This function is static
// ******************************************************************
{
    DEBUGMSG( ZONE_INIT, (TEXT("+CHW::EnterOperationalState\n")));

	// allow the OHCI to run freely now
	m_portBase->HcControl.HCFS = HcRegisters::HcControl::HCFS_OPERATIONAL;

    DEBUGMSG( ZONE_INIT, (TEXT("-CHW::EnterOperationalState\n")));
}

// ******************************************************************
void CHW::StopHostController( void )
//
// Purpose: Signal the host controller to stop processing the schedule
//
// Parameters: None
//
// Returns: Nothing.
//
// Notes: This function can be called from the power handler callbacks and must
//        therefore abide by the restrictions. No system calls, no blocking.
//        Hence no DEBUGMSG's either.
//        This function is static
// ******************************************************************
{
    //DEBUGMSG( ZONE_INIT, (TEXT("+CHW::StopHostController\n")));
    if ( m_portBase != 0 ) {
        m_portBase->HcInterruptDisable.MIE = 1; // disable all interrupts
        m_portBase->HcControl.HCFS = HcRegisters::HcControl::HCFS_RESET;
    }
    //DEBUGMSG( ZONE_INIT, (TEXT("-CHW::StopHostController\n")));
}

// ******************************************************************
void CHW::ListControl( IN const DWORD bfList, IN const BOOL fEnable, IN const BOOL fFill )
//
// Purpose: Instruct the host controller to modify its list management policies.
//
// Parameters: a bitfield indicating which lists to affect;
//             whether to enable or disable processing of the affected lists;    
//         and whether to signal the HC that new transfers are on the affected lists.
//
// Returns: Nothing.
//
// Notes: It's kind of pointless to set bFill when a list is disabled.
//        Also, fFill is ignored for periodic (intr, isoch) lists.
//        Enabling a list may also reset its currentED pointer
//        because CPipe only knows about the HeadED pointers;
//        it does so only when the list starts out disabled in order
//        to avoid tickling a bug whereby the HC becomes confused about
//        what the current ED is during TD retirement.
//
//        This function is static
// ******************************************************************
{
    DEBUGMSG( ZONE_INIT && ZONE_VERBOSE, (TEXT("+CHW::ListControl %x ena:%d fill:%d\n"), bfList, fEnable, fFill));

    if (bfList & LIST_CONTROL) {
        if (fEnable && !m_portBase->HcControl.CLE)
            m_portBase->HcControlCurrentED = 0;

⌨️ 快捷键说明

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