os/kernelhwsrv/bsptemplate/asspandvariant/template_assp/pa_usbc.h
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright (c) 2004-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 // template\template_assp\pa_usbc.h
    15 // Platform-dependent USB client controller layer (USB PSL).
    16 // 
    17 //
    18 
    19 
    20 #ifndef __PA_USBC_H__
    21 #define __PA_USBC_H__
    22 
    23 
    24 // This is the header file for the implementation of the USB driver PSL layer for an imaginary USB client
    25 // (device) controller.
    26 // For simplicity's sake we assume the following endpoint layout of the controller.
    27 // We have 5 endpoints in total - two Bulk endpoints (IN and OUT), two Isochronous endpoint (IN and OUT),
    28 // one Interrupt endpoint (IN), and of course endpoint zero (Ep0).
    29 //
    30 // This is the mapping of "Hardware Endpoint Numbers" to "Real Endpoints" (and thus is also
    31 // used as the array index for our local TTemplateAsspUsbcc::iEndpoints[]):
    32 //
    33 //	0 -	 0 (Ep0 OUT)
    34 //	0 -	 1 (Ep0 IN)
    35 //	1 -	 3 (Bulk  IN, Address 0x11, -> EpAddr2Idx(0x11) =  3)
    36 //	2 -	 4 (Bulk OUT, Address 0x02, -> EpAddr2Idx(0x02) =  4)
    37 //	3 -	 7 (Iso   IN, Address 0x13, -> EpAddr2Idx(0x13) =  7)
    38 //	4 -	 8 (Iso  OUT, Address 0x04, -> EpAddr2Idx(0x04) =  8)
    39 //	5 - 11 (Int   IN, Address 0x15, -> EpAddr2Idx(0x15) = 11)
    40 //
    41 // For the reason why this is so (or rather for the perhaps not so obvious system behind it),
    42 // see the comment at the beginning of \e32\drivers\usbcc\ps_usbc.cpp and also the structure
    43 // DeviceEndpoints[] at the top of pa_usbc.cpp.
    44 
    45 // The total number of endpoints in our local endpoint array:
    46 static const TInt KUsbTotalEndpoints = 12;
    47 
    48 // The numbers used in the following macros are 'aRealEndpoint's (i.e. array indices):
    49 #define IS_VALID_ENDPOINT(x)	((x) > 0 && (x) < KUsbTotalEndpoints)
    50 #define IS_OUT_ENDPOINT(x)		((x) == 0 || (x) == 4 || (x) == 8)
    51 #define IS_IN_ENDPOINT(x)		((x) == 1 || (x) == 3 || (x) == 7 || (x) == 11)
    52 #define IS_BULK_IN_ENDPOINT(x)	((x) == 3)
    53 #define IS_BULK_OUT_ENDPOINT(x) ((x) == 4)
    54 #define IS_BULK_ENDPOINT(x)		(IS_BULK_IN_ENDPOINT(x) || IS_BULK_OUT_ENDPOINT(x))
    55 #define IS_ISO_IN_ENDPOINT(x)	((x) == 7)
    56 #define IS_ISO_OUT_ENDPOINT(x)	((x) == 8)
    57 #define IS_ISO_ENDPOINT(x)		(IS_ISO_IN_ENDPOINT(x) || IS_ISO_OUT_ENDPOINT(x))
    58 #define IS_INT_IN_ENDPOINT(x)	((x) == 11)
    59 
    60 // This takes as an index the TTemplateAsspUsbcc::iEndpoints index (== aRealEndpoint) 0..11
    61 // and returns the hardware endpoint number 0..5 (note that not all input indices are valid;
    62 // these will return -1):
    63 static const TInt TemplateAsspEndpoints[KUsbTotalEndpoints] =
    64 	{0, 0, -1, 1, 2, -1, -1, 3, 4, -1, -1, 5};
    65 
    66 // And here is a function to use the above array:
    67 static inline TInt ArrayIdx2TemplateEp(TInt aRealEndpoint)
    68 	{
    69 	if (IS_VALID_ENDPOINT(aRealEndpoint)) return TemplateAsspEndpoints[aRealEndpoint];
    70 	else return -1;
    71 	}
    72 
    73 // Endpoint max packet sizes
    74 static const TInt KEp0MaxPktSz = 16;						// Control
    75 static const TInt KIntMaxPktSz = 8;							// Interrupt
    76 static const TInt KBlkMaxPktSz = 64;						// Bulk
    77 static const TInt KIsoMaxPktSz = 256;						// Isochronous
    78 static const TInt KEp0MaxPktSzMask = KUsbEpSize16;			// Control
    79 static const TInt KIntMaxPktSzMask = KUsbEpSize8;			// Interrupt
    80 static const TInt KBlkMaxPktSzMask = KUsbEpSize64;			// Bulk
    81 static const TInt KIsoMaxPktSzMask = KUsbEpSize256;			// Isochronous
    82 
    83 // 1 ms (i.e. the shortest delay possible with the sort of timer used) seems to give
    84 // the best results, both for Bulk and Iso, and also (in the USBRFLCT test program)
    85 // both for loop tests as well as unidirectional transfers.
    86 static const TInt KRxTimerTimeout = 1;						// milliseconds
    87 
    88 // Used in descriptors
    89 static const TUint16 KUsbVendorId	= KUsbVendorId_Symbian;	// Symbian
    90 static const TUint16 KUsbProductId	= 0x0666;				// bogus...
    91 static const TUint16 KUsbDevRelease = 0x0100;				// bogus... (BCD!)
    92 static const TUint16 KUsbLangId		= 0x0409;				// English (US) Language ID
    93 
    94 // String descriptor default values
    95 static const wchar_t KStringManufacturer[] = L"Nokia Corporation and/or its subsidiary(-ies).";
    96 static const wchar_t KStringProduct[]	   = L"Template USB Test Driver";
    97 static const wchar_t KStringSerialNo[]	   = L"0123456789";
    98 static const wchar_t KStringConfig[]	   = L"First and Last and Always";
    99 
   100 
   101 // We use our own Ep0 state enum:
   102 enum TEp0State
   103 	{
   104 	EP0_IDLE = 0,											// These identifiers don't conform to
   105 	EP0_OUT_DATA_PHASE = 1,									// Symbian's coding standard... ;)
   106 	EP0_IN_DATA_PHASE = 2,
   107 	EP0_END_XFER = 3,
   108 	};
   109 
   110 
   111 class TTemplateAsspUsbcc;
   112 // The lowest level endpoint abstraction
   113 struct TEndpoint
   114 	{
   115 	TEndpoint();
   116 	static void RxTimerCallback(TAny* aPtr);
   117 	// data
   118 	TTemplateAsspUsbcc* iController;						// pointer to controller object
   119 	union
   120 		{
   121 		TUint8* iRxBuf;										// where to store /
   122 		const TUint8* iTxBuf;								// from where to send
   123 		};
   124 	union
   125 		{
   126 		TInt iReceived;										// bytes already rx'ed /
   127 		TInt iTransmitted;									// bytes already tx'ed
   128 		};
   129 	TInt iLength;											// number of bytes to be transferred
   130 	TBool iZlpReqd;											// ZeroLengthPacketRequired
   131 	TBool iNoBuffer;										// no data buffer was available when it was needed
   132 	TBool iDisabled;										// dto but stronger
   133 	TInt iPackets;											// number of packets rx'ed or tx'ed
   134 	TInt iLastError;										//
   135 	TUsbcRequestCallback* iRequest;							//
   136 	NTimer iRxTimer;										//
   137 	TBool iRxTimerSet;										// true if iRxTimer is running
   138 	TBool iRxMoreDataRcvd;									// true if after setting timer data have arrived
   139 	TUsbcPacketArray* iPacketIndex;							// actually TUsbcPacketArray (*)[]
   140 	TUsbcPacketArray* iPacketSize;							// actually TUsbcPacketArray (*)[]
   141 	};
   142 
   143 
   144 // The hardware driver object proper
   145 class TTemplate;
   146 class TTemplateAsspUsbcc : public DUsbClientController
   147 	{
   148 friend void TEndpoint::RxTimerCallback(TAny*);
   149 
   150 public:
   151 	TTemplateAsspUsbcc();
   152 	TInt Construct();
   153 	virtual ~TTemplateAsspUsbcc();
   154 	virtual void DumpRegisters();
   155 
   156 private:
   157 	virtual TInt SetDeviceAddress(TInt aAddress);
   158 	virtual TInt ConfigureEndpoint(TInt aRealEndpoint, const TUsbcEndpointInfo& aEndpointInfo);
   159 	virtual TInt DeConfigureEndpoint(TInt aRealEndpoint);
   160 	virtual TInt AllocateEndpointResource(TInt aRealEndpoint, TUsbcEndpointResource aResource);
   161 	virtual TInt DeAllocateEndpointResource(TInt aRealEndpoint, TUsbcEndpointResource aResource);
   162 	virtual TBool QueryEndpointResource(TInt aRealEndpoint, TUsbcEndpointResource aResource) const;
   163 	virtual TInt OpenDmaChannel(TInt aRealEndpoint);
   164 	virtual void CloseDmaChannel(TInt aRealEndpoint);
   165 	virtual TInt SetupEndpointRead(TInt aRealEndpoint, TUsbcRequestCallback& aCallback);
   166 	virtual TInt SetupEndpointWrite(TInt aRealEndpoint, TUsbcRequestCallback& aCallback);
   167 	virtual TInt CancelEndpointRead(TInt aRealEndpoint);
   168 	virtual TInt CancelEndpointWrite(TInt aRealEndpoint);
   169 	virtual TInt SetupEndpointZeroRead();
   170 	virtual TInt SetupEndpointZeroWrite(const TUint8* aBuffer, TInt aLength, TBool aZlpReqd = EFalse);
   171 	virtual TInt SendEp0ZeroByteStatusPacket();
   172 	virtual TInt StallEndpoint(TInt aRealEndpoint);
   173 	virtual TInt ClearStallEndpoint(TInt aRealEndpoint);
   174 	virtual TInt EndpointStallStatus(TInt aRealEndpoint) const;
   175 	virtual TInt EndpointErrorStatus(TInt aRealEndpoint) const;
   176 	virtual TInt ResetDataToggle(TInt aRealEndpoint);
   177 	virtual TInt SynchFrameNumber() const;
   178 	virtual void SetSynchFrameNumber(TInt aFrameNumber);
   179 	virtual TInt StartUdc();
   180 	virtual TInt StopUdc();
   181 	virtual TInt UdcConnect();
   182 	virtual TInt UdcDisconnect();
   183 	virtual TBool UsbConnectionStatus() const;
   184 	virtual TBool UsbPowerStatus() const;
   185 	virtual TBool DeviceSelfPowered() const;
   186 	virtual const TUsbcEndpointCaps* DeviceEndpointCaps() const;
   187 	virtual TInt DeviceTotalEndpoints() const;
   188 	virtual TBool SoftConnectCaps() const;
   189 	virtual TBool DeviceStateChangeCaps() const;
   190 	virtual void Suspend();
   191 	virtual void Resume();
   192 	virtual void Reset();
   193 	virtual TInt SignalRemoteWakeup();
   194 	virtual void Ep0ReadSetupPktProceed();
   195 	virtual void Ep0ReceiveProceed();
   196 	virtual TDfcQue* DfcQ(TInt aUnit);
   197 
   198 private:
   199 	// general
   200 	void EnableEndpointInterrupt(TInt aEndpoint);
   201 	void DisableEndpointInterrupt(TInt aEndpoint);
   202 	void ClearEndpointInterrupt(TInt aEndpoint);
   203 	void InitialiseUdcRegisters();
   204 	void UdcEnable();
   205 	void UdcDisable();
   206 	TInt SetupUdcInterrupt();
   207 	void ReleaseUdcInterrupt();
   208 	void UdcInterruptService();
   209 	void EndpointIntService(TInt aEndpoint);
   210 	TInt ResetIntService();
   211 	void SuspendIntService();
   212 	void ResumeIntService();
   213 	void SofIntService();
   214 	static void UdcIsr(TAny* aPtr);
   215 	static TInt UsbClientConnectorCallback(TAny* aPtr);
   216 	// endpoint zero
   217 	void Ep0IntService();
   218 	void Ep0ReadSetupPkt();
   219 	void Ep0Receive();
   220 	void Ep0Transmit();
   221 	void Ep0EndXfer();
   222 	void Ep0Cancel();
   223 	void Ep0PrematureStatusOut();
   224 	void Ep0StatusIn();
   225 	void Ep0NextState(TEp0State aNextState);
   226 	// endpoint n with n != 0
   227 	void BulkTransmit(TInt aEndpoint);
   228 	void BulkReceive(TInt aEndpoint);
   229 	void BulkReadRxFifo(TInt aEndpoint);
   230 	void IsoTransmit(TInt aEndpoint);
   231 	void IsoReceive(TInt aEndpoint);
   232 	void IsoReadRxFifo(TInt aEndpoint);
   233 	void IntTransmit(TInt aEndpoint);
   234 	void RxComplete(TEndpoint* aEndpoint);
   235 	void StopRxTimer(TEndpoint* aEndpoint);
   236 
   237 private:
   238 	// general
   239 	TBool iSoftwareConnectable;
   240 	TBool iCableDetectable;
   241 	TBool iCableConnected;
   242 	TBool iBusIsPowered;
   243 	TBool iInitialized;
   244 	TInt (*iUsbClientConnectorCallback)(TAny *);
   245 	TemplateAssp* iAssp;
   246 	// endpoint zero
   247 	TBool iEp0Configured;
   248 	TEp0State iEp0State;
   249 	// endpoints n
   250 	TEndpoint iEndpoints[KUsbTotalEndpoints];				// for how this is indexed, see top of pa_usbc.cpp
   251 	};
   252 
   253 
   254 #endif // __PA_USBC_H__