sl@0: // Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0: // All rights reserved.
sl@0: // This component and the accompanying materials are made available
sl@0: // under the terms of the License "Eclipse Public License v1.0"
sl@0: // which accompanies this distribution, and is available
sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0: //
sl@0: // Initial Contributors:
sl@0: // Nokia Corporation - initial contribution.
sl@0: //
sl@0: // Contributors:
sl@0: //
sl@0: // Description:
sl@0: //
sl@0: 
sl@0: #include <f32file.h>
sl@0: #include <d32usbdi_hubdriver.h>
sl@0: #include <d32usbdi.h>
sl@0: #include <d32otgdi.h>
sl@0: #include <d32usbdescriptors.h>
sl@0: #include <d32usbtransfers.h>
sl@0: 
sl@0: #include <shared.h>
sl@0: 
sl@0: #include "rusbhostmsdevice.h"
sl@0: 
sl@0: #include "rextfilesystem.h"
sl@0: #include "usbtypes.h"
sl@0: #include "cusbmsmountmanager.h"
sl@0: #include "tmslog.h"
sl@0: #include "debug.h"
sl@0: 
sl@0: 
sl@0: CDevice* CDevice::NewL()
sl@0:     {
sl@0:     __MSFNSLOG
sl@0: 	CDevice* r = new (ELeave) CDevice();
sl@0: 	CleanupStack::PushL(r);
sl@0: 
sl@0: 	r->ConstructL();
sl@0: 	CleanupStack::Pop();
sl@0: 	return r;
sl@0:     }
sl@0: 
sl@0: 
sl@0: void CDevice::ConstructL()
sl@0:     {
sl@0:     __MSFNLOG
sl@0:     }
sl@0: 
sl@0: 
sl@0: CDevice::CDevice()
sl@0:     {
sl@0:     __MSFNLOG
sl@0:     }
sl@0: 
sl@0: 
sl@0: CDevice::~CDevice()
sl@0:     {
sl@0:     __MSFNLOG
sl@0:     __USBHOSTPRINT1(_L("~CDevice Token=%d"), iDeviceToken);
sl@0:     }
sl@0: 
sl@0: 
sl@0: TToken CDevice::OpenDeviceL(TUint aDeviceHandle, RUsbHubDriver& aHub)
sl@0:     {
sl@0:     __MSFNLOG
sl@0:     __USBHOSTPRINT1(_L("CDevice::OpenDeviceL Handle=%d"), aDeviceHandle);
sl@0: 
sl@0:     TInt err = iUsbDevice.Open(aHub, aDeviceHandle);
sl@0:     __USBHOSTPRINT1(_L(" - returned %d\n"), err);
sl@0:     User::LeaveIfError(err);
sl@0: 
sl@0:     /* Retrieve the device descriptor */
sl@0:     TUsbDeviceDescriptor devDescriptor;
sl@0:     User::LeaveIfError(iUsbDevice.GetDeviceDescriptor(devDescriptor));
sl@0:     iUsbPrint.PrintDescriptor(devDescriptor, 0, &iUsbDevice);
sl@0: 
sl@0:     iUsbPrint.PrintTree(devDescriptor);
sl@0: 
sl@0:     /* Retrieve the configuration descriptor */
sl@0:     TUsbConfigurationDescriptor configDescriptor;
sl@0:     User::LeaveIfError(iUsbDevice.GetConfigurationDescriptor(configDescriptor));
sl@0:     iUsbPrint.PrintDescriptor(configDescriptor, 0, &iUsbDevice);
sl@0: 
sl@0:     /* Get the token for interface 0 */
sl@0:     TUint32 token;
sl@0:     err = iUsbDevice.GetTokenForInterface(0, token);
sl@0:     __USBHOSTPRINT2(_L("RUsbDevice::GetTokenForInterface returned error %d, token %08x"), err, token);
sl@0:     User::LeaveIfError(err);
sl@0: 
sl@0:     /* open the interface */
sl@0:     RUsbInterface interface_ep0;
sl@0:     err = interface_ep0.Open(token);
sl@0:     __USBHOSTPRINT1(_L("RUsbInterface::Open returned error %d"), err);
sl@0:     User::LeaveIfError(err);
sl@0: 
sl@0:     /* Retrieve the interface and device descriptors */
sl@0:     TUsbInterfaceDescriptor ifDescriptor;
sl@0:     User::LeaveIfError(interface_ep0.GetInterfaceDescriptor(ifDescriptor));
sl@0:     iUsbPrint.PrintDescriptor(ifDescriptor);
sl@0: 
sl@0:     if (!IsDeviceMassStorage(ifDescriptor, devDescriptor))
sl@0:         {
sl@0:         RDebug::Print(_L("ATTACHED DEVICE IS NOT A MASS STORAGE DEVICE!\n"));
sl@0:         User::Leave(KErrGeneral);
sl@0:         }
sl@0: 
sl@0: 	TUint8 iProtocolId = ifDescriptor.InterfaceSubClass();
sl@0: 	TUint8 iTransportId = ifDescriptor.InterfaceProtocol();
sl@0: 
sl@0:     interface_ep0.Close();
sl@0: 
sl@0:     THostMassStorageConfig msConfig;
sl@0:     msConfig.iInterfaceToken = token;
sl@0: 	msConfig.iProtocolId =iProtocolId;
sl@0: 	msConfig.iTransportId = iTransportId;
sl@0: 	msConfig.iStatusPollingInterval = 10; // 10 secs
sl@0: 
sl@0:     TUint32 numLun;
sl@0: 
sl@0:     TRequestStatus status;
sl@0:     iUsbHostMsDevice.Add(msConfig, status);
sl@0: 	User::WaitForRequest(status);
sl@0: 	if (status.Int() != KErrNone)
sl@0:         {
sl@0: 		__USBHOSTPRINT(_L("Add device failed"));
sl@0: 		User::Leave(status.Int());
sl@0:         }
sl@0: 	TInt r = iUsbHostMsDevice.GetNumLun(numLun);
sl@0: 	if (r != KErrNone)
sl@0:         {
sl@0: 		__USBHOSTPRINT(_L("GetNumLun failed"));
sl@0: 		User::Leave(r);
sl@0:         }
sl@0: 
sl@0:     if (numLun > KMaxLun)
sl@0:         {
sl@0:         __USBHOSTPRINT1(_L("Device MaxLun = %d. Error MaxLun > MAXLUN !"), numLun);
sl@0:         User::Leave(KErrGeneral);
sl@0:         }
sl@0: 
sl@0:     __USBHOSTPRINT1(_L("MSC registered with %d Luns"), numLun);
sl@0: 
sl@0:     iDeviceToken = token;
sl@0:     iNumLuns = numLun;
sl@0:     iDeviceHandle = aDeviceHandle;
sl@0:     return token;
sl@0:     }
sl@0: 
sl@0: 
sl@0: void CDevice::CloseDeviceL()
sl@0:     {
sl@0:     __MSFNLOG
sl@0: 
sl@0:     THostMassStorageConfig msConfig;
sl@0:     msConfig.iInterfaceToken = iDeviceToken;
sl@0: 
sl@0:     iUsbHostMsDevice.Remove();
sl@0:     iUsbDevice.Close();
sl@0:     }
sl@0: 
sl@0: 
sl@0: void CDevice::MountLogicalUnitsL()
sl@0:     {
sl@0:     __MSFNLOG
sl@0:     iExt.OpenL();
sl@0: 
sl@0:     for (TInt lun = 0; lun < iNumLuns; lun++)
sl@0:         {
sl@0:         TDriveNumber driveNumber = iExt.GetDriveL();
sl@0:         __PRINT2(_L("Mounting drive=%d lun=%d..."), driveNumber, lun);
sl@0:         RDebug::Print(_L("Mounting drive=%d lun=%d..."), driveNumber, lun);
sl@0:         TRAPD(err, iExt.MountL(iUsbHostMsDevice, driveNumber, iDeviceToken, lun));
sl@0:         if (err == KErrNone || err == KErrNotReady || err == KErrCorrupt)
sl@0:             {
sl@0:             iLuList.Append(driveNumber);
sl@0:             }
sl@0:         __PRINT1(_L("%d"), err);
sl@0:         RDebug::Print(_L("err=%d"), err);
sl@0:         }
sl@0:     }
sl@0: 
sl@0: 
sl@0: void CDevice::DismountLogicalUnitsL()
sl@0:     {
sl@0:     __MSFNLOG
sl@0:     for (TInt lun = 0; lun < iLuList.Count(); lun++)
sl@0:         {
sl@0:         TDriveNumber driveNumber = iLuList[lun];
sl@0:         iExt.DismountL(iUsbHostMsDevice, driveNumber);
sl@0:         }
sl@0:     iLuList.Reset();
sl@0: 
sl@0:     iExt.CloseL();
sl@0:     }
sl@0: 
sl@0: 
sl@0: TInt CDevice::GetEndpointAddress(RUsbInterface& aUsbInterface,
sl@0:                                  TInt aInterfaceSetting,
sl@0:                                  TUint8 aTransferType,
sl@0:                                  TUint8 aDirection,
sl@0:                                  TInt& aEndpointAddress) const
sl@0: 	{
sl@0:     __MSFNSLOG
sl@0: 
sl@0: 	// Get the interface descriptor
sl@0: 	RDebug::Print(_L("GetEndpointAddress : Getting the interface descriptor for this alternate setting"));
sl@0: 
sl@0: 	TUsbInterfaceDescriptor alternateInterfaceDescriptor;
sl@0: 	TInt err = aUsbInterface.GetAlternateInterfaceDescriptor(aInterfaceSetting, alternateInterfaceDescriptor);
sl@0: 
sl@0: 	if (err)
sl@0: 		{
sl@0: 		RDebug::Print(_L("GetEndpointAddress : <Error %d> Unable to get alternate interface (%d) descriptor"),err,aInterfaceSetting);
sl@0: 		return err;
sl@0: 		}
sl@0: 
sl@0: 	// Parse the descriptor tree from the interface
sl@0: 	RDebug::Print(_L("Search the child descriptors for matching endpoint attributes"));
sl@0: 
sl@0: 	TUsbGenericDescriptor* descriptor = alternateInterfaceDescriptor.iFirstChild;
sl@0: 
sl@0: 	while (descriptor)
sl@0: 		{
sl@0: 		RDebug::Print(_L("GetEndpointAddress : Check descriptor type for endpoint"));
sl@0: 
sl@0: 		// Cast the descriptor to an endpoint descriptor
sl@0: 		TUsbEndpointDescriptor* endpoint = TUsbEndpointDescriptor::Cast(descriptor);
sl@0: 
sl@0: 		if (endpoint)
sl@0: 			{
sl@0: 			RDebug::Print(_L("GetEndpointAddress : Match attributes for transfer type"));
sl@0: 
sl@0: 			if ( (endpoint->Attributes() & aTransferType) == aTransferType)
sl@0: 				{
sl@0: 				RDebug::Print(_L("GetEndpointAddress : Match attributes for endpoint direction"));
sl@0: 
sl@0: 				if ( (endpoint->EndpointAddress() & 0x80) == aDirection)
sl@0: 					{
sl@0: 					aEndpointAddress = endpoint->EndpointAddress();
sl@0: 					RDebug::Print(_L("GetEndpointAddress : Endpoint address found"));
sl@0: 					return KErrNone;
sl@0: 					}
sl@0: 				}
sl@0: 			}
sl@0: 
sl@0: 		descriptor = descriptor->iNextPeer;
sl@0: 		}
sl@0: 
sl@0: 	// Unable to find the endpoint address
sl@0: 	RDebug::Print(_L("GetEndpointAddress : Unable to find endpoint address matching the specified attributes"));
sl@0: 
sl@0: 	return KErrNotFound;
sl@0: 	}
sl@0: 
sl@0: 
sl@0: TBool CDevice::IsDeviceMassStorage(const TUsbInterfaceDescriptor& aInterfaceDesc,
sl@0:                                    const TUsbDeviceDescriptor& aDeviceDesc) const
sl@0:     {
sl@0:     __MSFNSLOG
sl@0: 	/* check the interface descriptor */
sl@0: 	if(aInterfaceDesc.InterfaceClass() == 0x08 &&
sl@0: 		aInterfaceDesc.InterfaceSubClass() == 0x06 &&
sl@0: 		aInterfaceDesc.InterfaceProtocol() == 0x50)
sl@0:         {
sl@0: 		if(aDeviceDesc.DeviceClass() == 0x00 &&
sl@0: 			aDeviceDesc.DeviceSubClass() == 0x00 &&
sl@0: 			aDeviceDesc.DeviceProtocol() == 0x00)
sl@0: 			return ETrue;
sl@0:         }
sl@0: 
sl@0: 	return EFalse;
sl@0: 
sl@0:     }
sl@0: 
sl@0: 
sl@0: TLun CDevice::DriveMap(TDriveMap& aDriveMap) const
sl@0:     {
sl@0:     __MSFNSLOG
sl@0:     TDriveNumber driveNumber;
sl@0:     RDebug::Printf("LuList.Count=%d", iLuList.Count());
sl@0:     for (TInt i = 0; i < iLuList.Count(); i++)
sl@0:         {
sl@0:         driveNumber = iLuList[i];
sl@0:         aDriveMap[driveNumber] = iDeviceToken;
sl@0:         RDebug::Printf("Device %d token=%d driveNumber=%d", i, iDeviceToken, driveNumber);
sl@0:         }
sl@0: 
sl@0:     return iNumLuns;
sl@0:     }
sl@0: 
sl@0: 
sl@0: TLun CDevice::DeviceMap(TDeviceMap& aDeviceMap) const
sl@0:     {
sl@0:     __MSFNSLOG
sl@0:     TDriveNumber driveNumber;
sl@0:     RDebug::Printf("LuList.Count=%d", iLuList.Count());
sl@0:     for (TInt i = 0; i < iLuList.Count(); i++)
sl@0:         {
sl@0:         driveNumber = iLuList[i];
sl@0:         aDeviceMap[i] = driveNumber;
sl@0:         RDebug::Printf("CDevice LUN=%d driveNumber=%d", i, driveNumber);
sl@0:         }
sl@0: 
sl@0:     return iNumLuns;
sl@0:     }
sl@0: 
sl@0: 
sl@0: CUsbMsMountManager* CUsbMsMountManager::NewL()
sl@0:     {
sl@0:     __MSFNSLOG
sl@0: 	CUsbMsMountManager* r = new (ELeave) CUsbMsMountManager();
sl@0: 	CleanupStack::PushL(r);
sl@0: 
sl@0: 	r->ConstructL();
sl@0: 	CleanupStack::Pop();
sl@0: 	return r;
sl@0:     }
sl@0: 
sl@0: 
sl@0: void CUsbMsMountManager::ConstructL()
sl@0:     {
sl@0:     __MSFNLOG
sl@0:     }
sl@0: 
sl@0: 
sl@0: CUsbMsMountManager::CUsbMsMountManager()
sl@0:     {
sl@0:     __MSFNLOG
sl@0:     }
sl@0: 
sl@0: 
sl@0: CUsbMsMountManager::~CUsbMsMountManager()
sl@0:     {
sl@0:     __MSFNLOG
sl@0:     iDeviceList.ResetAndDestroy();
sl@0:     }
sl@0: 
sl@0: 
sl@0: // adds new entry for this device
sl@0: void CUsbMsMountManager::AddDeviceL(CDevice* aDevice)
sl@0:     {
sl@0:     __MSFNLOG
sl@0:     iDeviceList.Append(aDevice);
sl@0:     }
sl@0: 
sl@0: 
sl@0: CDevice* CUsbMsMountManager::RemoveDeviceL(TUint aDeviceHandle)
sl@0:     {
sl@0:     __MSFNLOG
sl@0: 	TInt index = GetHandleIndexL(aDeviceHandle);
sl@0: 	CDevice* device = iDeviceList[index];
sl@0:     iDeviceList.Remove(index);
sl@0:     return device;
sl@0:     }
sl@0: 
sl@0: void CUsbMsMountManager::CloseAllDevicesL()
sl@0:     {
sl@0:     __MSFNLOG
sl@0:     for (TInt i = 0; i < iDeviceList.Count(); i++)
sl@0:         {
sl@0:         iDeviceList[i]->CloseDeviceL();
sl@0:         }
sl@0:     }
sl@0: 
sl@0: 
sl@0: TInt CUsbMsMountManager::GetDeviceIndexL(TToken aDeviceToken) const
sl@0:     {
sl@0:     __MSFNSLOG
sl@0:     TInt index;
sl@0:     for (index = 0; index < iDeviceList.Count(); index++)
sl@0:         {
sl@0:         if (aDeviceToken == iDeviceList[index]->DeviceToken())
sl@0:             {
sl@0:             break;
sl@0:             }
sl@0:         }
sl@0: 
sl@0:     if (index == iDeviceList.Count())
sl@0:         {
sl@0:         User::Leave(KErrNotFound);
sl@0:         }
sl@0: 
sl@0:     return index;
sl@0:     }
sl@0: 
sl@0: 
sl@0: TInt CUsbMsMountManager::GetHandleIndexL(TUint aDeviceHandle) const
sl@0:     {
sl@0:     __MSFNSLOG
sl@0:     TInt index;
sl@0:     for (index = 0; index < iDeviceList.Count(); index++)
sl@0:         {
sl@0:         if (aDeviceHandle == iDeviceList[index]->DeviceHandle())
sl@0:             {
sl@0:             break;
sl@0:             }
sl@0:         }
sl@0: 
sl@0:     if (index == iDeviceList.Count())
sl@0:         {
sl@0:         User::Leave(KErrNotFound);
sl@0:         }
sl@0: 
sl@0:     return index;
sl@0:     }
sl@0: 
sl@0: 
sl@0: 
sl@0: // mounts all LUNs for the device
sl@0: void CUsbMsMountManager::MountDeviceL(TUint aDeviceHandle)
sl@0:     {
sl@0:     __MSFNLOG
sl@0:     TInt index = GetHandleIndexL(aDeviceHandle);
sl@0:     iDeviceList[index]->MountLogicalUnitsL();
sl@0:     }
sl@0: 
sl@0: 
sl@0: 
sl@0: // dismount all LUNs for this device
sl@0: void CUsbMsMountManager::DismountDeviceL(TUint aDeviceHandle)
sl@0:     {
sl@0:     __MSFNLOG
sl@0:     TInt index = GetHandleIndexL(aDeviceHandle);
sl@0:     iDeviceList[index]->DismountLogicalUnitsL();
sl@0:     }
sl@0: 
sl@0: 
sl@0: // dismount all LUNs
sl@0: void CUsbMsMountManager::DismountL()
sl@0:     {
sl@0:     __MSFNLOG
sl@0:     for (TInt i = 0; i < iDeviceList.Count(); i++)
sl@0:         {
sl@0:         iDeviceList[i]->DismountLogicalUnitsL();
sl@0:         }
sl@0:     }
sl@0: 
sl@0: 
sl@0: void CUsbMsMountManager::DriveMap(TDriveMap& aDriveMap) const
sl@0:     {
sl@0:     __MSFNSLOG
sl@0:     TInt maxLun = 0;
sl@0:     RDebug::Printf("DeviceList.Count=%d", iDeviceList.Count());
sl@0:     for (TInt i = 0; i < iDeviceList.Count(); i++)
sl@0:         {
sl@0:         maxLun = iDeviceList[i]->DriveMap(aDriveMap);
sl@0:         RDebug::Printf("%d %d", i, maxLun);
sl@0:         }
sl@0:     }
sl@0: 
sl@0: 
sl@0: void CUsbMsMountManager::DeviceMap(TInt aDeviceIndex, TDeviceMap& aDeviceMap) const
sl@0:     {
sl@0:     __MSFNSLOG
sl@0:     RDebug::Printf("Device=%d", aDeviceIndex);
sl@0: 
sl@0:     __ASSERT_DEBUG(aDeviceIndex < iDeviceList.Count(), User::Invariant());
sl@0:     iDeviceList[aDeviceIndex]->DeviceMap(aDeviceMap);
sl@0:     }
sl@0: 
sl@0: 
sl@0: 
sl@0: TUsbPrint::TUsbPrint()
sl@0: :   iDebug(EFalse)
sl@0:     {
sl@0:     }
sl@0: 
sl@0: void TUsbPrint::PrintTree(const TUsbGenericDescriptor& aDesc, TInt aDepth)
sl@0: 	{
sl@0:     if (!iDebug)
sl@0:         {
sl@0:         return;
sl@0:         }
sl@0: 
sl@0: 	TBuf<20> buf;
sl@0: 	for(TInt depth=aDepth;depth>=0;--depth)
sl@0: 		{
sl@0: 		buf.Append(_L("  "));
sl@0: 		}
sl@0: 	if(aDesc.iRecognisedAndParsed == TUsbGenericDescriptor::ERecognised)
sl@0: 		{
sl@0: 		RDebug::Print(_L("%S+0x%08x - %d 0x%02x"), &buf, &aDesc, aDesc.ibLength, aDesc.ibDescriptorType);
sl@0: 		}
sl@0: 	else
sl@0: 		{
sl@0: 		RDebug::Print(_L("%S-0x%08x - %d 0x%02x"), &buf, &aDesc, aDesc.ibLength, aDesc.ibDescriptorType);
sl@0: 		}
sl@0: 	HBufC* blob = HBufC::New(5*aDesc.iBlob.Length()); // 5* for " 0x" + 2*bytes for hex representation
sl@0: 	if(blob)
sl@0: 		{
sl@0: 		for(TInt i=0;i<aDesc.iBlob.Length();++i)
sl@0: 			{
sl@0: 			blob->Des().AppendFormat(_L("0x%02x "), aDesc.iBlob[i]);
sl@0: 			}
sl@0: 		RDebug::Print(_L("%S >%S"), &buf, blob);
sl@0: 		delete blob;
sl@0: 		}
sl@0: 	if(aDesc.iFirstChild)
sl@0: 		{
sl@0: 		RDebug::Print(_L("%S \\ "), &buf);
sl@0: 		PrintTree(*(aDesc.iFirstChild), aDepth+1);
sl@0: 		RDebug::Print(_L("%S / "), &buf);
sl@0: 		}
sl@0: 	if(aDesc.iNextPeer)
sl@0: 		{
sl@0: 		PrintTree(*(aDesc.iNextPeer), aDepth);
sl@0: 		}
sl@0: 	}
sl@0: 
sl@0: 
sl@0: static TUint16 gLangId = 0x0000;
sl@0: 
sl@0: void TUsbPrint::SetLanguageToPrintL(RUsbDevice& aDevice)
sl@0: 	{
sl@0:     if (!iDebug) return;
sl@0: 
sl@0: 	// Try to set language to US Eng, otherwise take the first one listed.
sl@0: 	if(gLangId == 0x0000) // Only make the request if not been made before.
sl@0: 		{
sl@0: 		// Get string descriptor 0.
sl@0: 		TBuf8<256> stringBuf;
sl@0: 		TUsbStringDescriptor* stringDesc = NULL;
sl@0: 		User::LeaveIfError(aDevice.GetStringDescriptor(stringDesc, stringBuf, 0));
sl@0: 		CleanupStack::PushL(*stringDesc);
sl@0: 
sl@0: 		// Search for US English
sl@0: 		TBool usEngLang = EFalse;
sl@0: 		TInt langId = 0;
sl@0: 		TInt index = 0;
sl@0: 		const TUint16 KLangIdUsEng = 0x0409;
sl@0: 		while(!usEngLang && langId != KErrNotFound)
sl@0: 			{
sl@0: 			langId = stringDesc->GetLangId(index);
sl@0: 			usEngLang = (langId == KLangIdUsEng);
sl@0: 			index++;
sl@0: 			}
sl@0: 
sl@0: 		// Set the language appropriately
sl@0: 		if(usEngLang)
sl@0: 			{
sl@0:         	gLangId = KLangIdUsEng;
sl@0: 			}
sl@0: 		else
sl@0: 			{
sl@0: 			gLangId = stringDesc->GetLangId(0);
sl@0: 			}
sl@0: 
sl@0: 		CleanupStack::PopAndDestroy(); // stringDesc
sl@0: 		}
sl@0: 	}
sl@0: 
sl@0: 
sl@0: void TUsbPrint::PrintStringFromIndex(const TDesC& aFormatString,
sl@0:                                      TInt aIndex,
sl@0:                                      RUsbDevice* aDevice)
sl@0: 	{
sl@0:     if (!iDebug) return;
sl@0: 
sl@0: 	// If we have no device handle, we cannot go and get any strings.
sl@0: 	// If we have index 0, this indicates we don't have a string for this entry.
sl@0: 	if(aDevice && aIndex != 0)
sl@0: 		{
sl@0: 		TRAPD(err, SetLanguageToPrintL(*aDevice));
sl@0: 		if(err == KErrNone)
sl@0: 			{
sl@0: 			TBuf8<255> stringBuf;
sl@0: 			TUsbStringDescriptor* stringDesc = NULL;
sl@0: 			err = aDevice->GetStringDescriptor(stringDesc, stringBuf, aIndex, gLangId);
sl@0: 			if(err == KErrNone)
sl@0: 				{
sl@0: 				TBuf<128> buf;
sl@0: 				stringDesc->StringData(buf);
sl@0: 				RDebug::Print(aFormatString, &buf);
sl@0: 				stringDesc->DestroyTree();
sl@0: 				}
sl@0: 			delete stringDesc;
sl@0: 			}
sl@0: 		else
sl@0: 			{
sl@0: 			RDebug::Print(_L("Error while Selecting Langauge %d\n"), err);
sl@0: 			}
sl@0: 		}
sl@0: 	}
sl@0: 
sl@0: 
sl@0: void TUsbPrint::PrintDescriptor(const TUsbDeviceDescriptor& aDeviceDesc,
sl@0:                                 TInt /*aVariant*/,
sl@0:                                 RUsbDevice* aDevice)
sl@0: 	{
sl@0:     if (!iDebug) return;
sl@0: 
sl@0: 	RDebug::Print(_L("USBBcd = 0x%04x\n"), aDeviceDesc.USBBcd());
sl@0: 	RDebug::Print(_L("DeviceClass = 0x%02x\n"), aDeviceDesc.DeviceClass());
sl@0: 	RDebug::Print(_L("DeviceSubClass = 0x%02x\n"), aDeviceDesc.DeviceSubClass());
sl@0: 	RDebug::Print(_L("DeviceProtocol = 0x%02x\n"), aDeviceDesc.DeviceProtocol());
sl@0: 	RDebug::Print(_L("MaxPacketSize0 = 0x%02x\n"), aDeviceDesc.MaxPacketSize0());
sl@0: 	RDebug::Print(_L("VendorId = 0x%04x\n"), aDeviceDesc.VendorId());
sl@0: 	RDebug::Print(_L("ProductId = 0x%04x\n"), aDeviceDesc.ProductId());
sl@0: 	RDebug::Print(_L("DeviceBcd = 0x%04x\n"), aDeviceDesc.DeviceBcd());
sl@0: 	RDebug::Print(_L("ManufacturerIndex = 0x%02x\n"), aDeviceDesc.ManufacturerIndex());
sl@0: 	PrintStringFromIndex(_L("ManufacturerString = %S\n"), aDeviceDesc.ManufacturerIndex(), aDevice);
sl@0: 	RDebug::Print(_L("ProductIndex = 0x%02x\n"), aDeviceDesc.ProductIndex());
sl@0: 	PrintStringFromIndex(_L("ProductString = %S\n"), aDeviceDesc.ProductIndex(), aDevice);
sl@0: 	RDebug::Print(_L("SerialNumberIndex = 0x%02x\n"), aDeviceDesc.SerialNumberIndex());
sl@0: 	PrintStringFromIndex(_L("SerialNumberString = %S\n"), aDeviceDesc.SerialNumberIndex(), aDevice);
sl@0: 	RDebug::Print(_L("NumConfigurations = 0x%02x\n"), aDeviceDesc.NumConfigurations());
sl@0: 	}
sl@0: 
sl@0: 
sl@0: void TUsbPrint::PrintDescriptor(const TUsbConfigurationDescriptor& aConfigDesc,
sl@0:                                 TInt /*aVariant*/,
sl@0:                                 RUsbDevice* aDevice)
sl@0: 	{
sl@0:     if (!iDebug) return;
sl@0: 	RDebug::Print(_L("TotalLength = 0x%04x\n"), aConfigDesc.TotalLength());
sl@0: 	RDebug::Print(_L("NumInterfaces = 0x%02x\n"), aConfigDesc.NumInterfaces());
sl@0: 	RDebug::Print(_L("ConfigurationValue = 0x%02x\n"), aConfigDesc.ConfigurationValue());
sl@0: 	RDebug::Print(_L("ConfigurationIndex = 0x%02x\n"), aConfigDesc.ConfigurationIndex());
sl@0: 	PrintStringFromIndex(_L("ConfigurationString = %S\n"), aConfigDesc.ConfigurationIndex(), aDevice);
sl@0: 	RDebug::Print(_L("Attributes = 0x%02x\n"), aConfigDesc.Attributes());
sl@0: 	RDebug::Print(_L("MaxPower = 0x%02x\n"), aConfigDesc.MaxPower());
sl@0: 	}
sl@0: 
sl@0: 
sl@0: void TUsbPrint::PrintDescriptor(const TUsbEndpointDescriptor& aEndpointDesc,
sl@0:                                 TInt /*aVariant*/,
sl@0:                                 RUsbDevice* /*aDevice*/)
sl@0: 	{
sl@0:     if (!iDebug) return;
sl@0: 	RDebug::Print(_L("EndpointAddress = 0x%02x\n"), aEndpointDesc.EndpointAddress());
sl@0: 	RDebug::Print(_L("Attributes = 0x%02x\n"), aEndpointDesc.Attributes());
sl@0: 	RDebug::Print(_L("MaxPacketSize = 0x%04x\n"), aEndpointDesc.MaxPacketSize());
sl@0: 	RDebug::Print(_L("Interval = 0x%02x\n"), aEndpointDesc.Interval());
sl@0: 	}
sl@0: 
sl@0: 
sl@0: void TUsbPrint::PrintDescriptor(const TUsbInterfaceDescriptor& aInterfaceDesc,
sl@0:                                 TInt /*aVariant*/,
sl@0:                                 RUsbDevice* /*aDevice*/)
sl@0: 	{
sl@0:     if (!iDebug) return;
sl@0: 	RDebug::Print(_L("InterfaceNumber = 0x%02x\n"), aInterfaceDesc.InterfaceNumber());
sl@0: 	RDebug::Print(_L("AlternateSetting = 0x%02x\n"), aInterfaceDesc.AlternateSetting());
sl@0: 	RDebug::Print(_L("NumEndpoints = 0x%02x\n"), aInterfaceDesc.NumEndpoints());
sl@0: 	RDebug::Print(_L("InterfaceClass = 0x%02x\n"), aInterfaceDesc.InterfaceClass());
sl@0: 	RDebug::Print(_L("InterfaceSubClass = 0x%02x\n"), aInterfaceDesc.InterfaceSubClass());
sl@0: 	RDebug::Print(_L("InterfaceProtocol = 0x%02x\n"), aInterfaceDesc.InterfaceProtocol());
sl@0: 	RDebug::Print(_L("Interface = 0x%02x\n"), aInterfaceDesc.Interface());
sl@0: 	}
sl@0: 
sl@0: 
sl@0: void TUsbPrint::PrintDescriptor(const TUsbStringDescriptor& aStringDesc,
sl@0:                                 TInt aVariant,
sl@0:                                 RUsbDevice* /*aDevice*/)
sl@0: 	{
sl@0:     if (!iDebug) return;
sl@0: 	if(aVariant == 0)
sl@0: 		{
sl@0: 		RDebug::Print(_L("String Descriptor Zero\n"));
sl@0: 		TInt index = 0;
sl@0: 		TInt langId = 0;
sl@0: 		while((langId = aStringDesc.GetLangId(index)) != KErrNotFound)
sl@0: 			{
sl@0: 			RDebug::Print(_L("  >0x%04x\n"), langId);
sl@0: 			++index;
sl@0: 			}
sl@0: 		}
sl@0: 	else
sl@0: 		{
sl@0: 		RDebug::Print(_L("Generic String Descriptor\n"));
sl@0: 		HBufC16* string = HBufC16::New(128);
sl@0: 		if(string)
sl@0: 			{
sl@0: 			TPtr16 stringPtr = string->Des();
sl@0: 			aStringDesc.StringData(stringPtr);
sl@0: 			RDebug::Print(_L("  >%S\n"), string);
sl@0: 			}
sl@0: 		delete string;
sl@0: 		}
sl@0: 	}