Update contrib.
1 // Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32/drivers/usbcc/misc.cpp
15 // Platform independent layer (PIL) of the USB Device controller driver:
16 // Implementations of misc. classes defined in usbc.h.
25 #include <drivers/usbc.h>
28 /** Helper function for logical endpoints and endpoint descriptors:
29 Split single Ep size into separate FS/HS sizes.
30 This function modifies its arguments.
32 TInt TUsbcEndpointInfo::AdjustEpSizes(TInt& aEpSize_Fs, TInt& aEpSize_Hs) const
34 if (iType == KUsbEpTypeBulk)
36 // FS: [8|16|32|64] HS: 512
47 else if (iType == KUsbEpTypeInterrupt)
49 // FS: [0..64] HS: [0..1024]
60 else if (iType == KUsbEpTypeIsochronous)
62 // FS: [0..1023] HS: [0..1024]
73 else if (iType == KUsbEpTypeControl)
75 // FS: [8|16|32|64] HS: 64
88 aEpSize_Fs = aEpSize_Hs = 0;
92 // For the reason of the following checks see Table 9-14. "Allowed wMaxPacketSize
93 // Values for Different Numbers of Transactions per Microframe".
94 if ((iType == KUsbEpTypeInterrupt) || (iType == KUsbEpTypeIsochronous))
96 if (iTransactions == 1)
100 __KTRACE_OPT(KPANIC, Kern::Printf(" Warning: Ep size too small: %d < 513. Correcting...",
105 else if (iTransactions == 2)
107 if (aEpSize_Hs < 683)
109 __KTRACE_OPT(KPANIC, Kern::Printf(" Warning: Ep size too small: %d < 683. Correcting...",
119 /** Helper function for logical endpoints and endpoint descriptors:
120 If not set, assign a valid and meaningful value to iInterval_Hs, deriving from iInterval.
121 This function modifies the objects's data member(s).
123 TInt TUsbcEndpointInfo::AdjustPollInterval()
125 if (iInterval_Hs != -1)
130 if ((iType == KUsbEpTypeBulk) || (iType == KUsbEpTypeControl))
132 // Valid range: 0..255 (maximum NAK rate).
133 // (The host controller will probably ignore this value though -
134 // see the last sentence of section 9.6.6 for details.)
137 else if (iType == KUsbEpTypeInterrupt)
139 // HS interval = 2^(iInterval_Hs-1) with a valid iInterval_Hs range of 1..16.
140 // The following table shows the mapping of HS values to actual intervals (and
141 // thus FS values) for the range of possible FS values (1..255).
142 // There is not always a 1:1 mapping possible, but we want at least to make sure
143 // that the HS polling interval is never longer than the FS one (except for 255).
154 if (iInterval == 255)
156 else if (iInterval >= 128)
158 else if (iInterval >= 64)
160 else if (iInterval >= 32)
162 else if (iInterval >= 16)
164 else if (iInterval >= 8)
166 else if (iInterval >= 4)
168 else if (iInterval >= 2)
170 else if (iInterval == 1)
174 // iInterval wasn't set properly by the user
179 else if (iType == KUsbEpTypeIsochronous)
181 // Interpretation is the same for FS and HS.
182 iInterval_Hs = iInterval;
186 // '1' is a valid value for all endpoint types...
194 TUsbcPhysicalEndpoint::TUsbcPhysicalEndpoint()
195 : iEndpointAddr(0), iIfcNumber(NULL), iLEndpoint(NULL), iSettingReserve(EFalse), iHalt(EFalse)
197 __KTRACE_OPT(KUSB, Kern::Printf("TUsbcPhysicalEndpoint::TUsbcPhysicalEndpoint"));
201 TInt TUsbcPhysicalEndpoint::TypeAvailable(TUint aType) const
203 __KTRACE_OPT(KUSB, Kern::Printf("TUsbcPhysicalEndpoint::TypeAvailable"));
206 case KUsbEpTypeControl:
207 return (iCaps.iTypesAndDir & KUsbEpTypeControl);
208 case KUsbEpTypeIsochronous:
209 return (iCaps.iTypesAndDir & KUsbEpTypeIsochronous);
211 return (iCaps.iTypesAndDir & KUsbEpTypeBulk);
212 case KUsbEpTypeInterrupt:
213 return (iCaps.iTypesAndDir & KUsbEpTypeInterrupt);
215 __KTRACE_OPT(KPANIC, Kern::Printf(" Error: invalid EP type: %d", aType));
221 TInt TUsbcPhysicalEndpoint::DirAvailable(TUint aDir) const
223 __KTRACE_OPT(KUSB, Kern::Printf("TUsbcPhysicalEndpoint::DirAvailable"));
227 return (iCaps.iTypesAndDir & KUsbEpDirIn);
229 return (iCaps.iTypesAndDir & KUsbEpDirOut);
231 __KTRACE_OPT(KPANIC, Kern::Printf(" Error: invalid EP direction: %d", aDir));
237 TInt TUsbcPhysicalEndpoint::EndpointSuitable(const TUsbcEndpointInfo* aEpInfo, TInt aIfcNumber) const
239 __KTRACE_OPT(KUSB, Kern::Printf("TUsbcPhysicalEndpoint::EndpointSuitable"));
240 __KTRACE_OPT(KUSB, Kern::Printf(" looking for EP: type=0x%x dir=0x%x size=%d (ifc_num=%d)",
241 aEpInfo->iType, aEpInfo->iDir, aEpInfo->iSize, aIfcNumber));
244 __KTRACE_OPT(KUSB, Kern::Printf(" -> setting conflict"));
247 // (aIfcNumber == -1) means the ep is for a new default interface setting
248 else if (iIfcNumber && (*iIfcNumber != aIfcNumber))
250 // If this endpoint has already been claimed (iIfcNumber != NULL),
251 // but by a different interface(-set) than the currently looking one
252 // (*iIfcNumber != aIfcNumber), then it's not available.
253 // This works because we can assign the same physical endpoint
254 // to different alternate settings of the *same* interface, and
255 // because we check for available endpoints for every alternate setting
257 __KTRACE_OPT(KUSB, Kern::Printf(" -> ifc conflict"));
260 else if (!TypeAvailable(aEpInfo->iType))
262 __KTRACE_OPT(KUSB, Kern::Printf(" -> type conflict"));
265 else if (!DirAvailable(aEpInfo->iDir))
267 __KTRACE_OPT(KUSB, Kern::Printf(" -> direction conflict"));
270 else if (!(iCaps.iSizes & PacketSize2Mask(aEpInfo->iSize)) && !(iCaps.iSizes & KUsbEpSizeCont))
272 __KTRACE_OPT(KUSB, Kern::Printf(" -> size conflict"));
280 TUsbcPhysicalEndpoint::~TUsbcPhysicalEndpoint()
282 __KTRACE_OPT(KUSB, Kern::Printf("TUsbcPhysicalEndpoint::~TUsbcPhysicalEndpoint()"));
287 TUsbcLogicalEndpoint::TUsbcLogicalEndpoint(DUsbClientController* aController, TUint aEndpointNum,
288 const TUsbcEndpointInfo& aEpInfo, TUsbcInterface* aInterface,
289 TUsbcPhysicalEndpoint* aPEndpoint)
290 : iController(aController), iLEndpointNum(aEndpointNum), iInfo(aEpInfo), iInterface(aInterface),
291 iPEndpoint(aPEndpoint)
293 __KTRACE_OPT(KUSB, Kern::Printf("TUsbcLogicalEndpoint::TUsbcLogicalEndpoint()"));
294 // Adjust FS/HS endpoint sizes
295 if (iInfo.AdjustEpSizes(iEpSize_Fs, iEpSize_Hs) != KErrNone)
297 __KTRACE_OPT(KPANIC, Kern::Printf(" Error: Unknown endpoint type: %d", iInfo.iType));
299 __KTRACE_OPT(KUSB, Kern::Printf(" Now set: iEpSize_Fs=%d iEpSize_Hs=%d (iInfo.iSize=%d)",
300 iEpSize_Fs, iEpSize_Hs, iInfo.iSize));
301 // Adjust HS polling interval
302 if (iInfo.AdjustPollInterval() != KErrNone)
304 __KTRACE_OPT(KPANIC, Kern::Printf(" Error: Unknown ep type (%d) or invalid interval value (%d)",
305 iInfo.iType, iInfo.iInterval));
307 __KTRACE_OPT(KUSB, Kern::Printf(" Now set: iInfo.iInterval=%d iInfo.iInterval_Hs=%d",
308 iInfo.iInterval, iInfo.iInterval_Hs));
309 // Additional transactions requested on a non High Bandwidth ep?
310 if ((iInfo.iTransactions > 0) && !aPEndpoint->iCaps.iHighBandwidth)
313 Kern::Printf(" Warning: Additional transactions requested but not a High Bandwidth ep"));
318 TUsbcLogicalEndpoint::~TUsbcLogicalEndpoint()
320 __KTRACE_OPT(KUSB, Kern::Printf("TUsbcLogicalEndpoint::~TUsbcLogicalEndpoint: #%d", iLEndpointNum));
321 // If the real endpoint this endpoint points to is also used by
322 // any other logical endpoint in any other setting of this interface
323 // then we leave the real endpoint marked as used. Otherwise we mark
324 // it as available (set its ifc number pointer to NULL).
325 const TInt n = iInterface->iInterfaceSet->iInterfaces.Count();
326 for (TInt i = 0; i < n; ++i)
328 const TUsbcInterface* const ifc = iInterface->iInterfaceSet->iInterfaces[i];
329 const TInt m = ifc->iEndpoints.Count();
330 for (TInt j = 0; j < m; ++j)
332 const TUsbcLogicalEndpoint* const ep = ifc->iEndpoints[j];
333 if ((ep->iPEndpoint == iPEndpoint) && (ep != this))
335 __KTRACE_OPT(KUSB, Kern::Printf(" Physical endpoint still in use -> we leave it as is"));
340 __KTRACE_OPT(KUSB, Kern::Printf(" Closing DMA channel"));
341 const TInt idx = iController->EpAddr2Idx(iPEndpoint->iEndpointAddr);
342 // If the endpoint doesn't support DMA (now or ever) the next operation will be a no-op.
343 iController->CloseDmaChannel(idx);
344 __KTRACE_OPT(KUSB, Kern::Printf(" Setting physical ep 0x%02x ifc number to NULL (was %d)",
345 iPEndpoint->iEndpointAddr, *iPEndpoint->iIfcNumber));
346 iPEndpoint->iIfcNumber = NULL;
350 TUsbcInterface::TUsbcInterface(TUsbcInterfaceSet* aIfcSet, TUint8 aSetting, TBool aNoEp0Requests)
351 : iEndpoints(2), iInterfaceSet(aIfcSet), iSettingCode(aSetting), iNoEp0Requests(aNoEp0Requests)
353 __KTRACE_OPT(KUSB, Kern::Printf("TUsbcInterface::TUsbcInterface()"));
357 TUsbcInterface::~TUsbcInterface()
359 __KTRACE_OPT(KUSB, Kern::Printf("TUsbcInterface::~TUsbcInterface()"));
360 iEndpoints.ResetAndDestroy();
364 TUsbcInterfaceSet::TUsbcInterfaceSet(const DBase* aClientId, TUint8 aIfcNum)
365 : iInterfaces(2), iClientId(aClientId), iInterfaceNumber(aIfcNum), iCurrentInterface(0)
367 __KTRACE_OPT(KUSB, Kern::Printf("TUsbcInterfaceSet::TUsbcInterfaceSet()"));
371 TUsbcInterfaceSet::~TUsbcInterfaceSet()
373 __KTRACE_OPT(KUSB, Kern::Printf("TUsbcInterfaceSet::~TUsbcInterfaceSet()"));
374 iInterfaces.ResetAndDestroy();
378 TUsbcConfiguration::TUsbcConfiguration(TUint8 aConfigVal)
379 : iInterfaceSets(1), iConfigValue(aConfigVal) // iInterfaceSets(1): granularity
381 __KTRACE_OPT(KUSB, Kern::Printf("TUsbcConfiguration::TUsbcConfiguration()"));
385 TUsbcConfiguration::~TUsbcConfiguration()
387 __KTRACE_OPT(KUSB, Kern::Printf("TUsbcConfiguration::~TUsbcConfiguration()"));
388 iInterfaceSets.ResetAndDestroy();
392 _LIT(KDriverName, "Usbcc");
394 DUsbcPowerHandler::DUsbcPowerHandler(DUsbClientController* aController)
395 : DPowerHandler(KDriverName), iController(aController)
399 void DUsbcPowerHandler::PowerUp()
402 iController->iPowerUpDfc.Enque();
406 void DUsbcPowerHandler::PowerDown(TPowerState)
409 iController->iPowerDownDfc.Enque();