os/kernelhwsrv/kernel/eka/drivers/pbus/spbus.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// e32\drivers\pbus\spbus.cpp
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include <drivers/pbus.h>
sl@0
    19
#include "OstTraceDefinitions.h"
sl@0
    20
#ifdef OST_TRACE_COMPILER_IN_USE
sl@0
    21
#include "locmedia_ost.h"
sl@0
    22
#ifdef __VC32__
sl@0
    23
#pragma warning(disable: 4127) // disabling warning "conditional expression is constant"
sl@0
    24
#endif
sl@0
    25
#include "spbusTraces.h"
sl@0
    26
#endif
sl@0
    27
sl@0
    28
sl@0
    29
const TInt KPBusSocketThreadPriority=26;
sl@0
    30
sl@0
    31
GLDEF_D DMediaChangeBase* TheMediaChanges[KMaxMediaChanges];
sl@0
    32
GLDEF_D DPBusSocket* TheSockets[KMaxPBusSockets];
sl@0
    33
GLDEF_D DPBusPsuBase* TheVccs[KMaxPBusVccs];
sl@0
    34
GLDEF_D DPBusPsuBase* TheVccCores[KMaxPBusVccs]; 
sl@0
    35
sl@0
    36
/********************************************
sl@0
    37
 * Peripheral bus callback
sl@0
    38
 ********************************************/
sl@0
    39
EXPORT_C TPBusCallBack::TPBusCallBack()
sl@0
    40
	:	iSocket(NULL), iFunction(NULL), iIntMask(0), iPtr(NULL)
sl@0
    41
	{
sl@0
    42
	OstTraceFunctionEntry0( TPBUSCALLBACK_TPBUSCALLBACK1_ENTRY );
sl@0
    43
	iNext=NULL;
sl@0
    44
	OstTraceFunctionExit0( TPBUSCALLBACK_TPBUSCALLBACK1_EXIT );
sl@0
    45
	}
sl@0
    46
sl@0
    47
EXPORT_C TPBusCallBack::TPBusCallBack(TPBusCallBackFn aFunction, TAny* aPtr)
sl@0
    48
	: iSocket(NULL), iFunction(aFunction), iIntMask(0), iPtr(aPtr)
sl@0
    49
	{
sl@0
    50
	OstTraceFunctionEntry0( TPBUSCALLBACK_TPBUSCALLBACK2_ENTRY );
sl@0
    51
	iNext=NULL;
sl@0
    52
	OstTraceFunctionExit0( TPBUSCALLBACK_TPBUSCALLBACK2_EXIT );
sl@0
    53
	}
sl@0
    54
sl@0
    55
EXPORT_C TPBusCallBack::TPBusCallBack(TPBusIsr anIsr, TAny* aPtr, TUint anIntMask)
sl@0
    56
	: iSocket(NULL), iFunction(NULL), iIntMask(anIntMask), iIsr(anIsr), iPtr(aPtr)
sl@0
    57
	{
sl@0
    58
	OstTraceFunctionEntry0( TPBUSCALLBACK_TPBUSCALLBACK3_ENTRY );
sl@0
    59
	iNext=NULL;
sl@0
    60
	OstTraceFunctionExit0( TPBUSCALLBACK_TPBUSCALLBACK3_EXIT );
sl@0
    61
	}
sl@0
    62
sl@0
    63
EXPORT_C void TPBusCallBack::Remove()
sl@0
    64
	{
sl@0
    65
	OstTraceFunctionEntry0( TPBUSCALLBACK_REMOVE_ENTRY );
sl@0
    66
	TInt irq=NKern::DisableAllInterrupts();
sl@0
    67
	if (iNext)
sl@0
    68
		Deque();
sl@0
    69
	iNext=NULL;
sl@0
    70
	NKern::RestoreInterrupts(irq);
sl@0
    71
	OstTraceFunctionExit0( TPBUSCALLBACK_REMOVE_EXIT );
sl@0
    72
	}
sl@0
    73
sl@0
    74
EXPORT_C void TPBusCallBack::SetSocket(TInt aSocket)
sl@0
    75
	{
sl@0
    76
	OstTraceFunctionEntryExt( TPBUSCALLBACK_SETSOCKET_ENTRY, this );
sl@0
    77
	iSocket=TheSockets[aSocket];
sl@0
    78
	OstTraceFunctionExit1( TPBUSCALLBACK_SETSOCKET_EXIT, this );
sl@0
    79
	}
sl@0
    80
sl@0
    81
/********************************************
sl@0
    82
 * Media change base class
sl@0
    83
 ********************************************/
sl@0
    84
 
sl@0
    85
/**
sl@0
    86
 * Constructor for a DMediaChangeBase object.
sl@0
    87
 *
sl@0
    88
 * @param aMediaChangeNum The media change number
sl@0
    89
 */
sl@0
    90
EXPORT_C DMediaChangeBase::DMediaChangeBase(TInt aMediaChangeNum)
sl@0
    91
	:	iMediaChangeNum(aMediaChangeNum),
sl@0
    92
		iReplyCount(0),
sl@0
    93
		iDoorOpenDfc(DoorOpenDfcFn,this,Kern::DfcQue1(),1)
sl@0
    94
	{
sl@0
    95
	OstTraceFunctionEntryExt( DMEDIACHANGEBASE_DMEDIACHANGEBASE_ENTRY, this );
sl@0
    96
	}
sl@0
    97
sl@0
    98
sl@0
    99
/**
sl@0
   100
 * Creates a DMediaChangeBase object.
sl@0
   101
 * This should be overridden at the media and variant layer to allow
sl@0
   102
 * interrupts and other media/variant-specific parameters to be initialised.
sl@0
   103
 *
sl@0
   104
 * Method should be called post object creation, although could be used to
sl@0
   105
 * re-initialise parameters.
sl@0
   106
 *
sl@0
   107
 * @return KErrNone Default
sl@0
   108
 */
sl@0
   109
EXPORT_C TInt DMediaChangeBase::Create()
sl@0
   110
	{
sl@0
   111
	return KErrNone;
sl@0
   112
	}
sl@0
   113
sl@0
   114
/**
sl@0
   115
 * Called from ISR triggered by media change or from 
sl@0
   116
 * the Peripheral Bus Controller Media Driver context
sl@0
   117
 * if a media change is being forced.
sl@0
   118
 *
sl@0
   119
 * Method adds/enques a media change event on to the door 
sl@0
   120
 * open DFC queue. If called by PBUS thread then DFC queue 
sl@0
   121
 * is by-passed and change event is dealt with synchronously.
sl@0
   122
 *
sl@0
   123
 * Media change events are platform specific although are 
sl@0
   124
 * generally related to a media door or slot being opened.
sl@0
   125
 */
sl@0
   126
EXPORT_C void DMediaChangeBase::DoorOpenService()
sl@0
   127
	{
sl@0
   128
	if (NKern::CurrentContext()==NKern::EInterrupt)
sl@0
   129
	    {
sl@0
   130
	    OstTrace0(TRACE_INTERNALS, DMEDIACHANGEBASE_DOOROPENSERVICE, "Interrupt driven asynchronous media change event");
sl@0
   131
		iDoorOpenDfc.Add();
sl@0
   132
	    }
sl@0
   133
	else 
sl@0
   134
		{
sl@0
   135
		if (Kern::DfcQue1()->iThread==(NThreadBase *)NKern::CurrentThread()) 	// check if this is being called from PBUS thread
sl@0
   136
		    {
sl@0
   137
		    OstTrace0(TRACE_INTERNALS, DMEDIACHANGEBASE_DOOROPENSERVICE2, "Synchronous media change event");
sl@0
   138
			MediaChangeEvent(ETrue);
sl@0
   139
		    }
sl@0
   140
		else
sl@0
   141
		    {
sl@0
   142
		    OstTrace0(TRACE_INTERNALS, DMEDIACHANGEBASE_DOOROPENSERVICE3, "Different thread is queueing request, asynchronous media change event");
sl@0
   143
			iDoorOpenDfc.Enque();
sl@0
   144
		    }
sl@0
   145
		}
sl@0
   146
	}
sl@0
   147
sl@0
   148
sl@0
   149
/**
sl@0
   150
 * High priority DFC triggered by media change interrupt.
sl@0
   151
 * 
sl@0
   152
 * Media changes events are added/enqued by DMediaChangeBase::DoorOpenService().
sl@0
   153
 * 
sl@0
   154
 * @param aPtr Pointer to an instantiated class which enqued/added this DFC event
sl@0
   155
 */
sl@0
   156
void DMediaChangeBase::DoorOpenDfcFn(TAny* aPtr)
sl@0
   157
	{
sl@0
   158
	OstTraceFunctionEntry0( DMEDIACHANGEBASE_DOOROPENDFCFN_ENTRY );
sl@0
   159
	DMediaChangeBase* pM=(DMediaChangeBase*)aPtr;
sl@0
   160
	pM->MediaChangeEvent(ETrue);
sl@0
   161
	OstTraceFunctionExit0( DMEDIACHANGEBASE_DOOROPENDFCFN_EXIT );
sl@0
   162
	}
sl@0
   163
sl@0
   164
/**
sl@0
   165
 *
sl@0
   166
 * Notifies sockets of door close event.
sl@0
   167
 *
sl@0
   168
 * This function must be called by variant when door close has been detected.
sl@0
   169
 */
sl@0
   170
EXPORT_C void DMediaChangeBase::DoorClosedService()
sl@0
   171
	{
sl@0
   172
	OstTraceFunctionEntry1( DMEDIACHANGEBASE_DOORCLOSEDSERVICE_ENTRY, this );
sl@0
   173
	MediaChangeEvent(EFalse);
sl@0
   174
	OstTraceFunctionExit1( DMEDIACHANGEBASE_DOORCLOSEDSERVICE_EXIT, this );
sl@0
   175
	}
sl@0
   176
sl@0
   177
/**
sl@0
   178
 * Notifies relevant peripheral bus sockets of door open or close events.
sl@0
   179
 *
sl@0
   180
 * @param aDoorOpened  ETrue if door is opened
sl@0
   181
 *
sl@0
   182
 * @see DPBusSocket::DPBusSocket
sl@0
   183
 */
sl@0
   184
void DMediaChangeBase::MediaChangeEvent(TBool aDoorOpened)
sl@0
   185
	{
sl@0
   186
	OstTraceFunctionEntry1( DMEDIACHANGEBASE_MEDIACHANGEEVENT_ENTRY, this );
sl@0
   187
	__KTRACE_OPT(KPBUS1,Kern::Printf(">DMediaChangeBase(%d)::MediaChangeEvent(%d)",iMediaChangeNum,aDoorOpened));
sl@0
   188
	OstTraceExt2(TRACE_INTERNALS, DMEDIACHANGEBASE_DMEDIACHANGEBASE, "iMediaChangeNum=%d; aDoorOpened=%d", iMediaChangeNum,aDoorOpened);
sl@0
   189
	TInt i;
sl@0
   190
sl@0
   191
	// notify all sockets affected
sl@0
   192
	for (i=0; i<KMaxPBusSockets; i++)
sl@0
   193
		{
sl@0
   194
		DPBusSocket* pS=TheSockets[i];
sl@0
   195
		if (pS && pS->iMediaChange==this)
sl@0
   196
			{
sl@0
   197
			// Only increment base reply count if actually adding a DFC
sl@0
   198
			if (!pS->iMediaChangeDfc.Queued())
sl@0
   199
			    __e32_atomic_add_ord32(&iReplyCount, 1);
sl@0
   200
			pS->MediaChangeEvent(aDoorOpened);
sl@0
   201
			}
sl@0
   202
		}
sl@0
   203
	OstTraceFunctionExit1( DMEDIACHANGEBASE_MEDIACHANGEEVENT_EXIT, this );
sl@0
   204
	}
sl@0
   205
sl@0
   206
/**
sl@0
   207
 * To be called by peripheral bus socket derived classes when
sl@0
   208
 * door open/close event has been processed.
sl@0
   209
 *
sl@0
   210
 * @param aDoorOpened   ETrue door opened event processed,
sl@0
   211
 *                      EFalse door closed event processed
sl@0
   212
 *
sl@0
   213
 * @see DPBusSocket::DoorOpenEvent()
sl@0
   214
 * @see DPBusSocket::DoorCloseEvent()
sl@0
   215
 */
sl@0
   216
void DMediaChangeBase::AcknowledgeEvent(TBool aDoorOpened)
sl@0
   217
	{
sl@0
   218
	OstTraceFunctionEntryExt( DMEDIACHANGEBASE_ACKNOWLEDGEEVENT_ENTRY, this );
sl@0
   219
	TInt c = __e32_atomic_tas_ord32(&iReplyCount, 1, -1, 0);
sl@0
   220
	if (c==1)
sl@0
   221
		{
sl@0
   222
		if (aDoorOpened)
sl@0
   223
			DoDoorOpen();
sl@0
   224
		else
sl@0
   225
			DoDoorClosed();
sl@0
   226
		}
sl@0
   227
	OstTraceFunctionExit1( DMEDIACHANGEBASE_ACKNOWLEDGEEVENT_EXIT, this );
sl@0
   228
	}
sl@0
   229
sl@0
   230
/********************************************
sl@0
   231
 * Power supply base class
sl@0
   232
 ********************************************/
sl@0
   233
void psuTick(TAny* aPtr)
sl@0
   234
	{
sl@0
   235
	OstTraceFunctionEntry0( _PSUTICK_ENTRY );
sl@0
   236
	DPBusPsuBase* pP=(DPBusPsuBase*)aPtr;
sl@0
   237
	pP->iPsuDfc.Enque();
sl@0
   238
	OstTraceFunctionExit0( _PSUTICK_EXIT );
sl@0
   239
	}
sl@0
   240
	
sl@0
   241
void psuDfc(TAny* aPtr)
sl@0
   242
	{
sl@0
   243
	OstTraceFunctionEntry0( _PSUDFC_ENTRY );
sl@0
   244
	DPBusPsuBase* pP=(DPBusPsuBase*)aPtr;
sl@0
   245
	pP->DoTickService();
sl@0
   246
	OstTraceFunctionExit0( _PSUDFC_EXIT );
sl@0
   247
	}
sl@0
   248
sl@0
   249
/**
sl@0
   250
Constructor for a DPBusPsuBase object.
sl@0
   251
sl@0
   252
@param aPsuNum Unique power supply identification number
sl@0
   253
@param aMediaChangedNum Unique media change identification number
sl@0
   254
*/
sl@0
   255
DPBusPsuBase::DPBusPsuBase(TInt aPsuNum, TInt aMediaChangeNum)
sl@0
   256
	: iPsuNum(aPsuNum), iMediaChangeNum(aMediaChangeNum), iVoltCheckMethod(EPsuChkComparator), iState(EPsuOff),
sl@0
   257
	iPsuDfc(psuDfc, this, 4),
sl@0
   258
	iPwrDownCheckFn(DoPwrDownCheck)
sl@0
   259
	{
sl@0
   260
	OstTraceFunctionEntryExt( DPBUSPSUBASE_DPBUSPSUBASE_ENTRY, this );
sl@0
   261
//	iCurrLimited=EFalse;
sl@0
   262
//	iVoltageSupported=0;
sl@0
   263
//	iMaxCurrentInMicroAmps=0;
sl@0
   264
//	iVoltCheckInterval=0;
sl@0
   265
//	iInactivityCount=0;
sl@0
   266
//	iNotLockedCount=0;
sl@0
   267
//	iInactivityTimeout=0;
sl@0
   268
//	iNotLockedTimeout=0;
sl@0
   269
	}
sl@0
   270
sl@0
   271
void DPBusPsuBase::DoPwrDownCheck(TAny* aPtr)
sl@0
   272
	{
sl@0
   273
	OstTraceFunctionEntry0( DPBUSPSUBASE_DOPWRDOWNCHECK_ENTRY );
sl@0
   274
	DPBusPsuBase& self = *static_cast<DPBusPsuBase*>(aPtr);
sl@0
   275
	self.PwrDownCheck();
sl@0
   276
	OstTraceFunctionExit0( DPBUSPSUBASE_DOPWRDOWNCHECK_EXIT );
sl@0
   277
	}
sl@0
   278
sl@0
   279
/**
sl@0
   280
Initialises a DPBusPsuBase object.
sl@0
   281
sl@0
   282
Sets object information based on hardware variant PSU inforamtion.
sl@0
   283
Calls DoCreate to initialise the PSU.
sl@0
   284
sl@0
   285
@return Standard Symbian OS error code.
sl@0
   286
sl@0
   287
@see DPBusPsuBase::PsuInfo()
sl@0
   288
@see DPBusPsuBase::DoCreate()
sl@0
   289
*/
sl@0
   290
TInt DPBusPsuBase::Create()
sl@0
   291
	{
sl@0
   292
	OstTraceFunctionEntry1( DPBUSPSUBASE_CREATE_ENTRY, this );
sl@0
   293
sl@0
   294
	TPBusPsuInfo pi;
sl@0
   295
	PsuInfo(pi);
sl@0
   296
	iVoltageSupported=pi.iVoltageSupported;
sl@0
   297
	iMaxCurrentInMicroAmps=pi.iMaxCurrentInMicroAmps;
sl@0
   298
	iVoltCheckInterval=pi.iVoltCheckInterval;
sl@0
   299
	iVoltCheckMethod=pi.iVoltCheckMethod;
sl@0
   300
	iInactivityTimeout=pi.iInactivityTimeOut;
sl@0
   301
	iNotLockedTimeout=pi.iNotLockedTimeOut;
sl@0
   302
sl@0
   303
	TInt r=DoCreate();
sl@0
   304
	if (r!=KErrNone)
sl@0
   305
	    {
sl@0
   306
		OstTraceFunctionExitExt( DPBUSPSUBASE_CREATE_EXIT1, this, r );
sl@0
   307
		return r;
sl@0
   308
	    }
sl@0
   309
	
sl@0
   310
	iPsuDfc.SetDfcQ(&iSocket->iDfcQ);
sl@0
   311
	
sl@0
   312
	OstTraceFunctionExitExt( DPBUSPSUBASE_CREATE_EXIT2, this, KErrNone );
sl@0
   313
	return KErrNone;
sl@0
   314
	}
sl@0
   315
sl@0
   316
sl@0
   317
/**
sl@0
   318
Initialises the power supply unit.
sl@0
   319
sl@0
   320
The function is provided by the hardware variant layer, and needs to initialise 
sl@0
   321
interrupts and other variant-specific parameters.
sl@0
   322
sl@0
   323
The default implementation returns KErrNone.
sl@0
   324
sl@0
   325
@return KErrNone
sl@0
   326
*/
sl@0
   327
EXPORT_C TInt DPBusPsuBase::DoCreate()
sl@0
   328
	{
sl@0
   329
	OstTraceFunctionEntry1( DPBUSPSUBASE_DOCREATE_ENTRY, this );
sl@0
   330
	TInt r = KErrNone;
sl@0
   331
	OstTraceFunctionExitExt( DPBUSPSUBASE_DOCREATE_EXIT, this, r );
sl@0
   332
	return r;
sl@0
   333
	}
sl@0
   334
sl@0
   335
sl@0
   336
/**
sl@0
   337
Reset (turn off) the power supply unit.
sl@0
   338
Sets PSU state to EPsuOff.
sl@0
   339
*/
sl@0
   340
void DPBusPsuBase::Reset()
sl@0
   341
	{
sl@0
   342
	OstTraceFunctionEntry1( DPBUSPSUBASE_RESET_ENTRY, this );
sl@0
   343
	SetState(EPsuOff);
sl@0
   344
	iCurrLimited=EFalse;
sl@0
   345
	OstTraceFunctionExit1( DPBUSPSUBASE_RESET_EXIT, this );
sl@0
   346
	}
sl@0
   347
sl@0
   348
sl@0
   349
/**
sl@0
   350
Checks whether this PSU is powering a bus containing
sl@0
   351
a locked device, i.e. one that is recognised and in use by a client.
sl@0
   352
sl@0
   353
The function is provided at the media layer, could be used to ensure power is not
sl@0
   354
removed whilst media is locked or some other media specific power management activatity.
sl@0
   355
sl@0
   356
The default implementation just returns EFalse.
sl@0
   357
sl@0
   358
@return EFalse
sl@0
   359
*/
sl@0
   360
EXPORT_C TBool DPBusPsuBase::IsLocked()
sl@0
   361
	{
sl@0
   362
	return EFalse;
sl@0
   363
	}
sl@0
   364
sl@0
   365
/**
sl@0
   366
Controls the power supply state.
sl@0
   367
sl@0
   368
@param aState A TPBusPsuState enumeration specifying the required state
sl@0
   369
			 (EPsuOnFull, EPsuOff, EPsuOnCurLimit)
sl@0
   370
sl@0
   371
@return KErrNone if successful, otherwise one of the other system wide error codes.
sl@0
   372
sl@0
   373
@see TPBusPsuState
sl@0
   374
@see DPBusPsuBase::DoSetState()
sl@0
   375
*/
sl@0
   376
EXPORT_C TInt DPBusPsuBase::SetState(TPBusPsuState aState)
sl@0
   377
	{
sl@0
   378
	OstTraceFunctionEntry1( DPBUSPSUBASE_SETSTATE_ENTRY, this );
sl@0
   379
sl@0
   380
	TInt r=KErrGeneral;
sl@0
   381
	if (aState==EPsuOff)
sl@0
   382
		{
sl@0
   383
		iTickLink.Cancel(); // No point in having the 1 second tick running while the PSU is off
sl@0
   384
		}
sl@0
   385
	else
sl@0
   386
		{
sl@0
   387
		// Start the 1 second tick to monitor for inactivity, not in use and PSU level checking
sl@0
   388
		iTickLink.Cancel();
sl@0
   389
		iTickLink.Periodic(KPBusPsuTickInterval,psuTick,this);
sl@0
   390
		}
sl@0
   391
sl@0
   392
	// Don't turn the PSU back on if it has current limited since the last reset event 
sl@0
   393
	iInactivityCount=0;
sl@0
   394
	iNotLockedCount=0;
sl@0
   395
	if (aState==EPsuOff || !iCurrLimited)
sl@0
   396
		{
sl@0
   397
		DoSetState(aState);
sl@0
   398
		iState=aState;
sl@0
   399
		r=KErrNone;
sl@0
   400
		}
sl@0
   401
	__KTRACE_OPT(KPBUS2,Kern::Printf("<Psu(%d):Set(%d)-%d",iPsuNum,aState,r));
sl@0
   402
	OstTraceExt3(TRACE_INTERNALS, DPBUSPSUBASE_SETSTATE, "iPsuNum=%d; aState=%d; retval=%d", iPsuNum, (TInt) aState, r);
sl@0
   403
	OstTraceFunctionExit1( DPBUSPSUBASE_SETSTATE_EXIT, this );
sl@0
   404
	return r;
sl@0
   405
	}
sl@0
   406
sl@0
   407
sl@0
   408
/**
sl@0
   409
Check the voltage level of the power supply unit is as expected.
sl@0
   410
This method is called every PSU tick.
sl@0
   411
sl@0
   412
@param aCheckStatus Power check status in which voltage check can be performed (e.g. KPsuChkOnPwrUp).
sl@0
   413
sl@0
   414
@return KErrNone Voltage checking has been performed.
sl@0
   415
        KErrNotSupported Voltage checking is not supported by the hardware variant.
sl@0
   416
        
sl@0
   417
@see KPsuChkOnPwrUp       
sl@0
   418
@see KPsuChkWhileOn
sl@0
   419
@see DPBusPsuBase::DoTickService()
sl@0
   420
@see DPBusPsuBase::DoCheckVoltage()
sl@0
   421
*/
sl@0
   422
TInt DPBusPsuBase::CheckVoltage(TUint aCheckStatus)
sl@0
   423
	{
sl@0
   424
	OstTraceFunctionEntryExt( DPBUSPSUBASE_CHECKVOLTAGE_ENTRY, this );
sl@0
   425
	// Check that voltage checking is in order at this time
sl@0
   426
	if (
sl@0
   427
		(aCheckStatus&iVoltCheckInterval) &&
sl@0
   428
		((aCheckStatus&KPsuChkOnPwrUp) || ((aCheckStatus&KPsuChkWhileOn)&&iState==EPsuOnFull))
sl@0
   429
	   )
sl@0
   430
		{
sl@0
   431
		DoCheckVoltage();
sl@0
   432
		OstTraceFunctionExitExt( DPBUSPSUBASE_CHECKVOLTAGE_EXIT, this, KErrNone );
sl@0
   433
		return KErrNone;
sl@0
   434
		}
sl@0
   435
	OstTraceFunctionExitExt( DPBUSPSUBASE_CHECKVOLTAGE_EXIT2, this, KErrNotSupported );
sl@0
   436
	return KErrNotSupported;
sl@0
   437
	}
sl@0
   438
sl@0
   439
sl@0
   440
/**
sl@0
   441
Reports the result of the voltage check.
sl@0
   442
sl@0
   443
The function is called by the variant implementation of DoCheckVoltage() 
sl@0
   444
to report the result.
sl@0
   445
sl@0
   446
Reporting a result of KErrGeneral (to indicate a failure) will result in a 
sl@0
   447
call to DPBusSocket::PsuFault(), otherwise report KErrNone to indicate a pass
sl@0
   448
and KErrNotReady if the voltage check was not completed.
sl@0
   449
sl@0
   450
@param anError System wide error code
sl@0
   451
sl@0
   452
@see DPBusPsuBase::DoCheckVoltage()
sl@0
   453
@see DPBusSocket::PsuFault()
sl@0
   454
*/
sl@0
   455
EXPORT_C void DPBusPsuBase::ReceiveVoltageCheckResult(TInt anError)
sl@0
   456
	{
sl@0
   457
	OstTraceFunctionEntryExt( DPBUSPSUBASE_RECEIVEVOLTAGECHECKRESULT_ENTRY, this );
sl@0
   458
//	__KTRACE_OPT(KPBUS1,Kern::Printf("DPBusPsuBase(%d)::ReceiveVoltageCheckResult(%d)",iPsuNum,anError));
sl@0
   459
	OstTraceExt2(TRACE_INTERNALS, DPBUSPSUBASE_RECEVIVEVOLTAGECHECKRESULT,"iPsuNum=%d; ReceiveVoltageCheckResult=%d",iPsuNum,anError );
sl@0
   460
	if (anError==KErrGeneral)
sl@0
   461
		{
sl@0
   462
		SetCurrLimited();
sl@0
   463
		iSocket->PsuFault(KErrCorrupt);
sl@0
   464
		}
sl@0
   465
	OstTraceFunctionExit1( DPBUSPSUBASE_RECEIVEVOLTAGECHECKRESULT_EXIT, this );
sl@0
   466
	}
sl@0
   467
sl@0
   468
/**
sl@0
   469
Get the current power supply unit status
sl@0
   470
sl@0
   471
@return PSU status.
sl@0
   472
sl@0
   473
@see TPBusPsuStatus
sl@0
   474
*/
sl@0
   475
TPBusPsuStatus DPBusPsuBase::Status()
sl@0
   476
	{
sl@0
   477
	OstTraceFunctionEntry1( DPBUSPSUBASE_STATUS_ENTRY, this );
sl@0
   478
	if (iCurrLimited)
sl@0
   479
	    {
sl@0
   480
		OstTraceFunctionExit1( DPBUSPSUBASE_STATUS_EXIT1, this );
sl@0
   481
		return(EPsuStatError);
sl@0
   482
	    }
sl@0
   483
	else
sl@0
   484
	    {
sl@0
   485
	    OstTraceFunctionExit1( DPBUSPSUBASE_STATUS_EXIT2, this );
sl@0
   486
		return( (iState==EPsuOff) ? EPsuStatOff : EPsuStatOn );
sl@0
   487
	    }
sl@0
   488
	}
sl@0
   489
sl@0
   490
sl@0
   491
/**
sl@0
   492
Checks if power supply unit can be turned off.
sl@0
   493
sl@0
   494
@see DPBusPsuBase::DoTickService()
sl@0
   495
*/
sl@0
   496
void DPBusPsuBase::PwrDownCheck()
sl@0
   497
	{
sl@0
   498
OstTraceFunctionEntry1( DPBUSPSUBASE_PWRDOWNCHECK_ENTRY, this );
sl@0
   499
	if (
sl@0
   500
		(iNotLockedTimeout&&!IsLocked()&&++iNotLockedCount>iNotLockedTimeout) ||
sl@0
   501
		(iInactivityTimeout&&++iInactivityCount>iInactivityTimeout)
sl@0
   502
	   )
sl@0
   503
			iSocket->PsuTimeout();
sl@0
   504
	OstTraceFunctionExit1( DPBUSPSUBASE_PWRDOWNCHECK_EXIT, this );
sl@0
   505
	}
sl@0
   506
	
sl@0
   507
	
sl@0
   508
/**
sl@0
   509
Services the Pc Card Tick (called in timer thread).
sl@0
   510
*/
sl@0
   511
EXPORT_C void DPBusPsuBase::DoTickService()
sl@0
   512
	{
sl@0
   513
	OstTraceFunctionEntry1( DPBUSPSUBASE_DOTICKSERVICE_ENTRY, this );
sl@0
   514
	if (iPwrDownCheckFn)
sl@0
   515
		(*iPwrDownCheckFn)(this);	
sl@0
   516
	CheckVoltage(KPsuChkWhileOn);	// Check voltage level
sl@0
   517
	OstTraceFunctionExit1( DPBUSPSUBASE_DOTICKSERVICE_EXIT, this );
sl@0
   518
	}
sl@0
   519
sl@0
   520
sl@0
   521
/********************************************
sl@0
   522
 * Peripheral bus power handler
sl@0
   523
 ********************************************/
sl@0
   524
DPBusPowerHandler::DPBusPowerHandler(DPBusSocket* aSocket)
sl@0
   525
	:	DPowerHandler(*aSocket->iName),
sl@0
   526
		iSocket(aSocket)
sl@0
   527
	{
sl@0
   528
	OstTraceFunctionEntryExt( DPBUSPOWERHANDLER_DPBUSPOWERHANDLER_ENTRY, this );
sl@0
   529
	}
sl@0
   530
sl@0
   531
void DPBusPowerHandler::PowerUp()
sl@0
   532
	{
sl@0
   533
	OstTraceFunctionEntry1( DPBUSPOWERHANDLER_POWERUP_ENTRY, this );
sl@0
   534
	iSocket->iPowerUpDfc.Enque();
sl@0
   535
	OstTraceFunctionExit1( DPBUSPOWERHANDLER_POWERUP_EXIT, this );
sl@0
   536
	}
sl@0
   537
sl@0
   538
void DPBusPowerHandler::PowerDown(TPowerState)
sl@0
   539
	{
sl@0
   540
	OstTraceFunctionEntry1( DPBUSPOWERHANDLER_POWERDOWN_ENTRY, this );
sl@0
   541
	iSocket->iPowerDownDfc.Enque();
sl@0
   542
	OstTraceFunctionExit1( DPBUSPOWERHANDLER_POWERDOWN_EXIT, this );
sl@0
   543
	}
sl@0
   544
sl@0
   545
/********************************************
sl@0
   546
 * Peripheral bus socket base class
sl@0
   547
 ********************************************/
sl@0
   548
void mediaChangeDfc(TAny* aPtr)
sl@0
   549
	{
sl@0
   550
	OstTraceFunctionEntry0( _MEDIACHANGEDFC_ENTRY );
sl@0
   551
	DPBusSocket* pS=(DPBusSocket*)aPtr;
sl@0
   552
	if (pS->iDoorOpened)
sl@0
   553
		pS->DoorOpenEvent();
sl@0
   554
	else
sl@0
   555
		pS->DoorCloseEvent();
sl@0
   556
	OstTraceFunctionExit0( _MEDIACHANGEDFC_EXIT );
sl@0
   557
	}
sl@0
   558
sl@0
   559
void powerUpDfc(TAny* aPtr)
sl@0
   560
	{
sl@0
   561
	OstTraceFunctionEntry0( _POWERUPDFC_ENTRY );
sl@0
   562
	DPBusSocket* pS=(DPBusSocket*)aPtr;
sl@0
   563
	pS->DoPowerUp();
sl@0
   564
	OstTraceFunctionExit0( _POWERUPDFC_EXIT );
sl@0
   565
	}
sl@0
   566
sl@0
   567
void powerDownDfc(TAny* aPtr)
sl@0
   568
	{
sl@0
   569
	OstTraceFunctionEntry0( _POWERDOWNDFC_ENTRY );
sl@0
   570
	DPBusSocket* pS=(DPBusSocket*)aPtr;
sl@0
   571
	pS->DoPowerDown();
sl@0
   572
	OstTraceFunctionExit0( _POWERDOWNDFC_EXIT );
sl@0
   573
	}
sl@0
   574
sl@0
   575
	/**
sl@0
   576
    PBus Socket panics. Faults the system. 
sl@0
   577
	This will start the Crash Debugger if it is present, otherwise the system is rebooted by calling Kern::Restart(0)
sl@0
   578
	@param aPanic	The panic to be raised
sl@0
   579
	@see DPBusSocket::TPanic
sl@0
   580
	*/
sl@0
   581
EXPORT_C void DPBusSocket::Panic(DPBusSocket::TPanic aPanic)
sl@0
   582
	{
sl@0
   583
	Kern::Fault("PBUS",aPanic);
sl@0
   584
	}
sl@0
   585
sl@0
   586
	/**
sl@0
   587
    Flags the media driver as entering a critical part of its processing.
sl@0
   588
    In this context, critical means that the driver must be allowed to complete its current activity.
sl@0
   589
	
sl@0
   590
	@return	KErrNone if successful,
sl@0
   591
			KErrNotReady if there is any postponed events outstanding.
sl@0
   592
	@see DPBusSocket::EndInCritical()
sl@0
   593
	*/
sl@0
   594
EXPORT_C TInt DPBusSocket::InCritical()
sl@0
   595
	{
sl@0
   596
	OstTraceFunctionEntry1( DPBUSSOCKET_INCRITICAL_ENTRY, this );
sl@0
   597
	__KTRACE_OPT(KPBUS1,Kern::Printf(">DPBusSocket(%d)::InCritical",iSocketNumber));
sl@0
   598
	OstTrace1(TRACE_INTERNALS, DPBUSSOCKET_INCRITICAL, "iSocketNumber=%d",iSocketNumber );
sl@0
   599
	if (iPostponeCount==0 && iPostponedEvents!=0)
sl@0
   600
	    {
sl@0
   601
		OstTraceFunctionExitExt( DPBUSSOCKET_INCRITICAL_EXIT1, this, KErrNotReady );
sl@0
   602
		return KErrNotReady;	// we are about to do media change/power down
sl@0
   603
	    }
sl@0
   604
	++iPostponeCount;
sl@0
   605
	OstTraceFunctionExitExt( DPBUSSOCKET_INCRITICAL_EXIT2, this, KErrNone );
sl@0
   606
	return KErrNone;
sl@0
   607
	}
sl@0
   608
sl@0
   609
	/**
sl@0
   610
    Flags the media driver as leaving a critical part of its processing.
sl@0
   611
	This function enque the media change DFC or power down DFC depending on the event DPBusSocket::iPostponedEvents.
sl@0
   612
	@see TPostponedEvent
sl@0
   613
	@see DPBusSocket::InCritical()
sl@0
   614
	*/
sl@0
   615
EXPORT_C void DPBusSocket::EndInCritical()
sl@0
   616
	{
sl@0
   617
	OstTraceFunctionEntry1( DPBUSSOCKET_ENDINCRITICAL_ENTRY, this );
sl@0
   618
	__KTRACE_OPT(KPBUS1,Kern::Printf(">DPBusSocket(%d)::EndInCritical",iSocketNumber));
sl@0
   619
	OstTrace1(TRACE_INTERNALS, DPBUSSOCKET_ENDINCRITICAL1, "iSocketNumber=%d",iSocketNumber);
sl@0
   620
	if (iPostponeCount && --iPostponeCount==0)
sl@0
   621
		{
sl@0
   622
		if (iPostponedEvents & EMediaChange)
sl@0
   623
			{
sl@0
   624
			iMediaChangeDfc.Enque();
sl@0
   625
			__KTRACE_OPT(KPBUS1,Kern::Printf("Media change - done postponed"));
sl@0
   626
			OstTrace0(TRACE_INTERNALS, DPBUSSOCKET_ENDINCRITICAL2, "Media change - done postponed");
sl@0
   627
			}
sl@0
   628
		if (iPostponedEvents & EPowerDown)
sl@0
   629
			{
sl@0
   630
			iPowerDownDfc.Enque();
sl@0
   631
			__KTRACE_OPT2(KPBUS1,KPOWER,Kern::Printf("Power down - done postponed"));
sl@0
   632
			OstTrace0(TRACE_INTERNALS, DPBUSSOCKET_ENDINCRITICAL3, "Power down - done postponed");
sl@0
   633
			}
sl@0
   634
		}
sl@0
   635
	OstTraceFunctionExit1( DPBUSSOCKET_ENDINCRITICAL_EXIT, this );
sl@0
   636
	}
sl@0
   637
sl@0
   638
	/**
sl@0
   639
	Sets the incremental value of current consumption to aCurrent.
sl@0
   640
    @param	aCurrent	Delta Current in Milliamps 
sl@0
   641
	@see DPowerHandler::DeltaCurrentConsumption()
sl@0
   642
	*/
sl@0
   643
EXPORT_C void DPBusSocket::DeltaCurrentConsumption(TInt aDelta)
sl@0
   644
	{
sl@0
   645
	OstTraceFunctionEntryExt( DPBUSSOCKET_DELTACURRENTCONSUMPTION_ENTRY, this );
sl@0
   646
	iPowerHandler->DeltaCurrentConsumption(aDelta);
sl@0
   647
	OstTraceFunctionExit1( DPBUSSOCKET_DELTACURRENTCONSUMPTION_EXIT, this );
sl@0
   648
	}
sl@0
   649
sl@0
   650
	/**
sl@0
   651
	Constructor for DPBusSocket.
sl@0
   652
	Sets the iSocketNumber and initializes the DFC queue for Media Change Dfc, PowerUp Dfc, PowerDown Dfc and PSU Dfc queue.
sl@0
   653
    @param	aSocketNumber Pbus socket number
sl@0
   654
	*/
sl@0
   655
DPBusSocket::DPBusSocket(TInt aSocketNumber)
sl@0
   656
	:	iSocketNumber(aSocketNumber),
sl@0
   657
		iMediaChangeDfc(mediaChangeDfc, this, 6),
sl@0
   658
		iPowerUpDfc(powerUpDfc, this, 4),
sl@0
   659
		iPowerDownDfc(powerDownDfc, this, 4),
sl@0
   660
		iPsuDfc(psuDfc, this, 4)
sl@0
   661
	{
sl@0
   662
	OstTraceFunctionEntryExt( DPBUSSOCKET_DPBUSSOCKET_ENTRY, this );
sl@0
   663
//	iPowerGroup=0;
sl@0
   664
//	iName=NULL;
sl@0
   665
//	iState=EPBusCardAbsent;
sl@0
   666
//	iPostponeCount=0;
sl@0
   667
//	iPostponedEvents=0;
sl@0
   668
//	iPowerHandler=NULL;
sl@0
   669
	}
sl@0
   670
sl@0
   671
	/**
sl@0
   672
	Creates a new Socket.
sl@0
   673
	This method sets the DFC Queue for the driver associated,
sl@0
   674
    Constructs power handler and registers it with the Power Manager.
sl@0
   675
	@param  aName	Assigns aName to the PBus socket.
sl@0
   676
	@return KErrNone	if successful, otherwise one of the other system wide error codes.
sl@0
   677
	@see DPBusPowerHandler
sl@0
   678
	@see iMediaChangeDfc
sl@0
   679
	@see iPowerUpDfc
sl@0
   680
	@see iPowerDownDfc
sl@0
   681
	@see iPsuDfc
sl@0
   682
	*/
sl@0
   683
TInt DPBusSocket::Create(const TDesC* aName)
sl@0
   684
	{
sl@0
   685
	OstTraceFunctionEntry1( DPBUSSOCKET_CREATE_ENTRY, this );
sl@0
   686
	__KTRACE_OPT(KPBUS1,Kern::Printf(">DPBusSocket(%d)::Create %lS",iSocketNumber,aName));
sl@0
   687
	OstTrace1(TRACE_INTERNALS, DPBUSSOCKET_CREATE, "iSocketNumber=%d",iSocketNumber);
sl@0
   688
	iName=aName;
sl@0
   689
	DPBusPowerHandler* pH=new DPBusPowerHandler(this);
sl@0
   690
	if (!pH)
sl@0
   691
	    {
sl@0
   692
		OstTraceFunctionExitExt( DPBUSSOCKET_CREATE_EXIT1, this, KErrNoMemory );
sl@0
   693
		return KErrNoMemory;
sl@0
   694
	    }
sl@0
   695
	iPowerHandler=pH;
sl@0
   696
	pH->Add();		// register power handler
sl@0
   697
	TInt r=Kern::DfcQInit(&iDfcQ, KPBusSocketThreadPriority, iName);
sl@0
   698
	if (r!=KErrNone)
sl@0
   699
	    {
sl@0
   700
		OstTraceFunctionExitExt( DPBUSSOCKET_CREATE_EXIT2, this, r );
sl@0
   701
		return r;
sl@0
   702
	    }
sl@0
   703
	iMediaChangeDfc.SetDfcQ(&iDfcQ);
sl@0
   704
	iPowerUpDfc.SetDfcQ(&iDfcQ);
sl@0
   705
	iPowerDownDfc.SetDfcQ(&iDfcQ);
sl@0
   706
	
sl@0
   707
	OstTraceFunctionExitExt( DPBUSSOCKET_CREATE_EXIT3, this, KErrNone );
sl@0
   708
	return KErrNone;
sl@0
   709
	}
sl@0
   710
sl@0
   711
	/**
sl@0
   712
	Initializes the PBus socket by changing its state to EPBusOff.
sl@0
   713
	@return	KErrNone	if successful,
sl@0
   714
			otherwise one of the other system wide error codes.
sl@0
   715
	*/
sl@0
   716
TInt DPBusSocket::Init()
sl@0
   717
	{
sl@0
   718
	OstTraceFunctionEntry1( DPBUSSOCKET_INIT_ENTRY, this );
sl@0
   719
	__KTRACE_OPT(KPBUS1,Kern::Printf(">DPBusSocket(%d)::Init",iSocketNumber));
sl@0
   720
	OstTrace1(TRACE_INTERNALS, DPBUSSOCKET_INIT, "iSocketNumber=%d",iSocketNumber);
sl@0
   721
	__PM_ASSERT(iState == EPBusCardAbsent);
sl@0
   722
	if (MediaState()==EDoorClosed && CardIsPresent())
sl@0
   723
		ChangeState(EPBusOff,KErrNotReady);
sl@0
   724
	OstTraceFunctionExitExt( DPBUSSOCKET_INIT_EXIT, this, KErrNone );
sl@0
   725
	return KErrNone;
sl@0
   726
	}
sl@0
   727
sl@0
   728
void DPBusSocket::ResetSocket(TBool aFullReset)
sl@0
   729
	{
sl@0
   730
	OstTraceFunctionEntryExt( DPBUSSOCKET_RESETSOCKET_ENTRY, this );
sl@0
   731
	OstTraceExt2(TRACE_INTERNALS, DPBUSSOCKET_RESETSOCKET, "iSocketNumber=%d; aFullReset=%d", iSocketNumber, aFullReset);
sl@0
   732
	Reset1();
sl@0
   733
	iVcc->Reset();
sl@0
   734
	if (aFullReset)
sl@0
   735
		Reset2();
sl@0
   736
	OstTraceFunctionExit1( DPBUSSOCKET_RESETSOCKET_EXIT, this );
sl@0
   737
	}
sl@0
   738
sl@0
   739
void DPBusSocket::ChangeState(TInt aState, TInt anError)
sl@0
   740
//
sl@0
   741
// Change state, notifying all clients
sl@0
   742
//
sl@0
   743
	{
sl@0
   744
	OstTraceFunctionEntryExt( DPBUSSOCKET_CHANGESTATE_ENTRY, this );
sl@0
   745
	__KTRACE_OPT(KPBUS1,Kern::Printf("Socket %d ChangeState %d to %d, err %d",iSocketNumber,iState,aState,anError));
sl@0
   746
	OstTraceExt4(TRACE_INTERNALS, DPBUSSOCKET_CHANGESTATE , "iSocketNumber=%d; ChangeState %d to %d; anError=%d",iSocketNumber,iState,aState,anError);
sl@0
   747
	if (iState!=aState)
sl@0
   748
		{
sl@0
   749
		if(iState == EPBusCardAbsent && aState == EPBusOff && anError == KErrTimedOut)
sl@0
   750
			{
sl@0
   751
			// Maintain the internal state to EPBusCardAbsent when PSU
sl@0
   752
			// times out to prevent the media from being powered back up.
sl@0
   753
			}
sl@0
   754
		else
sl@0
   755
			{
sl@0
   756
			iState=aState;
sl@0
   757
			}
sl@0
   758
sl@0
   759
		// notify all clients of state change
sl@0
   760
		SDblQueLink* pC=iCallBackQ.iA.iNext;
sl@0
   761
		while (pC && pC!=&iCallBackQ.iA)
sl@0
   762
			{
sl@0
   763
			((TPBusCallBack*)pC)->NotifyPBusStateChange(aState,anError);
sl@0
   764
			pC=pC->iNext;
sl@0
   765
			}
sl@0
   766
		}
sl@0
   767
	OstTraceFunctionExit1( DPBUSSOCKET_CHANGESTATE_EXIT, this );
sl@0
   768
	}
sl@0
   769
sl@0
   770
void DPBusSocket::Isr(TInt anId)
sl@0
   771
//
sl@0
   772
// Service a card interrupt
sl@0
   773
//
sl@0
   774
	{
sl@0
   775
	OstTraceFunctionEntry1( DPBUSSOCKET_ISR_ENTRY, this );
sl@0
   776
	OstTraceExt2(TRACE_INTERNALS, DPBUSSOCKET_ISR, "iSocketNumber=%d; anId=%d", iSocketNumber, anId );
sl@0
   777
	// notify all interested clients of interrupt
sl@0
   778
	SDblQueLink* pC=iCallBackQ.iA.iNext;
sl@0
   779
#ifdef _DEBUG
sl@0
   780
	TInt n=0;
sl@0
   781
#endif
sl@0
   782
	while (pC!=&iCallBackQ.iA)
sl@0
   783
		{
sl@0
   784
#ifdef _DEBUG
sl@0
   785
		n++;
sl@0
   786
#endif
sl@0
   787
		((TPBusCallBack*)pC)->Isr(anId);
sl@0
   788
		pC=pC->iNext;
sl@0
   789
		}
sl@0
   790
#ifdef _DEBUG
sl@0
   791
	__KTRACE_OPT(KPBUS1,Kern::Printf("!%d",n));
sl@0
   792
	OstTrace1(TRACE_INTERNALS, DPBUSSOCKET_ISR2, "!%d", n);
sl@0
   793
#endif
sl@0
   794
	OstTraceFunctionExit1( DPBUSSOCKET_ISR_EXIT, this );
sl@0
   795
	}
sl@0
   796
sl@0
   797
	/**
sl@0
   798
	This function adds a callback function to the socket.
sl@0
   799
	@param aCallBack is a pointer to PBus callback function for event notification.
sl@0
   800
	@see TPBusCallBack
sl@0
   801
	*/
sl@0
   802
EXPORT_C void DPBusSocket::Add(TPBusCallBack* aCallBack)
sl@0
   803
	{
sl@0
   804
	OstTraceFunctionEntry1( DPBUSSOCKET_ADD_ENTRY, this );
sl@0
   805
	__KTRACE_OPT(KPBUS1,Kern::Printf("DPBusSocket(%d)::Add(%08x) next %08x",iSocketNumber,aCallBack,aCallBack->iNext));
sl@0
   806
	OstTraceExt3(TRACE_INTERNALS, DPBUSSOCKET_ADD, "iSocketNumber=%d; aCallBack=0x%08x; aCallBack->iNext=0x%08x",iSocketNumber, (TUint) aCallBack, (TUint) aCallBack->iNext);
sl@0
   807
	TInt irq=NKern::DisableAllInterrupts();
sl@0
   808
	if (!aCallBack->iNext)
sl@0
   809
		iCallBackQ.Add(aCallBack);
sl@0
   810
	NKern::RestoreInterrupts(irq);
sl@0
   811
	OstTraceFunctionExit1( DPBUSSOCKET_ADD_EXIT, this );
sl@0
   812
	}
sl@0
   813
sl@0
   814
	/**
sl@0
   815
	Called by clients to power up the socket.
sl@0
   816
	@return	KErrNone	if successful, otherwise one of the other system-wide error codes including: 
sl@0
   817
				KErrNotReady if card absent or media change has occurred,
sl@0
   818
				KErrServerBusy if already powering up,
sl@0
   819
				KErrCompletion if already powered up,
sl@0
   820
				KErrCorrupt if PSU fault occurs.
sl@0
   821
sl@0
   822
    @panic PBUS 1, if PBUS state is invalid.
sl@0
   823
	@see TPBusState
sl@0
   824
	*/
sl@0
   825
EXPORT_C TInt DPBusSocket::PowerUp()
sl@0
   826
	{
sl@0
   827
	OstTraceFunctionEntry1( DPBUSSOCKET_POWERUP_ENTRY, this );
sl@0
   828
	__KTRACE_OPT2(KPBUS1,KPOWER,Kern::Printf(">DPBusSocket(%d)::PowerUp state %d",iSocketNumber,iState));
sl@0
   829
	OstTraceExt2(TRACE_INTERNALS, DPBUSSOCKET_POWERUP1, "iSocketNumber=%d; iState=%d",iSocketNumber,iState);
sl@0
   830
	TInt r=KErrNone;
sl@0
   831
	switch (iState)
sl@0
   832
		{
sl@0
   833
		case EPBusCardAbsent:		// card absent or media change has occurred
sl@0
   834
			r=KErrNotReady;
sl@0
   835
			break;
sl@0
   836
		case EPBusOff:
sl@0
   837
			break;
sl@0
   838
		case EPBusPoweringUp:		// already powering up
sl@0
   839
		case EPBusPowerUpPending:
sl@0
   840
			r=KErrServerBusy;
sl@0
   841
			break;
sl@0
   842
		case EPBusOn:				// already powered up
sl@0
   843
			r=KErrCompletion;
sl@0
   844
			break;
sl@0
   845
		case EPBusPsuFault:
sl@0
   846
			r=KErrCorrupt;
sl@0
   847
			break;
sl@0
   848
		default:
sl@0
   849
			Panic(EPowerUpInvalidState);
sl@0
   850
		}
sl@0
   851
	if (r==KErrNone)
sl@0
   852
		{
sl@0
   853
		if (iStandby)
sl@0
   854
			{
sl@0
   855
			// machine is powering down, so delay client until machine powers back up
sl@0
   856
			// remember to power up when machine powers back up
sl@0
   857
			ChangeState(EPBusPowerUpPending,KErrNone);
sl@0
   858
			}
sl@0
   859
		else
sl@0
   860
			{
sl@0
   861
			ChangeState(EPBusPoweringUp,KErrNone);
sl@0
   862
			InitiatePowerUpSequence();
sl@0
   863
			}
sl@0
   864
		}
sl@0
   865
	__KTRACE_OPT2(KPBUS1,KPOWER,Kern::Printf("<DPBusSocket(%d)::PowerUp ret %d, state %d",iSocketNumber,r,iState));
sl@0
   866
	OstTraceExt2(TRACE_INTERNALS, DPBUSSOCKET_POWERUP2, "iSocketNumber=%d; iState=%d",iSocketNumber,iState);
sl@0
   867
	OstTraceFunctionExitExt( DPBUSSOCKET_POWERUP_EXIT, this, r );
sl@0
   868
	return r;
sl@0
   869
	}
sl@0
   870
	/**
sl@0
   871
	This function is called upon completion of the power up sequence of the device.
sl@0
   872
	This is method is called by the derived class methods to terminate the powerup sequence with error codes.
sl@0
   873
sl@0
   874
	@param anError	One of the system wide error codes.
sl@0
   875
	@see DPBusSocket::InitiatePowerUpSequence()
sl@0
   876
	*/
sl@0
   877
sl@0
   878
EXPORT_C void DPBusSocket::PowerUpSequenceComplete(TInt anError)
sl@0
   879
	{
sl@0
   880
	OstTraceFunctionEntry1( DPBUSSOCKET_POWERUPSEQUENCECOMPLETE_ENTRY, this );
sl@0
   881
	__KTRACE_OPT2(KPBUS1,KPOWER,Kern::Printf("DPBusSocket(%d)::PowerUpSequenceComplete state %d error %d",iSocketNumber,iState,anError));
sl@0
   882
	OstTraceExt3(TRACE_INTERNALS, DPBUSSOCKET_POWERUPSEQUENCECOMPLETE, "iSocketNumber=%d; iState=%d; anError=%d",iSocketNumber,iState,anError);
sl@0
   883
	if (iState!=EPBusCardAbsent && iState!=EPBusOff)
sl@0
   884
		{
sl@0
   885
		if (anError==KErrNone)
sl@0
   886
			ChangeState(EPBusOn,KErrNone);
sl@0
   887
		else if (anError==KErrBadPower || anError==KErrAbort || anError==KErrTimedOut)
sl@0
   888
			ChangeState(EPBusOff,anError);
sl@0
   889
		else if (anError == KErrNotReady)
sl@0
   890
			ChangeState(EPBusCardAbsent,KErrAbort);
sl@0
   891
		else
sl@0
   892
			ChangeState(EPBusPsuFault,anError);
sl@0
   893
		}
sl@0
   894
	OstTraceFunctionExit1( DPBUSSOCKET_POWERUPSEQUENCECOMPLETE_EXIT, this );
sl@0
   895
	}
sl@0
   896
sl@0
   897
void DPBusSocket::PsuFault(TInt anError)
sl@0
   898
	{
sl@0
   899
	__KTRACE_OPT(KPBUS1,Kern::Printf(">DPBusSocket(%d)::PsuFault state %d error %d",iSocketNumber,iState,anError));
sl@0
   900
	OstTraceExt2(TRACE_INTERNALS, DPBUSSOCKET_PSUFAULT, "iSocketNumber=%d; iState=%d",iSocketNumber,iState );
sl@0
   901
	ResetSocket(ETrue);
sl@0
   902
	ChangeState(EPBusPsuFault,anError);
sl@0
   903
	}
sl@0
   904
sl@0
   905
void DPBusSocket::PsuTimeout()
sl@0
   906
	{
sl@0
   907
	__KTRACE_OPT(KPBUS1,Kern::Printf(">DPBusSocket(%d)::PsuTimeout state %d",iSocketNumber,iState));
sl@0
   908
	OstTraceExt2(TRACE_INTERNALS, DPBUSSOCKET_PSUTIMEOUT, "iSocketNumber=%d; iState=%d",iSocketNumber,iState );
sl@0
   909
	ResetSocket(EFalse);
sl@0
   910
	ChangeState(EPBusOff,KErrTimedOut);
sl@0
   911
	}
sl@0
   912
sl@0
   913
void DPBusSocket::DoPowerUp()
sl@0
   914
//
sl@0
   915
// Called on transition from standby
sl@0
   916
//
sl@0
   917
	{
sl@0
   918
	OstTraceFunctionEntry1( DPBUSSOCKET_DOPOWERUP_ENTRY, this );
sl@0
   919
	
sl@0
   920
	__KTRACE_OPT2(KPBUS1,KPOWER,Kern::Printf("DPBusSocket(%d)::DoPowerUp state %d",iSocketNumber,iState));
sl@0
   921
	OstTraceExt2(TRACE_INTERNALS, DPBUSSOCKET_DOPOWERUP, "iSocketNumber=%d; iState=%d",iSocketNumber,iState );
sl@0
   922
	__PM_ASSERT(iStandby);
sl@0
   923
	if (iState!=EPBusCardAbsent && iState!=EPBusOff && iState!=EPBusPowerUpPending)
sl@0
   924
		Panic(EMcPowerUpInvalidState);
sl@0
   925
sl@0
   926
	// when we power up, check whether the door is closed and a card is present
sl@0
   927
	// if so we should start in state Off otherwise in state CardAbsent
sl@0
   928
sl@0
   929
	TMediaState doorState = MediaState();
sl@0
   930
	TBool cardIsPresent = CardIsPresent();
sl@0
   931
sl@0
   932
#ifdef __ENABLE_SIMULATED_MEDIA_CHANGE
sl@0
   933
	// Override the default media state is we are simulating media change
sl@0
   934
	if(iSimulatedMediaState != DPBusSocket::EPeriphBusMediaNormal)
sl@0
   935
		{
sl@0
   936
		doorState     = (iSimulatedMediaState == DPBusSocket::EPeriphBusDoorOpen) ? EDoorOpen : EDoorClosed;
sl@0
   937
		cardIsPresent = (iSimulatedMediaState == DPBusSocket::EPeriphBusMediaPresent);
sl@0
   938
		}
sl@0
   939
#endif
sl@0
   940
sl@0
   941
	if (!(doorState==EDoorClosed && cardIsPresent))
sl@0
   942
		ChangeState(EPBusCardAbsent,KErrNotReady);
sl@0
   943
	else if (iState==EPBusPowerUpPending)
sl@0
   944
		{
sl@0
   945
		// if a power-up request is pending, process it now
sl@0
   946
		ChangeState(EPBusPoweringUp,KErrNone);
sl@0
   947
		InitiatePowerUpSequence();
sl@0
   948
		}
sl@0
   949
	else
sl@0
   950
		ChangeState(EPBusOff,KErrNotReady);
sl@0
   951
	iStandby = EFalse;
sl@0
   952
	iPowerHandler->PowerUpDone();
sl@0
   953
	OstTraceFunctionExit1( DPBUSSOCKET_DOPOWERUP_EXIT, this );
sl@0
   954
	}
sl@0
   955
sl@0
   956
void DPBusSocket::DoPowerDown()
sl@0
   957
//
sl@0
   958
// Called by DPowerManager on transition to standby
sl@0
   959
//
sl@0
   960
	{
sl@0
   961
	OstTraceFunctionEntry1( DPBUSSOCKET_DOPOWERDOWN_ENTRY, this );
sl@0
   962
	__KTRACE_OPT2(KPBUS1,KPOWER,Kern::Printf("DPBusSocket(%d)::DoPowerDown state %d",iSocketNumber,iState));
sl@0
   963
	OstTraceExt2(TRACE_INTERNALS, DPBUSSOCKET_DOPOWERDOWN1, "iSocketNumber=%d; iState=%d",iSocketNumber,iState );
sl@0
   964
	__PM_ASSERT(!iStandby);
sl@0
   965
	if (iPostponeCount)
sl@0
   966
		{
sl@0
   967
		iPostponedEvents |= EPowerDown;
sl@0
   968
		__KTRACE_OPT(KPBUS1,Kern::Printf("Power down postponed"));
sl@0
   969
		OstTrace0(TRACE_INTERNALS, DPBUSSOCKET_DOPOWERDOWN2, "Power down postponed");
sl@0
   970
		return;
sl@0
   971
		}
sl@0
   972
	iPostponedEvents &= ~EPowerDown;
sl@0
   973
	switch (iState)
sl@0
   974
		{
sl@0
   975
		case EPBusPoweringUp:
sl@0
   976
		case EPBusOn:
sl@0
   977
		case EPBusPsuFault:
sl@0
   978
			ChangeState(EPBusOff,KErrNone);
sl@0
   979
		case EPBusCardAbsent:
sl@0
   980
		case EPBusOff:
sl@0
   981
		case EPBusPowerUpPending:
sl@0
   982
			break;
sl@0
   983
		default:
sl@0
   984
			Panic(EEmergencyPowerDownInvalidState);
sl@0
   985
		}
sl@0
   986
		
sl@0
   987
	if(iRequestPowerDownCount == 0)
sl@0
   988
		{
sl@0
   989
		ResetSocket(EFalse);
sl@0
   990
		iStandby = ETrue;
sl@0
   991
		iPowerHandler->PowerDownDone();
sl@0
   992
		}
sl@0
   993
	OstTraceFunctionExit1( DUP1_DPBUSSOCKET_DOPOWERDOWN_EXIT, this );
sl@0
   994
	}
sl@0
   995
sl@0
   996
	/**
sl@0
   997
	Notifies the socket that we are deferring this power down event. 
sl@0
   998
	The function increments the iRequestPowerDownCount reference count
sl@0
   999
	@see DPBusSocket::PowerDownComplete()
sl@0
  1000
	*/
sl@0
  1001
EXPORT_C void DPBusSocket::RequestAsyncPowerDown()
sl@0
  1002
	{
sl@0
  1003
	__KTRACE_OPT(KPBUS1,Kern::Printf("DPBusSocket::RequestAsyncPowerDown"));
sl@0
  1004
	OstTrace0(TRACE_INTERNALS, DPBUSSOCKET_REQUESTASYNCPOWERDOWN1, "DPBusSocket::RequestAsyncPowerDown");
sl@0
  1005
    __e32_atomic_add_ord32(&iRequestPowerDownCount, 1);
sl@0
  1006
	__KTRACE_OPT(KPBUS1,Kern::Printf("   >> count=%d", iRequestPowerDownCount));
sl@0
  1007
	OstTrace1(TRACE_INTERNALS, DPBUSSOCKET_REQUESTASYNCPOWERDOWN2, "iRequestPowerDownCount=%d", iRequestPowerDownCount);
sl@0
  1008
	}
sl@0
  1009
sl@0
  1010
	/**
sl@0
  1011
	This function power down the PBus. Powers down the PBus if iRequestPowerDownCount is equal to 1.
sl@0
  1012
	@see DPBusSocket::RequestAsyncPowerDown()
sl@0
  1013
	@see iRequestPowerDownCount
sl@0
  1014
	*/
sl@0
  1015
EXPORT_C void DPBusSocket::PowerDownComplete()
sl@0
  1016
	{
sl@0
  1017
	__KTRACE_OPT(KPBUS1,Kern::Printf("DPBusSocket::PowerDownComplete"));
sl@0
  1018
	OstTrace0(TRACE_INTERNALS, DPBUSSOCKET_POWERDOWNCOMPLETE, "DPBusSocket::PowerDownComplete");
sl@0
  1019
	if (__e32_atomic_tas_ord32(&iRequestPowerDownCount, 1, -1, 0) == 1)
sl@0
  1020
		{
sl@0
  1021
		__KTRACE_OPT(KPBUS1,Kern::Printf("   > Signalling Power Down (deferred)"));
sl@0
  1022
		OstTrace0(TRACE_INTERNALS, DPBUSSOCKET_POWERDOWNCOMPLETE2, "Signalling Power Down (deferred)");
sl@0
  1023
		DoPowerDown();
sl@0
  1024
		}
sl@0
  1025
	__KTRACE_OPT(KPBUS1,Kern::Printf("   >> count=%d", iRequestPowerDownCount));
sl@0
  1026
	OstTrace1(TRACE_INTERNALS, DPBUSSOCKET_POWERDOWNCOMPLETE3, "iRequestPowerDownCount=%d", iRequestPowerDownCount);
sl@0
  1027
	}
sl@0
  1028
	
sl@0
  1029
	/**
sl@0
  1030
	This function is called by the local media device driver to force a remount of the media device.
sl@0
  1031
	@see DMediaChangeBase::ForceMediaChange()
sl@0
  1032
	*/
sl@0
  1033
EXPORT_C void DPBusSocket::ForceMediaChange()
sl@0
  1034
	{
sl@0
  1035
	OstTrace1(TRACE_INTERNALS, DPBUSSOCKET_FORCEMEDIACHANGE, "iSocketNumber=%d", iSocketNumber);
sl@0
  1036
	iMediaChange->ForceMediaChange();
sl@0
  1037
	}
sl@0
  1038
sl@0
  1039
void DPBusSocket::MediaChangeEvent(TBool aDoorOpened)
sl@0
  1040
//
sl@0
  1041
// Called in high-priority DFC
sl@0
  1042
//
sl@0
  1043
	{
sl@0
  1044
	__KTRACE_OPT(KPBUS1,Kern::Printf(">DPBusSocket(%d)::MediaChangeEvent %d state %d",iSocketNumber,aDoorOpened,iState));
sl@0
  1045
	OstTraceExt3(TRACE_INTERNALS, DPBUSSOCKET_MEDIACHANGEEVENT, "iSocketNumber=%d; aDoorOpened=%d; iState=%d",iSocketNumber,aDoorOpened,iState);
sl@0
  1046
	iDoorOpened=aDoorOpened;
sl@0
  1047
	iMediaChangeDfc.Enque();
sl@0
  1048
	}
sl@0
  1049
sl@0
  1050
void DPBusSocket::DoorOpenEvent()
sl@0
  1051
//
sl@0
  1052
// Called in socket thread
sl@0
  1053
//
sl@0
  1054
	{
sl@0
  1055
	OstTraceFunctionEntry1( DPBUSSOCKET_DOOROPENEVENT_ENTRY, this );
sl@0
  1056
	
sl@0
  1057
	__KTRACE_OPT(KPBUS1,Kern::Printf(">DPBusSocket(%d)::DoorOpenEvent state %d",iSocketNumber,iState));
sl@0
  1058
	OstTraceExt2(TRACE_INTERNALS, DPBUSSOCKET_DOOROPENEVENT1, "iSocketNumber=%d; iState=%d",iSocketNumber,iState);
sl@0
  1059
sl@0
  1060
	if (iPostponeCount)
sl@0
  1061
		{
sl@0
  1062
		iPostponedEvents |= EMediaChange;
sl@0
  1063
		__KTRACE_OPT(KPBUS1,Kern::Printf("Media change postponed"));
sl@0
  1064
		OstTraceFunctionExit1( DPBUSSOCKET_DOOROPENEVENT_EXIT1, this );
sl@0
  1065
		return;
sl@0
  1066
		}
sl@0
  1067
	iPostponedEvents &= ~EMediaChange;
sl@0
  1068
sl@0
  1069
    // notify all clients of media change
sl@0
  1070
	ChangeState(EPBusCardAbsent,KErrNotReady);
sl@0
  1071
	
sl@0
  1072
	// power down the socket
sl@0
  1073
	ResetSocket(ETrue);
sl@0
  1074
sl@0
  1075
	// get the media state befor calling AcknowledgeEvent() as the PSL may start a debounce 
sl@0
  1076
	// timer on this call and return EDoorOpen while the timer is active....
sl@0
  1077
	TMediaState mediaState = MediaState();
sl@0
  1078
sl@0
  1079
#ifdef __ENABLE_SIMULATED_MEDIA_CHANGE
sl@0
  1080
	// Only acknowledge the media change to the PSL if we are running in normal mode
sl@0
  1081
	if(iSimulatedMediaState == EPeriphBusMediaNormal)
sl@0
  1082
		iMediaChange->AcknowledgeEvent(ETrue);
sl@0
  1083
#else
sl@0
  1084
	iMediaChange->AcknowledgeEvent(ETrue);
sl@0
  1085
#endif
sl@0
  1086
sl@0
  1087
	// If there are multiple doors, then it is assumed that :
sl@0
  1088
	// - DMediaChangeBase::MediaState() will return EDoorClosed if ANY door is closed, and 
sl@0
  1089
	// - DPBusSocket::CardIsPresent() will return ETrue if ANY card is present
sl@0
  1090
	// so that if, for example,  one door is  open and one door closed, then the bus will 
sl@0
  1091
	// power down and then up again when one of the cards is next accessed.
sl@0
  1092
	// NB This doesn't worrk for a simulated media change since this doesn't affect the 
sl@0
  1093
	// PSL's door state
sl@0
  1094
#ifdef __ENABLE_SIMULATED_MEDIA_CHANGE
sl@0
  1095
	if ((iSimulatedMediaState == EPeriphBusMediaNormal) &&
sl@0
  1096
		(mediaState == EDoorClosed && CardIsPresent()))
sl@0
  1097
#else
sl@0
  1098
	if (mediaState == EDoorClosed && CardIsPresent())
sl@0
  1099
#endif
sl@0
  1100
		{
sl@0
  1101
		__KTRACE_OPT(KPBUS1,Kern::Printf("At least 1 door still closed"));;
sl@0
  1102
		OstTrace0(TRACE_INTERNALS, DPBUSSOCKET_DOOROPENEVENT2 , "At least 1 door still closed");
sl@0
  1103
		ChangeState(EPBusOff,KErrNotReady);
sl@0
  1104
		}
sl@0
  1105
sl@0
  1106
	OstTraceFunctionExit1( DPBUSSOCKET_DOOROPENEVENT_EXIT2, this );
sl@0
  1107
	}
sl@0
  1108
sl@0
  1109
void DPBusSocket::DoorCloseEvent()
sl@0
  1110
	{
sl@0
  1111
	OstTraceFunctionEntry1( DPBUSSOCKET_DOORCLOSEEVENT_ENTRY, this );
sl@0
  1112
	__KTRACE_OPT(KPBUS1,Kern::Printf(">DPBusSocket(%d)::DoorCloseEvent state %d",iSocketNumber,iState));
sl@0
  1113
	OstTraceExt2(TRACE_INTERNALS, DPBUSSOCKET_DOORCLOSEEVENT , "iSocketNumber=%d; iState=%d",iSocketNumber,iState);
sl@0
  1114
sl@0
  1115
	// NB If there are multiple doors then the bus may already be powererd up, 
sl@0
  1116
	// so it's not possible to determine the bus state.
sl@0
  1117
	//if (iState!=EPBusCardAbsent)
sl@0
  1118
	//	Panic(EDoorCloseInvalidState);
sl@0
  1119
sl@0
  1120
	// door has been closed - check for a card
sl@0
  1121
sl@0
  1122
	TBool cardIsPresent = CardIsPresent();
sl@0
  1123
sl@0
  1124
#ifdef __ENABLE_SIMULATED_MEDIA_CHANGE
sl@0
  1125
	// Override the default drive state if we are simulating the media state
sl@0
  1126
	if((iSimulatedMediaState == EPeriphBusDoorOpen) || (iSimulatedMediaState == EPeriphBusMediaRemoved))
sl@0
  1127
		cardIsPresent = EFalse;
sl@0
  1128
#endif
sl@0
  1129
sl@0
  1130
	
sl@0
  1131
	if (cardIsPresent)
sl@0
  1132
		{
sl@0
  1133
		if (iState == EPBusCardAbsent)
sl@0
  1134
			{
sl@0
  1135
			// Notifies clients of a media change
sl@0
  1136
			ChangeState(EPBusOff,KErrNotReady);
sl@0
  1137
			}
sl@0
  1138
		else	// if there's already a card present (iState != EPBusCardAbsent), power the bus off and on
sl@0
  1139
			{
sl@0
  1140
			// Notify clients of a media change, cancel any outstanding requests, close media driver(s)
sl@0
  1141
			// and set the DPrimaryMediaBase's state to EClosed to force a subsequent power-up
sl@0
  1142
			ChangeState(EPBusCardAbsent,KErrNotReady);
sl@0
  1143
			ChangeState(EPBusOff,KErrNotReady);
sl@0
  1144
			// NB Don't power down the socket when iState == EPBusCardAbsent as this can take a small amount of time 
sl@0
  1145
			// and will cause DPBusPrimaryMedia::QuickCheckStatus() to return KErrNotReady in the meantime: this will 
sl@0
  1146
			// result in any requests to the DPrimaryMediaBase being completed IMMEDIATELY with KErrNotReady, i.e. the
sl@0
  1147
			// requests won't be queued until the power up completes.
sl@0
  1148
			ResetSocket(ETrue);
sl@0
  1149
			}
sl@0
  1150
		}
sl@0
  1151
sl@0
  1152
#ifdef __ENABLE_SIMULATED_MEDIA_CHANGE
sl@0
  1153
	// Only acknowledge the media change to the PSL if we are running in normal mode
sl@0
  1154
	if(iSimulatedMediaState == EPeriphBusMediaNormal)
sl@0
  1155
		iMediaChange->AcknowledgeEvent(EFalse);
sl@0
  1156
#else
sl@0
  1157
	iMediaChange->AcknowledgeEvent(EFalse);
sl@0
  1158
#endif
sl@0
  1159
	OstTraceFunctionExit1( DPBUSSOCKET_DOORCLOSEEVENT_EXIT, this );
sl@0
  1160
	}
sl@0
  1161
	/**
sl@0
  1162
	Gets pointer to the PBus Socket corresponding to the opened logical unit.
sl@0
  1163
	@param anId	logical id of the PBus Socket.	
sl@0
  1164
	@return Pointer to the PBusSocket for valid anId, else NULL.
sl@0
  1165
	*/
sl@0
  1166
EXPORT_C DPBusSocket* DPBusSocket::SocketFromId(TInt anId)
sl@0
  1167
	{
sl@0
  1168
	OstTraceFunctionEntry0( DPBUSSOCKET_SOCKETFROMID_ENTRY );
sl@0
  1169
	if (anId>=0 && anId<KMaxPBusSockets)
sl@0
  1170
	    {
sl@0
  1171
		OstTraceFunctionExit0( DPBUSSOCKET_SOCKETFROMID_EXIT1 );
sl@0
  1172
		return TheSockets[anId];
sl@0
  1173
	    }
sl@0
  1174
	OstTraceFunctionExit0( DPBUSSOCKET_SOCKETFROMID_EXIT2 );
sl@0
  1175
	return NULL;
sl@0
  1176
	}
sl@0
  1177
sl@0
  1178
	/**
sl@0
  1179
	Default implementation for handling debug functionality.
sl@0
  1180
	This function can only be used if __ENABLE_SIMULATED_MEDIA_CHANGE is defined. 
sl@0
  1181
	Otherwise, this method is not implemented and it always returns KErrNotSupported.
sl@0
  1182
sl@0
  1183
	@param aFunction	refer to TPBusDebugFunction
sl@0
  1184
	@param aParam1	Simulated media state.
sl@0
  1185
	@param aParam2 Not used in this method.
sl@0
  1186
	@return  KErrNone - if successful, otherwise one of the other system-wide error 
sl@0
  1187
	codes including: 
sl@0
  1188
			KErrNotSupported - if aFunction is invalid or __ENABLE_SIMULATED_MEDIA_CHANGE 
sl@0
  1189
	is not defined,
sl@0
  1190
			KErrArgument -  if aParam1 does not corresponds to TPBusSimulateMediaState.
sl@0
  1191
	@see TPBusSimulateMediaState
sl@0
  1192
	@see TPBusDebugFunction
sl@0
  1193
	*/
sl@0
  1194
EXPORT_C TInt DPBusSocket::ControlIO(TInt aFunction, TAny* aParam1, TAny* /*aParam2*/)
sl@0
  1195
	{
sl@0
  1196
	OstTraceExt3(TRACE_FLOW, DPBUSSOCKET_CONTROLIO_ENTRY,"DPBusSocket::ControlIO;aFunction=%d;aParam1=%d;this=%x", (TInt) aFunction, (TInt) aParam1, (TUint) this);
sl@0
  1197
	TInt err = KErrNone;
sl@0
  1198
sl@0
  1199
	switch(aFunction)
sl@0
  1200
		{
sl@0
  1201
		case EControlMediaState:
sl@0
  1202
			//
sl@0
  1203
			// EOverrideMediaState - Set the media state manually for simulation purposes.
sl@0
  1204
			//	- aParam1 : Simulated Media State (TPBusSimulateMediaState)
sl@0
  1205
			//
sl@0
  1206
			{
sl@0
  1207
#ifdef __ENABLE_SIMULATED_MEDIA_CHANGE
sl@0
  1208
			TUint16 newState = (TUint16)(TInt)aParam1;
sl@0
  1209
			if(newState != iSimulatedMediaState)
sl@0
  1210
				{
sl@0
  1211
				iSimulatedMediaState = newState;
sl@0
  1212
	            OstTrace1(TRACE_INTERNALS, DPBUSSOCKET_CONTROLIO , "iSimulatedMediaState=%d",iSimulatedMediaState);
sl@0
  1213
				switch(iSimulatedMediaState)
sl@0
  1214
					{
sl@0
  1215
					case EPeriphBusMediaNormal:
sl@0
  1216
						//
sl@0
  1217
						// Normal state
sl@0
  1218
						// - Signal that the door is open and generate a media change.
sl@0
  1219
						//
sl@0
  1220
						iMediaChange->MediaChangeEvent(ETrue);
sl@0
  1221
						break;
sl@0
  1222
					
sl@0
  1223
					case EPeriphBusDoorOpen:
sl@0
  1224
						//
sl@0
  1225
						// Simulated door open or back to normal state.
sl@0
  1226
						// - Signal that the door is open and generate a media change.
sl@0
  1227
						//
sl@0
  1228
						MediaChangeEvent(ETrue);
sl@0
  1229
						break;
sl@0
  1230
					
sl@0
  1231
					case EPeriphBusMediaRemoved:
sl@0
  1232
					case EPeriphBusMediaPresent:
sl@0
  1233
						//
sl@0
  1234
						// Simulated door close with media present or absent
sl@0
  1235
						// - Signal that the door is closed.
sl@0
  1236
						//
sl@0
  1237
						MediaChangeEvent(EFalse);
sl@0
  1238
						break;
sl@0
  1239
sl@0
  1240
					case EPeriphBusMediaDoubleDoorOpen:
sl@0
  1241
						// simulate 2 door open interrupts 
sl@0
  1242
						iSimulatedMediaState = EPeriphBusMediaNormal;
sl@0
  1243
						iMediaChange->MediaChangeEvent(ETrue);
sl@0
  1244
						iMediaChange->MediaChangeEvent(ETrue);
sl@0
  1245
						break;
sl@0
  1246
sl@0
  1247
					default:
sl@0
  1248
						//
sl@0
  1249
						// Unsupported media state
sl@0
  1250
						//
sl@0
  1251
						err = KErrArgument;
sl@0
  1252
						break;
sl@0
  1253
					}
sl@0
  1254
				}
sl@0
  1255
#else
sl@0
  1256
			aParam1 = aParam1;
sl@0
  1257
			err = KErrNotSupported;
sl@0
  1258
#endif
sl@0
  1259
			break;
sl@0
  1260
			}
sl@0
  1261
sl@0
  1262
		default:
sl@0
  1263
			err = KErrNotSupported;
sl@0
  1264
			break;
sl@0
  1265
		}
sl@0
  1266
sl@0
  1267
	OstTraceFunctionExitExt( DPBUSSOCKET_CONTROLIO_EXIT, this, err );
sl@0
  1268
	return err;
sl@0
  1269
	}
sl@0
  1270
sl@0
  1271
/********************************************
sl@0
  1272
 * Extension entry point
sl@0
  1273
 ********************************************/
sl@0
  1274
sl@0
  1275
GLDEF_C TInt KernelModuleEntry(TInt aReason)
sl@0
  1276
	{
sl@0
  1277
	if (aReason==KModuleEntryReasonExtensionInit0 || aReason==KModuleEntryReasonExtensionInit1)
sl@0
  1278
		return KErrNone;
sl@0
  1279
	return KErrArgument;
sl@0
  1280
	}
sl@0
  1281
sl@0
  1282
sl@0
  1283
sl@0
  1284
sl@0
  1285