os/kernelhwsrv/bsptemplate/asspandvariant/template_assp/pa_usbc.h
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/bsptemplate/asspandvariant/template_assp/pa_usbc.h	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,254 @@
     1.4 +// Copyright (c) 2004-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 +// template\template_assp\pa_usbc.h
    1.18 +// Platform-dependent USB client controller layer (USB PSL).
    1.19 +// 
    1.20 +//
    1.21 +
    1.22 +
    1.23 +#ifndef __PA_USBC_H__
    1.24 +#define __PA_USBC_H__
    1.25 +
    1.26 +
    1.27 +// This is the header file for the implementation of the USB driver PSL layer for an imaginary USB client
    1.28 +// (device) controller.
    1.29 +// For simplicity's sake we assume the following endpoint layout of the controller.
    1.30 +// We have 5 endpoints in total - two Bulk endpoints (IN and OUT), two Isochronous endpoint (IN and OUT),
    1.31 +// one Interrupt endpoint (IN), and of course endpoint zero (Ep0).
    1.32 +//
    1.33 +// This is the mapping of "Hardware Endpoint Numbers" to "Real Endpoints" (and thus is also
    1.34 +// used as the array index for our local TTemplateAsspUsbcc::iEndpoints[]):
    1.35 +//
    1.36 +//	0 -	 0 (Ep0 OUT)
    1.37 +//	0 -	 1 (Ep0 IN)
    1.38 +//	1 -	 3 (Bulk  IN, Address 0x11, -> EpAddr2Idx(0x11) =  3)
    1.39 +//	2 -	 4 (Bulk OUT, Address 0x02, -> EpAddr2Idx(0x02) =  4)
    1.40 +//	3 -	 7 (Iso   IN, Address 0x13, -> EpAddr2Idx(0x13) =  7)
    1.41 +//	4 -	 8 (Iso  OUT, Address 0x04, -> EpAddr2Idx(0x04) =  8)
    1.42 +//	5 - 11 (Int   IN, Address 0x15, -> EpAddr2Idx(0x15) = 11)
    1.43 +//
    1.44 +// For the reason why this is so (or rather for the perhaps not so obvious system behind it),
    1.45 +// see the comment at the beginning of \e32\drivers\usbcc\ps_usbc.cpp and also the structure
    1.46 +// DeviceEndpoints[] at the top of pa_usbc.cpp.
    1.47 +
    1.48 +// The total number of endpoints in our local endpoint array:
    1.49 +static const TInt KUsbTotalEndpoints = 12;
    1.50 +
    1.51 +// The numbers used in the following macros are 'aRealEndpoint's (i.e. array indices):
    1.52 +#define IS_VALID_ENDPOINT(x)	((x) > 0 && (x) < KUsbTotalEndpoints)
    1.53 +#define IS_OUT_ENDPOINT(x)		((x) == 0 || (x) == 4 || (x) == 8)
    1.54 +#define IS_IN_ENDPOINT(x)		((x) == 1 || (x) == 3 || (x) == 7 || (x) == 11)
    1.55 +#define IS_BULK_IN_ENDPOINT(x)	((x) == 3)
    1.56 +#define IS_BULK_OUT_ENDPOINT(x) ((x) == 4)
    1.57 +#define IS_BULK_ENDPOINT(x)		(IS_BULK_IN_ENDPOINT(x) || IS_BULK_OUT_ENDPOINT(x))
    1.58 +#define IS_ISO_IN_ENDPOINT(x)	((x) == 7)
    1.59 +#define IS_ISO_OUT_ENDPOINT(x)	((x) == 8)
    1.60 +#define IS_ISO_ENDPOINT(x)		(IS_ISO_IN_ENDPOINT(x) || IS_ISO_OUT_ENDPOINT(x))
    1.61 +#define IS_INT_IN_ENDPOINT(x)	((x) == 11)
    1.62 +
    1.63 +// This takes as an index the TTemplateAsspUsbcc::iEndpoints index (== aRealEndpoint) 0..11
    1.64 +// and returns the hardware endpoint number 0..5 (note that not all input indices are valid;
    1.65 +// these will return -1):
    1.66 +static const TInt TemplateAsspEndpoints[KUsbTotalEndpoints] =
    1.67 +	{0, 0, -1, 1, 2, -1, -1, 3, 4, -1, -1, 5};
    1.68 +
    1.69 +// And here is a function to use the above array:
    1.70 +static inline TInt ArrayIdx2TemplateEp(TInt aRealEndpoint)
    1.71 +	{
    1.72 +	if (IS_VALID_ENDPOINT(aRealEndpoint)) return TemplateAsspEndpoints[aRealEndpoint];
    1.73 +	else return -1;
    1.74 +	}
    1.75 +
    1.76 +// Endpoint max packet sizes
    1.77 +static const TInt KEp0MaxPktSz = 16;						// Control
    1.78 +static const TInt KIntMaxPktSz = 8;							// Interrupt
    1.79 +static const TInt KBlkMaxPktSz = 64;						// Bulk
    1.80 +static const TInt KIsoMaxPktSz = 256;						// Isochronous
    1.81 +static const TInt KEp0MaxPktSzMask = KUsbEpSize16;			// Control
    1.82 +static const TInt KIntMaxPktSzMask = KUsbEpSize8;			// Interrupt
    1.83 +static const TInt KBlkMaxPktSzMask = KUsbEpSize64;			// Bulk
    1.84 +static const TInt KIsoMaxPktSzMask = KUsbEpSize256;			// Isochronous
    1.85 +
    1.86 +// 1 ms (i.e. the shortest delay possible with the sort of timer used) seems to give
    1.87 +// the best results, both for Bulk and Iso, and also (in the USBRFLCT test program)
    1.88 +// both for loop tests as well as unidirectional transfers.
    1.89 +static const TInt KRxTimerTimeout = 1;						// milliseconds
    1.90 +
    1.91 +// Used in descriptors
    1.92 +static const TUint16 KUsbVendorId	= KUsbVendorId_Symbian;	// Symbian
    1.93 +static const TUint16 KUsbProductId	= 0x0666;				// bogus...
    1.94 +static const TUint16 KUsbDevRelease = 0x0100;				// bogus... (BCD!)
    1.95 +static const TUint16 KUsbLangId		= 0x0409;				// English (US) Language ID
    1.96 +
    1.97 +// String descriptor default values
    1.98 +static const wchar_t KStringManufacturer[] = L"Nokia Corporation and/or its subsidiary(-ies).";
    1.99 +static const wchar_t KStringProduct[]	   = L"Template USB Test Driver";
   1.100 +static const wchar_t KStringSerialNo[]	   = L"0123456789";
   1.101 +static const wchar_t KStringConfig[]	   = L"First and Last and Always";
   1.102 +
   1.103 +
   1.104 +// We use our own Ep0 state enum:
   1.105 +enum TEp0State
   1.106 +	{
   1.107 +	EP0_IDLE = 0,											// These identifiers don't conform to
   1.108 +	EP0_OUT_DATA_PHASE = 1,									// Symbian's coding standard... ;)
   1.109 +	EP0_IN_DATA_PHASE = 2,
   1.110 +	EP0_END_XFER = 3,
   1.111 +	};
   1.112 +
   1.113 +
   1.114 +class TTemplateAsspUsbcc;
   1.115 +// The lowest level endpoint abstraction
   1.116 +struct TEndpoint
   1.117 +	{
   1.118 +	TEndpoint();
   1.119 +	static void RxTimerCallback(TAny* aPtr);
   1.120 +	// data
   1.121 +	TTemplateAsspUsbcc* iController;						// pointer to controller object
   1.122 +	union
   1.123 +		{
   1.124 +		TUint8* iRxBuf;										// where to store /
   1.125 +		const TUint8* iTxBuf;								// from where to send
   1.126 +		};
   1.127 +	union
   1.128 +		{
   1.129 +		TInt iReceived;										// bytes already rx'ed /
   1.130 +		TInt iTransmitted;									// bytes already tx'ed
   1.131 +		};
   1.132 +	TInt iLength;											// number of bytes to be transferred
   1.133 +	TBool iZlpReqd;											// ZeroLengthPacketRequired
   1.134 +	TBool iNoBuffer;										// no data buffer was available when it was needed
   1.135 +	TBool iDisabled;										// dto but stronger
   1.136 +	TInt iPackets;											// number of packets rx'ed or tx'ed
   1.137 +	TInt iLastError;										//
   1.138 +	TUsbcRequestCallback* iRequest;							//
   1.139 +	NTimer iRxTimer;										//
   1.140 +	TBool iRxTimerSet;										// true if iRxTimer is running
   1.141 +	TBool iRxMoreDataRcvd;									// true if after setting timer data have arrived
   1.142 +	TUsbcPacketArray* iPacketIndex;							// actually TUsbcPacketArray (*)[]
   1.143 +	TUsbcPacketArray* iPacketSize;							// actually TUsbcPacketArray (*)[]
   1.144 +	};
   1.145 +
   1.146 +
   1.147 +// The hardware driver object proper
   1.148 +class TTemplate;
   1.149 +class TTemplateAsspUsbcc : public DUsbClientController
   1.150 +	{
   1.151 +friend void TEndpoint::RxTimerCallback(TAny*);
   1.152 +
   1.153 +public:
   1.154 +	TTemplateAsspUsbcc();
   1.155 +	TInt Construct();
   1.156 +	virtual ~TTemplateAsspUsbcc();
   1.157 +	virtual void DumpRegisters();
   1.158 +
   1.159 +private:
   1.160 +	virtual TInt SetDeviceAddress(TInt aAddress);
   1.161 +	virtual TInt ConfigureEndpoint(TInt aRealEndpoint, const TUsbcEndpointInfo& aEndpointInfo);
   1.162 +	virtual TInt DeConfigureEndpoint(TInt aRealEndpoint);
   1.163 +	virtual TInt AllocateEndpointResource(TInt aRealEndpoint, TUsbcEndpointResource aResource);
   1.164 +	virtual TInt DeAllocateEndpointResource(TInt aRealEndpoint, TUsbcEndpointResource aResource);
   1.165 +	virtual TBool QueryEndpointResource(TInt aRealEndpoint, TUsbcEndpointResource aResource) const;
   1.166 +	virtual TInt OpenDmaChannel(TInt aRealEndpoint);
   1.167 +	virtual void CloseDmaChannel(TInt aRealEndpoint);
   1.168 +	virtual TInt SetupEndpointRead(TInt aRealEndpoint, TUsbcRequestCallback& aCallback);
   1.169 +	virtual TInt SetupEndpointWrite(TInt aRealEndpoint, TUsbcRequestCallback& aCallback);
   1.170 +	virtual TInt CancelEndpointRead(TInt aRealEndpoint);
   1.171 +	virtual TInt CancelEndpointWrite(TInt aRealEndpoint);
   1.172 +	virtual TInt SetupEndpointZeroRead();
   1.173 +	virtual TInt SetupEndpointZeroWrite(const TUint8* aBuffer, TInt aLength, TBool aZlpReqd = EFalse);
   1.174 +	virtual TInt SendEp0ZeroByteStatusPacket();
   1.175 +	virtual TInt StallEndpoint(TInt aRealEndpoint);
   1.176 +	virtual TInt ClearStallEndpoint(TInt aRealEndpoint);
   1.177 +	virtual TInt EndpointStallStatus(TInt aRealEndpoint) const;
   1.178 +	virtual TInt EndpointErrorStatus(TInt aRealEndpoint) const;
   1.179 +	virtual TInt ResetDataToggle(TInt aRealEndpoint);
   1.180 +	virtual TInt SynchFrameNumber() const;
   1.181 +	virtual void SetSynchFrameNumber(TInt aFrameNumber);
   1.182 +	virtual TInt StartUdc();
   1.183 +	virtual TInt StopUdc();
   1.184 +	virtual TInt UdcConnect();
   1.185 +	virtual TInt UdcDisconnect();
   1.186 +	virtual TBool UsbConnectionStatus() const;
   1.187 +	virtual TBool UsbPowerStatus() const;
   1.188 +	virtual TBool DeviceSelfPowered() const;
   1.189 +	virtual const TUsbcEndpointCaps* DeviceEndpointCaps() const;
   1.190 +	virtual TInt DeviceTotalEndpoints() const;
   1.191 +	virtual TBool SoftConnectCaps() const;
   1.192 +	virtual TBool DeviceStateChangeCaps() const;
   1.193 +	virtual void Suspend();
   1.194 +	virtual void Resume();
   1.195 +	virtual void Reset();
   1.196 +	virtual TInt SignalRemoteWakeup();
   1.197 +	virtual void Ep0ReadSetupPktProceed();
   1.198 +	virtual void Ep0ReceiveProceed();
   1.199 +	virtual TDfcQue* DfcQ(TInt aUnit);
   1.200 +
   1.201 +private:
   1.202 +	// general
   1.203 +	void EnableEndpointInterrupt(TInt aEndpoint);
   1.204 +	void DisableEndpointInterrupt(TInt aEndpoint);
   1.205 +	void ClearEndpointInterrupt(TInt aEndpoint);
   1.206 +	void InitialiseUdcRegisters();
   1.207 +	void UdcEnable();
   1.208 +	void UdcDisable();
   1.209 +	TInt SetupUdcInterrupt();
   1.210 +	void ReleaseUdcInterrupt();
   1.211 +	void UdcInterruptService();
   1.212 +	void EndpointIntService(TInt aEndpoint);
   1.213 +	TInt ResetIntService();
   1.214 +	void SuspendIntService();
   1.215 +	void ResumeIntService();
   1.216 +	void SofIntService();
   1.217 +	static void UdcIsr(TAny* aPtr);
   1.218 +	static TInt UsbClientConnectorCallback(TAny* aPtr);
   1.219 +	// endpoint zero
   1.220 +	void Ep0IntService();
   1.221 +	void Ep0ReadSetupPkt();
   1.222 +	void Ep0Receive();
   1.223 +	void Ep0Transmit();
   1.224 +	void Ep0EndXfer();
   1.225 +	void Ep0Cancel();
   1.226 +	void Ep0PrematureStatusOut();
   1.227 +	void Ep0StatusIn();
   1.228 +	void Ep0NextState(TEp0State aNextState);
   1.229 +	// endpoint n with n != 0
   1.230 +	void BulkTransmit(TInt aEndpoint);
   1.231 +	void BulkReceive(TInt aEndpoint);
   1.232 +	void BulkReadRxFifo(TInt aEndpoint);
   1.233 +	void IsoTransmit(TInt aEndpoint);
   1.234 +	void IsoReceive(TInt aEndpoint);
   1.235 +	void IsoReadRxFifo(TInt aEndpoint);
   1.236 +	void IntTransmit(TInt aEndpoint);
   1.237 +	void RxComplete(TEndpoint* aEndpoint);
   1.238 +	void StopRxTimer(TEndpoint* aEndpoint);
   1.239 +
   1.240 +private:
   1.241 +	// general
   1.242 +	TBool iSoftwareConnectable;
   1.243 +	TBool iCableDetectable;
   1.244 +	TBool iCableConnected;
   1.245 +	TBool iBusIsPowered;
   1.246 +	TBool iInitialized;
   1.247 +	TInt (*iUsbClientConnectorCallback)(TAny *);
   1.248 +	TemplateAssp* iAssp;
   1.249 +	// endpoint zero
   1.250 +	TBool iEp0Configured;
   1.251 +	TEp0State iEp0State;
   1.252 +	// endpoints n
   1.253 +	TEndpoint iEndpoints[KUsbTotalEndpoints];				// for how this is indexed, see top of pa_usbc.cpp
   1.254 +	};
   1.255 +
   1.256 +
   1.257 +#endif // __PA_USBC_H__