?? chw.cpp
字號:
//
// 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"
#include "cuhcd.hpp"
#include <nkintr.h>
//#pragma warning (4 : 4100) // temporary, will clean later. JEFFRO.
CHW::CHW( IN const REGISTER portBase,
IN const DWORD dwSysIntr,
IN CPhysMem *pCPhysMem,
IN LPVOID pvUhcdPddObject)
:m_portBase((HcRegisters *)portBase)
,m_pMem(pCPhysMem)
,m_pPddContext(pvUhcdPddObject)
,m_dwSysIntr(dwSysIntr)
{
// definitions for static variables
g_fPowerUpFlag = FALSE;
g_fPowerResuming = FALSE;
m_pHCCA = 0;
m_wFrameHigh = 0;
m_hUsbInterruptEvent = NULL;
m_hUsbInterruptThread = NULL;
m_fUsbInterruptThreadClosing = FALSE;
m_fFrameLengthIsBeingAdjusted = FALSE;
m_fStopAdjustingFrameLength = FALSE;
m_hAdjustDoneCallbackEvent = NULL;
m_uNewFrameLength = 0;
m_pControlHead = 0;
m_pBulkHead = 0;
m_pInterruptTable = 0;
InitializeCriticalSection( &m_csFrameCounter );
m_dwCapability = 0;
m_bDoResume=FALSE;
}
CHW::~CHW()
{
DeInitialize();
DeleteCriticalSection( &m_csFrameCounter );
}
// ******************************************************************
BOOL CHW::Initialize(void)
//
// 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"), m_portBase, m_dwSysIntr));
#ifdef DEBUG
dwTickCountLastTime = GetTickCount();
#endif
WORD lastFn = 0;
DEBUGCHK( m_wFrameHigh == 0 );
if ( m_portBase == 0 ) {
DEBUGMSG( ZONE_ERROR, (TEXT("-CHW::Initialize - zero Register Base\n")));
return FALSE;
}
// set up the Host Controller Communications Area
if (m_pMem->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 = m_pMem->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.
m_portBase->HcRhDescriptorA.NPS = 1;
m_portBase->HcRhStatus.reg= HcRegisters::HcRhStatus::LPSC;
// 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, this, 0, NULL );
if ( m_hUsbInterruptThread == NULL ) {
DEBUGMSG(ZONE_ERROR, (TEXT("-CHW::Initialize. Error creating IST\n")));
return FALSE;
}
CeSetThreadPriority( m_hUsbInterruptThread, g_IstThreadPriority );
// Enable interrupts
m_portBase->HcInterruptEnable.WDH = 1; // writeback done head was written
m_portBase->HcInterruptEnable.UE = 1; // unrecoverable error
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
// Enable Periodic Endpt scan before enable controller. Some HCD require this.
ListControl(LIST_INTERRUPT, TRUE, TRUE);
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 );
}
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")));
DEBUGMSG(ZONE_INIT && ZONE_REGISTERS && ZONE_VERBOSE, (TEXT("CHW::EnterOperationalState - setting USBCMD run bit\n")));
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;
m_portBase->HcControl.CLE=fEnable;
if (fFill) m_portBase->HcCommandStatus.CLF=1;
}
if (bfList & LIST_BULK) {
if (fEnable && !m_portBase->HcControl.BLE)
m_portBase->HcBulkCurrentED = 0;
m_portBase->HcControl.BLE=fEnable;
if (fFill) m_portBase->HcCommandStatus.BLF=1;
}
if (bfList & LIST_INTERRUPT) {
m_portBase->HcControl.PLE=fEnable;
}
if (bfList & LIST_ISOCH) {
if (fEnable)
m_portBase->HcControl.PLE=fEnable;
m_portBase->HcControl.IE=fEnable;
}
}
DWORD CALLBACK CHW::CeResumeThreadStub( IN PVOID context )
{
return ((CHW *)context)->CeResumeThread();
}
// ******************************************************************
DWORD CHW::CeResumeThread ()
//
// Purpose: Force the HCD to reset and regenerate itself after power loss.
//
// Parameters: None
//
// Returns: Nothing.
//
// Notes: Because the PDD is probably maintaining pointers to the Hcd and Memory
// objects, we cannot free/delete them and then reallocate. Instead, we destruct
// them explicitly and use the placement form of the new operator to reconstruct
// them in situ. The two flags synchronize access to the objects so that they
// cannot be accessed before being reconstructed while also guaranteeing that
// we don't miss power-on events that occur during the reconstruction.
//
// This function is static
// ******************************************************************
{
// reconstruct the objects at the same addresses where they were before;
// this allows us not to have to alert the PDD that the addresses have changed.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -