sl@0: // Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of the License "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // e32\include\drivers\usbcshared.inl sl@0: // Kernel side definitions for the USB Device driver stack (PIL + LDD). sl@0: // sl@0: // sl@0: sl@0: /** sl@0: @file usbcshared.inl sl@0: @internalTechnology sl@0: */ sl@0: sl@0: #ifndef __USBCSHARED_INL__ sl@0: #define __USBCSHARED_INL__ sl@0: sl@0: // sl@0: // --- DUsbClientController (USB PDD) --- sl@0: // sl@0: sl@0: // --- Private member functions, used by controller itself --- sl@0: sl@0: const DBase* DUsbClientController::PEndpoint2ClientId(TInt aRealEndpoint) const sl@0: { sl@0: if (iRealEndpoints[aRealEndpoint].iLEndpoint) sl@0: return iRealEndpoints[aRealEndpoint].iLEndpoint->iInterface->iInterfaceSet->iClientId; sl@0: else sl@0: return NULL; sl@0: } sl@0: sl@0: sl@0: TInt DUsbClientController::PEndpoint2LEndpoint(TInt aRealEndpoint) const sl@0: { sl@0: if (iRealEndpoints[aRealEndpoint].iLEndpoint) sl@0: return iRealEndpoints[aRealEndpoint].iLEndpoint->iLEndpointNum; sl@0: else sl@0: return -1; sl@0: } sl@0: sl@0: sl@0: const TUsbcConfiguration* DUsbClientController::CurrentConfig() const sl@0: { sl@0: return (iCurrentConfig ? iConfigs[iCurrentConfig - 1] : NULL); sl@0: } sl@0: sl@0: sl@0: TUsbcConfiguration* DUsbClientController::CurrentConfig() sl@0: { sl@0: return (iCurrentConfig ? iConfigs[iCurrentConfig - 1] : NULL); sl@0: } sl@0: sl@0: sl@0: TBool DUsbClientController::InterfaceExists(TInt aNumber) const sl@0: { sl@0: const TInt num_ifcsets = iConfigs[0]->iInterfaceSets.Count(); sl@0: RPointerArray& ifcsets = iConfigs[0]->iInterfaceSets; sl@0: for (TInt i = 0; i < num_ifcsets; i++) sl@0: { sl@0: if (ifcsets[i]->iInterfaceNumber == aNumber) sl@0: { sl@0: return ETrue; sl@0: } sl@0: } sl@0: return EFalse; sl@0: } sl@0: sl@0: sl@0: TBool DUsbClientController::EndpointExists(TUint aAddress) const sl@0: { sl@0: // Ep0 doesn't have a "logical ep" pointer (there's no virtual endpoint zero); sl@0: // that's why this pointer being non-NULL is not a sufficient criterion for sl@0: // endpoint-existence. (Apart from that, ep0 always exists.) sl@0: const TInt idx = EpAddr2Idx(aAddress); sl@0: return ((idx < iDeviceTotalEndpoints) && sl@0: ((iRealEndpoints[idx].iLEndpoint != NULL) || sl@0: ((aAddress & KUsbEpAddress_Portmask) == 0))); sl@0: } sl@0: sl@0: sl@0: void DUsbClientController::Buffer2Setup(const TAny* aBuf, TUsbcSetup& aSetup) const sl@0: { sl@0: // TUint8 index sl@0: aSetup.iRequestType = static_cast(aBuf)[0]; sl@0: aSetup.iRequest = static_cast(aBuf)[1]; sl@0: // TUint16 index from here! sl@0: aSetup.iValue = SWAP_BYTES_16((static_cast(aBuf))[1]); sl@0: aSetup.iIndex = SWAP_BYTES_16((static_cast(aBuf))[2]); sl@0: aSetup.iLength = SWAP_BYTES_16((static_cast(aBuf))[3]); sl@0: } sl@0: sl@0: sl@0: TUint DUsbClientController::EpIdx2Addr(TUint aRealEndpoint) const sl@0: { sl@0: return ((aRealEndpoint << 7) & 0x80) | ((aRealEndpoint >> 1) & 0x0f); sl@0: } sl@0: sl@0: sl@0: TUint DUsbClientController::EpAddr2Idx(TUint aAddress) const sl@0: { sl@0: return ((aAddress & 0x80) >> 7) | ((aAddress & 0x0f) << 1); sl@0: } sl@0: sl@0: sl@0: void DUsbClientController::SetEp0DataOutVars(const TUsbcSetup& aPacket, const DBase* aClientId) sl@0: { sl@0: __KTRACE_OPT(KUSB, Kern::Printf("DUsbClientController::SetEp0DataOutVars()")); sl@0: iSetup = aPacket; sl@0: iEp0DataReceiving = ETrue; sl@0: iEp0DataReceived = 0; sl@0: iEp0ClientId = aClientId; sl@0: } sl@0: sl@0: sl@0: void DUsbClientController::ResetEp0DataOutVars() sl@0: { sl@0: __KTRACE_OPT(KUSB, Kern::Printf("DUsbClientController::ResetEp0DataOutVars()")); sl@0: iEp0DataReceiving = EFalse; sl@0: iEp0DataReceived = 0; sl@0: iEp0ClientId = NULL; sl@0: } sl@0: sl@0: sl@0: TBool DUsbClientController::IsInTheRequestList(const TUsbcRequestCallback& aCallback) sl@0: { sl@0: const TInt irq = NKern::DisableAllInterrupts(); sl@0: TSglQueIter iter(iEp0ReadRequestCallbacks); sl@0: TUsbcRequestCallback* p; sl@0: while ((p = iter++) != NULL) sl@0: { sl@0: if (p == &aCallback) sl@0: { sl@0: NKern::RestoreInterrupts(irq); sl@0: return ETrue; sl@0: } sl@0: } sl@0: NKern::RestoreInterrupts(irq); sl@0: return EFalse; sl@0: } sl@0: sl@0: sl@0: TBool DUsbClientController::IsInTheStatusList(const TUsbcStatusCallback& aCallback) sl@0: { sl@0: const TInt irq = NKern::DisableAllInterrupts(); sl@0: TSglQueIter iter(iStatusCallbacks); sl@0: TUsbcStatusCallback* p; sl@0: while ((p = iter++) != NULL) sl@0: { sl@0: if (p == &aCallback) sl@0: { sl@0: NKern::RestoreInterrupts(irq); sl@0: return ETrue; sl@0: } sl@0: } sl@0: NKern::RestoreInterrupts(irq); sl@0: return EFalse; sl@0: } sl@0: sl@0: sl@0: TBool DUsbClientController::IsInTheEpStatusList(const TUsbcEndpointStatusCallback& aCallback) sl@0: { sl@0: const TInt irq = NKern::DisableAllInterrupts(); sl@0: TSglQueIter iter(iEpStatusCallbacks); sl@0: TUsbcEndpointStatusCallback* p; sl@0: while ((p = iter++) != NULL) sl@0: { sl@0: if (p == &aCallback) sl@0: { sl@0: NKern::RestoreInterrupts(irq); sl@0: return ETrue; sl@0: } sl@0: } sl@0: NKern::RestoreInterrupts(irq); sl@0: return EFalse; sl@0: } sl@0: sl@0: sl@0: TBool DUsbClientController::IsInTheOtgFeatureList(const TUsbcOtgFeatureCallback& aCallback) sl@0: { sl@0: const TInt irq = NKern::DisableAllInterrupts(); sl@0: TSglQueIter iter(iOtgCallbacks); sl@0: TUsbcOtgFeatureCallback* p; sl@0: while ((p = iter++) != NULL) sl@0: { sl@0: if (p == &aCallback) sl@0: { sl@0: NKern::RestoreInterrupts(irq); sl@0: return ETrue; sl@0: } sl@0: } sl@0: NKern::RestoreInterrupts(irq); sl@0: return EFalse; sl@0: } sl@0: sl@0: // sl@0: // --- Misc classes --- sl@0: // sl@0: sl@0: // --- TUsbcClientCallback sl@0: sl@0: /** Constructor. sl@0: */ sl@0: TUsbcClientCallback::TUsbcClientCallback(DBase* aOwner, TDfcFn aCallback, TInt aPriority) sl@0: : iOwner(aOwner), sl@0: iDfc(aCallback, aOwner, aPriority) sl@0: {} sl@0: sl@0: sl@0: /** Returns a pointer to the owner of this request. sl@0: sl@0: @return A pointer to the owner of this request. sl@0: */ sl@0: DBase* TUsbcClientCallback::Owner() const sl@0: { sl@0: return iOwner; sl@0: } sl@0: sl@0: sl@0: /** Executes the callback function set by the owner of this request. sl@0: sl@0: @return KErrNone. sl@0: */ sl@0: TInt TUsbcClientCallback::DoCallback() sl@0: { sl@0: __ASSERT_DEBUG((NKern::CurrentContext() == EThread), Kern::Fault(KUsbPILPanicCat, __LINE__)); sl@0: iDfc.Enque(); sl@0: return KErrNone; sl@0: } sl@0: sl@0: sl@0: /** Cancels the callback function set by the owner of this request. sl@0: */ sl@0: void TUsbcClientCallback::Cancel() sl@0: { sl@0: iDfc.Cancel(); sl@0: } sl@0: sl@0: sl@0: /** Sets the DFC queue used by the callback function. sl@0: @param aDfcQ DFC queue to be set sl@0: */ sl@0: void TUsbcClientCallback::SetDfcQ(TDfcQue* aDfcQ) sl@0: { sl@0: iDfc.SetDfcQ(aDfcQ); sl@0: } sl@0: sl@0: sl@0: // --- TUsbcEndpointStatusCallback sl@0: sl@0: /** Constructor. sl@0: */ sl@0: TUsbcEndpointStatusCallback::TUsbcEndpointStatusCallback(DBase* aOwner, TDfcFn aCallback, sl@0: TInt aPriority) sl@0: : iOwner(aOwner), sl@0: iDfc(aCallback, aOwner, aPriority), sl@0: iState(0) sl@0: {} sl@0: sl@0: sl@0: /** Sets the state of this request to aState. sl@0: sl@0: @param aState The new state to be set. sl@0: */ sl@0: void TUsbcEndpointStatusCallback::SetState(TUint aState) sl@0: { sl@0: iState = aState; sl@0: } sl@0: sl@0: sl@0: /** Returns the state value of this request. sl@0: sl@0: @return The state value of this request. sl@0: */ sl@0: TUint TUsbcEndpointStatusCallback::State() const sl@0: { sl@0: return iState; sl@0: } sl@0: sl@0: sl@0: /** Returns a pointer to the owner of this request. sl@0: sl@0: @return A pointer to the owner of this request. sl@0: */ sl@0: DBase* TUsbcEndpointStatusCallback::Owner() const sl@0: { sl@0: return iOwner; sl@0: } sl@0: sl@0: sl@0: /** Executes the callback function set by the owner of this request. sl@0: sl@0: @return KErrNone. sl@0: */ sl@0: TInt TUsbcEndpointStatusCallback::DoCallback() sl@0: { sl@0: if (NKern::CurrentContext() == EThread) sl@0: iDfc.Enque(); sl@0: else sl@0: iDfc.Add(); sl@0: return KErrNone; sl@0: } sl@0: sl@0: sl@0: /** Cancels the callback function set by the owner of this request. sl@0: */ sl@0: void TUsbcEndpointStatusCallback::Cancel() sl@0: { sl@0: iDfc.Cancel(); sl@0: } sl@0: sl@0: sl@0: /** Sets the DFC queue used by the callback function. sl@0: */ sl@0: void TUsbcEndpointStatusCallback::SetDfcQ(TDfcQue* aDfcQ) sl@0: { sl@0: iDfc.SetDfcQ(aDfcQ); sl@0: } sl@0: sl@0: sl@0: // --- TUsbcStatusCallback sl@0: sl@0: /** Constructor. sl@0: */ sl@0: TUsbcStatusCallback::TUsbcStatusCallback(DBase* aOwner, TDfcFn aCallback, TInt aPriority) sl@0: : iOwner(aOwner), sl@0: iDfc(aCallback, aOwner, aPriority) sl@0: { sl@0: ResetState(); sl@0: } sl@0: sl@0: sl@0: /** Sets the state of this request to aState (at the first available position sl@0: in the state value array). sl@0: sl@0: @param aState The new state to be set. sl@0: */ sl@0: void TUsbcStatusCallback::SetState(TUsbcDeviceState aState) sl@0: { sl@0: for (TInt i = 0; i < KUsbcDeviceStateRequests; i++) sl@0: { sl@0: if (iState[i] == EUsbcNoState) sl@0: { sl@0: iState[i] = aState; sl@0: return; sl@0: } sl@0: } sl@0: __KTRACE_OPT(KPANIC, Kern::Printf(" Error: KUsbcDeviceStateRequests too small (%d)!", sl@0: KUsbcDeviceStateRequests)); sl@0: } sl@0: sl@0: sl@0: /** Returns the state value of this request at a certain index. sl@0: sl@0: @param aIndex The index to be used for referencing the state array. sl@0: sl@0: @return The state value of this request at aIndex. sl@0: */ sl@0: TUsbcDeviceState TUsbcStatusCallback::State(TInt aIndex) const sl@0: { sl@0: if (aIndex >= 0 && aIndex < KUsbcDeviceStateRequests) sl@0: { sl@0: return iState[aIndex]; sl@0: } sl@0: else sl@0: { sl@0: __KTRACE_OPT(KPANIC, Kern::Printf(" Error: aIndex too large (%d)!", aIndex)); sl@0: return EUsbcNoState; sl@0: } sl@0: } sl@0: sl@0: sl@0: /** Resets the entire state value array of this request. sl@0: */ sl@0: void TUsbcStatusCallback::ResetState() sl@0: { sl@0: for (TInt i = 0; i < KUsbcDeviceStateRequests; ++i) sl@0: { sl@0: iState[i] = EUsbcNoState; sl@0: } sl@0: } sl@0: sl@0: sl@0: /** Returns a pointer to the owner of this request. sl@0: sl@0: @return A pointer to the owner of this request. sl@0: */ sl@0: DBase* TUsbcStatusCallback::Owner() const sl@0: { sl@0: return iOwner; sl@0: } sl@0: sl@0: sl@0: /** Executes the callback function set by the owner of this request. sl@0: sl@0: @return KErrNone. sl@0: */ sl@0: TInt TUsbcStatusCallback::DoCallback() sl@0: { sl@0: if (NKern::CurrentContext() == EThread) sl@0: iDfc.Enque(); sl@0: else sl@0: iDfc.Add(); sl@0: return KErrNone; sl@0: } sl@0: sl@0: sl@0: /** Cancels the callback function set by the owner of this request. sl@0: */ sl@0: void TUsbcStatusCallback::Cancel() sl@0: { sl@0: iDfc.Cancel(); sl@0: } sl@0: sl@0: sl@0: /** Sets the DFC queue used by the callback function. sl@0: */ sl@0: void TUsbcStatusCallback::SetDfcQ(TDfcQue* aDfcQ) sl@0: { sl@0: iDfc.SetDfcQ(aDfcQ); sl@0: } sl@0: sl@0: // --- TUsbcRequestCallback sl@0: sl@0: /** Constructor. sl@0: */ sl@0: TUsbcRequestCallback::TUsbcRequestCallback(const DBase* aOwner, TInt aEndpointNum, TDfcFn aDfcFunc, sl@0: TAny* aEndpoint, TDfcQue* aDfcQ, TInt aPriority) sl@0: : iEndpointNum(aEndpointNum), sl@0: iRealEpNum(-1), sl@0: iOwner(aOwner), sl@0: iDfc(aDfcFunc, aEndpoint, aDfcQ, aPriority), sl@0: iTransferDir(EControllerNone), sl@0: iBufferStart(NULL), sl@0: iPacketIndex(NULL), // actually TUint16 (*)[] sl@0: iPacketSize(NULL), // actually TUint16 (*)[] sl@0: iLength(0), sl@0: iZlpReqd(EFalse), sl@0: iTxBytes(0), sl@0: iRxPackets(0), sl@0: iError(KErrNone) sl@0: { sl@0: } sl@0: sl@0: sl@0: /** Destructor. sl@0: */ sl@0: TUsbcRequestCallback::~TUsbcRequestCallback() sl@0: { sl@0: __KTRACE_OPT(KUSB, Kern::Printf("TUsbcRequestCallback::~TUsbcRequestCallback()")); sl@0: iDfc.Cancel(); sl@0: } sl@0: sl@0: sl@0: /** Sets some data members of this request for a read request. sl@0: sl@0: @param aBufferStart The start of the data buffer to be filled. sl@0: @param aBufferAddr The physical address of the buffer (used for DMA). sl@0: @param aPacketIndex A pointer to the packet index values array. sl@0: @param aPacketSize A pointer to the packet size values array. sl@0: @param aLength The number of bytes to be received. sl@0: */ sl@0: void TUsbcRequestCallback::SetRxBufferInfo(TUint8* aBufferStart, TPhysAddr aBufferAddr, sl@0: TUsbcPacketArray* aPacketIndex, sl@0: TUsbcPacketArray* aPacketSize, sl@0: TInt aLength) sl@0: { sl@0: iTransferDir = EControllerRead; sl@0: iBufferStart = aBufferStart; sl@0: iBufferAddr = aBufferAddr; sl@0: iPacketIndex = aPacketIndex; sl@0: iPacketSize = aPacketSize; sl@0: iLength = aLength; sl@0: } sl@0: sl@0: sl@0: /** Sets some data members of this request for a write request. sl@0: sl@0: @param aBufferStart The start of the buffer that contains the data to be sent. sl@0: @param aBufferAddr The physical address of the buffer (used for DMA). sl@0: @param aLength The number of bytes to be transmitted. sl@0: */ sl@0: void TUsbcRequestCallback::SetTxBufferInfo(TUint8* aBufferStart, TPhysAddr aBufferAddr, TInt aLength) sl@0: { sl@0: iTransferDir = EControllerWrite; sl@0: iBufferStart = aBufferStart; sl@0: iBufferAddr = aBufferAddr; sl@0: iLength = aLength; sl@0: } sl@0: sl@0: sl@0: /** Sets the transfer direction for this request. sl@0: sl@0: @param aTransferDir The new transfer direction. sl@0: */ sl@0: void TUsbcRequestCallback::SetTransferDirection(TTransferDirection aTransferDir) sl@0: { sl@0: iTransferDir = aTransferDir; sl@0: } sl@0: sl@0: sl@0: /** Returns a pointer to the owner of this request. sl@0: sl@0: @return A pointer to the owner of this request. sl@0: */ sl@0: const DBase* TUsbcRequestCallback::Owner() const sl@0: { sl@0: return iOwner; sl@0: } sl@0: sl@0: sl@0: /** Executes the callback function set by the owner of this request. sl@0: sl@0: @return KErrNone. sl@0: */ sl@0: TInt TUsbcRequestCallback::DoCallback() sl@0: { sl@0: if (NKern::CurrentContext() == NKern::EThread) sl@0: iDfc.Enque(); sl@0: else sl@0: iDfc.Add(); sl@0: return KErrNone; sl@0: } sl@0: sl@0: sl@0: /** Cancels the callback function set by the owner of this request. sl@0: */ sl@0: void TUsbcRequestCallback::Cancel() sl@0: { sl@0: iDfc.Cancel(); sl@0: } sl@0: sl@0: // --- TUsbcOtgFeatureCallback sl@0: sl@0: /** Constructor. sl@0: */ sl@0: TUsbcOtgFeatureCallback::TUsbcOtgFeatureCallback(DBase* aOwner, TDfcFn aCallback, sl@0: TInt aPriority) sl@0: : iOwner(aOwner), sl@0: iDfc(aCallback, aOwner, aPriority), sl@0: iValue(0) sl@0: {} sl@0: sl@0: sl@0: /** Returns a pointer to the owner of this request. sl@0: @return A pointer to the owner of this request. sl@0: */ sl@0: DBase* TUsbcOtgFeatureCallback::Owner() const sl@0: { sl@0: return iOwner; sl@0: } sl@0: sl@0: sl@0: /** Set feature value which is to be notified to client. sl@0: @param OTG feature value to be set sl@0: */ sl@0: void TUsbcOtgFeatureCallback::SetFeatures(TUint8 aFeatures) sl@0: { sl@0: iValue = aFeatures; sl@0: } sl@0: sl@0: sl@0: /** Set feature value which is to be notified to client. sl@0: @return Value of OTG features sl@0: */ sl@0: TUint8 TUsbcOtgFeatureCallback::Features() const sl@0: { sl@0: return iValue; sl@0: } sl@0: sl@0: sl@0: /** Set DFC queue. sl@0: @param aDfcQ DFC queue to be set sl@0: */ sl@0: void TUsbcOtgFeatureCallback::SetDfcQ(TDfcQue* aDfcQ) sl@0: { sl@0: iDfc.SetDfcQ(aDfcQ); sl@0: } sl@0: sl@0: sl@0: /** Executes the callback function set by the owner of this request. sl@0: @return KErrNone. sl@0: */ sl@0: TInt TUsbcOtgFeatureCallback::DoCallback() sl@0: { sl@0: if (NKern::CurrentContext() == EThread) sl@0: iDfc.Enque(); sl@0: else sl@0: iDfc.Add(); sl@0: return KErrNone; sl@0: } sl@0: sl@0: sl@0: /** Cancels the callback function set by the owner of this request. sl@0: */ sl@0: void TUsbcOtgFeatureCallback::Cancel() sl@0: { sl@0: iDfc.Cancel(); sl@0: } sl@0: sl@0: sl@0: /** Returns a pointer to the currently selected (active) setting of this interface. sl@0: sl@0: @return A pointer to the currently selected (active) setting of this interface. sl@0: */ sl@0: const TUsbcInterface* TUsbcInterfaceSet::CurrentInterface() const sl@0: { sl@0: return iInterfaces[iCurrentInterface]; sl@0: } sl@0: sl@0: sl@0: /** Returns a pointer to the currently selected (active) setting of this interface. sl@0: sl@0: @return A pointer to the currently selected (active) setting of this interface. sl@0: */ sl@0: sl@0: TUsbcInterface* TUsbcInterfaceSet::CurrentInterface() sl@0: { sl@0: return iInterfaces[iCurrentInterface]; sl@0: } sl@0: sl@0: sl@0: #endif // __USBCSHARED_INL__ sl@0: sl@0: sl@0: