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

📄 tmchnl.c

📁 wince host 和 target PCI驱动程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/*---------------------------------------------------------------------------- 
COPYRIGHT (c) 1995 by Philips Semiconductors

THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY ONLY BE USED AND COPIED IN 
ACCORDANCE WITH THE TERMS AND CONDITIONS OF SUCH A LICENSE AND WITH THE 
INCLUSION OF THE THIS COPY RIGHT NOTICE. THIS SOFTWARE OR ANY OTHER COPIES 
OF THIS SOFTWARE MAY NOT BE PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY OTHER
PERSON. THE OWNERSHIP AND TITLE OF THIS SOFTWARE IS NOT TRANSFERRED. 

THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT ANY PRIOR NOTICE
AND SHOULD NOT BE CONSTRUED AS A COMMITMENT BY Philips Semiconductor. 

PHILIPS ASSUMES NO RESPONSIBILITY FOR THE USE OR RELIABILITY OF THIS SOFTWARE
ON PLATFORMS OTHER THAN THE ONE ON WHICH THIS SOFTWARE IS FURNISHED.
----------------------------------------------------------------------------*/
/*
	HISTORY
	#define	TR	Tilakraj Roy
	960530	TR 	Created
	960905	TR	Pulled in from host channel sources
	960907	TR	Moved packet queuing to higher layer
	961029	TR	Moved in Mailbox packet queuing here
*/

/*----------------------------------------------------------------------------
          SYSTEM INCLUDE FILES
----------------------------------------------------------------------------*/
#define WANTVXDWRAPS
#include <basedef.h>
#include <vmm.h>
#include <vxdwraps.h>

#undef CHNL_DEBUG

/*----------------------------------------------------------------------------
          DRIVER SPECIFIC INCLUDE FILES
----------------------------------------------------------------------------*/
#include "vxstd.h"
#include "vxwin.h"
#include "vxdbg.h"
#include "tmwincom.h"

#include "tmhd.h"
#include "tmshare.h"
#include "tmchnl.h"
#include "cqueue.h"

#pragma VxD_LOCKED_CODE_SEG
#pragma	VxD_LOCKED_DATA_SEG

VOID	chnlmDebugDump ( PVOID pDevice );
STATUS	chnlmCallback ( DWORD InterruptID, DWORD Count,	PVOID pContext );
STATUS	chnlmOnPacketSend ( 
	PVOID pContext );		/* pointer to the channel manager object */
STATUS	chnlmOnAsyncCallback ( PVOID pContext );

static LastPacketReceived = 0;

/* 
	chnlmCreate
	Creates a channel manager object dynamically. 
	Allocates an array of pointers for storing pointers to channel objects 
	that it will manage.
	Opens and registers callbacks with the IPC interface.
*/
STATUS	chnlmCreate ( 
	PVOID	pContainer,		/* pointer to the driver object */
	DWORD	ChannelCount,	/* number of channels that will be supported */
	PVOID	*ppChnlMgr )	/* address of memory where object ptr is stored */
{
	PTMCHNL_MGR_OBJECT	this;
	STATUS	Status;
	DWORD	IdxChnl;

	if ( ( this = (PTMCHNL_MGR_OBJECT)vxdMalloc ( 
		sizeof ( TMCHNL_MGR_OBJECT ) ) ) == NULL )
	{
		Status = TM_STATUS ( TMCHNL_ERR_MGROBJMALLOCFAIL );
		goto	chnlmCreate_fail1;
	}

	if ( ( this->pChnlTab = 
		(PVOID)vxdMalloc ( sizeof ( PVOID ) * ChannelCount ) ) == NULL )
	{
		Status = TM_STATUS ( TMCHNL_ERR_OBJPTRTABMALLOCFAIL );
		goto	chnlmCreate_fail2;
	}

	for ( IdxChnl = 0 ; IdxChnl < ChannelCount ; IdxChnl ++ )
	{
		this->pChnlTab[IdxChnl] = NULL;
	}

	this->Flags = 0;
	this->Size = sizeof (TMCHNL_MGR_OBJECT);
	this->pContainer = pContainer;
	this->ChannelCount = ChannelCount;
	this->AllocatedCount = 0;
	this->IdxSent = 0;
	this->pIPC	= GetIPCObject(this->pContainer);
	
	if ( ( Status = shmemAllocate ( GetShMemObject(this->pContainer), 0 ,
		sizeof ( TMHD_CHNL_SHARED ), &this->pSharedData,
		&this->SharedDataPhys ) ) != TMOK )
		goto	chnlmCreate_fail3;

	boardParameterDWORDSet ( this->pContainer, TMHD_PARAM_CHNL,
		this->SharedDataPhys );

	if ( ( Status = ipcRegisterCallback  (
		GetIPCObject(this->pContainer),	TMHD_IPC_CHNLINTERRUPT, chnlmCallback,
		this ) ) != TMOK )
	{
		goto	chnlmCreate_fail4;
	}
	
	dbgRegister ( chnlmDebugDump, "Channel Manager");
	
	FlagSet ( this->Flags, TMCHNL_MGR_FLAGINITIALIZED);
	*ppChnlMgr =  this;

	/* these funciton goes in chnlmReset */
	this->pSharedData->ToDSP.dwReadIndex = 0;
	this->pSharedData->ToDSP.dwWriteIndex = 0;
	this->pSharedData->ToDSP.IntCmd = 0;


	this->pSharedData->ToHost.dwReadIndex = 0;
	this->pSharedData->ToHost.dwWriteIndex = 0;
	this->pSharedData->ToHost.IntCmd = 0;

	/* statistics initialization */
	this->DPCReqCount = 0;
	this->DPCAckCount = 0;
	this->PktSendCount = 0;
	this->PktRecvCount = 0;
	this->PktSendDropCount = 0;
	this->PktRecvDropCount = 0;
	this->PktRecvInvDirCount = 0;
	this->PktRecvInvChCount = 0;
	this->PktSendSequence = 0;
	this->PktRecvSequence = 0;
	this->DPCEntered = 0;
	this->DPCRunning = 0;
	this->DPCScheduled = 0;

	for ( IdxChnl = 0 ; IdxChnl < ChannelCount ; IdxChnl ++ )
	{
		if ( this->pChnlTab[IdxChnl] != NULL )
		{
			chnlDestroy( this->pChnlTab[IdxChnl] );
		}
	}
	
	return TMOK;
/*
chnlmCreate_fail5:
	ipcUnregisterCallback ( GetIPCObject(this->pContainer),
		TMHD_IPC_CHNLINTERRUPT );
*/
chnlmCreate_fail4:
	shmemFree ( GetShMemObject(this->pContainer), this->pSharedData );
chnlmCreate_fail3:
	vxdFree ( this->pChnlTab );
chnlmCreate_fail2:
	vxdFree ( this );
chnlmCreate_fail1:
	return Status;
}

STATUS	chnlmReset ( PVOID	pChnlMgr )
{
	PTMCHNL_MGR_OBJECT	this = (PTMCHNL_MGR_OBJECT)pChnlMgr;
	
	this->pSharedData->ToDSP.dwReadIndex = 0;
	this->pSharedData->ToDSP.dwWriteIndex = 0;
	this->pSharedData->ToDSP.IntCmd = 0;


	this->pSharedData->ToHost.dwReadIndex = 0;
	this->pSharedData->ToHost.dwWriteIndex = 0;
	this->pSharedData->ToHost.IntCmd = 0;

	/* statistics initialization */
	this->DPCReqCount = 0;
	this->DPCAckCount = 0;
	this->PktSendCount = 0;
	this->PktRecvCount = 0;
	this->PktSendDropCount = 0;
	this->PktRecvDropCount = 0;
	this->PktRecvInvDirCount = 0;
	this->PktRecvInvChCount = 0;
	this->PktSendSequence = 0;
	this->PktRecvSequence = 0;
/*
	for ( IdxChnl = 0 ; IdxChnl < this->ChannelCount ; IdxChnl ++ )
	{
		if ( this->pChnlTab[IdxChnl] != NULL )
		{
			chnlDestroy( this->pChnlTab[IdxChnl] );
		}
	}
*/
	return TMOK;
}

/*
	chnlmCreateChnl
	Allocates the number of packet buffers requried by the caller i.e
	message and streams.
	These buffers are allocated out of page locked memory. 
	The channel manager could be aware of all these buffers or the channel
	manager could be calling functions with each of these channel objects 
	to retrieve packets out of these buffers.
	Channels are inherently unidirectional. This means that both messages
	and streams require two channels for bi-directional communications.
*/
STATUS	chnlmCreateChnl ( 
	PVOID	pChnlMgr, 		/* pointer to channel manager */
	BOOL	Direction, 		/* channel direction */
	DWORD	ChannelID, 		/* channel ID - passed by the host */
	DWORD	QueueSize,		/* queue depth */
	PVOID 	OnCallback, 	/* receive & send callback */
	PVOID	pContext, 		/*	receive and send callback context */
	PVOID	*ppChnl )		/* address of pointer for string chnl object */
{
	PTMCHNL_MGR_OBJECT	this = (PTMCHNL_MGR_OBJECT)pChnlMgr;
	PTMCHNL_OBJECT		pChannel;
	STATUS	Status;

	/* validate parameters here 
		OnRecvPacket
		ChannelID
		Direction
	*/
	/* sanity check - no need to perform SEH */
	if ( OnCallback == NULL )
	{
		return TM_STATUS ( TMCHNL_ERR_INVALIDPARAM ) ;
	}

	if ( this->AllocatedCount >= this->ChannelCount )
	{
		return TM_STATUS(TMCHNL_ERR_OUTOFCHANNELS);
	}

	if ( ChannelID >= this->ChannelCount )
	{
		return TM_STATUS(TMCHNL_ERR_CHNLIDOUTOFLIMIT);
	}

	if ( this->pChnlTab[ChannelID] )
	{
		return TM_STATUS(TMCHNL_ERR_CHNLALREADYALLOCATED);
	}

	if ( ( pChannel = vxdMalloc ( sizeof ( TMCHNL_OBJECT ) * 1 ) ) == NULL )
	{
		return TM_STATUS ( TMCHNL_ERR_OBJALLOCFAIL );
	}

	this->pChnlTab[ChannelID] = (PVOID)pChannel;
	this->AllocatedCount++;

	FlagSet ( pChannel->Flags, TMCHNL_CHNL_FLAGALLOCATED );
	pChannel->Size = sizeof ( TMCHNL_OBJECT );
	pChannel->OnCallback = OnCallback;
	pChannel->pContext = pContext;
	pChannel->Direction = Direction;
	pChannel->pContainer = this->pContainer;
	pChannel->ID = ChannelID;
	pChannel->PacketCounter = 0;
	/*	
	TR960907 : commented out since queuing is the higher layers responsibility
	*/

	pChannel->pQueue = NULL;

	if ( cqueueCreate ( QueueSize, sizeof ( TMSTD_PACKET ), 
		NULL, 
		&pChannel->pQueue ) == FALSE )
	{
		Status = TM_STATUS ( TMCHNL_ERR_QUEUECREATEFAIL );	
		goto chnlmCreateChnl_fail1;
	}

	*ppChnl = (PVOID)pChannel;

	return TMOK;
/*
chnlmCreateChnl_fail2:
	cqueueDestroy ( pChannel->pQueue );
*/
chnlmCreateChnl_fail1:
	FlagClr ( pChannel->Flags, TMCHNL_CHNL_FLAGALLOCATED );
	pChannel->Size = 0;
	vxdFree ( pChannel );
	this->AllocatedCount--;
	this->pChnlTab[ChannelID] = NULL;
	return Status;

}

STATUS	chnlDestroy ( 
	PVOID pChnl )		/* pointer to the channel object */
{
	PTMCHNL_OBJECT	this = (PTMCHNL_OBJECT)pChnl;
	STATUS	Status;

	if ( ( Status = chnlValidateHandle ( pChnl )) != TMOK )
	{
		return Status;
	}

	cqueueDestroy (this->pQueue );

	FlagClr ( this->Flags, TMCHNL_CHNL_FLAGALLOCATED );
	this->Size = 0;
	((PTMCHNL_MGR_OBJECT)GetChnlMgrObject(this->pContainer))->AllocatedCount--;
	((PTMCHNL_MGR_OBJECT)GetChnlMgrObject(this->pContainer))->pChnlTab[this->ID]
		 = NULL;
	vxdFree ( this );

	return TMOK;

}

STATUS	chnlmDestroy (PVOID pChnlMgr)
{
	PTMCHNL_MGR_OBJECT	this = (PTMCHNL_MGR_OBJECT)pChnlMgr;

	ipcUnregisterCallback  (
		GetIPCObject(this->pContainer), TMHD_IPC_CHNLINTERRUPT );
	FlagClr ( this->Flags, TMCHNL_MGR_FLAGINITIALIZED);
	this->Size = 0;
	vxdFree ( this->pChnlTab );
	vxdFree ( this );
	return TMOK;
}



STATUS	chnlPacketSend ( PVOID pChnl, PVOID pPacket ) 
{
	PTMCHNL_OBJECT	this = (PTMCHNL_OBJECT)pChnl;
	PTMCHNL_MGR_OBJECT	pChnlMgr = 
		(PTMCHNL_MGR_OBJECT)GetChnlMgrObject (this->pContainer);
	STATUS	Status;	
	PTMHD_CHNL_MAILQUEUE 	pDSP = &pChnlMgr->pSharedData->ToDSP;
	PTMHD_CHNL_MAILQUEUE 	pHost = &pChnlMgr->pSharedData->ToHost;

	if ( ( Status = chnlValidateHandle ( this ) ) != TMOK )
	{
		return Status;
	}
	pChnlMgr->PktSendCount++;

	DP( 3,"TM:[QSEND|CH:%x|CM:%x|A0:%x|A1:%x|A2:%x|A3:%x|A4:%x]\n",
		this->ID,
		((PTMSTD_PACKET)pPacket)->dwCommand,
		((PTMSTD_PACKET)pPacket)->dwArgument[0],
		((PTMSTD_PACKET)pPacket)->dwArgument[1],
		((PTMSTD_PACKET)pPacket)->dwArgument[2],
		((PTMSTD_PACKET)pPacket)->dwArgument[3],
		((PTMSTD_PACKET)pPacket)->dwArgument[4] );

	if ( cqueueInsert ( this->pQueue, pPacket ) == FALSE )
	{
		pChnlMgr->PktSendDropCount++;
		DP(0, "TM:chnlPacketSend:cqueueInsert:FAIL\n");
		return TM_STATUS ( TMCHNL_ERR_CHANNELQUEUEFULL );
	}

	/*
	dump not only this packet but all the packets in all the queues
	 this is called by IPC but we call this function directly from here
	*/

	chnlmOnPacketSend ( pChnlMgr );

	if ( pDSP->dwReadIndex != pDSP->dwWriteIndex  )
	{
		pChnlMgr->pSharedData->ToDSP.IntCmd = TMHD_CHNL_MBOXREADY;
		ipcGenerateIRQ ( GetIPCObject (this->pContainer),
			TMHD_IPC_CHNLINTERRUPT);
	}

	return TMOK;
}

/*
	chnlmOnPacketSend

	This function may be called by ipc as a callback when it detect available 
	room in tis queue or by the higher layer after it inserts a packet into
	its queue and wants it to be sent to the IPC.
	This function simply calls the upper layers to retrieve a packet that
	has been queued up for sending to the host.
	The higher layer function can do two things -
	.	Either call chnlSend and allow that to send the packet to IPC
	.	Return the pointer to the packet to this function and let it perform
		the send to IPC.
	The second approach will better handle the situation where the IPC 
	queue is full.
*/
STATUS	chnlmOnPacketSend ( 
	PVOID pContext )		/* pointer to the channel manager object */
{
	PTMCHNL_MGR_OBJECT	this = (PTMCHNL_MGR_OBJECT)pContext;
	PTMCHNL_OBJECT		pChannel;
	TMHD_CHNL_MAILSLOT		MailSlot;
	DWORD				Count, dwWriteIndex;
	PTMHD_CHNL_MAILQUEUE 	pDSP = &this->pSharedData->ToDSP;
	PTMHD_CHNL_MAILQUEUE 	pHost = &this->pSharedData->ToHost;
	DWORD	Flags;
	vxdEnterCritical ( &Flags);

	for ( Count = 0 ; 
		Count < this->ChannelCount ; 
		Count ++, this->IdxSent = ( this->IdxSent + 1 ) % this->ChannelCount )
	{
		pChannel = this->pChnlTab[ this->IdxSent ];

		if ( pChannel ==  NULL )
			continue;

		if ( pChannel->Direction != TMCHNL_DIRECTION_SEND )
			continue;

		while ( ! cqueueIsEmpty ( pChannel->pQueue ) )
		{
			cqueueRetrieve ( pChannel->pQueue, &MailSlot.Packet );

			MailSlot.dwChannel	= this->IdxSent;
			MailSlot.dwMessage	= 0;

			dwWriteIndex = pDSP->dwWriteIndex;

			dwWriteIndex = ( dwWriteIndex + 1 ) % (  TMHD_CHNL_MBOXSLOTS  );

#ifdef CHNL_DEBUG
			if ( MailSlot.Packet.dwArgument[4] != pChannel->PacketCounter )
			{
				DP( 0,"TM:chnlmOnPacketSend:PacketCoutner:Expected[%x]:Sending[%x]:ERROR\n",
					pChannel->PacketCounter,
					MailSlot.Packet.dwArgument[4] );
				DP( 0,"TM:[OnSend|SQ:%x|CH:%x|CM:%x|A0:%x|A1:%x|A2:%x|A3:%x|A4:%x]\n",
					MailSlot.dwPriority,
					MailSlot.dwChannel,

⌨️ 快捷键说明

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