baseport/syborg/soundsc/virtio_iohandler.cpp
changeset 45 01c1ffcc4fca
child 71 d00bf4f57250
equal deleted inserted replaced
44:72a7468afdd4 45:01c1ffcc4fca
       
     1 /*
       
     2 * This component and the accompanying materials are made available
       
     3 * under the terms of the License "Eclipse Public License v1.0"
       
     4 * which accompanies this distribution, and is available
       
     5 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     6 *
       
     7 * Initial Contributors:
       
     8 * Accenture Ltd
       
     9 *
       
    10 * Contributors:
       
    11 *
       
    12 * Description: This file is a part of sound driver for Syborg adaptation.
       
    13 *
       
    14 */
       
    15 
       
    16 #include "virtio_iohandler.h"
       
    17 #include "virtio_queue.h"
       
    18 #include "virtio_io.h"
       
    19 
       
    20 namespace VirtIo
       
    21 {
       
    22 
       
    23 DIoHandler::DIoHandler( TAny* aVirtIoBase, TUint aIntNum, TDfcQue* aDfcQue )
       
    24 	: iVirtIoBase( aVirtIoBase ), iIntNum( aIntNum ), iDfcQue( aDfcQue ),
       
    25 	iDfc( ServeDfc, this, 0 ),
       
    26 	iVirtIo( 0 ), iQueueCount( 3 ), iQueue( 0 ),
       
    27 	iClientCount( 0 )
       
    28 	{
       
    29 	SYBORG_VIRTIO_DEBUG("DIoHandler IoBase %x DfcQ %x", iVirtIoBase, iDfcQue);
       
    30 	}
       
    31 
       
    32 TInt DIoHandler::Construct()
       
    33 	{
       
    34 	TInt err = KErrNone;
       
    35 	TUint i;
       
    36 
       
    37 	SYBORG_VIRTIO_DEBUG("Creating DIo");
       
    38 
       
    39 	InstallIsr();
       
    40 
       
    41 	iVirtIo = new DIo(iVirtIoBase);
       
    42 	if (iVirtIo == NULL )
       
    43 		{ return KErrNoMemory; }
       
    44 	iVirtIo->SetStatus( EStatusAcknowledge
       
    45 			| EStatusDriverFound );
       
    46 
       
    47 	iQueue = new DQueue*[iQueueCount];
       
    48 	
       
    49 	if (iQueue == NULL )
       
    50 		{ Wipe(); return KErrNoMemory; }
       
    51 	
       
    52 	// This is to make Wipe work
       
    53 	for ( i = 0; i < iQueueCount; ++i )
       
    54 		{ iQueue[i] = NULL; }
       
    55 	
       
    56 	for ( i = 0; i < iQueueCount; ++i )
       
    57 		{
       
    58 		SYBORG_VIRTIO_DEBUG("Creating DQueue %d",i);
       
    59 		DQueue* q = new DQueue(
       
    60 			*iVirtIo, i, iVirtIo->GetQueueCount( i ) );
       
    61 		if (!q)
       
    62 			{ Wipe(); return KErrNoMemory; }
       
    63 		err = q->Construct();
       
    64 		if (err != KErrNone)
       
    65 			{ Wipe(); return err; }
       
    66 		iQueue[i] = q;
       
    67 		}
       
    68 		
       
    69 	iVirtIo->SetStatus(
       
    70 		EStatusAcknowledge
       
    71 		| EStatusDriverFound
       
    72 		| EStatusDriverInitialised );
       
    73 
       
    74 	iVirtIo->EnableInterrupt( ETrue );
       
    75 	Interrupt::Enable(iIntNum);
       
    76 	return KErrNone;
       
    77 	}
       
    78 
       
    79 void DIoHandler::Wipe()
       
    80 	{
       
    81 	if (iQueue)
       
    82 		{
       
    83 		for ( TUint i = 0; i < iQueueCount; ++i )
       
    84 			{
       
    85 			if (iQueue[i])
       
    86 				{ delete iQueue[i]; }
       
    87 			}
       
    88 		delete[] iQueue;
       
    89 		iQueue = NULL;
       
    90 		}
       
    91 	if (iVirtIo)
       
    92 		{
       
    93 		delete iVirtIo;
       
    94 		iVirtIo = NULL;
       
    95 		}
       
    96 	}
       
    97 
       
    98 DIoHandler::~DIoHandler()
       
    99 	{
       
   100 
       
   101 	Interrupt::Disable(iIntNum);
       
   102 	UninstallIsr();
       
   103 	iVirtIo->EnableInterrupt( EFalse );
       
   104 	iDfc.Cancel();
       
   105 	iVirtIo->ClearInteruptStatus();
       
   106 
       
   107 	WaitForCompletion();
       
   108 
       
   109 	iVirtIo->SetStatus( EStatusAcknowledge
       
   110 		| EStatusDriverFound );
       
   111 	iVirtIo->SetStatus( EStatusAcknowledge );
       
   112 
       
   113 	Wipe();
       
   114 	}
       
   115 	
       
   116 MQueue& DIoHandler::Queue( TUint id )
       
   117 	{ return *(iQueue[id]); }	
       
   118 
       
   119 void DIoHandler::ScheduleCallback()
       
   120 	{
       
   121 	iDfc.Enque();
       
   122 	}
       
   123 
       
   124 
       
   125 // Waits until device processes all pending requests
       
   126 // This code should be really handled by each queue individually
       
   127 void DIoHandler::WaitForCompletion()
       
   128 	{
       
   129 	SYBORG_VIRTIO_DEBUG("WaitForCompletion : {");
       
   130 
       
   131 	TInt st = Kern::PollingWait( &DIoHandler::CheckProcessing, this, 10, 100 );
       
   132 
       
   133 	ASSERT( st == KErrNone );
       
   134 
       
   135 	for ( TUint i = 0; i < iQueueCount; ++i )
       
   136 		{
       
   137 		while ( iQueue[i]->Completed() )
       
   138 			{ InterruptHandler(); }
       
   139 		}
       
   140 
       
   141 	SYBORG_VIRTIO_DEBUG("WaitForCompletion : }");
       
   142 	}
       
   143 
       
   144 TBool DIoHandler::CheckProcessing( TAny* aSelf )
       
   145 	{
       
   146 	DIoHandler* self = reinterpret_cast<DIoHandler*>( aSelf );
       
   147 	for ( TUint i = 0; i < self->iQueueCount; ++i )
       
   148 		{
       
   149 		if (self->iQueue[i]->Processing()!=0)
       
   150 			{ return EFalse; }
       
   151 		}
       
   152 	return ETrue;
       
   153 	}
       
   154 
       
   155 void DIoHandler::InstallIsr()
       
   156 	{
       
   157 	iDfc.SetDfcQ( iDfcQue );
       
   158 	Interrupt::Bind(iIntNum,ServeIsr,this);
       
   159 	}
       
   160 
       
   161 void DIoHandler::UninstallIsr()
       
   162 	{
       
   163 	Interrupt::Unbind(iIntNum);
       
   164 	iDfc.Cancel();
       
   165 	}
       
   166 
       
   167 void DIoHandler::ServeIsr( TAny* aSelf )
       
   168 	{
       
   169 	DIoHandler* self = reinterpret_cast<DIoHandler*>( aSelf );
       
   170 	Interrupt::Clear( self->iIntNum );
       
   171 	self->iVirtIo->ClearInteruptStatus();
       
   172 	self->iDfc.Add();
       
   173 	}
       
   174 
       
   175 void DIoHandler::ServeDfc( TAny* aSelf )
       
   176 	{
       
   177 	reinterpret_cast<DIoHandler*>( aSelf )->InterruptHandler();
       
   178 	}
       
   179 
       
   180 	
       
   181 // Although the function notifies all clients
       
   182 // usually only one of them is an adressee
       
   183 // the rest would just check flags/compare numbers and return.
       
   184 // If at least one client did some crucial processing 
       
   185 // (indicating that by returning EFalse from VirtIoCallback)
       
   186 // then NotifyClients returns EFalse as well.
       
   187 TBool DIoHandler::NotifyClients( MQueue& aQueue, Token aToken, TUint aBytesTransferred )
       
   188 	{
       
   189 	TBool r = ETrue;
       
   190 	for ( TUint i = 0 ; i < iClientCount; ++i )
       
   191 		{
       
   192 		r &= iClients[i]->VirtIoCallback( *this, aQueue, aToken, aBytesTransferred );
       
   193 		}
       
   194 	return r;
       
   195 	}
       
   196 	
       
   197 // Here buffers processed by the device are iterated.
       
   198 // After the first serious buffer processing (as indicated by NotifyClients)
       
   199 // further buffer processing is Deferred in another DFC callback.
       
   200 void DIoHandler::InterruptHandler()
       
   201 	{
       
   202 	for ( TUint q = 0; q < iQueueCount; ++q )
       
   203 		{
       
   204 		TUint transferred;
       
   205 		TUint cnt = ETrue;
       
   206 		do 
       
   207 			{
       
   208 			Token t = Queue(q).GetBuf(transferred);
       
   209 			if (t)
       
   210 				{ cnt = NotifyClients( Queue(q), t, transferred); }
       
   211 			else 
       
   212 				{ break; }
       
   213 			} while(cnt);
       
   214 		if (!cnt)
       
   215 			{
       
   216 			ScheduleCallback(); 
       
   217 			break;
       
   218 			}
       
   219 		}
       
   220 	}
       
   221 
       
   222 void DIoHandler::UnregisterClient( MIoCallback* aClient )
       
   223 	{
       
   224 	ASSERT( iClientCount );
       
   225 	TUint i;
       
   226 	for ( i = 0; i < iClientCount; ++i)
       
   227 		{
       
   228 		if (iClients[i] == aClient )
       
   229 			{ break; }
       
   230 		}
       
   231 	ASSERT( i < iClientCount );
       
   232 	--iClientCount;
       
   233 	// move the rest of the table one slot to the front
       
   234 	for ( ; i < iClientCount; ++i) 
       
   235 		{ iClients[i] = iClients[i+1]; }
       
   236 	}	
       
   237 
       
   238 } // namespace VirtIo