os/kernelhwsrv/kernel/eka/drivers/usbcc/misc.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 2000-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/usbcc/misc.cpp
sl@0
    15
// Platform independent layer (PIL) of the USB Device controller driver:
sl@0
    16
// Implementations of misc. classes defined in usbc.h.
sl@0
    17
// 
sl@0
    18
//
sl@0
    19
sl@0
    20
/**
sl@0
    21
 @file misc.cpp
sl@0
    22
 @internalTechnology
sl@0
    23
*/
sl@0
    24
sl@0
    25
#include <drivers/usbc.h>
sl@0
    26
sl@0
    27
sl@0
    28
/** Helper function for logical endpoints and endpoint descriptors:
sl@0
    29
	Split single Ep size into separate FS/HS sizes.
sl@0
    30
	This function modifies its arguments.
sl@0
    31
 */
sl@0
    32
TInt TUsbcEndpointInfo::AdjustEpSizes(TInt& aEpSize_Fs, TInt& aEpSize_Hs) const
sl@0
    33
	{
sl@0
    34
	if (iType == KUsbEpTypeBulk)
sl@0
    35
		{
sl@0
    36
		// FS: [8|16|32|64] HS: 512
sl@0
    37
		if (iSize < 64)
sl@0
    38
			{
sl@0
    39
			aEpSize_Fs = iSize;
sl@0
    40
			}
sl@0
    41
		else
sl@0
    42
			{
sl@0
    43
			aEpSize_Fs = 64;
sl@0
    44
			}
sl@0
    45
		aEpSize_Hs = 512;
sl@0
    46
		}
sl@0
    47
	else if (iType == KUsbEpTypeInterrupt)
sl@0
    48
		{
sl@0
    49
		// FS: [0..64] HS: [0..1024]
sl@0
    50
		if (iSize < 64)
sl@0
    51
			{
sl@0
    52
			aEpSize_Fs = iSize;
sl@0
    53
			}
sl@0
    54
		else
sl@0
    55
			{
sl@0
    56
			aEpSize_Fs = 64;
sl@0
    57
			}
sl@0
    58
		aEpSize_Hs = iSize;
sl@0
    59
		}
sl@0
    60
	else if (iType == KUsbEpTypeIsochronous)
sl@0
    61
		{
sl@0
    62
		// FS: [0..1023] HS: [0..1024]
sl@0
    63
		if (iSize < 1023)
sl@0
    64
			{
sl@0
    65
			aEpSize_Fs = iSize;
sl@0
    66
			}
sl@0
    67
		else
sl@0
    68
			{
sl@0
    69
			aEpSize_Fs = 1023;
sl@0
    70
			}
sl@0
    71
		aEpSize_Hs = iSize;
sl@0
    72
		}
sl@0
    73
	else if (iType == KUsbEpTypeControl)
sl@0
    74
		{
sl@0
    75
		// FS: [8|16|32|64] HS: 64
sl@0
    76
		if (iSize < 64)
sl@0
    77
			{
sl@0
    78
			aEpSize_Fs = iSize;
sl@0
    79
			}
sl@0
    80
		else
sl@0
    81
			{
sl@0
    82
			aEpSize_Fs = 64;
sl@0
    83
			}
sl@0
    84
		aEpSize_Hs = 64;
sl@0
    85
		}
sl@0
    86
	else
sl@0
    87
		{
sl@0
    88
		aEpSize_Fs = aEpSize_Hs = 0;
sl@0
    89
		return KErrGeneral;
sl@0
    90
		}
sl@0
    91
sl@0
    92
	// For the reason of the following checks see Table 9-14. "Allowed wMaxPacketSize
sl@0
    93
	// Values for Different Numbers of Transactions per Microframe".
sl@0
    94
	if ((iType == KUsbEpTypeInterrupt) || (iType == KUsbEpTypeIsochronous))
sl@0
    95
		{
sl@0
    96
		if (iTransactions == 1)
sl@0
    97
			{
sl@0
    98
			if (aEpSize_Hs < 513)
sl@0
    99
				{
sl@0
   100
				__KTRACE_OPT(KPANIC, Kern::Printf("  Warning: Ep size too small: %d < 513. Correcting...",
sl@0
   101
												  aEpSize_Hs));
sl@0
   102
				aEpSize_Hs = 513;
sl@0
   103
				}
sl@0
   104
			}
sl@0
   105
		else if (iTransactions == 2)
sl@0
   106
			{
sl@0
   107
			if (aEpSize_Hs < 683)
sl@0
   108
				{
sl@0
   109
				__KTRACE_OPT(KPANIC, Kern::Printf("  Warning: Ep size too small: %d < 683. Correcting...",
sl@0
   110
												  aEpSize_Hs));
sl@0
   111
				aEpSize_Hs = 683;
sl@0
   112
				}
sl@0
   113
			}
sl@0
   114
		}
sl@0
   115
	return KErrNone;
sl@0
   116
	}
sl@0
   117
sl@0
   118
sl@0
   119
/** Helper function for logical endpoints and endpoint descriptors:
sl@0
   120
	If not set, assign a valid and meaningful value to iInterval_Hs, deriving from iInterval.
sl@0
   121
	This function modifies the objects's data member(s).
sl@0
   122
 */
sl@0
   123
TInt TUsbcEndpointInfo::AdjustPollInterval()
sl@0
   124
	{
sl@0
   125
	if (iInterval_Hs != -1)
sl@0
   126
		{
sl@0
   127
		// Already done.
sl@0
   128
		return KErrNone;
sl@0
   129
		}
sl@0
   130
	if ((iType == KUsbEpTypeBulk) || (iType == KUsbEpTypeControl))
sl@0
   131
		{
sl@0
   132
		// Valid range: 0..255 (maximum NAK rate).
sl@0
   133
		// (The host controller will probably ignore this value though -
sl@0
   134
		//  see the last sentence of section 9.6.6 for details.)
sl@0
   135
		iInterval_Hs = 255;
sl@0
   136
		}
sl@0
   137
	else if (iType == KUsbEpTypeInterrupt)
sl@0
   138
		{
sl@0
   139
		// HS interval = 2^(iInterval_Hs-1) with a valid iInterval_Hs range of 1..16.
sl@0
   140
		// The following table shows the mapping of HS values to actual intervals (and
sl@0
   141
		// thus FS values) for the range of possible FS values (1..255).
sl@0
   142
		// There is not always a 1:1 mapping possible, but we want at least to make sure
sl@0
   143
		// that the HS polling interval is never longer than the FS one (except for 255).
sl@0
   144
		//
sl@0
   145
		// 1 = 1
sl@0
   146
		// 2 = 2
sl@0
   147
		// 3 = 4
sl@0
   148
		// 4 = 8
sl@0
   149
		// 5 = 16
sl@0
   150
		// 6 = 32
sl@0
   151
		// 7 = 64
sl@0
   152
		// 8 = 128
sl@0
   153
		// 9 = 256
sl@0
   154
		if (iInterval == 255)
sl@0
   155
			iInterval_Hs = 9;
sl@0
   156
		else if (iInterval >= 128)
sl@0
   157
			iInterval_Hs = 8;
sl@0
   158
		else if (iInterval >= 64)
sl@0
   159
			iInterval_Hs = 7;
sl@0
   160
		else if (iInterval >= 32)
sl@0
   161
			iInterval_Hs = 6;
sl@0
   162
		else if (iInterval >= 16)
sl@0
   163
			iInterval_Hs = 5;
sl@0
   164
		else if (iInterval >= 8)
sl@0
   165
			iInterval_Hs = 4;
sl@0
   166
		else if (iInterval >= 4)
sl@0
   167
			iInterval_Hs = 3;
sl@0
   168
		else if (iInterval >= 2)
sl@0
   169
			iInterval_Hs = 2;
sl@0
   170
		else if (iInterval == 1)
sl@0
   171
			iInterval_Hs = 1;
sl@0
   172
		else
sl@0
   173
			{
sl@0
   174
			// iInterval wasn't set properly by the user
sl@0
   175
			iInterval_Hs = 1;
sl@0
   176
			return KErrGeneral;
sl@0
   177
			}
sl@0
   178
		}
sl@0
   179
	else if (iType == KUsbEpTypeIsochronous)
sl@0
   180
		{
sl@0
   181
		// Interpretation is the same for FS and HS.
sl@0
   182
		iInterval_Hs = iInterval;
sl@0
   183
		}
sl@0
   184
	else
sl@0
   185
		{
sl@0
   186
		// '1' is a valid value for all endpoint types...
sl@0
   187
		iInterval_Hs = 1;
sl@0
   188
		return KErrGeneral;
sl@0
   189
		}
sl@0
   190
	return KErrNone;
sl@0
   191
	}
sl@0
   192
sl@0
   193
sl@0
   194
TUsbcPhysicalEndpoint::TUsbcPhysicalEndpoint()
sl@0
   195
	: iEndpointAddr(0), iIfcNumber(NULL), iLEndpoint(NULL), iSettingReserve(EFalse), iHalt(EFalse)
sl@0
   196
	{
sl@0
   197
	__KTRACE_OPT(KUSB, Kern::Printf("TUsbcPhysicalEndpoint::TUsbcPhysicalEndpoint"));
sl@0
   198
	}
sl@0
   199
sl@0
   200
sl@0
   201
TInt TUsbcPhysicalEndpoint::TypeAvailable(TUint aType) const
sl@0
   202
	{
sl@0
   203
	__KTRACE_OPT(KUSB, Kern::Printf("TUsbcPhysicalEndpoint::TypeAvailable"));
sl@0
   204
	switch (aType)
sl@0
   205
		{
sl@0
   206
	case KUsbEpTypeControl:
sl@0
   207
		return (iCaps.iTypesAndDir & KUsbEpTypeControl);
sl@0
   208
	case KUsbEpTypeIsochronous:
sl@0
   209
		return (iCaps.iTypesAndDir & KUsbEpTypeIsochronous);
sl@0
   210
	case KUsbEpTypeBulk:
sl@0
   211
		return (iCaps.iTypesAndDir & KUsbEpTypeBulk);
sl@0
   212
	case KUsbEpTypeInterrupt:
sl@0
   213
		return (iCaps.iTypesAndDir & KUsbEpTypeInterrupt);
sl@0
   214
	default:
sl@0
   215
		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: invalid EP type: %d", aType));
sl@0
   216
		return 0;
sl@0
   217
		}
sl@0
   218
	}
sl@0
   219
sl@0
   220
sl@0
   221
TInt TUsbcPhysicalEndpoint::DirAvailable(TUint aDir) const
sl@0
   222
	{
sl@0
   223
	__KTRACE_OPT(KUSB, Kern::Printf("TUsbcPhysicalEndpoint::DirAvailable"));
sl@0
   224
	switch (aDir)
sl@0
   225
		{
sl@0
   226
	case KUsbEpDirIn:
sl@0
   227
		return (iCaps.iTypesAndDir & KUsbEpDirIn);
sl@0
   228
	case KUsbEpDirOut:
sl@0
   229
		return (iCaps.iTypesAndDir & KUsbEpDirOut);
sl@0
   230
	default:
sl@0
   231
		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: invalid EP direction: %d", aDir));
sl@0
   232
		return 0;
sl@0
   233
		}
sl@0
   234
	}
sl@0
   235
sl@0
   236
sl@0
   237
TInt TUsbcPhysicalEndpoint::EndpointSuitable(const TUsbcEndpointInfo* aEpInfo, TInt aIfcNumber) const
sl@0
   238
	{
sl@0
   239
	__KTRACE_OPT(KUSB, Kern::Printf("TUsbcPhysicalEndpoint::EndpointSuitable"));
sl@0
   240
	__KTRACE_OPT(KUSB, Kern::Printf("  looking for EP: type=0x%x dir=0x%x size=%d (ifc_num=%d)",
sl@0
   241
									aEpInfo->iType, aEpInfo->iDir, aEpInfo->iSize, aIfcNumber));
sl@0
   242
	if (iSettingReserve)
sl@0
   243
		{
sl@0
   244
		__KTRACE_OPT(KUSB, Kern::Printf("  -> setting conflict"));
sl@0
   245
		return 0;
sl@0
   246
		}
sl@0
   247
	// (aIfcNumber == -1) means the ep is for a new default interface setting
sl@0
   248
	else if (iIfcNumber && (*iIfcNumber != aIfcNumber))
sl@0
   249
		{
sl@0
   250
		// If this endpoint has already been claimed (iIfcNumber != NULL),
sl@0
   251
		// but by a different interface(-set) than the currently looking one
sl@0
   252
		// (*iIfcNumber != aIfcNumber), then it's not available.
sl@0
   253
		// This works because we can assign the same physical endpoint
sl@0
   254
		// to different alternate settings of the *same* interface, and
sl@0
   255
		// because we check for available endpoints for every alternate setting
sl@0
   256
		// as a whole.
sl@0
   257
		__KTRACE_OPT(KUSB, Kern::Printf("  -> ifc conflict"));
sl@0
   258
		return 0;
sl@0
   259
		}
sl@0
   260
	else if (!TypeAvailable(aEpInfo->iType))
sl@0
   261
		{
sl@0
   262
		__KTRACE_OPT(KUSB, Kern::Printf("  -> type conflict"));
sl@0
   263
		return 0;
sl@0
   264
		}
sl@0
   265
	else if (!DirAvailable(aEpInfo->iDir))
sl@0
   266
		{
sl@0
   267
		__KTRACE_OPT(KUSB, Kern::Printf("  -> direction conflict"));
sl@0
   268
		return 0;
sl@0
   269
		}
sl@0
   270
	else if (!(iCaps.iSizes & PacketSize2Mask(aEpInfo->iSize)) && !(iCaps.iSizes & KUsbEpSizeCont))
sl@0
   271
		{
sl@0
   272
		__KTRACE_OPT(KUSB, Kern::Printf("  -> size conflict"));
sl@0
   273
		return 0;
sl@0
   274
		}
sl@0
   275
	else
sl@0
   276
		return 1;
sl@0
   277
	}
sl@0
   278
sl@0
   279
sl@0
   280
TUsbcPhysicalEndpoint::~TUsbcPhysicalEndpoint()
sl@0
   281
	{
sl@0
   282
	__KTRACE_OPT(KUSB, Kern::Printf("TUsbcPhysicalEndpoint::~TUsbcPhysicalEndpoint()"));
sl@0
   283
	iLEndpoint = NULL;
sl@0
   284
	}
sl@0
   285
sl@0
   286
sl@0
   287
TUsbcLogicalEndpoint::TUsbcLogicalEndpoint(DUsbClientController* aController, TUint aEndpointNum,
sl@0
   288
										   const TUsbcEndpointInfo& aEpInfo, TUsbcInterface* aInterface,
sl@0
   289
										   TUsbcPhysicalEndpoint* aPEndpoint)
sl@0
   290
	: iController(aController), iLEndpointNum(aEndpointNum), iInfo(aEpInfo), iInterface(aInterface),
sl@0
   291
	  iPEndpoint(aPEndpoint)
sl@0
   292
	{
sl@0
   293
	__KTRACE_OPT(KUSB, Kern::Printf("TUsbcLogicalEndpoint::TUsbcLogicalEndpoint()"));
sl@0
   294
	//  Adjust FS/HS endpoint sizes
sl@0
   295
	if (iInfo.AdjustEpSizes(iEpSize_Fs, iEpSize_Hs) != KErrNone)
sl@0
   296
		{
sl@0
   297
		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Unknown endpoint type: %d", iInfo.iType));
sl@0
   298
		}
sl@0
   299
	__KTRACE_OPT(KUSB, Kern::Printf("  Now set: iEpSize_Fs=%d iEpSize_Hs=%d (iInfo.iSize=%d)",
sl@0
   300
									iEpSize_Fs, iEpSize_Hs, iInfo.iSize));
sl@0
   301
	//  Adjust HS polling interval
sl@0
   302
	if (iInfo.AdjustPollInterval() != KErrNone)
sl@0
   303
		{
sl@0
   304
		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Unknown ep type (%d) or invalid interval value (%d)",
sl@0
   305
										  iInfo.iType, iInfo.iInterval));
sl@0
   306
		}
sl@0
   307
	__KTRACE_OPT(KUSB, Kern::Printf("  Now set: iInfo.iInterval=%d iInfo.iInterval_Hs=%d",
sl@0
   308
									iInfo.iInterval, iInfo.iInterval_Hs));
sl@0
   309
	// Additional transactions requested on a non High Bandwidth ep?
sl@0
   310
	if ((iInfo.iTransactions > 0) && !aPEndpoint->iCaps.iHighBandwidth)
sl@0
   311
		{
sl@0
   312
		__KTRACE_OPT(KPANIC,
sl@0
   313
					 Kern::Printf("  Warning: Additional transactions requested but not a High Bandwidth ep"));
sl@0
   314
		}
sl@0
   315
	}
sl@0
   316
sl@0
   317
sl@0
   318
TUsbcLogicalEndpoint::~TUsbcLogicalEndpoint()
sl@0
   319
	{
sl@0
   320
	__KTRACE_OPT(KUSB, Kern::Printf("TUsbcLogicalEndpoint::~TUsbcLogicalEndpoint: #%d", iLEndpointNum));
sl@0
   321
	// If the real endpoint this endpoint points to is also used by
sl@0
   322
	// any other logical endpoint in any other setting of this interface
sl@0
   323
	// then we leave the real endpoint marked as used. Otherwise we mark
sl@0
   324
	// it as available (set its ifc number pointer to NULL).
sl@0
   325
	const TInt n = iInterface->iInterfaceSet->iInterfaces.Count();
sl@0
   326
	for (TInt i = 0; i < n; ++i)
sl@0
   327
		{
sl@0
   328
		const TUsbcInterface* const ifc = iInterface->iInterfaceSet->iInterfaces[i];
sl@0
   329
		const TInt m = ifc->iEndpoints.Count();
sl@0
   330
		for (TInt j = 0; j < m; ++j)
sl@0
   331
			{
sl@0
   332
			const TUsbcLogicalEndpoint* const ep = ifc->iEndpoints[j];
sl@0
   333
			if ((ep->iPEndpoint == iPEndpoint) && (ep != this))
sl@0
   334
				{
sl@0
   335
				__KTRACE_OPT(KUSB, Kern::Printf("  Physical endpoint still in use -> we leave it as is"));
sl@0
   336
				return;
sl@0
   337
				}
sl@0
   338
			}
sl@0
   339
		}
sl@0
   340
	__KTRACE_OPT(KUSB, Kern::Printf("  Closing DMA channel"));
sl@0
   341
	const TInt idx = iController->EpAddr2Idx(iPEndpoint->iEndpointAddr);
sl@0
   342
	// If the endpoint doesn't support DMA (now or ever) the next operation will be a no-op.
sl@0
   343
	iController->CloseDmaChannel(idx);
sl@0
   344
	__KTRACE_OPT(KUSB, Kern::Printf("  Setting physical ep 0x%02x ifc number to NULL (was %d)",
sl@0
   345
									iPEndpoint->iEndpointAddr, *iPEndpoint->iIfcNumber));
sl@0
   346
	iPEndpoint->iIfcNumber = NULL;
sl@0
   347
	}
sl@0
   348
sl@0
   349
sl@0
   350
TUsbcInterface::TUsbcInterface(TUsbcInterfaceSet* aIfcSet, TUint8 aSetting, TBool aNoEp0Requests)
sl@0
   351
	: iEndpoints(2), iInterfaceSet(aIfcSet), iSettingCode(aSetting), iNoEp0Requests(aNoEp0Requests)
sl@0
   352
	{
sl@0
   353
	__KTRACE_OPT(KUSB, Kern::Printf("TUsbcInterface::TUsbcInterface()"));
sl@0
   354
	}
sl@0
   355
sl@0
   356
sl@0
   357
TUsbcInterface::~TUsbcInterface()
sl@0
   358
	{
sl@0
   359
	__KTRACE_OPT(KUSB, Kern::Printf("TUsbcInterface::~TUsbcInterface()"));
sl@0
   360
	iEndpoints.ResetAndDestroy();
sl@0
   361
	}
sl@0
   362
sl@0
   363
sl@0
   364
TUsbcInterfaceSet::TUsbcInterfaceSet(const DBase* aClientId, TUint8 aIfcNum)
sl@0
   365
	: iInterfaces(2), iClientId(aClientId), iInterfaceNumber(aIfcNum), iCurrentInterface(0)
sl@0
   366
	{
sl@0
   367
	__KTRACE_OPT(KUSB, Kern::Printf("TUsbcInterfaceSet::TUsbcInterfaceSet()"));
sl@0
   368
	}
sl@0
   369
sl@0
   370
sl@0
   371
TUsbcInterfaceSet::~TUsbcInterfaceSet()
sl@0
   372
	{
sl@0
   373
	__KTRACE_OPT(KUSB, Kern::Printf("TUsbcInterfaceSet::~TUsbcInterfaceSet()"));
sl@0
   374
	iInterfaces.ResetAndDestroy();
sl@0
   375
	}
sl@0
   376
sl@0
   377
sl@0
   378
TUsbcConfiguration::TUsbcConfiguration(TUint8 aConfigVal)
sl@0
   379
	: iInterfaceSets(1), iConfigValue(aConfigVal)			// iInterfaceSets(1): granularity
sl@0
   380
	{
sl@0
   381
	__KTRACE_OPT(KUSB, Kern::Printf("TUsbcConfiguration::TUsbcConfiguration()"));
sl@0
   382
	}
sl@0
   383
sl@0
   384
sl@0
   385
TUsbcConfiguration::~TUsbcConfiguration()
sl@0
   386
	{
sl@0
   387
	__KTRACE_OPT(KUSB, Kern::Printf("TUsbcConfiguration::~TUsbcConfiguration()"));
sl@0
   388
	iInterfaceSets.ResetAndDestroy();
sl@0
   389
	}
sl@0
   390
sl@0
   391
sl@0
   392
_LIT(KDriverName, "Usbcc");
sl@0
   393
sl@0
   394
DUsbcPowerHandler::DUsbcPowerHandler(DUsbClientController* aController)
sl@0
   395
	: DPowerHandler(KDriverName), iController(aController)
sl@0
   396
	{}
sl@0
   397
sl@0
   398
sl@0
   399
void DUsbcPowerHandler::PowerUp()
sl@0
   400
	{
sl@0
   401
	if (iController)
sl@0
   402
		iController->iPowerUpDfc.Enque();
sl@0
   403
	}
sl@0
   404
sl@0
   405
sl@0
   406
void DUsbcPowerHandler::PowerDown(TPowerState)
sl@0
   407
	{
sl@0
   408
	if (iController)
sl@0
   409
		iController->iPowerDownDfc.Enque();
sl@0
   410
	}
sl@0
   411
sl@0
   412
sl@0
   413
// -eof-