kernel/eka/drivers/usbc/d_usbc.cpp
changeset 33 0173bcd7697c
parent 0 a41df078684a
child 62 4a8fed1c0ef6
child 90 947f0dc9f7a8
equal deleted inserted replaced
31:56f325a607ea 33:0173bcd7697c
   125 	  iEndpointStatusChangePtr(NULL),
   125 	  iEndpointStatusChangePtr(NULL),
   126 	  iEndpointStatusCallbackInfo(this, DLddUsbcChannel::EndpointStatusChangeCallback,
   126 	  iEndpointStatusCallbackInfo(this, DLddUsbcChannel::EndpointStatusChangeCallback,
   127 								  KUsbRequestCallbackPriority),
   127 								  KUsbRequestCallbackPriority),
   128       iOtgFeatureChangePtr(NULL),
   128       iOtgFeatureChangePtr(NULL),
   129       iOtgFeatureCallbackInfo(this, DLddUsbcChannel::OtgFeatureChangeCallback, KUsbRequestCallbackPriority),
   129       iOtgFeatureCallbackInfo(this, DLddUsbcChannel::OtgFeatureChangeCallback, KUsbRequestCallbackPriority),
   130 	  iBufferBaseEp0(NULL),
       
   131 	  iBufferSizeEp0(0),
       
   132 	  iNumberOfEndpoints(0),
   130 	  iNumberOfEndpoints(0),
   133 	  iHwChunkIN(NULL),
       
   134 	  iHwChunkOUT(NULL),
       
   135 	  iHwChunkEp0(NULL),
       
   136 	  iDeviceState(EUsbcDeviceStateUndefined),
   131 	  iDeviceState(EUsbcDeviceStateUndefined),
   137 	  iOwnsDeviceControl(EFalse),
   132 	  iOwnsDeviceControl(EFalse),
   138 	  iAlternateSetting(0),
   133 	  iAlternateSetting(0),
   139 	  iDeviceStatusNeeded(EFalse),
   134 	  iDeviceStatusNeeded(EFalse),
   140 	  iChannelClosing(EFalse)
   135 	  iChannelClosing(EFalse)
  1729 
  1724 
  1730 	// [The next 4 variables have to be initialized here because of the goto's that follow.]
  1725 	// [The next 4 variables have to be initialized here because of the goto's that follow.]
  1731 	// Both IN and OUT buffers will be fully cached:
  1726 	// Both IN and OUT buffers will be fully cached:
  1732 	const TUint32 cacheAttribs = EMapAttrSupRw | EMapAttrCachedMax;
  1727 	const TUint32 cacheAttribs = EMapAttrSupRw | EMapAttrCachedMax;
  1733 	const TUint32 bandwidthPriority = aInfoBuf->iBandwidthPriority;
  1728 	const TUint32 bandwidthPriority = aInfoBuf->iBandwidthPriority;
  1734 	TInt totalINBufferSize = 0;
  1729 
  1735 	TInt totalOUTBufferSize = 0;
  1730 	// Supports ep0+5 endpoints
  1736 
       
  1737 	TInt real_ep_numbers[6] = {-1, -1, -1, -1, -1, -1};
  1731 	TInt real_ep_numbers[6] = {-1, -1, -1, -1, -1, -1};
  1738 
  1732 
  1739     // See if PIL will accept this interface
  1733     // See if PIL will accept this interface
  1740 	__KTRACE_OPT(KUSB, Kern::Printf("SetInterface Calling controller"));
  1734 	__KTRACE_OPT(KUSB, Kern::Printf("SetInterface Calling controller"));
  1741 	r = iController->SetInterface(this,
  1735 	r = iController->SetInterface(this,
  1776 		{
  1770 		{
  1777 		r = KErrNoMemory;
  1771 		r = KErrNoMemory;
  1778 		goto F1;
  1772 		goto F1;
  1779 		}
  1773 		}
  1780 
  1774 
       
  1775 	__KTRACE_OPT(KUSB, Kern::Printf("DLddUsbcChannel::SetInterface num_endpoints=%d", num_endpoints));
       
  1776 
  1781 	// other endpoints
  1777 	// other endpoints
       
  1778 	// calculate the total buffer size
  1782 	for (TInt i = 1; i <= num_endpoints; i++, pEndpointData++)
  1779 	for (TInt i = 1; i <= num_endpoints; i++, pEndpointData++)
  1783 		{
  1780 		{
  1784 		__KTRACE_OPT(KUSB, Kern::Printf("SetInterface for ep=%d", i));
  1781 		__KTRACE_OPT(KUSB, Kern::Printf("SetInterface for ep=%d", i));
  1785 		if (!ValidateEndpoint(pEndpointData))
  1782 		if (!ValidateEndpoint(pEndpointData))
  1786 			{
  1783 			{
  1787 			r = KErrUsbBadEndpoint;
  1784 			r = KErrUsbBadEndpoint;
  1788 			goto F2;
  1785 			goto F2;
  1789 			}
  1786 			}
       
  1787 
  1790 		TUsbcEndpoint* ep = new TUsbcEndpoint(this, iController, pEndpointData, i, bandwidthPriority);
  1788 		TUsbcEndpoint* ep = new TUsbcEndpoint(this, iController, pEndpointData, i, bandwidthPriority);
  1791 		alternateSettingListRec->iEndpoint[i] = ep;
  1789 		alternateSettingListRec->iEndpoint[i] = ep;
  1792 		if (!ep)
  1790 		if (!ep)
  1793 			{
  1791 			{
  1794 			r = KErrNoMemory;
  1792 			r = KErrNoMemory;
  1797 		if (ep->Construct() != KErrNone)
  1795 		if (ep->Construct() != KErrNone)
  1798 			{
  1796 			{
  1799 			r = KErrNoMemory;
  1797 			r = KErrNoMemory;
  1800 			goto F2;
  1798 			goto F2;
  1801 			}
  1799 			}
  1802 		if (pEndpointData->iDir == KUsbEpDirIn)
  1800 
  1803 			{
       
  1804 			totalINBufferSize += ep->BufferTotalSize();
       
  1805 			__KTRACE_OPT(KUSB, Kern::Printf("IN buffering now %d", totalINBufferSize));
       
  1806 			}
       
  1807 		else if (pEndpointData->iDir == KUsbEpDirOut)
       
  1808 			{
       
  1809 			totalOUTBufferSize += ep->BufferTotalSize();
       
  1810 			__KTRACE_OPT(KUSB, Kern::Printf("OUT buffering now %d", totalOUTBufferSize));
       
  1811 			}
       
  1812 		__KTRACE_OPT(KUSB, Kern::Printf("SetInterface for ep=%d rec=0x%08x ep==0x%08x",
  1801 		__KTRACE_OPT(KUSB, Kern::Printf("SetInterface for ep=%d rec=0x%08x ep==0x%08x",
  1813 										i, alternateSettingListRec, ep));
  1802 										i, alternateSettingListRec, ep));
  1814 		}
  1803 		}
       
  1804 
       
  1805 	// buf size of each endpoint
       
  1806 	TInt bufSizes[KMaxEndpointsPerClient + 1];
       
  1807 	TInt epNum[KMaxEndpointsPerClient + 1];
       
  1808 
       
  1809     // init
       
  1810     for( TInt i=0;i<KMaxEndpointsPerClient+1;i++ )
       
  1811         {
       
  1812         bufSizes[i] = -1;
       
  1813         epNum[i] = i;
       
  1814         }
       
  1815 
       
  1816 	// Record the actual buf size of each endpoint
       
  1817 	for( TInt i=1;i<=num_endpoints;i++ )
       
  1818 	    {
       
  1819 	    bufSizes[i] = alternateSettingListRec->iEndpoint[i]->BufferSize();
       
  1820 	    }
       
  1821 
       
  1822 	__KTRACE_OPT(KUSB, Kern::Printf("Sort the endpoints:"));
       
  1823 
       
  1824     // sort the endpoint number by the bufsize decreasely
       
  1825 	for( TInt i=1;i<num_endpoints;i++ )
       
  1826 	    {
       
  1827 	    TInt epMaxBuf = i;
       
  1828 	    for(TInt k=i+1;k<=num_endpoints;k++ )
       
  1829 	        {
       
  1830 	        if( bufSizes[epMaxBuf]<bufSizes[k])
       
  1831 	            {
       
  1832 	            epMaxBuf = k;
       
  1833 	            }
       
  1834 	        }
       
  1835 	    TInt temp = bufSizes[i];
       
  1836 	    bufSizes[i] = bufSizes[epMaxBuf];
       
  1837 	    bufSizes[epMaxBuf] = temp;
       
  1838 
       
  1839 	    temp = epNum[i];
       
  1840         epNum[i] = epNum[epMaxBuf];
       
  1841         epNum[epMaxBuf] = temp;
       
  1842 
       
  1843 	    alternateSettingListRec->iEpNumDeOrderedByBufSize[i] = epNum[i];
       
  1844 
       
  1845 	    __KTRACE_OPT(KUSB, Kern::Printf(" %d:%d", epNum[i], bufSizes[i]));
       
  1846 	    }
       
  1847     alternateSettingListRec->iEpNumDeOrderedByBufSize[num_endpoints] = epNum[num_endpoints];
       
  1848     __KTRACE_OPT(KUSB, Kern::Printf(" %d:%d", epNum[num_endpoints], bufSizes[num_endpoints]));
       
  1849     __KTRACE_OPT(KUSB, Kern::Printf("\n"));
  1815 
  1850 
  1816 	// chain in this alternate setting
  1851 	// chain in this alternate setting
  1817 	alternateSettingListRec->iNext = iAlternateSettingList;
  1852 	alternateSettingListRec->iNext = iAlternateSettingList;
  1818 	iAlternateSettingList = alternateSettingListRec;
  1853 	iAlternateSettingList = alternateSettingListRec;
  1819 	alternateSettingListRec->iSetting = aInterfaceNumber;
  1854 	alternateSettingListRec->iSetting = aInterfaceNumber;
  1824 	for (TInt i = 1; i <= num_endpoints; i++)
  1859 	for (TInt i = 1; i <= num_endpoints; i++)
  1825 		{
  1860 		{
  1826 		alternateSettingListRec->iEndpoint[i]->SetRealEpNumber(real_ep_numbers[i]);
  1861 		alternateSettingListRec->iEndpoint[i]->SetRealEpNumber(real_ep_numbers[i]);
  1827 		}
  1862 		}
  1828 
  1863 
  1829 	if (totalOUTBufferSize != 0)
  1864 	r = SetupInterfaceMemory(iHwChunks, cacheAttribs );
  1830 		{
  1865 	if( r==KErrNone )
  1831 		// maximally cached always
  1866 	    {
  1832 		__KTRACE_OPT(KUSB, Kern::Printf("SetInterface setting up OUT buffering size=%d", totalOUTBufferSize));
  1867         __KTRACE_OPT(KUSB, Kern::Printf("SetInterface ready to exit"));
  1833 		iHwChunkOUT = SetupInterfaceMemory(totalOUTBufferSize, iHwChunkOUT, KUsbEpDirOut, cacheAttribs);
  1868     
  1834 		if (iHwChunkOUT == NULL)
  1869         if (aInterfaceNumber == 0)
  1835 			{
  1870             {
  1836 			__KTRACE_OPT(KPANIC, Kern::Printf("SetInterface can't get chunk for OUT buffering size=%d reason=%d",
  1871             // make sure we're ready to go with the main interface
  1837 											  totalOUTBufferSize, r));
  1872             iValidInterface = ETrue;
  1838 			r = KErrNoMemory;
  1873             __KTRACE_OPT(KUSB, Kern::Printf("SetInterface SelectAlternateSetting"));
  1839 			goto KillAll;
  1874             SelectAlternateSetting(0);
  1840 			}
  1875             }
  1841 		}
  1876         return KErrNone;
  1842 	if (totalINBufferSize != 0)
  1877 	    }
  1843 		{
  1878 	else
  1844 		__KTRACE_OPT(KUSB, Kern::Printf("SetInterface setting up IN buffering size=%d", totalINBufferSize));
  1879 	    {
  1845 		iHwChunkIN = SetupInterfaceMemory(totalINBufferSize, iHwChunkIN, KUsbEpDirIn, cacheAttribs);
  1880         __KTRACE_OPT(KUSB, Kern::Printf("Destroying all interfaces"));
  1846 		if (iHwChunkIN == NULL)
  1881         DestroyAllInterfaces();
  1847 			{
  1882         DestroyEp0();
  1848 			__KTRACE_OPT(KPANIC, Kern::Printf("SetInterface can't get chunk for IN buffering size=%d reason=%d",
  1883         return r;
  1849 											  totalOUTBufferSize, r));
  1884 	    }
  1850 			r = KErrNoMemory;
       
  1851 			goto KillAll;
       
  1852 			}
       
  1853 		}
       
  1854 	__KTRACE_OPT(KUSB, Kern::Printf("SetInterface ready to exit"));
       
  1855 
       
  1856 	if (aInterfaceNumber == 0)
       
  1857 		{
       
  1858 		// make sure we're ready to go with the main interface
       
  1859 		iValidInterface = ETrue;
       
  1860 		__KTRACE_OPT(KUSB, Kern::Printf("SetInterface SelectAlternateSetting"));
       
  1861 		SelectAlternateSetting(0);
       
  1862 		}
       
  1863 	return KErrNone;
       
  1864 
       
  1865  KillAll:
       
  1866 	__KTRACE_OPT(KUSB, Kern::Printf("Destroying all interfaces"));
       
  1867 	DestroyAllInterfaces();
       
  1868 	DestroyEp0();
       
  1869 	return r;
       
  1870 
  1885 
  1871  F2:
  1886  F2:
  1872 	delete alternateSettingListRec;
  1887 	delete alternateSettingListRec;
  1873 	//Fall through
  1888 	//Fall through
  1874  
  1889  
  1880 	(void)	iController->ReleaseInterface(this, aInterfaceNumber);
  1895 	(void)	iController->ReleaseInterface(this, aInterfaceNumber);
  1881 #endif
  1896 #endif
  1882 	return r;
  1897 	return r;
  1883 	}
  1898 	}
  1884 
  1899 
  1885 
  1900 // realloc the memory, and set the previous interfaces 
  1886 DPlatChunkHw* DLddUsbcChannel::SetupInterfaceMemory(TInt aBufferSize, DPlatChunkHw* aHwChunk,
  1901 TInt DLddUsbcChannel::SetupInterfaceMemory(RArray<DPlatChunkHw*> &aHwChunks, 
  1887 													TUint aDirection, TUint32 aCacheAttribs)
  1902         TUint32 aCacheAttribs )
  1888 	{
  1903     {
  1889 	TUint8* oldBase = NULL;
  1904     TUsbcAlternateSettingList* asRec = iAlternateSettingList;
  1890 	if (aHwChunk != NULL)
  1905 
  1891 		oldBase = reinterpret_cast<TUint8*>(aHwChunk->LinearAddress());
  1906     // if buffers has been changed
  1892 
  1907     TBool chunkChanged = EFalse;
  1893 	DPlatChunkHw* chunk = ReAllocate(aBufferSize, aHwChunk, aCacheAttribs);
  1908     TInt numOfEp = asRec->iNumberOfEndpoints;
  1894 	if (chunk == NULL)
  1909  
  1895 		{
  1910     // 1, collect all bufs' sizes for the current interface
  1896 		// lost all interfaces:
  1911     //    to realloc all the chunks
  1897 		// Tell Controller to release Interface and h/w resources associated with this
  1912     __KTRACE_OPT(KUSB, Kern::Printf("Collect all buffer sizes:"));
  1898 		iController->DeRegisterClient(this);
  1913     RArray<TInt> bufSizes;
  1899 		}
  1914     for(TInt i=1;i<=numOfEp;i++)
  1900 	else
  1915         {
  1901 		{
  1916         TInt nextEp = asRec->iEpNumDeOrderedByBufSize[i];
  1902 		// Parcel out the memory between endpoints
  1917         TInt epBufCount = asRec->iEndpoint[nextEp]->BufferNumber();
  1903 		TUint8* newBase = reinterpret_cast<TUint8*>(chunk->LinearAddress());
  1918         __KTRACE_OPT(KUSB, Kern::Printf(" ep %d, buf count %d", nextEp, epBufCount ));
  1904 		TBool needsRebase = (newBase != oldBase);
  1919         for(TInt k=0;k<epBufCount;k++)
  1905 		TUint8* pBuf = newBase;
  1920             {
  1906 		TUint8* pBufIf = pBuf;							   // this is where an interface's ep buffering starts
  1921             TInt epBufSize = asRec->iEndpoint[nextEp]->BufferSize();
  1907 		TUsbcAlternateSettingList* asRec = iAlternateSettingList;
  1922             TInt r = bufSizes.Append(epBufSize);
  1908 		// the current interface
  1923             if(r!=KErrNone)
  1909 		__KTRACE_OPT(KUSB, Kern::Printf("SetupInterfaceMemory rebasing setting=%d", asRec->iSetting));
  1924                 {
  1910 		RebaseInterfaceMemory(asRec, pBuf, aDirection);
  1925                 iController->DeRegisterClient(this);
  1911 		// now the others if a rebase has occured
  1926                 bufSizes.Close();
  1912 		if (needsRebase)
  1927                 return r;
  1913 			{
  1928                 }
  1914 			__KTRACE_OPT(KUSB, Kern::Printf("SetupInterfaceMemory rebasing "));
  1929             __KTRACE_OPT(KUSB,Kern::Printf(" %d", epBufSize ));
  1915 			asRec = asRec->iNext;
  1930             }
  1916 			while (asRec)
  1931         __KTRACE_OPT(KUSB, Kern::Printf("\n"));
  1917 				{
  1932 
  1918 				// Interfaces are not concurrent so they can all start at the same logical address
  1933         }
  1919 				__KTRACE_OPT(KUSB, Kern::Printf("SetupInterfaceMemory rebasing setting=%d", asRec->iSetting));
  1934    
  1920 				pBuf = pBufIf;
  1935     // 2, alloc the buffer decreasely, biggest-->smallest
  1921 				RebaseInterfaceMemory(asRec, pBuf, aDirection);
  1936     //   2.1 check the existing chunks
  1922 				asRec = asRec->iNext;
  1937     TInt bufCount = bufSizes.Count();
  1923 				}
  1938     __KTRACE_OPT(KUSB, Kern::Printf(" ep buf number needed %d", bufCount ));
  1924 			}
  1939     __KTRACE_OPT(KUSB, Kern::Printf(" chunks available %d", aHwChunks.Count() ));
  1925 		__KTRACE_OPT(KUSB, Kern::Printf("SetInterface numberOfEndpoints"));
  1940 
  1926 		}
  1941     TInt chunkInd = 0;
  1927 	return chunk;
  1942     while( (chunkInd<aHwChunks.Count())&& (chunkInd<bufCount))
  1928 	}
  1943         {
  1929 
  1944         TUint8* oldAddr = NULL;
       
  1945         oldAddr = reinterpret_cast<TUint8*>(aHwChunks[chunkInd]->LinearAddress());
       
  1946 
       
  1947         DPlatChunkHw* chunk = ReAllocate(bufSizes[chunkInd], aHwChunks[chunkInd], aCacheAttribs);
       
  1948         if (chunk == NULL)
       
  1949             {
       
  1950             __KTRACE_OPT(KUSB, Kern::Printf("Failed to alloc chunks size %d!", bufSizes[chunkInd]));
       
  1951             // lost all interfaces:
       
  1952             // Tell Controller to release Interface and h/w resources associated with this
       
  1953             iController->DeRegisterClient(this);
       
  1954             bufSizes.Close();
       
  1955             return KErrNoMemory;
       
  1956             }
       
  1957         else
       
  1958             {
       
  1959             // Parcel out the memory between endpoints
       
  1960             TUint8* newAddr = reinterpret_cast<TUint8*>(chunk->LinearAddress());
       
  1961             __KTRACE_OPT(KUSB, Kern::Printf("SetupInterfaceMemory alloc new chunk=0x%x, size=%d", newAddr,bufSizes[chunkInd]));
       
  1962             chunkChanged = (newAddr != oldAddr);
       
  1963             aHwChunks[chunkInd] = chunk;
       
  1964             }
       
  1965         chunkInd++;
       
  1966         }
       
  1967     
       
  1968     //   2.2 in case available chunks are not enough
       
  1969     while( chunkInd<bufCount)
       
  1970         {
       
  1971         DPlatChunkHw* chunk = NULL;
       
  1972         chunk = Allocate( bufSizes[chunkInd], aCacheAttribs);
       
  1973         if (chunk == NULL)
       
  1974             {
       
  1975             __KTRACE_OPT(KUSB, Kern::Printf("Failed to alloc chunk, size %d!", bufSizes[chunkInd]));
       
  1976             // lost all interfaces:
       
  1977             // Tell Controller to release Interface and h/w resources associated with this
       
  1978             iController->DeRegisterClient(this);
       
  1979             bufSizes.Close();
       
  1980             return KErrNoMemory;
       
  1981             }
       
  1982         else
       
  1983             {
       
  1984             // Parcel out the memory between endpoints
       
  1985             __KTRACE_OPT(KUSB, Kern::Printf("SetupInterfaceMemory alloc new chunk=0x%x, size=%d",
       
  1986             						reinterpret_cast<TUint8*>(chunk->LinearAddress()), bufSizes[chunkInd]));
       
  1987             TInt r = aHwChunks.Append(chunk);
       
  1988             if(r!=KErrNone)
       
  1989                 {
       
  1990                 ClosePhysicalChunk(chunk);
       
  1991                 iController->DeRegisterClient(this);
       
  1992                 bufSizes.Close();
       
  1993                 return r;
       
  1994                 }
       
  1995             }
       
  1996         chunkInd++;
       
  1997         }
       
  1998 
       
  1999     // 3, Set the the bufs of the interfaces
       
  2000     
       
  2001     ReSetInterfaceMemory(asRec, aHwChunks);
       
  2002 
       
  2003     if(chunkChanged)
       
  2004         {
       
  2005         __KTRACE_OPT(KUSB, Kern::Printf("SetupInterfaceMemory readdressing."));
       
  2006         asRec = asRec->iNext;
       
  2007         while (asRec)
       
  2008             {
       
  2009             // Interfaces are not concurrent so they can all start at the same logical address
       
  2010             __KTRACE_OPT(KUSB, Kern::Printf("SetupInterfaceMemory readdressing setting=%d", asRec->iSetting));
       
  2011             ReSetInterfaceMemory(asRec, aHwChunks);
       
  2012             asRec = asRec->iNext;
       
  2013             }
       
  2014         }
       
  2015     return KErrNone;
       
  2016     }
  1930 
  2017 
  1931 TInt DLddUsbcChannel::SetupEp0()
  2018 TInt DLddUsbcChannel::SetupEp0()
  1932 	{
  2019 	{
  1933 	__KTRACE_OPT(KUSB, Kern::Printf("SetupEp0 entry %x", this));
  2020 	__KTRACE_OPT(KUSB, Kern::Printf("SetupEp0 entry %x", this));
  1934 	TInt ep0Size = iController->Ep0PacketSize();
  2021 	TInt ep0Size = iController->Ep0PacketSize();
  1943 	TInt r = ep0->Construct();
  2030 	TInt r = ep0->Construct();
  1944 	if (r != KErrNone)
  2031 	if (r != KErrNone)
  1945 		{
  2032 		{
  1946 		return KErrNoMemory;
  2033 		return KErrNoMemory;
  1947 		}
  2034 		}
  1948 	TInt bufferSize = ep0->BufferTotalSize();
  2035 
  1949 	TUint32 cacheAttribs = EMapAttrSupRw | EMapAttrCachedMax;
  2036     TInt bufferNum = ep0->BufferNumber();
  1950 	iHwChunkEp0 = Allocate(bufferSize, cacheAttribs);
  2037     TInt bufferSize = ep0->BufferSize();
  1951 	if (iHwChunkEp0 == NULL)
  2038     TUint32 cacheAttribs = EMapAttrSupRw | EMapAttrCachedMax;
  1952 		{
  2039 
  1953 		return KErrNoMemory;
  2040     for(TInt i=0;i<bufferNum;i++)
  1954 		}
  2041         {
  1955 	iBufferSizeEp0 = bufferSize;
  2042         DPlatChunkHw* chunk = Allocate(bufferSize, cacheAttribs );
  1956 	iBufferBaseEp0 = (TUint8*) iHwChunkEp0->LinearAddress();
  2043         if(chunk==NULL)
  1957 	ep0->SetBufferBase(iBufferBaseEp0);
  2044             {
  1958 	ep0->SetRealEpNumber(0);
  2045             return KErrNoMemory;
  1959 	__KTRACE_OPT(KUSB, Kern::Printf("SetupEp0 60 buffersize=%d", iBufferSizeEp0));
  2046             }
  1960 	__KTRACE_OPT(KUSB, Kern::Printf("SetupEp0 exit bufferbase=0x%08x", iBufferBaseEp0));
  2047         TInt r = iHwChunksEp0.Append(chunk);
       
  2048         if(r!=KErrNone)
       
  2049             {
       
  2050             ClosePhysicalChunk(chunk);
       
  2051             return r;
       
  2052             }
       
  2053         TUint8 * buf;
       
  2054         buf = (TUint8*) chunk->LinearAddress();
       
  2055         ep0->SetBufferAddr( i, buf);
       
  2056         __KTRACE_OPT(KUSB, Kern::Printf("SetupEp0 60 buffer number %d", i));
       
  2057         __KTRACE_OPT(KUSB, Kern::Printf("SetupEp0 60 buffer size %d", bufferSize));
       
  2058         }
       
  2059 
       
  2060     ep0->SetRealEpNumber(0);
  1961 	return KErrNone;
  2061 	return KErrNone;
  1962 	}
  2062 	}
  1963 
  2063 
  1964 
  2064 // Set buffer address of the interface
  1965 void DLddUsbcChannel::RebaseInterfaceMemory(TUsbcAlternateSettingList* aAlternateSettingListRec,
  2065 // Precondition: Enough chunks available.
  1966 											TUint8* aBase, TUint aDirection)
  2066 void DLddUsbcChannel::ReSetInterfaceMemory(TUsbcAlternateSettingList* aAlternateSettingListRec,
  1967 	{
  2067         RArray<DPlatChunkHw*> &aHwChunks)
  1968 	TUint8* pBuf = aBase;
  2068     {
  1969 	__KTRACE_OPT(KUSB, Kern::Printf("RebaseInterfaceMemory buffer base rec= 0x%08x", aAlternateSettingListRec));
  2069     TUsbcAlternateSettingList* asRec = aAlternateSettingListRec;
  1970 	for (TInt i = 1; i <= aAlternateSettingListRec->iNumberOfEndpoints; i++)
  2070 
  1971 		{
  2071     // set all the interfaces
  1972 		TUsbcEndpoint* ep = aAlternateSettingListRec->iEndpoint[i];
  2072     TInt chunkInd = 0;
  1973 		if (ep != NULL && (ep->EndpointInfo()->iDir == aDirection))
  2073     TInt numOfEp = asRec->iNumberOfEndpoints;
  1974 			{
  2074 
  1975 			__KTRACE_OPT(KUSB, Kern::Printf("RebaseInterfaceMemory buffer base for ep%d 0x%08x 0x%08x",
  2075     for (TInt i = 1; i <= numOfEp; i++)
  1976 											i, pBuf, ep));
  2076         {
  1977 			pBuf = ep->SetBufferBase(pBuf);
  2077         TInt nextEp = asRec->iEpNumDeOrderedByBufSize[i];
  1978 			}
  2078         TInt epBufCount = asRec->iEndpoint[nextEp]->BufferNumber();
  1979 		else
  2079         for(TInt k=0;k<epBufCount;k++)
  1980 			{
  2080             {
  1981 			__KTRACE_OPT(KUSB, Kern::Printf("RebaseInterfaceMemory ep%d wrong direction", i));
  2081             TUsbcEndpoint* ep = asRec->iEndpoint[nextEp];
  1982 			}
  2082             if (ep != NULL )
  1983 		}
  2083                 {
  1984 	}
  2084                 TUint8* pBuf = NULL;
  1985 
  2085                 pBuf = reinterpret_cast<TUint8*>(aHwChunks[chunkInd]->LinearAddress());
       
  2086                 ep->SetBufferAddr( k, pBuf);
       
  2087                 __KTRACE_OPT(KUSB, Kern::Printf("  ep %d, buf %d, addr 0x%x", nextEp, k, pBuf ));
       
  2088                 chunkInd++;
       
  2089                 __ASSERT_DEBUG(chunkInd<=aHwChunks.Count(),
       
  2090                                Kern::Printf("  Error: available chunks %d, run out at epInd%d, bufInd%d",
       
  2091                                        aHwChunks.Count(), i, k));
       
  2092                 __ASSERT_DEBUG(chunkInd<=aHwChunks.Count(),
       
  2093                                    Kern::Fault("usbc.ldd", __LINE__));
       
  2094                 }
       
  2095             }
       
  2096         }
       
  2097 
       
  2098     }
  1986 
  2099 
  1987 void DLddUsbcChannel::DestroyAllInterfaces()
  2100 void DLddUsbcChannel::DestroyAllInterfaces()
  1988 	{
  2101 	{
  1989 	// Removes all interfaces
  2102 	// Removes all interfaces
  1990 	TUsbcAlternateSettingList* alternateSettingListRec = iAlternateSettingList;
  2103 	TUsbcAlternateSettingList* alternateSettingListRec = iAlternateSettingList;
  1996 		alternateSettingListRec = alternateSettingListRecNext;
  2109 		alternateSettingListRec = alternateSettingListRecNext;
  1997 		}
  2110 		}
  1998 	iNumberOfEndpoints = 0;
  2111 	iNumberOfEndpoints = 0;
  1999 	iAlternateSettingList = NULL;
  2112 	iAlternateSettingList = NULL;
  2000 
  2113 
  2001 	ClosePhysicalChunk(iHwChunkIN);
  2114     for(TInt i=0;i<iHwChunks.Count();i++)
  2002 	ClosePhysicalChunk(iHwChunkOUT);
  2115         {
       
  2116         ClosePhysicalChunk( iHwChunks[i]);
       
  2117         }
       
  2118 	iHwChunks.Close();
  2003 
  2119 
  2004 	iValidInterface = EFalse;
  2120 	iValidInterface = EFalse;
  2005 	}
  2121 	}
  2006 
  2122 
  2007 
  2123 
  2043 		}
  2159 		}
  2044 
  2160 
  2045 	if (iAlternateSettingList == NULL)
  2161 	if (iAlternateSettingList == NULL)
  2046 		{
  2162 		{
  2047 		// if no interfaces left destroy non-ep0 buffering
  2163 		// if no interfaces left destroy non-ep0 buffering
  2048 		ClosePhysicalChunk(iHwChunkIN);
  2164 		for(TInt i=0;i<iHwChunks.Count();i++)
  2049 		ClosePhysicalChunk(iHwChunkOUT);
  2165 	        {
       
  2166 	        ClosePhysicalChunk( iHwChunks[i]);
       
  2167 	        }
       
  2168 	    iHwChunks.Close();
  2050 		}
  2169 		}
  2051 	}
  2170 	}
  2052 
  2171 
  2053 
  2172 
  2054 void DLddUsbcChannel::DestroyEp0()
  2173 void DLddUsbcChannel::DestroyEp0()
  2055 	{
  2174 	{
  2056 	delete iEndpoint[0];
  2175 	delete iEndpoint[0];
  2057 	iEndpoint[0] = NULL;
  2176 	iEndpoint[0] = NULL;
  2058 	ClosePhysicalChunk(iHwChunkEp0);
  2177 	for(TInt i=0;i<iHwChunksEp0.Count();i++)
       
  2178 	    {
       
  2179 	    ClosePhysicalChunk( iHwChunksEp0[i] );
       
  2180 	    }
       
  2181 	iHwChunksEp0.Close();
  2059 	}
  2182 	}
  2060 
  2183 
  2061 
  2184 
  2062 void DLddUsbcChannel::EndpointStatusChangeCallback(TAny* aDLddUsbcChannel)
  2185 void DLddUsbcChannel::EndpointStatusChangeCallback(TAny* aDLddUsbcChannel)
  2063     {
  2186     {
  2489     for (TInt i = 0; i < KUsbcMaxRequests; i++)
  2612     for (TInt i = 0; i < KUsbcMaxRequests; i++)
  2490         {
  2613         {
  2491         if (iRequestStatus[i])
  2614         if (iRequestStatus[i])
  2492             {
  2615             {
  2493             __KTRACE_OPT(KUSB, Kern::Printf("Complete request 0x%x", iRequestStatus[i]));
  2616             __KTRACE_OPT(KUSB, Kern::Printf("Complete request 0x%x", iRequestStatus[i]));
  2494 		CompleteBufferRequest(iClient, i, KErrDisconnected);
  2617 
       
  2618             if (i == RDevUsbcClient::ERequestAlternateDeviceStatusNotify)
       
  2619                 {
       
  2620 
       
  2621                 iDeviceStatusNeeded = EFalse;
       
  2622                 iStatusFifo->FlushQueue();
       
  2623 
       
  2624                 if (iStatusChangePtr)
       
  2625                     {
       
  2626                     iStatusChangeReq->Data() = iController->GetDeviceStatus();
       
  2627                     iStatusChangePtr = NULL;
       
  2628 
       
  2629                     if (iStatusChangeReq->IsReady())
       
  2630                         {
       
  2631                         iRequestStatus[i] = NULL;
       
  2632                         Kern::QueueRequestComplete(iClient, iStatusChangeReq,
       
  2633                                 KErrDisconnected);
       
  2634                         }
       
  2635                     }
       
  2636 
       
  2637                 }
       
  2638             else if (i == RDevUsbcClient::ERequestEndpointStatusNotify)
       
  2639                 {
       
  2640                 	
       
  2641                	if (iEndpointStatusChangePtr)
       
  2642 					{
       
  2643 	                TUint epBitmap = 0;
       
  2644 					for (TInt i = 0; i <= iNumberOfEndpoints; i++)
       
  2645 						{
       
  2646 						TInt v = iController->GetEndpointStatus(this, iEndpoint[i]->RealEpNumber());
       
  2647 						TUint b;
       
  2648 						(v == EEndpointStateStalled) ? b = 1 : b = 0;
       
  2649 						epBitmap |= b << i;
       
  2650 						}	
       
  2651 
       
  2652 					iEndpointStatusChangeReq->Data() = epBitmap;
       
  2653 					iEndpointStatusChangePtr = NULL;
       
  2654 					}
       
  2655 
       
  2656                 if (iEndpointStatusChangeReq->IsReady())
       
  2657                     {
       
  2658 					iRequestStatus[i] = NULL;
       
  2659 					Kern::QueueRequestComplete(iClient,iEndpointStatusChangeReq,KErrDisconnected);
       
  2660 					}
       
  2661 
       
  2662                 }
       
  2663             else if (i == RDevUsbcClient::ERequestOtgFeaturesNotify)
       
  2664                 {
       
  2665                 	
       
  2666                 if (iOtgFeatureChangePtr)
       
  2667 			        {
       
  2668 			        TUint8 features;
       
  2669 			        iController->GetCurrentOtgFeatures(features);
       
  2670 					iOtgFeatureChangeReq->Data()=features;
       
  2671 			        iOtgFeatureChangePtr = NULL;
       
  2672 			        }
       
  2673                 	
       
  2674                 if (iOtgFeatureChangeReq->IsReady())
       
  2675                     {
       
  2676                     iRequestStatus[i] = NULL;
       
  2677                     Kern::QueueRequestComplete(iClient, iOtgFeatureChangeReq,
       
  2678                             KErrDisconnected);
       
  2679                     }
       
  2680 
       
  2681                 }
       
  2682             else
       
  2683             	{
       
  2684 				CompleteBufferRequest(iClient, i, KErrDisconnected);
       
  2685 				}
       
  2686 
  2495             }
  2687             }
  2496         }
  2688         }
       
  2689 
  2497     iStatusCallbackInfo.Cancel();
  2690     iStatusCallbackInfo.Cancel();
  2498     iEndpointStatusCallbackInfo.Cancel();
  2691     iEndpointStatusCallbackInfo.Cancel();
  2499     iOtgFeatureCallbackInfo.Cancel();
  2692     iOtgFeatureCallbackInfo.Cancel();
  2500 	return KErrNone;
  2693 	return KErrNone;
  2501 	}
  2694 	}
  2909 		{
  3102 		{
  2910 		// only send a zlp if this is the last buffer of the transfer
  3103 		// only send a zlp if this is the last buffer of the transfer
  2911 		zlpReqd = iTransferInfo.iZlpReqd;
  3104 		zlpReqd = iTransferInfo.iZlpReqd;
  2912 		}
  3105 		}
  2913 	r = iDmaBuffers->TxStoreData(iLdd->Client(), iLdd->GetClientBuffer(iEndpointNumber), length, iBytesTransferred);
  3106 	r = iDmaBuffers->TxStoreData(iLdd->Client(), iLdd->GetClientBuffer(iEndpointNumber), length, iBytesTransferred);
  2914 	if (r != KErrNone)											
  3107 	if (r != KErrNone)
  2915 		return r;
  3108 		return r;
  2916 	iDmaBuffers->TxSetActive();
  3109 	iDmaBuffers->TxSetActive();
  2917 	iRequestCallbackInfo->SetTxBufferInfo(bufferAddr, physAddr, length);
  3110 	iRequestCallbackInfo->SetTxBufferInfo(bufferAddr, physAddr, length);
  2918 	iRequestCallbackInfo->iZlpReqd = zlpReqd;
  3111 	iRequestCallbackInfo->iZlpReqd = zlpReqd;
  2919 #if 0
  3112 #if 0
  2976 	  iNumberOfEndpoints(0),
  3169 	  iNumberOfEndpoints(0),
  2977 	  iSetting(0)
  3170 	  iSetting(0)
  2978 	{
  3171 	{
  2979 	for (TInt i = 0; i <= KMaxEndpointsPerClient; i++)
  3172 	for (TInt i = 0; i <= KMaxEndpointsPerClient; i++)
  2980 		{
  3173 		{
       
  3174 		iEpNumDeOrderedByBufSize[i] = -1;
  2981 		iEndpoint[i] = NULL;
  3175 		iEndpoint[i] = NULL;
  2982 		}
  3176 		}
  2983 	}
  3177 	}
  2984 
  3178 
  2985 
  3179