1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kernel/eka/include/drivers/usbc.h Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,358 @@
1.4 +// Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of the License "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// e32/include/drivers/usbc.h
1.18 +// Kernel side definitions for the USB Device driver stack (PIL + LDD).
1.19 +//
1.20 +//
1.21 +
1.22 +/**
1.23 + @file usbc.h
1.24 + @internalTechnology
1.25 +*/
1.26 +
1.27 +#ifndef __USBC_H__
1.28 +#define __USBC_H__
1.29 +
1.30 +#include <kernel/kernel.h>
1.31 +#include <kernel/kern_priv.h>
1.32 +#include <kernel/kpower.h>
1.33 +#include <platform.h>
1.34 +
1.35 +#include <d32usbc.h>
1.36 +
1.37 +#include <drivers/usbcshared.h>
1.38 +
1.39 +
1.40 +
1.41 +/** LDD Major version, This should agree with the information in RDevUsbcClient::TVer.
1.42 +*/
1.43 +const TInt KUsbcMajorVersion = 0;
1.44 +
1.45 +/** LDD Minor version, This should agree with the information in RDevUsbcClient::TVer.
1.46 +*/
1.47 +const TInt KUsbcMinorVersion = 1;
1.48 +
1.49 +/** LDD Build version, This should agree with the information in RDevUsbcClient::TVer.
1.50 +*/
1.51 +const TInt KUsbcBuildVersion = KE32BuildVersionNumber;
1.52 +
1.53 +/** Must correspond to the max enum of TRequest + 1;
1.54 + currently this is ERequestOtgFeaturesNotify = 10.
1.55 +*/
1.56 +const TInt KUsbcMaxRequests = 11;
1.57 +
1.58 +//
1.59 +//########################### Logical Device Driver (LDD) #############################
1.60 +//
1.61 +
1.62 +/** USB LDD factory class.
1.63 +*/
1.64 +class DUsbcLogDevice : public DLogicalDevice
1.65 + {
1.66 +public:
1.67 + DUsbcLogDevice();
1.68 + virtual TInt Install();
1.69 + virtual void GetCaps(TDes8& aDes) const;
1.70 + virtual TInt Create(DLogicalChannelBase*& aChannel);
1.71 + };
1.72 +
1.73 +
1.74 +/** OUT buffering is a collection of flat buffers. Each is either fillable or drainable.
1.75 + When one buffer becomes full (notified by the PIL) it is marked as not-fillable and the next
1.76 + fillable buffer is used. When the buffer has finished draining it is marked as fillable.
1.77 +*/
1.78 +class TDmaBuf
1.79 + {
1.80 +public:
1.81 + TDmaBuf();
1.82 + TDmaBuf(TUsbcEndpointInfo* aEndpointInfo, TInt aBandwidthPriority);
1.83 + ~TDmaBuf();
1.84 + TInt Construct(TUsbcEndpointInfo* aEndpointInfo);
1.85 + TInt BufferTotalSize() const;
1.86 + TInt BufferSize() const;
1.87 + TInt SetBufferAddr(TInt aBufInd, TUint8* aBufAddr);
1.88 + TInt BufferNumber() const;
1.89 + void SetMaxPacketSize(TInt aSize);
1.90 + void Flush();
1.91 + // Rx (OUT) variants
1.92 + void RxSetActive();
1.93 + void RxSetInActive();
1.94 + TBool RxIsActive();
1.95 + TBool IsReaderEmpty();
1.96 + void ReadXferComplete(TInt aNoBytesRx, TInt aNoPacketsRx, TInt aErrorCode);
1.97 + TInt RxCopyDataToClient(DThread* aThread, TClientBuffer *aTcb, TInt aLength, TUint32& aDestOffset,
1.98 + TBool aRUS, TBool& aCompleteNow);
1.99 + TInt RxCopyPacketToClient(DThread* aThread,TClientBuffer *aTcb, TInt aLength);
1.100 + TInt RxGetNextXfer(TUint8*& aBufferAddr, TUsbcPacketArray*& aIndexArray, TUsbcPacketArray*& aSizeArray,
1.101 + TInt& aLength, TPhysAddr& aBufferPhys);
1.102 + TBool RxIsEnoughSpace(TInt aSize);
1.103 + inline TInt RxBytesAvailable() const;
1.104 + inline void IncrementBufferIndex(TInt& aIndex);
1.105 + inline TInt NoRxPackets() const;
1.106 + TInt SetDrainable(TInt aBufferNum);
1.107 + // Tx (IN) variants
1.108 + void TxSetActive();
1.109 + void TxSetInActive();
1.110 + TBool TxIsActive();
1.111 + TInt TxStoreData(DThread* aThread,TClientBuffer *aTcb, TInt aTxLength, TUint32 aBufferOffset);
1.112 + TInt TxGetNextXfer(TUint8*& aBufferAddr, TInt& aTxLength, TPhysAddr& aBufferPhys);
1.113 + TBool ShortPacketExists();
1.114 +
1.115 +#if defined(USBC_LDD_BUFFER_TRACE)
1.116 + TInt NoRxPacketsAlt() const;
1.117 + TInt NoRxBytesAlt() const;
1.118 +#endif
1.119 +
1.120 +private:
1.121 + TBool AdvancePacket();
1.122 + inline TInt GetCurrentError();
1.123 + TBool NextDrainableBuffer();
1.124 + TBool NextFillableBuffer();
1.125 + void FreeDrainedBuffers();
1.126 + TInt PeekNextPacketSize();
1.127 + TInt PeekNextDrainableBuffer();
1.128 + void ModifyTotalRxBytesAvail(TInt aVal);
1.129 + void ModifyTotalRxPacketsAvail(TInt aVal);
1.130 + void AddToDrainQueue(TInt aBufferIndex);
1.131 + inline TInt CopyToUser(DThread* aThread, const TUint8* aSourceAddr, TInt aLength,
1.132 + TClientBuffer *aTcb, TUint32& aDestOffset);
1.133 +private:
1.134 + TInt iExtractOffset; // offset into current packet for data read
1.135 + TInt iMaxPacketSize;
1.136 + TInt iNumberofBuffers;
1.137 + TInt iBufSz;
1.138 + TBool iRxActive;
1.139 + TBool iTxActive;
1.140 + TInt iTotalRxBytesAvail;
1.141 + TInt iTotalRxPacketsAvail;
1.142 + //
1.143 + TUint8* iBufBasePtr;
1.144 + TUint8* iCurrentDrainingBuffer;
1.145 + TInt iCurrentDrainingBufferIndex;
1.146 + TInt iCurrentFillingBufferIndex;
1.147 + TUint iCurrentPacket;
1.148 + TUsbcPacketArray* iCurrentPacketIndexArray;
1.149 + TUsbcPacketArray* iCurrentPacketSizeArray;
1.150 + TUint8* iBuffers[KUsbcDmaBufNumMax];
1.151 + TBool iDrainable[KUsbcDmaBufNumMax];
1.152 + TUsbcPacketArray iPacketInfoStorage[KUsbcDmaBufNumMax * KUsbcDmaBufNumArrays * KUsbcDmaBufMaxPkts];
1.153 + TUsbcPacketArray* iPacketIndex[KUsbcDmaBufNumMax];
1.154 + TUsbcPacketArray* iPacketSize[KUsbcDmaBufNumMax];
1.155 + TUint iNumberofBytesRx[KUsbcDmaBufNumMax];
1.156 + TUint iNumberofPacketsRx[KUsbcDmaBufNumMax];
1.157 + TInt iError[KUsbcDmaBufNumMax];
1.158 + TPhysAddr iBufferPhys[KUsbcDmaBufNumMax];
1.159 + TBool iCanBeFreed[KUsbcDmaBufNumMax];
1.160 + TInt iDrainQueue[KUsbcDmaBufNumMax + 1];
1.161 + TInt iDrainQueueIndex;
1.162 + TUint iEndpointType;
1.163 +
1.164 +#if defined(USBC_LDD_BUFFER_TRACE)
1.165 + TInt iFillingOrder;
1.166 + TInt iFillingOrderArray[KUsbcDmaBufNumMax];
1.167 + TInt iDrainingOrder;
1.168 + TUint iNumberofBytesRxRemain[KUsbcDmaBufNumMax];
1.169 + TUint iNumberofPacketsRxRemain[KUsbcDmaBufNumMax];
1.170 +#endif
1.171 + };
1.172 +
1.173 +
1.174 +class DLddUsbcChannel;
1.175 +
1.176 +/** Endpoint tracking for the LDD buffering etc.
1.177 +*/
1.178 +class TUsbcEndpoint
1.179 + {
1.180 +public:
1.181 + TUsbcEndpoint();
1.182 + TUsbcEndpoint(DLddUsbcChannel* aLDD, DUsbClientController* aController,
1.183 + const TUsbcEndpointInfo* aEndpointInfo, TInt aEndpointNum,
1.184 + TInt aBandwidthPriority);
1.185 + ~TUsbcEndpoint();
1.186 + TInt Construct();
1.187 + TInt TryToStartRead(TBool aReEntrant);
1.188 + TInt TryToStartWrite(TEndpointTransferInfo* pTfr);
1.189 + TInt CopyToClient(DThread* aThread, TClientBuffer *aTcb);
1.190 + TInt CopyToClient(DThread* aClient, TBool& aCompleteNow, TClientBuffer *aTcb);
1.191 + TInt ContinueWrite();
1.192 + void SetMaxPacketSize(TInt aSize);
1.193 + void CancelTransfer(DThread* aThread, TClientBuffer *aTcb);
1.194 + void AbortTransfer();
1.195 + inline TUsbcEndpointInfo* EndpointInfo();
1.196 + inline TInt RxBytesAvailable() const;
1.197 +
1.198 + inline TInt BufferSize() const;
1.199 + inline TInt SetBufferAddr( TInt aBufInd, TUint8* aAddr);
1.200 + inline TInt BufferNumber() const;
1.201 +
1.202 + inline void SetTransferInfo(TEndpointTransferInfo* aTransferInfo);
1.203 + inline void ResetTransferInfo();
1.204 + inline void SetClientReadPending(TBool aVal);
1.205 + inline void SetClientWritePending(TBool aVal);
1.206 + inline TBool ClientWritePending();
1.207 + inline TBool ClientReadPending();
1.208 + inline void SetRealEpNumber(TInt aRealEpNumber);
1.209 + inline TInt RealEpNumber() const;
1.210 +
1.211 +public:
1.212 + TDmaBuf* iDmaBuffers;
1.213 +
1.214 +private:
1.215 + static void RequestCallback(TAny* aTUsbcEndpoint);
1.216 + void TxComplete();
1.217 + TInt RxComplete(TBool aReEntrant);
1.218 + void RxCompleteNow();
1.219 + TInt EndpointComplete();
1.220 +
1.221 +private:
1.222 + DUsbClientController* iController;
1.223 + TUsbcEndpointInfo iEndpointInfo;
1.224 + TEndpointTransferInfo iTransferInfo;
1.225 + TBool iClientReadPending;
1.226 + TBool iClientWritePending;
1.227 + TInt iEndpointNumber;
1.228 + TInt iRealEpNumber;
1.229 + DLddUsbcChannel* iLdd;
1.230 + TInt iError;
1.231 + TUsbcRequestCallback* iRequestCallbackInfo;
1.232 + TUint32 iBytesTransferred;
1.233 + TInt iBandwidthPriority;
1.234 + };
1.235 +
1.236 +
1.237 +/** Linked list of 'alternate setting' info for use by the LDD.
1.238 +*/
1.239 +class TUsbcAlternateSettingList
1.240 + {
1.241 +public:
1.242 + TUsbcAlternateSettingList();
1.243 + ~TUsbcAlternateSettingList();
1.244 +
1.245 +public:
1.246 + TUsbcAlternateSettingList* iNext;
1.247 + TInt iNumberOfEndpoints;
1.248 + TUint iSetting;
1.249 + TInt iEpNumDeOrderedByBufSize[KMaxEndpointsPerClient + 1];
1.250 + TUsbcEndpoint* iEndpoint[KMaxEndpointsPerClient + 1];
1.251 + };
1.252 +
1.253 +
1.254 +struct TClientAsynchNotify
1.255 + {
1.256 + TClientBufferRequest *iBufferRequest;
1.257 + TClientBuffer *iClientBuffer;
1.258 + void Reset();
1.259 + };
1.260 +/** The channel class - the actual USB LDD.
1.261 +*/
1.262 +class DLddUsbcChannel : public DLogicalChannel
1.263 + {
1.264 +public:
1.265 + DLddUsbcChannel();
1.266 + ~DLddUsbcChannel();
1.267 + virtual TInt SendMsg(TMessageBase * aMsg);
1.268 + TInt PreSendRequest(TMessageBase * aMsg,TInt aReqNo, TRequestStatus* aStatus, TAny* a1, TAny* a2);
1.269 + TInt SendControl(TMessageBase* aMsg);
1.270 + virtual void HandleMsg(TMessageBase* aMsg);
1.271 + virtual TInt DoCreate(TInt aUnit, const TDesC8* aInfo, const TVersion& aVer);
1.272 + virtual TInt RequestUserHandle(DThread* aThread, TOwnerType aType);
1.273 + TInt DoRxComplete(TUsbcEndpoint* aTUsbcEndpoint, TInt aEndpoint, TBool aReentrant);
1.274 + void DoRxCompleteNow(TUsbcEndpoint* aTUsbcEndpoint, TInt aEndpoint);
1.275 + void DoTxComplete(TUsbcEndpoint* aTUsbcEndpoint, TInt aEndpoint, TInt aError);
1.276 + inline DThread* Client() const {return iClient;}
1.277 + inline TBool ChannelClosing() const {return iChannelClosing;}
1.278 + inline TUint AlternateSetting() const {return iAlternateSetting;}
1.279 + TClientBuffer *GetClientBuffer(TInt aEndpoint);
1.280 +
1.281 +private:
1.282 + TInt DoCancel(TInt aReqNo);
1.283 + void DoRequest(TInt aReqNo, TRequestStatus* aStatus, TAny* a1, TAny* a2);
1.284 + TInt DoControl(TInt aFunction, TAny* a1, TAny* a2);
1.285 + TInt DoTransferAsyncReq(TInt aEndpointNum, TAny* a1, TAny* a2, TBool& aNeedsCompletion);
1.286 + TInt DoOtherAsyncReq(TInt aReqNo, TAny* a1, TAny* a2, TBool& aNeedsCompletion);
1.287 + TBool AlternateDeviceStateTestComplete();
1.288 + TInt SetInterface(TInt aInterfaceNum, TUsbcIfcInfo* aUserInterfaceInfoBuf);
1.289 + void StartEpReads();
1.290 + void DestroyAllInterfaces();
1.291 + void DestroyInterface(TUint aInterface);
1.292 + void DestroyEp0();
1.293 + inline TBool ValidEndpoint(TInt aEndpoint);
1.294 + TInt DoEmergencyComplete();
1.295 + void ReadDes8(const TAny* aPtr, TDes8& aDes);
1.296 + TInt SetupEp0();
1.297 + DPlatChunkHw* ReAllocate(TInt aBuffersize, DPlatChunkHw* aHwChunk, TUint32 aCacheAttribs);
1.298 + DPlatChunkHw* Allocate(TInt aBuffersize, TUint32 aCacheAttribs);
1.299 + void ClosePhysicalChunk(DPlatChunkHw* &aHwChunk);
1.300 + void CancelNotifyEndpointStatus();
1.301 + void CancelNotifyOtgFeatures();
1.302 + static void StatusChangeCallback(TAny* aDLddUsbcChannel);
1.303 + static void EndpointStatusChangeCallback(TAny* aDLddUsbcChannel);
1.304 + static void OtgFeatureChangeCallback(TAny* aDLddUsbcChannel);
1.305 + static void EmergencyCompleteDfc(TAny* aDLddUsbcChannel);
1.306 + void DeConfigure(TInt aErrorCode);
1.307 + TInt SelectAlternateSetting(TUint aAlternateSetting);
1.308 + TInt EpFromAlternateSetting(TUint aAlternateSetting, TInt aEndpoint);
1.309 + TInt ProcessAlternateSetting(TUint aAlternateSetting);
1.310 + TInt ProcessDeviceState(TUsbcDeviceState aDeviceState);
1.311 + void ResetInterface(TInt aErrorCode);
1.312 + void AbortInterface();
1.313 + // Set buffer address of the interface
1.314 + void ReSetInterfaceMemory(TUsbcAlternateSettingList* aAlternateSettingListRec,
1.315 + RArray<DPlatChunkHw*> &aHwChunks );
1.316 + void UpdateEndpointSizes();
1.317 + // Check and alloc memory for the interface
1.318 + TInt SetupInterfaceMemory(RArray<DPlatChunkHw*> &aHwChunks,
1.319 + TUint32 aCacheAttribs );
1.320 + void PanicClientThread(TInt aReason);
1.321 + TInt PinMemory(TDesC8 *aDes, TVirtualPinObject *iPinObj); //Descriptor pinning helper.
1.322 + void CompleteBufferRequest(DThread* aThread, TInt aReqNo, TInt aReason);
1.323 +private:
1.324 + DUsbClientController* iController;
1.325 + DThread* iClient;
1.326 + TBool iValidInterface;
1.327 + TUsbcAlternateSettingList* iAlternateSettingList;
1.328 + TUsbcEndpoint* iEndpoint[KMaxEndpointsPerClient + 1]; // include ep0
1.329 + TRequestStatus* iRequestStatus[KUsbcMaxRequests];
1.330 + TClientAsynchNotify* iClientAsynchNotify[KUsbcMaxRequests];
1.331 + TUsbcClientCallback iCompleteAllCallbackInfo;
1.332 + TAny* iStatusChangePtr;
1.333 + TUsbcStatusCallback iStatusCallbackInfo;
1.334 + TAny* iEndpointStatusChangePtr;
1.335 + TUsbcEndpointStatusCallback iEndpointStatusCallbackInfo;
1.336 + TAny* iOtgFeatureChangePtr;
1.337 + TUsbcOtgFeatureCallback iOtgFeatureCallbackInfo;
1.338 + TInt iNumberOfEndpoints;
1.339 + RArray<DPlatChunkHw*> iHwChunksEp0;
1.340 + RArray<DPlatChunkHw*> iHwChunks;
1.341 +
1.342 + TUsbcDeviceState iDeviceState;
1.343 + TUsbcDeviceState iOldDeviceState;
1.344 + TBool iOwnsDeviceControl;
1.345 + TUint iAlternateSetting;
1.346 + TBool iDeviceStatusNeeded;
1.347 + TUsbcDeviceStatusQueue* iStatusFifo;
1.348 + TBool iChannelClosing;
1.349 + TVirtualPinObject *iPinObj1;
1.350 + TVirtualPinObject *iPinObj2;
1.351 + TVirtualPinObject *iPinObj3;
1.352 + TClientDataRequest<TUint> *iStatusChangeReq;
1.353 + TClientDataRequest<TUint> *iEndpointStatusChangeReq;
1.354 + TClientDataRequest<TUint> *iOtgFeatureChangeReq;
1.355 + TEndpointTransferInfo iTfrInfo;
1.356 + };
1.357 +
1.358 +
1.359 +#include <drivers/usbc.inl>
1.360 +
1.361 +#endif // __USBC_H__