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