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

📄 tmhal.c

📁 wince host 和 target PCI驱动程序
💻 C
📖 第 1 页 / 共 3 页
字号:
	}

	return statusSuccess;
}

BOOLEAN	halHardwareInterruptHandler (
    PKINTERRUPT Interrupt,
    PVOID ServiceContext  )
{
	// we should be getting the board handle from the IRQHandle
	HalObject*	Hal = (HalObject*)ServiceContext;
	UInt32	InterruptControl;


	InterruptControl = *(Hal->MMIOAddrKernel + INT_CTL);

	// here SelfInterrupt indicates PCI Interrupt A
	// INT#A = 0, INT#B = 1, INT#C = 2, INT#D = 3

	if ( ( InterruptControl &
		( (1 << Hal->SelfInterrupt)  | (0x10 << Hal->SelfInterrupt) ) ) !=
		(ULONG)( (1 << Hal->SelfInterrupt)  | (0x10 << Hal->SelfInterrupt) ) )
	{
		return FALSE;
	}

	/*
	DPF(0,("tmman:halHardwareInterruptHandler:Interrupt[%x]:ServiceContext[%x]\n",
		Interrupt, ServiceContext ));	
	*/

	// check if some device driver has installed a ISR hook function */
	if ( Hal->ISRHookFunction )
	{
		/* call the function */
		if ( Hal->ISRHookFunction ( Hal->ISRHookContext ) == FALSE )
		{
			/* Queue the DPc only if the Hook did not handle the ISR*/
			IoRequestDpc ( Hal->DeviceObject, Hal->DeviceObject->CurrentIrp, Hal );
		}
	}
	else
	{
		IoRequestDpc ( Hal->DeviceObject, Hal->DeviceObject->CurrentIrp, Hal );
	}

	halAcknowledgeInterrupt ( (UInt32)Hal ); 

    return TRUE;
}

VOID	halDeferredInterruptHandler (
	PKDPC Dpc,
	PDEVICE_OBJECT DeviceObject,
	PIRP Irp,
	PVOID Context )
{
	HalObject*	Hal = (HalObject*)Context;
/*	DPF(0,("HL{")); */
	/*
	DPF(0,("tmman:halDeferredInterruptHandler:DPC[%x]:DeviceObject[%x]:Irp[%x]:Context[%x]\n",
		Dpc, DeviceObject, Irp, Context ));	
	*/

	// check if some device driver has installed a ISR hook function */
	if ( Hal->DPCHookFunction )
	{
		/* call the function */
		if ( Hal->DPCHookFunction ( Hal->DPCHookContext ) == FALSE )
		{
			/* Queue the DPc only if the Hook did not handle the ISR*/
			Hal->Handler(Hal->Context);
		}
	}
	else
	{
		Hal->Handler(Hal->Context);
	}

/*	DPF(0,("}HL" )); */
}


UInt32	halAccess32( 
	UInt32	HalHandle,
	UInt32 volatile Value )
{
	UInt32	SwappedValue,TempValue;
	TempValue = Value;

	/* we don't validate the object for efficiency reasons */
	if ( ((HalObject*)HalHandle)->Swapping )
	{
		((UInt8*)&SwappedValue)[3] = ((UInt8*)&TempValue)[0];
		((UInt8*)&SwappedValue)[2] = ((UInt8*)&TempValue)[1];
		((UInt8*)&SwappedValue)[1] = ((UInt8*)&TempValue)[2];
		((UInt8*)&SwappedValue)[0] = ((UInt8*)&TempValue)[3];
/*		DPF(0,("[%x->%x]",TempValue, SwappedValue )); */
		return ( SwappedValue );
	}
	else
	{
		return Value;
	}
	
}

UInt16	halAccess16 ( 
	UInt32	HalHandle,
	UInt16 volatile Value )
{
	/* we don't validate the object for efficiency reasons */
	UInt16	SwappedValue,TempValue;
	TempValue = Value;

	if ( ((HalObject*)HalHandle)->Swapping )
	{
		((UInt8*)&SwappedValue)[1] = ((UInt8*)&TempValue)[0];
		((UInt8*)&SwappedValue)[0] = ((UInt8*)&TempValue)[1];
		/* DPF(0,("[%x->%x]",TempValue, SwappedValue )); */
		return ( SwappedValue );
	}
	else
	{
		return Value;
	}
	return Value;
}

void	halCopyback( Pointer CacheBlock, UInt32 BlockCount  )
{
	/*	no implementation for this platform */
}

void	halAccessEnable ( 
	UInt32	HalHandle )
{
	/*	no implementation for this platform */
}

void	halAccessDisable ( 
	UInt32	HalHandle )
{
	/*	no implementation for this platform */
}


void	halDumpObject (
 	UInt32	HalHandle )
{
	HalObject*	Hal = (HalObject*)HalHandle;

    DPF(1,("\ntmman:halDumpObject\n"));
	
    DPF(1,("[SelfInterrupt:%x]\n", Hal->SelfInterrupt));
	DPF(1,("[PeerInterrupt:%x]\n", Hal->PeerInterrupt));
	DPF(1,("[Handler:%x]\n", Hal->Handler));
	DPF(1,("[Context:%x]\n", Hal->Context));
	DPF(1,("[BusNumber:%x]\n", Hal->BusNumber));
	DPF(1,("[SlotNumber|DeviceNumber:%x|FunctionNumber:%x]\n",
		Hal->SlotNumber.u.bits.DeviceNumber, Hal->SlotNumber.u.bits.FunctionNumber));

	DPF(1,("[MMIOAddrPhysical|HighPart:%x|LowPart:%x]\n", 
		Hal->MMIOAddrPhysical.u.HighPart, Hal->MMIOAddrPhysical.u.LowPart));
	DPF(1,("[MMIOLength:%x]\n", Hal->MMIOLength));
	DPF(1,("[MMIOAddrKernel:%x]\n", Hal->MMIOAddrKernel));


	DPF(1,("[SDRAMAddrPhysical|HighPart:%x|LowPart:%x]\n", 
		Hal->SDRAMAddrPhysical.u.HighPart, Hal->SDRAMAddrPhysical.u.LowPart));
	DPF(1,("[SDRAMLength:%x]\n", Hal->SDRAMLength));
	DPF(1,("[SDRAMAddrKernel:%x]\n", Hal->SDRAMAddrKernel));

	DPF(1,("[MappedInterruptVector:%x]\n", Hal->MappedInterruptVector));
	DPF(1,("[Irql:%x]\n", Hal->Irql));
	DPF(1,("[ProcessorEnableMask:%x]\n", Hal->ProcessorEnableMask));
	DPF(1,("[InterruptObject:%x]\n", Hal->InterruptObject));
	DPF(1,("[DeviceObject:%x]\n", Hal->DeviceObject));
	DPF(1,("[DriverObject:%x]\n", Hal->DriverObject));
	DPF(1,("[TMManDeviceObject:%x]\n", Hal->TMManDeviceObject));
	DPF(1,("\n"));

}    



///////////////////////////////////////////////////////////////////////////
/////				These Headers should be moved to platform.h 
///////////////////////////////////////////////////////////////////////////
/* Helper functions */
PVOID	halMapKernelAddressToUserAddress ( 
	HANDLE PhysicalMemoryHandle, 
	PVOID KernelModeVirtualAddress, 
	ULONG Length )
{
    NTSTATUS NTStatus;
    PHYSICAL_ADDRESS PhysicalAddress, PhysicalAddressBase;
    PVOID UserModeVirtualAddress = NULL;

    // need the physical address
    PhysicalAddressBase = 
		PhysicalAddress = 
		MmGetPhysicalAddress (KernelModeVirtualAddress);

	// request an additional page - avaoid problems with page boundaries
	Length += PAGE_SIZE;

     
    // get a user address for the physical address
    NTStatus = ZwMapViewOfSection (
        PhysicalMemoryHandle,      // IN HANDLE SectionHandle
        (HANDLE) -1,   // IN HANDLE ProcessHandle
        &UserModeVirtualAddress, // IN OUT PVOID *BaseAddress
        0L,            // IN ULONG ZeroBits
        Length,        // IN ULONG CommitSize
        &PhysicalAddressBase, // IN OUT PLARGE_INTEGER SectionOffset (optional)
        &Length,       // IN OUT PULONG ViewSize
        ViewShare,     // IN SECTION_INHERIT InheritDisposition
        0,             // IN ULONG AllocationType
        PAGE_READWRITE | PAGE_NOCACHE // IN ULONG Protect
    );
    
	if (!NT_SUCCESS (NTStatus)) 
	{
        DPF(0,("tmman:halMapKernelAddressToUserAddress:ZwMapViewOfSection:FAIL[%x]", NTStatus));
		return NULL;
    }

    (ULONG)UserModeVirtualAddress += 
		(ULONG)PhysicalAddress.LowPart - (ULONG)PhysicalAddressBase.LowPart;

	return UserModeVirtualAddress;
}

VOID	halUnmapUserAddress ( PVOID UserModeVirtualAddress )
{
	ZwUnmapViewOfSection( (HANDLE)-1, UserModeVirtualAddress );
}

// these functions go into hal.c
BOOLEAN	halAllocateBusMasterChannel ( 
	UInt32 HalHandle,
	Pointer Context,
	PDRIVER_CONTROL ExecutionRoutine )
{	
	HalObject*	Hal = (HalObject*)HalHandle;
	NTSTATUS NTStatus;
    KIRQL	OldIrql;

    KeRaiseIrql( DISPATCH_LEVEL, &OldIrql );

	NTStatus = IoAllocateAdapterChannel (
		Hal->AdapterObject,
		Hal->DeviceObject,
		Hal->NumberOfMapRegisters,
		ExecutionRoutine,
		Context );

    if ( ! NT_SUCCESS(NTStatus) )
    {
        DPF(0,("tmman:halAllocateBusMasterChannel:IoAllocateAdapterChannel:FAIL[%x]\n", NTStatus ));
		KeLowerIrql( OldIrql );

		return False;
			
	}


    KeLowerIrql( OldIrql );

	return True;
}

BOOLEAN	halFreeBusMasterChannel ( 
	UInt32 HalHandle, 
	Pointer MapRegisterBase, 
	UInt32 MapRegisterCount )
{
	HalObject*	Hal = (HalObject*)HalHandle;
	KIRQL		Irql;
	
	KeRaiseIrql ( DISPATCH_LEVEL, &Irql );

	IoFreeMapRegisters ( Hal->AdapterObject,
		MapRegisterBase,
		MapRegisterCount );

	KeLowerIrql ( Irql );

	return TRUE;
}

Pointer	halGetBusMasterChannel ( 
	UInt32 HalHandle )
{
	HalObject*	Hal = (HalObject*)HalHandle;
	return Hal->AdapterObject;
}

TMStatus	halSwapEndianess ( 
	UInt32	HalHandle,
	Bool	SwapEnable )
{
	HalObject*	Hal = (HalObject*)HalHandle;
	Hal->Swapping = SwapEnable;
	DPF(0,("tmman:halSwapEndianess:Swapping[%x]\n", SwapEnable )); 

	return statusSuccess;
}

TMStatus	halGetEndianess ( 
	UInt32	HalHandle,
	Bool*	SwapEnablePtr )
{
	HalObject*	Hal = (HalObject*)HalHandle;
	*SwapEnablePtr = Hal->Swapping;

	return statusSuccess;
}

PVOID	halMapAdapterMemory ( 
	ULONG				BusNumber,
	PHYSICAL_ADDRESS	PhysicalAddress,
	ULONG				Length )
{
	PHYSICAL_ADDRESS	TranslatedAddress;
	PVOID				MappedAddress;
	ULONG				AddressSpace;

	// Map MMIO address space
    AddressSpace = 0x0; // request memory (0x1 for io space)

	if ( ! HalTranslateBusAddress (
        PCIBus,
        BusNumber,         
        PhysicalAddress,	
        &AddressSpace,			
        &TranslatedAddress ) )
	{
        DPF(0,("tmman:halMapAdapterMemory:HalTranslateBusAddress:FAIL\n" ));
		return NULL;
	}

	if ( ( MappedAddress = MmMapIoSpace(
		TranslatedAddress, 
		Length,
		FALSE ) ) == NULL )
	{
		DPF(0,("tmman:halMapAdapterMemory:MmMapIoSpace:FAIL\n" ));
		return NULL;
	}

	return MappedAddress;
}


VOID halUnmapAdapterMemory (
	PVOID	MappedAddress,
	ULONG	Length )
{
	MmUnmapIoSpace ( MappedAddress, Length );


}


BOOLEAN	halMapSDRAM ( UInt32 HalHandle )
{
	HalObject*	Hal = (HalObject*)HalHandle;

	if ( TMManGlobal->MapSDRAM )
	{
		return TRUE;
	}


	if ( InterlockedIncrement ( &Hal->SDRAMMapCount ) != 1 )
	{
		return TRUE;
	}

	if ( ( Hal->SDRAMAddrKernel = halMapAdapterMemory ( 
		Hal->BusNumber,
		Hal->SDRAMAddrPhysical,
		Hal->SDRAMLength ) ) == NULL )
	{
		DPF(0,("tmman:halMapSDRAM:halMapAdapterMemory:SDRAM:FAIL\n" ));
		InterlockedDecrement ( &Hal->SDRAMMapCount );
		return  FALSE;
	}

	return TRUE;

}

BOOLEAN	halUnmapSDRAM ( UInt32 HalHandle )
{
	HalObject*	Hal = (HalObject*)HalHandle;

	if ( TMManGlobal->MapSDRAM )
	{
		return TRUE;
	}

	if ( Hal->SDRAMMapCount == 0 )
	{
		return FALSE;
	}

	if ( InterlockedDecrement  ( &Hal->SDRAMMapCount ) != 0 )
	{
		return TRUE;
	}

	halUnmapAdapterMemory ( Hal->SDRAMAddrKernel, Hal->SDRAMLength );

 	return TRUE;
}

UInt32	halTranslateTargetPhysicalAddress ( 
	UInt32 HalHandle, 
	UInt32 PhysicalAddress )
{
	HalObject*	Hal = (HalObject*)HalHandle;

	return ( PhysicalAddress  + Hal->Offset );
}

Bool	halHookISR ( 
	UInt32 HalHandle, 
	Pointer Function, 
	Pointer Context, 
	UInt32 Flags )
{
	HalObject*	Hal = (HalObject*)HalHandle;

	if ( Hal->ISRHookFunction )
		return FALSE;

	Hal->ISRHookFunction = Function;
	Hal->ISRHookContext = Context;
	Hal->ISRHookFlags = Flags;
	
	return TRUE;
}

Bool	halUnhookISR ( 
	UInt32 HalHandle )
{
	HalObject*	Hal = (HalObject*)HalHandle;

	if ( ! Hal->ISRHookFunction )
		return FALSE;

	Hal->ISRHookFunction = Null;
	Hal->ISRHookContext = Null;
	Hal->ISRHookFlags = 0;
	
	return TRUE;
}

Bool	halHookDPC ( 
	UInt32 HalHandle, 
	Pointer Function, 
	Pointer Context, 
	UInt32 Flags )
{
	HalObject*	Hal = (HalObject*)HalHandle;

	if ( Hal->DPCHookFunction )
		return FALSE;

	Hal->DPCHookFunction = Function;
	Hal->DPCHookContext = Context;
	Hal->DPCHookFlags = Flags;
	
	return TRUE;
}

Bool	halUnhookDPC ( 
	UInt32 HalHandle )
{
	HalObject*	Hal = (HalObject*)HalHandle;

	if ( ! Hal->DPCHookFunction )
		return FALSE;

	Hal->DPCHookFunction = Null;
	Hal->DPCHookContext = Null;
	Hal->DPCHookFlags = 0;
	
	return TRUE;
}

⌨️ 快捷键说明

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