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

📄 tmman32.c

📁 wince host 和 target PCI驱动程序
💻 C
📖 第 1 页 / 共 4 页
字号:
/*
	tmman32.dll
	
	provides services to Win32 clients for communicating with the DSP.

	HISTORY
	960417	Tilakraj Roy	Created
	960620	Tilakraj Roy	Started changes for release candicate 3.
	961015	Tilakraj Roy	Adding ArgC & ArgV support to tmDSpLoadExecutable
	970229	Tilakraj Roy	Removed thread and event semantics for NT. Made it KISS
	970812	Tilakraj Roy	Ported to generic TmMan interface V5.0
	000404  Added "MessageCriticalSection" for NT multiprocessor environment 
*/
#include "windows.h"
#include "winioctl.h"

#include "tmmanapi.h"
#include "tmif.h"
#include "stdio.h"
#include "verinfo.h"
#include "stdarg.h"
#include "tchar.h"

#include "TMDownLoader.h"
#include "TMObj.h"

#define	WINDOWS_OS_WIN95		1
#define	WINDOWS_OS_WIN98		2
#define	WINDOWS_OS_WINNT40		3
#define	WINDOWS_OS_WINNT50		4
#define	WINDOWS_OS_WINCE2X		5

typedef	struct	_TMMAN_GLOBAL
{
	HANDLE				hDevice;
	DWORD				dwClientHandle;
	DWORD				MapSDRAM;
	DWORD				DisableDialogBox;
	CRITICAL_SECTION	SDRAMCriticalSection;
	tmmanMemoryBlock	MMIO[constTMMANMaximumDeviceCount];
	tmmanMemoryBlock	SDRAM[constTMMANMaximumDeviceCount];
	tmmanMemoryBlock	Memory[constTMMANMaximumDeviceCount];
	DWORD				MemoryMapped[constTMMANMaximumDeviceCount];
	DWORD				SDRAMMapped[constTMMANMaximumDeviceCount];
	DWORD				MMIOMapped[constTMMANMaximumDeviceCount];
	CRITICAL_SECTION	MessageCriticalSection;
}	TMMAN_GLOBAL, *PTMMAN_GLOBAL;

TMMAN_GLOBAL	Global;

TCHAR			szTemp[2048];

ULONG			WindowsVersion;


// Global Shared Section mapped to all process address spaces
#pragma data_seg (".TMSHARED")
HANDLE   m_hRegTmm                          = NULL;
DWORD    dwInstanceCtr                      = 0;
#pragma data_seg ( )



//-----------------------------------------------------------------------------
//	PROTOTYPES
//-----------------------------------------------------------------------------

BOOL WINAPI tmmanControlHandler ( DWORD dwCtrlType );
void	ErrorBox ( TCHAR* ErrorString );
void	WarningBox ( TCHAR* ErrorString );

#define tmmanDownloaderStatusToTMManStatus(x) \
	(((x)!=TMDwnLdr_OK)?(x+0x70):statusSuccess)

Int8*	errorGetErrorString (
	UInt32 ErrorCode );

TCHAR* CreateUnicodeStr(
	char* szString);

void DeleteUnicodeStr( 
	TCHAR* wsString);

#ifdef	TMMAN_WINCE

typedef BOOL
(WINAPI *PHANDLER_ROUTINE) (
    DWORD CtrlType );

BOOL SetConsoleCtrlHandler ( 
	PHANDLER_ROUTINE HandlerRoutine, 
	BOOL Add );

#else

HANDLE RegisterDevice( 
	TCHAR* lpszType,
	DWORD dwIndex,
	TCHAR* lpszLib, 
	DWORD dwInfo );

BOOL DeregisterDevice( HANDLE hDevice );

#endif

//-----------------------------------------------------------------------------
//	TMMAN SUPPORTING FUNCTIONS
//-----------------------------------------------------------------------------

BOOL	WINAPI	DllMain ( 
	PVOID DllHandle,	
	DWORD dwReason, 
	PVOID pvContext )
{
	DWORD	BytesReturned;
	tmifNegotiateVersion	Version;
	OSVERSIONINFO		OSVersion;

	DWORD		DeviceIdx;
	HKEY		RegistryHandle;
	HANDLE		FileHandle;
	TCHAR		FilePath[MAX_PATH];

	switch ( dwReason )
	{
		case DLL_PROCESS_ATTACH :

		
		InitializeCriticalSection ( &Global.MessageCriticalSection );
		


		wsprintf ( szTemp, TEXT("\ntmman32:Version[%d.%d.%d]\n"),
			verGetFileMajorVersion(),
			verGetFileMinorVersion(),
			verGetFileBuildVersion() );
		OutputDebugString ( szTemp );

		// initialize the values for PROCES_DETACH
		Global.hDevice = INVALID_HANDLE_VALUE;
		Global.dwClientHandle = 0;

		OSVersion.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
		GetVersionEx( &OSVersion );

		wsprintf ( szTemp,
			TEXT("tmman32:DLLInitialize:GetVersionEx:Maj[%x]:Min[%x]:Bld[%x]:PID[%x]\n"),
			OSVersion.dwMajorVersion,
			OSVersion.dwMinorVersion,
			OSVersion.dwBuildNumber,
			OSVersion.dwPlatformId );

		OutputDebugString ( szTemp );


		if ( ERROR_SUCCESS == RegOpenKeyEx(  
			HKEY_LOCAL_MACHINE,
			constTMManRegistryPath,
			0,
			KEY_READ,
			&RegistryHandle ) )
		{
			ULONG	BytesXfered;
			
			BytesXfered = sizeof ( ULONG );

			if ( ERROR_SUCCESS != RegQueryValueEx(
				RegistryHandle,
				TEXT("DisableDialogBox"),
				NULL,
				NULL,
				(BYTE*)&Global.DisableDialogBox,
				&BytesXfered ) )
			{
				Global.DisableDialogBox = 0;
			}

			BytesXfered = sizeof ( ULONG );

			if ( ERROR_SUCCESS != RegQueryValueEx(
				RegistryHandle,
				TEXT("MapSDRAM"),
				NULL,
				NULL,
				(BYTE*)&Global.MapSDRAM,
				&BytesXfered ) )
			{
				Global.MapSDRAM = 1;
			}

			RegCloseKey ( RegistryHandle );
		}



		// check for NT4.0 & 98(WDM) & NT5.0
		switch ( OSVersion.dwPlatformId )
		{
			case	VER_PLATFORM_WIN32_NT :
			WindowsVersion = WINDOWS_OS_WINNT40;
			break;

			case	VER_PLATFORM_WIN32_WINDOWS :
			if ( OSVersion.dwMinorVersion > 0 ) 
			{
				WindowsVersion = WINDOWS_OS_WIN98;
			}
			else
			{
				WindowsVersion = WINDOWS_OS_WIN95;
			}
			break;

			default :
			WindowsVersion = WINDOWS_OS_WIN95;
			break;
	
		}

// this has to be removed depending on what GetVersionEx returns for CE.
#ifdef	TMMAN_WINCE
		WindowsVersion  = WINDOWS_OS_WINCE2X;
#endif

		switch ( WindowsVersion )
		{
			case WINDOWS_OS_WINNT40 :
			case WINDOWS_OS_WINNT50 :
			if ( ( Global.hDevice = CreateFile ( 
				TEXT("\\\\.\\TMMAN"),
				GENERIC_READ|GENERIC_WRITE,
				0,
				0, 
				OPEN_EXISTING, 
				FILE_ATTRIBUTE_NORMAL, 
				0 ) ) == INVALID_HANDLE_VALUE )
			{
				wsprintf ( szTemp,
					TEXT("tmman32:DLLInitialize:CreateFile:tmman:FAIL[%x]\n"),
					GetLastError() );
				OutputDebugString ( szTemp );
				ErrorBox  ( TEXT("TriMedia Device Driver (tmman.sys) not INSTALLED\nRefer to Troubleshooting Guide to install Device Driver(tmman.sys)" ));
				return FALSE;
			}
			break;

			case	WINDOWS_OS_WINCE2X :
			if ( m_hRegTmm == NULL )
			{
				if ((m_hRegTmm  = RegisterDevice( TEXT("TMM"), 1, TEXT("tmman.dll"), 0)) == 0)
				{
					OutputDebugString(TEXT("\ntmman32: error registering TMM driver (tmman32.dll) \n"));
					ErrorBox  ( TEXT("TriMedia Device Driver (tmman.dll) registration : FAILED\n") );
					return FALSE;        
				}
			}

			if ( ( Global.hDevice = CreateFile ( 
				TEXT("TMM1:"),
				GENERIC_READ|GENERIC_WRITE,
				0,
				0, 
				OPEN_EXISTING, 
				FILE_ATTRIBUTE_NORMAL, 
				0 ) ) == INVALID_HANDLE_VALUE )
			{
				wsprintf ( szTemp,
					TEXT("tmman32:DLLInitialize:CreateFile:tmman:FAIL[%x]\n"),
					GetLastError() );
				OutputDebugString ( szTemp );
				ErrorBox  ( TEXT("TriMedia Device Driver (tmman.dll) not INSTALLED\n" ));
				return FALSE;
			}
		    dwInstanceCtr++;
			break;

			case WINDOWS_OS_WIN95 :
			case WINDOWS_OS_WIN98 :

			// check for vtmman.vxd
			if ( ( Global.hDevice = 
				CreateFile ( "\\\\.\\vtmman.vxd",0,0,0, CREATE_NEW , 
				FILE_FLAG_DELETE_ON_CLOSE | FILE_FLAG_OVERLAPPED, 
				0 ) ) != INVALID_HANDLE_VALUE )
			{
				// vtmman.vxd found ( either installed or in system directory )
				wsprintf ( szTemp,
					TEXT("tmman32:DLLInitialize:CreateFile:vtmman.vxd:FOUND\n") );
				OutputDebugString ( szTemp );
				ErrorBox ( TEXT("OBSOLETE Version of Driver (vtmman.vxd) found\nRefer to Troubleshooting Guide to remove TriMedia Device and Device Driver and install new ones(tmman.vxd/tmman.sys)"));
				return FALSE;
			}	

			/*
			// check if the file vtmman.vxd exits in the windows\system directory
			GetSystemDirectory ( FilePath, MAX_PATH );
			strcat ( FilePath, "\\vtmman.vxd" );
			if ( INVALID_HANDLE_VALUE != ( FileHandle = CreateFile(  
				FilePath,				// pointer to name of the file
				GENERIC_READ,				// access (read-write) mode
				FILE_SHARE_READ,			// share mode
				NULL,						// pointer to security attributes
				OPEN_EXISTING,				// how to create
				FILE_ATTRIBUTE_NORMAL,		// file attributes
				NULL ) ) )					// handle to file with attributes to 
			{
				ErrorBox ( TEXT("OBSOLETE Version of Driver (vtmman.vxd) FOUND in Windows System Directory\nRefer to Troubleshooting Guide to remove TriMedia Device and Device Driver and install new ones(tmman.vxd/tmman.sys)"));
				CloseHandle ( FileHandle );
				return FALSE;
			}
			*/

			if ( ( Global.hDevice = CreateFile ( 
				TEXT("\\\\.\\tmman.vxd"),
				0,
				0,
				0, 
				CREATE_NEW, 
				FILE_FLAG_DELETE_ON_CLOSE | FILE_FLAG_OVERLAPPED, 
				0 ) ) == INVALID_HANDLE_VALUE )
			{
				// tmman.vxd is not installed
								
				if ( WindowsVersion == WINDOWS_OS_WIN98 )
				{
					if ( ( Global.hDevice = CreateFile ( 
						TEXT("\\\\.\\TMMAN"),
						GENERIC_READ|GENERIC_WRITE,
						0,
						0, 
						OPEN_EXISTING, 
						FILE_ATTRIBUTE_NORMAL, 
						0 ) ) != INVALID_HANDLE_VALUE )
					{
						// tmman.sys is installed
						break;
					}
				}

				wsprintf ( szTemp,
					TEXT("tmman32:DLLInitialize:CreateFile:tmman.sys:FAIL[%x]\n"),
						GetLastError() );
				OutputDebugString ( szTemp );
				ErrorBox  ( TEXT("TriMedia Device Driver NOT INSTALLED\nRefer to Troubleshooting Guide to install Device Driver (tmman.vxd/tmman.sys)"));
				return FALSE;
			}
			else
			{
				WindowsVersion = WINDOWS_OS_WIN95;
			}
			break;

			default :
			break;

		}

		Version.Version.Major = verGetFileMajorVersion();
		Version.Version.Minor = verGetFileMinorVersion();

		// perform the version control 
		if ( DeviceIoControl ( Global.hDevice,
			(DWORD)(constIOCTLtmmanNegotiateVersion),
			(PVOID)&Version, sizeof (tmifNegotiateVersion),
			(PVOID)&Version, sizeof (tmifNegotiateVersion),
			&BytesReturned, NULL ) != TRUE ) 
		{
			/* this will fail if vxd does not support negotiate version */
			wsprintf ( szTemp,
				TEXT("tmman32:DLLInitialize:DeviceIoControl:NEGOTIATEVERSION:FAIL[%x]\n"),
				GetLastError() );
			OutputDebugString ( szTemp );
			
			wsprintf ( szTemp,
				TEXT("TriMedia Device Driver (tmman.vxd/tmman.sys) is INCOMPATIBLE With TMMan32.dll Version[%d.%d]\nRefer to Troubleshooting Guide to fix this problem"),
				verGetFileMajorVersion(), verGetFileMinorVersion() );

			ErrorBox  ( szTemp );
			return FALSE;	
		}

		/*
		if ( Version.Status != statusSuccess )
		{
			wsprintf ( szTemp,
				TEXT("tmman32:DLLInitialize:NEGOTIATEVERSION:FAIL[%x]\n"), Version.Status );
			OutputDebugString ( szTemp );

			ErrorBox  ( TEXT("Obsolete Version of [tmman32.dll]" ));


			return FALSE;
		}
		*/

		if ( ( Version.Version.Major != verGetFileMajorVersion() ) ||
			( Version.Version.Minor != verGetFileMinorVersion()) )
		{
			OutputDebugString(
				TEXT("tmman32:DLLInitialize:VersionCheck:vtmman.vxd:FAIL\n"));

			wsprintf ( szTemp,
				TEXT("TriMedia Device Driver (tmman.vxd/tmman.sys) Version[%d.%d] is INCOMPATIBLE With TMMan32.dll Version[%d.%d]\nRefer to Troubleshooting Guide to fix this problem"),
				Version.Version.Major, Version.Version.Minor,
				verGetFileMajorVersion(), verGetFileMinorVersion() );
			ErrorBox  ( szTemp );

			return FALSE;
		}

#ifndef	TMMAN_WINCE
		GetWindowsDirectory ( FilePath, MAX_PATH );
		strcat ( FilePath, "\\tmman.ini" );

		// check for existence of tmman.ini
		if ( INVALID_HANDLE_VALUE != ( FileHandle = CreateFile(  
			FilePath,				// pointer to name of the file
			GENERIC_READ,				// access (read-write) mode
			FILE_SHARE_READ,			// share mode
			NULL,						// pointer to security attributes
			OPEN_EXISTING,				// how to create
			FILE_ATTRIBUTE_NORMAL,		// file attributes
			NULL ) ) )					// handle to file with attributes to 
		{
			WarningBox  ( 
				TEXT("Settings in TMMan.ini will be IGNORED.\nSettings will be read from the registry key at:\nHKLM\\SOFTWARE\\PhilipsSemiconductors\\TriMedia\\TMMan\nTo get rid of this Dialog Box DELETE tmman.ini from the Windows Directory" ));			

			CloseHandle ( FileHandle );
		}
#endif

		InitializeCriticalSection ( &Global.SDRAMCriticalSection );

		for ( DeviceIdx = 0 ; DeviceIdx < constTMMANMaximumDeviceCount ; DeviceIdx++ )
		{
			tmifDSPInfo	TMIFInfo;
			tmifDSPOpen	TMIFOpen;
			tmifDSPInternalInfo	TMIFInternalInfo;

			TMIFOpen.DSPNumber		= DeviceIdx;

			Global.SDRAMMapped[DeviceIdx] = 0;
			Global.MMIOMapped[DeviceIdx] = 0;
			Global.MemoryMapped[DeviceIdx] = 0;

			if ( DeviceIoControl ( Global.hDevice, 
				constIOCTLtmmanDSPOpen,
				(PVOID)&TMIFOpen, sizeof( tmifDSPOpen),
				(PVOID)&TMIFOpen, sizeof( tmifDSPOpen),
				&BytesReturned, NULL ) != TRUE )
			{
				return FALSE;
			}

			if ( TMIFOpen.Status == statusDSPNumberOutofRange )
			{
				// no more TriMedias - bail out of loop
				// this is not an error condition so we break
				// otherwise the initialization fails.
				break;
			}

			TMIFInfo.DSPHandle = TMIFOpen.DSPHandle;

			if ( DeviceIoControl ( Global.hDevice, 
				constIOCTLtmmanDSPInfo,
				(PVOID)&TMIFInfo, sizeof( tmifDSPInfo) ,
				(PVOID)&TMIFInfo, sizeof( tmifDSPInfo) ,
				&BytesReturned, NULL ) != TRUE )
			{
				return FALSE;
			}

			TMIFInternalInfo.DSPHandle = TMIFOpen.DSPHandle;

			if ( DeviceIoControl ( Global.hDevice, 
				constIOCTLtmmanDSPGetInternalInfo,
				(PVOID)&TMIFInternalInfo, sizeof( tmifDSPInternalInfo) ,
				(PVOID)&TMIFInternalInfo, sizeof( tmifDSPInternalInfo) ,
				&BytesReturned, NULL ) != TRUE )
			{
				return FALSE;
			}

			Global.SDRAM[DeviceIdx] = TMIFInfo.Info.SDRAM;
			Global.MMIO[DeviceIdx] = TMIFInfo.Info.MMIO;
			Global.Memory[DeviceIdx] = TMIFInternalInfo.Info.Memory;


			switch ( WindowsVersion )
			{
				case	WINDOWS_OS_WINCE2X :
#ifdef TMMAN_WINCE
				
				// Map MMIO into process V.A.S
				if ( ( Global.MMIO[DeviceIdx].MappedAddress = (DWORD)VirtualAlloc(
					0,
					TMIFInfo.Info.MMIO.Size,
					MEM_RESERVE,PAGE_NOACCESS) ) == 0 )
				{
					wsprintf ( szTemp,
						TEXT("tmman32:DLLInitialize:VirtualAlloc:MMIO:FAIL[%x]\n"), GetLastError());
					OutputDebugString ( szTemp );
					return FALSE;
				}

				if( ! VirtualCopy (
					(void *)Global.MMIO[DeviceIdx].MappedAddress,
					(void *)(Global.MMIO[DeviceIdx].PhysicalAddress>>8),
					Global.MMIO[DeviceIdx].Size,
					PAGE_READWRITE|PAGE_PHYSICAL|PAGE_NOCACHE ) )
				{
					wsprintf ( szTemp,
						TEXT("tmman32:DLLInitialize:VirtualCopy:MMIO:FAIL[%x]\n"), GetLastError());
					OutputDebugString ( szTemp );

					VirtualFree(
						(void*)Global.MMIO[DeviceIdx].MappedAddress,
						0,
						MEM_RELEASE );
					
					return FALSE;
				}

				Global.MMIOMapped[DeviceIdx] = 1;

				// Map Memory into process V.A.S
				if ( ( Global.Memory[DeviceIdx].MappedAddress = (DWORD)VirtualAlloc(
					0,
					Global.Memory[DeviceIdx].Size,
					MEM_RESERVE,PAGE_NOACCESS) ) == 0 )
				{
					wsprintf ( szTemp,
						TEXT("tmman32:DLLInitialize:VirtualAlloc:Memory:FAIL[%x]\n"), GetLastError() );
					OutputDebugString ( szTemp );

					VirtualFree(
						(void*)Global.MMIO[DeviceIdx].MappedAddress,
						0,
						MEM_RELEASE );

					return FALSE;
				}

				if( ! VirtualCopy (
					(void *)Global.Memory[DeviceIdx].MappedAddress,
					(void *)((Global.Memory[DeviceIdx].PhysicalAddress & 0xfffff000)|0x80000000),
					Global.Memory[DeviceIdx].Size,
					PAGE_READWRITE|PAGE_NOCACHE ) )
				{
					wsprintf ( szTemp,
						TEXT("tmman32:DLLInitialize:VirtualCopy:Memory:FAIL[%x]\n"), GetLastError() );

					OutputDebugString ( szTemp );

					VirtualFree(
						(void*)Global.Memory[DeviceIdx].MappedAddress,
						0,
						MEM_RELEASE );

					VirtualFree(
						(void*)Global.MMIO[DeviceIdx].MappedAddress,
						0,
						MEM_RELEASE );


					return FALSE;
				}

				Global.MemoryMapped[DeviceIdx] = 1;

				if ( Global.MapSDRAM )
				{
					// Map SDRAM into process V.A.S
					if ( ( Global.SDRAM[DeviceIdx].MappedAddress = (DWORD)VirtualAlloc(
						0,
						TMIFInfo.Info.SDRAM.Size,
						MEM_RESERVE,PAGE_NOACCESS) ) == 0 )
					{
						wsprintf ( szTemp,
							TEXT("tmman32:DLLInitialize:VirtualAlloc:SDRAM:FAIL[%x]\n"),GetLastError());
						OutputDebugString ( szTemp );
						return FALSE;
					}

					if( ! VirtualCopy (
						(void *)Global.SDRAM[DeviceIdx].MappedAddress,
						(void *)(Global.SDRAM[DeviceIdx].PhysicalAddress>>8),
						Global.SDRAM[DeviceIdx].Size,
						PAGE_READWRITE|PAGE_PHYSICAL|PAGE_NOCACHE ) )
					{
						wsprintf ( szTemp,
							TEXT("tmman32:DLLInitialize:VirtualCopy:SDRAM:FAIL[%x]\n"),  GetLastError());
						OutputDebugString ( szTemp );

						VirtualFree(
							(void*)Global.SDRAM[DeviceIdx].MappedAddress,
							0,
							MEM_RELEASE );

						VirtualFree(
							(void*)Global.Memory[DeviceIdx].MappedAddress,
							0,
							MEM_RELEASE );

						VirtualFree(
							(void*)Global.MMIO[DeviceIdx].MappedAddress,
							0,
							MEM_RELEASE );

						return FALSE;

					}

					Global.SDRAMMapped[DeviceIdx] = 1;
				}



#endif // TMMAN_WINCE
				break;

				default :
				break;

			}


		}

		if ( SetConsoleCtrlHandler  ( tmmanControlHandler , TRUE ) != TRUE )
		{
			wsprintf ( szTemp,
				TEXT("tmman32:DLLInitialize:SetConsoleCtrlHandler:FAIL[%x]\n"),
				GetLastError() );
			OutputDebugString ( szTemp );
			ErrorBox  ( TEXT("Cannot Install Console ^C Handler" ));
			return FALSE;
		}
		break;

⌨️ 快捷键说明

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