epoc32/include/drivers/usbc.h
branchSymbian3
changeset 4 837f303aceeb
equal deleted inserted replaced
3:e1b950c65cb4 4:837f303aceeb
       
     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".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // e32/include/drivers/usbc.h
       
    15 // Kernel side definitions for the USB Device driver stack (PIL + LDD).
       
    16 // 
       
    17 //
       
    18 
       
    19 /**
       
    20  @file usbc.h
       
    21  @internalTechnology
       
    22 */
       
    23 
       
    24 #ifndef __USBC_H__
       
    25 #define __USBC_H__
       
    26 
       
    27 #include <kernel/kernel.h>
       
    28 #include <kernel/kern_priv.h>
       
    29 #include <kernel/kpower.h>
       
    30 #include <platform.h>
       
    31 
       
    32 #include <d32usbc.h>
       
    33 
       
    34 #include <drivers/usbcshared.h>
       
    35 
       
    36 
       
    37 
       
    38 /** LDD Major version, This should agree with the information in RDevUsbcClient::TVer.
       
    39 */
       
    40 const TInt KUsbcMajorVersion = 0;
       
    41 
       
    42 /** LDD Minor version, This should agree with the information in RDevUsbcClient::TVer.
       
    43 */
       
    44 const TInt KUsbcMinorVersion = 1;
       
    45 
       
    46 /** LDD Build version, This should agree with the information in RDevUsbcClient::TVer.
       
    47 */
       
    48 const TInt KUsbcBuildVersion = KE32BuildVersionNumber;
       
    49 
       
    50 /** Must correspond to the max enum of TRequest + 1;
       
    51 	currently this is ERequestOtgFeaturesNotify = 10.
       
    52 */
       
    53 const TInt KUsbcMaxRequests = 11;
       
    54 
       
    55 //
       
    56 //########################### Logical Device Driver (LDD) #############################
       
    57 //
       
    58 
       
    59 /** USB LDD factory class.
       
    60 */
       
    61 class DUsbcLogDevice : public DLogicalDevice
       
    62 	{
       
    63 public:
       
    64 	DUsbcLogDevice();
       
    65 	virtual TInt Install();
       
    66 	virtual void GetCaps(TDes8& aDes) const;
       
    67 	virtual TInt Create(DLogicalChannelBase*& aChannel);
       
    68 	};
       
    69 
       
    70 
       
    71 /** OUT buffering is a collection of flat buffers. Each is either fillable or drainable.
       
    72 	When one buffer becomes full (notified by the PIL) it is marked as not-fillable and the next
       
    73 	fillable buffer is used. When the buffer has finished draining it is marked as fillable.
       
    74 */
       
    75 class TDmaBuf
       
    76 	{
       
    77 public:
       
    78 	TDmaBuf();
       
    79  	TDmaBuf(TUsbcEndpointInfo* aEndpointInfo, TInt aBandwidthPriority);
       
    80 	~TDmaBuf();
       
    81 	TInt Construct(TUsbcEndpointInfo* aEndpointInfo);
       
    82 	TInt BufferTotalSize() const;
       
    83 	TInt BufferSize() const;
       
    84 	TInt SetBufferAddr(TInt aBufInd, TUint8* aBufAddr);
       
    85 	TInt BufferNumber() const;
       
    86 	void SetMaxPacketSize(TInt aSize);
       
    87 	void Flush();
       
    88 	// Rx (OUT) variants
       
    89 	void RxSetActive();
       
    90 	void RxSetInActive();
       
    91 	TBool RxIsActive();
       
    92 	TBool IsReaderEmpty();
       
    93 	void ReadXferComplete(TInt aNoBytesRx, TInt aNoPacketsRx, TInt aErrorCode);
       
    94 	TInt RxCopyDataToClient(DThread* aThread, TClientBuffer *aTcb, TInt aLength, TUint32& aDestOffset,
       
    95 							TBool aRUS, TBool& aCompleteNow);
       
    96 	TInt RxCopyPacketToClient(DThread* aThread,TClientBuffer *aTcb, TInt aLength);
       
    97 	TInt RxGetNextXfer(TUint8*& aBufferAddr, TUsbcPacketArray*& aIndexArray, TUsbcPacketArray*& aSizeArray,
       
    98 					   TInt& aLength, TPhysAddr& aBufferPhys);
       
    99 	TBool RxIsEnoughSpace(TInt aSize);
       
   100 	inline TInt RxBytesAvailable() const;
       
   101 	inline void IncrementBufferIndex(TInt& aIndex);
       
   102 	inline TInt NoRxPackets() const;
       
   103 	TInt SetDrainable(TInt aBufferNum);
       
   104 	// Tx (IN) variants
       
   105 	void TxSetActive();
       
   106 	void TxSetInActive();
       
   107 	TBool TxIsActive();
       
   108 	TInt TxStoreData(DThread* aThread,TClientBuffer *aTcb, TInt aTxLength, TUint32 aBufferOffset);
       
   109 	TInt TxGetNextXfer(TUint8*& aBufferAddr, TInt& aTxLength, TPhysAddr& aBufferPhys);
       
   110 	TBool ShortPacketExists();
       
   111 
       
   112 #if defined(USBC_LDD_BUFFER_TRACE)
       
   113 	TInt NoRxPacketsAlt() const;
       
   114 	TInt NoRxBytesAlt() const;
       
   115 #endif
       
   116 
       
   117 private:
       
   118 	TBool AdvancePacket();
       
   119 	inline TInt GetCurrentError();
       
   120 	TBool NextDrainableBuffer();
       
   121 	TBool NextFillableBuffer();
       
   122 	void FreeDrainedBuffers();
       
   123 	TInt PeekNextPacketSize();
       
   124 	TInt PeekNextDrainableBuffer();
       
   125 	void ModifyTotalRxBytesAvail(TInt aVal);
       
   126 	void ModifyTotalRxPacketsAvail(TInt aVal);
       
   127 	void AddToDrainQueue(TInt aBufferIndex);
       
   128 	inline TInt CopyToUser(DThread* aThread, const TUint8* aSourceAddr, TInt aLength,
       
   129 						   TClientBuffer *aTcb, TUint32& aDestOffset);
       
   130 private:
       
   131 	TInt iExtractOffset;									// offset into current packet for data read
       
   132 	TInt iMaxPacketSize;
       
   133 	TInt iNumberofBuffers;
       
   134 	TInt iBufSz;
       
   135 	TBool iRxActive;
       
   136 	TBool iTxActive;
       
   137 	TInt iTotalRxBytesAvail;
       
   138 	TInt iTotalRxPacketsAvail;
       
   139 	//
       
   140 	TUint8* iBufBasePtr;
       
   141 	TUint8* iCurrentDrainingBuffer;
       
   142 	TInt iCurrentDrainingBufferIndex;
       
   143 	TInt iCurrentFillingBufferIndex;
       
   144 	TUint iCurrentPacket;
       
   145 	TUsbcPacketArray* iCurrentPacketIndexArray;
       
   146 	TUsbcPacketArray* iCurrentPacketSizeArray;
       
   147 	TUint8* iBuffers[KUsbcDmaBufNumMax];
       
   148 	TBool iDrainable[KUsbcDmaBufNumMax];
       
   149 	TUsbcPacketArray iPacketInfoStorage[KUsbcDmaBufNumMax * KUsbcDmaBufNumArrays * KUsbcDmaBufMaxPkts];
       
   150 	TUsbcPacketArray* iPacketIndex[KUsbcDmaBufNumMax];
       
   151 	TUsbcPacketArray* iPacketSize[KUsbcDmaBufNumMax];
       
   152 	TUint iNumberofBytesRx[KUsbcDmaBufNumMax];
       
   153 	TUint iNumberofPacketsRx[KUsbcDmaBufNumMax];
       
   154 	TInt iError[KUsbcDmaBufNumMax];
       
   155 	TPhysAddr iBufferPhys[KUsbcDmaBufNumMax];
       
   156 	TBool iCanBeFreed[KUsbcDmaBufNumMax];
       
   157 	TInt iDrainQueue[KUsbcDmaBufNumMax + 1];
       
   158 	TInt iDrainQueueIndex;
       
   159 	TUint iEndpointType;
       
   160 
       
   161 #if defined(USBC_LDD_BUFFER_TRACE)
       
   162 	TInt iFillingOrder;
       
   163 	TInt iFillingOrderArray[KUsbcDmaBufNumMax];
       
   164 	TInt iDrainingOrder;
       
   165  	TUint iNumberofBytesRxRemain[KUsbcDmaBufNumMax];
       
   166  	TUint iNumberofPacketsRxRemain[KUsbcDmaBufNumMax];
       
   167 #endif
       
   168 	};
       
   169 
       
   170 
       
   171 class DLddUsbcChannel;
       
   172 
       
   173 /** Endpoint tracking for the LDD buffering etc.
       
   174 */
       
   175 class TUsbcEndpoint
       
   176 	{
       
   177 public:
       
   178 	TUsbcEndpoint();
       
   179 	TUsbcEndpoint(DLddUsbcChannel* aLDD, DUsbClientController* aController,
       
   180 				  const TUsbcEndpointInfo* aEndpointInfo, TInt aEndpointNum,
       
   181 				  TInt aBandwidthPriority);
       
   182 	~TUsbcEndpoint();
       
   183 	TInt Construct();
       
   184 	TInt TryToStartRead(TBool aReEntrant);
       
   185 	TInt TryToStartWrite(TEndpointTransferInfo* pTfr);
       
   186 	TInt CopyToClient(DThread* aThread, TClientBuffer *aTcb);
       
   187 	TInt CopyToClient(DThread* aClient, TBool& aCompleteNow, TClientBuffer *aTcb);
       
   188 	TInt ContinueWrite();
       
   189 	void SetMaxPacketSize(TInt aSize);
       
   190 	void CancelTransfer(DThread* aThread, TClientBuffer *aTcb);
       
   191 	void AbortTransfer();
       
   192 	inline TUsbcEndpointInfo* EndpointInfo();
       
   193 	inline TInt RxBytesAvailable() const;
       
   194 
       
   195 	inline TInt BufferSize() const;
       
   196 	inline TInt SetBufferAddr( TInt aBufInd, TUint8* aAddr);
       
   197 	inline TInt BufferNumber() const;
       
   198 
       
   199 	inline void SetTransferInfo(TEndpointTransferInfo* aTransferInfo);
       
   200 	inline void ResetTransferInfo();
       
   201 	inline void SetClientReadPending(TBool aVal);
       
   202 	inline void SetClientWritePending(TBool aVal);
       
   203 	inline TBool ClientWritePending();
       
   204 	inline TBool ClientReadPending();
       
   205 	inline void SetRealEpNumber(TInt aRealEpNumber);
       
   206 	inline TInt RealEpNumber() const;
       
   207 
       
   208 public:
       
   209 	TDmaBuf* iDmaBuffers;
       
   210 
       
   211 private:
       
   212 	static void RequestCallback(TAny* aTUsbcEndpoint);
       
   213 	void TxComplete();
       
   214 	TInt RxComplete(TBool aReEntrant);
       
   215 	void RxCompleteNow();
       
   216 	TInt EndpointComplete();
       
   217 
       
   218 private:
       
   219 	DUsbClientController* iController;
       
   220 	TUsbcEndpointInfo iEndpointInfo;
       
   221 	TEndpointTransferInfo iTransferInfo;
       
   222 	TBool iClientReadPending;
       
   223 	TBool iClientWritePending;
       
   224 	TInt iEndpointNumber;
       
   225 	TInt iRealEpNumber;
       
   226 	DLddUsbcChannel* iLdd;
       
   227 	TInt iError;
       
   228 	TUsbcRequestCallback* iRequestCallbackInfo;
       
   229 	TUint32 iBytesTransferred;
       
   230 	TInt iBandwidthPriority;
       
   231 	};
       
   232 
       
   233 
       
   234 /** Linked list of 'alternate setting' info for use by the LDD.
       
   235 */
       
   236 class TUsbcAlternateSettingList
       
   237 	{
       
   238 public:
       
   239 	TUsbcAlternateSettingList();
       
   240 	~TUsbcAlternateSettingList();
       
   241 
       
   242 public:
       
   243 	TUsbcAlternateSettingList* iNext;
       
   244 	TInt iNumberOfEndpoints;
       
   245 	TUint iSetting;
       
   246 	TInt iEpNumDeOrderedByBufSize[KMaxEndpointsPerClient + 1];
       
   247 	TUsbcEndpoint* iEndpoint[KMaxEndpointsPerClient + 1];
       
   248 	};
       
   249 
       
   250 
       
   251 struct TClientAsynchNotify
       
   252 	{
       
   253 		TClientBufferRequest *iBufferRequest;
       
   254 		TClientBuffer *iClientBuffer;
       
   255 		void Reset();
       
   256 	};
       
   257 /** The channel class - the actual USB LDD.
       
   258 */
       
   259 class DLddUsbcChannel : public DLogicalChannel
       
   260 	{
       
   261 public:
       
   262 	DLddUsbcChannel();
       
   263 	~DLddUsbcChannel();
       
   264 	virtual TInt SendMsg(TMessageBase * aMsg);
       
   265 	TInt PreSendRequest(TMessageBase * aMsg,TInt aReqNo, TRequestStatus* aStatus, TAny* a1, TAny* a2);
       
   266 	TInt SendControl(TMessageBase* aMsg);
       
   267 	virtual void HandleMsg(TMessageBase* aMsg);
       
   268 	virtual TInt DoCreate(TInt aUnit, const TDesC8* aInfo, const TVersion& aVer);
       
   269 	virtual TInt RequestUserHandle(DThread* aThread, TOwnerType aType);
       
   270 	TInt DoRxComplete(TUsbcEndpoint* aTUsbcEndpoint, TInt aEndpoint, TBool aReentrant);
       
   271 	void DoRxCompleteNow(TUsbcEndpoint* aTUsbcEndpoint, TInt aEndpoint);
       
   272 	void DoTxComplete(TUsbcEndpoint* aTUsbcEndpoint, TInt aEndpoint, TInt aError);
       
   273 	inline DThread* Client() const {return iClient;}
       
   274 	inline TBool ChannelClosing() const {return iChannelClosing;}
       
   275 	inline TUint AlternateSetting() const {return iAlternateSetting;}
       
   276 	TClientBuffer *GetClientBuffer(TInt aEndpoint);
       
   277 
       
   278 private:
       
   279 	TInt DoCancel(TInt aReqNo);
       
   280 	void DoRequest(TInt aReqNo, TRequestStatus* aStatus, TAny* a1, TAny* a2);
       
   281 	TInt DoControl(TInt aFunction, TAny* a1, TAny* a2);
       
   282 	TInt DoTransferAsyncReq(TInt aEndpointNum, TAny* a1, TAny* a2, TBool& aNeedsCompletion);
       
   283 	TInt DoOtherAsyncReq(TInt aReqNo, TAny* a1, TAny* a2, TBool& aNeedsCompletion);
       
   284 	TBool AlternateDeviceStateTestComplete();
       
   285 	TInt SetInterface(TInt aInterfaceNum, TUsbcIfcInfo* aUserInterfaceInfoBuf);
       
   286 	void StartEpReads();
       
   287 	void DestroyAllInterfaces();
       
   288 	void DestroyInterface(TUint aInterface);
       
   289 	void DestroyEp0();
       
   290 	inline TBool ValidEndpoint(TInt aEndpoint);
       
   291 	TInt DoEmergencyComplete();
       
   292 	void ReadDes8(const TAny* aPtr, TDes8& aDes);
       
   293 	TInt SetupEp0();
       
   294 	DPlatChunkHw* ReAllocate(TInt aBuffersize, DPlatChunkHw* aHwChunk, TUint32 aCacheAttribs);
       
   295 	DPlatChunkHw* Allocate(TInt aBuffersize, TUint32 aCacheAttribs);
       
   296 	void ClosePhysicalChunk(DPlatChunkHw* &aHwChunk);
       
   297 	void CancelNotifyEndpointStatus();
       
   298 	void CancelNotifyOtgFeatures();
       
   299 	static void StatusChangeCallback(TAny* aDLddUsbcChannel);
       
   300 	static void EndpointStatusChangeCallback(TAny* aDLddUsbcChannel);
       
   301 	static void OtgFeatureChangeCallback(TAny* aDLddUsbcChannel);
       
   302 	static void EmergencyCompleteDfc(TAny* aDLddUsbcChannel);
       
   303 	void DeConfigure(TInt aErrorCode);
       
   304 	TInt SelectAlternateSetting(TUint aAlternateSetting);
       
   305 	TInt EpFromAlternateSetting(TUint aAlternateSetting, TInt aEndpoint);
       
   306 	TInt ProcessAlternateSetting(TUint aAlternateSetting);
       
   307 	TInt ProcessDeviceState(TUsbcDeviceState aDeviceState);
       
   308 	void ResetInterface(TInt aErrorCode);
       
   309 	void AbortInterface();
       
   310 	// Set buffer address of the interface
       
   311 	void ReSetInterfaceMemory(TUsbcAlternateSettingList* aAlternateSettingListRec,
       
   312 	        RArray<DPlatChunkHw*> &aHwChunks );
       
   313 	void UpdateEndpointSizes();
       
   314 	// Check and alloc memory for the interface
       
   315 	TInt SetupInterfaceMemory(RArray<DPlatChunkHw*> &aHwChunks, 
       
   316 	        TUint32 aCacheAttribs );
       
   317 	void PanicClientThread(TInt aReason);
       
   318 	TInt PinMemory(TDesC8 *aDes, TVirtualPinObject *iPinObj); //Descriptor pinning helper.
       
   319 	void CompleteBufferRequest(DThread* aThread, TInt aReqNo, TInt aReason);
       
   320 private:
       
   321 	DUsbClientController* iController;
       
   322 	DThread* iClient;
       
   323 	TBool iValidInterface;
       
   324 	TUsbcAlternateSettingList* iAlternateSettingList;
       
   325 	TUsbcEndpoint* iEndpoint[KMaxEndpointsPerClient + 1];	// include ep0
       
   326 	TRequestStatus* iRequestStatus[KUsbcMaxRequests];
       
   327 	TClientAsynchNotify* iClientAsynchNotify[KUsbcMaxRequests];
       
   328 	TUsbcClientCallback iCompleteAllCallbackInfo;
       
   329 	TAny* iStatusChangePtr;
       
   330 	TUsbcStatusCallback iStatusCallbackInfo;
       
   331 	TAny* iEndpointStatusChangePtr;
       
   332 	TUsbcEndpointStatusCallback iEndpointStatusCallbackInfo;
       
   333 	TAny* iOtgFeatureChangePtr;
       
   334 	TUsbcOtgFeatureCallback iOtgFeatureCallbackInfo;
       
   335 	TInt iNumberOfEndpoints;
       
   336     RArray<DPlatChunkHw*> iHwChunksEp0;
       
   337     RArray<DPlatChunkHw*> iHwChunks;
       
   338 
       
   339 	TUsbcDeviceState iDeviceState;
       
   340 	TUsbcDeviceState iOldDeviceState;
       
   341 	TBool iOwnsDeviceControl;
       
   342 	TUint iAlternateSetting;
       
   343 	TBool iDeviceStatusNeeded;
       
   344 	TUsbcDeviceStatusQueue* iStatusFifo;
       
   345 	TBool iChannelClosing;
       
   346 	TVirtualPinObject *iPinObj1;
       
   347 	TVirtualPinObject *iPinObj2;
       
   348 	TVirtualPinObject *iPinObj3;
       
   349 	TClientDataRequest<TUint> *iStatusChangeReq;
       
   350 	TClientDataRequest<TUint> *iEndpointStatusChangeReq;
       
   351 	TClientDataRequest<TUint> *iOtgFeatureChangeReq;
       
   352 	TEndpointTransferInfo iTfrInfo;
       
   353 	};
       
   354 
       
   355 
       
   356 #include <drivers/usbc.inl>
       
   357 
       
   358 #endif	// __USBC_H__