os/kernelhwsrv/kerneltest/f32test/shostmassstorage/msman/src/cusbmsmountmanager.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/f32test/shostmassstorage/msman/src/cusbmsmountmanager.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,682 @@
     1.4 +// Copyright (c) 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 +//
    1.18 +
    1.19 +#include <f32file.h>
    1.20 +#include <d32usbdi_hubdriver.h>
    1.21 +#include <d32usbdi.h>
    1.22 +#include <d32otgdi.h>
    1.23 +#include <d32usbdescriptors.h>
    1.24 +#include <d32usbtransfers.h>
    1.25 +
    1.26 +#include <shared.h>
    1.27 +
    1.28 +#include "rusbhostmsdevice.h"
    1.29 +
    1.30 +#include "rextfilesystem.h"
    1.31 +#include "usbtypes.h"
    1.32 +#include "cusbmsmountmanager.h"
    1.33 +#include "tmslog.h"
    1.34 +#include "debug.h"
    1.35 +
    1.36 +
    1.37 +CDevice* CDevice::NewL()
    1.38 +    {
    1.39 +    __MSFNSLOG
    1.40 +	CDevice* r = new (ELeave) CDevice();
    1.41 +	CleanupStack::PushL(r);
    1.42 +
    1.43 +	r->ConstructL();
    1.44 +	CleanupStack::Pop();
    1.45 +	return r;
    1.46 +    }
    1.47 +
    1.48 +
    1.49 +void CDevice::ConstructL()
    1.50 +    {
    1.51 +    __MSFNLOG
    1.52 +    }
    1.53 +
    1.54 +
    1.55 +CDevice::CDevice()
    1.56 +    {
    1.57 +    __MSFNLOG
    1.58 +    }
    1.59 +
    1.60 +
    1.61 +CDevice::~CDevice()
    1.62 +    {
    1.63 +    __MSFNLOG
    1.64 +    __USBHOSTPRINT1(_L("~CDevice Token=%d"), iDeviceToken);
    1.65 +    }
    1.66 +
    1.67 +
    1.68 +TToken CDevice::OpenDeviceL(TUint aDeviceHandle, RUsbHubDriver& aHub)
    1.69 +    {
    1.70 +    __MSFNLOG
    1.71 +    __USBHOSTPRINT1(_L("CDevice::OpenDeviceL Handle=%d"), aDeviceHandle);
    1.72 +
    1.73 +    TInt err = iUsbDevice.Open(aHub, aDeviceHandle);
    1.74 +    __USBHOSTPRINT1(_L(" - returned %d\n"), err);
    1.75 +    User::LeaveIfError(err);
    1.76 +
    1.77 +    /* Retrieve the device descriptor */
    1.78 +    TUsbDeviceDescriptor devDescriptor;
    1.79 +    User::LeaveIfError(iUsbDevice.GetDeviceDescriptor(devDescriptor));
    1.80 +    iUsbPrint.PrintDescriptor(devDescriptor, 0, &iUsbDevice);
    1.81 +
    1.82 +    iUsbPrint.PrintTree(devDescriptor);
    1.83 +
    1.84 +    /* Retrieve the configuration descriptor */
    1.85 +    TUsbConfigurationDescriptor configDescriptor;
    1.86 +    User::LeaveIfError(iUsbDevice.GetConfigurationDescriptor(configDescriptor));
    1.87 +    iUsbPrint.PrintDescriptor(configDescriptor, 0, &iUsbDevice);
    1.88 +
    1.89 +    /* Get the token for interface 0 */
    1.90 +    TUint32 token;
    1.91 +    err = iUsbDevice.GetTokenForInterface(0, token);
    1.92 +    __USBHOSTPRINT2(_L("RUsbDevice::GetTokenForInterface returned error %d, token %08x"), err, token);
    1.93 +    User::LeaveIfError(err);
    1.94 +
    1.95 +    /* open the interface */
    1.96 +    RUsbInterface interface_ep0;
    1.97 +    err = interface_ep0.Open(token);
    1.98 +    __USBHOSTPRINT1(_L("RUsbInterface::Open returned error %d"), err);
    1.99 +    User::LeaveIfError(err);
   1.100 +
   1.101 +    /* Retrieve the interface and device descriptors */
   1.102 +    TUsbInterfaceDescriptor ifDescriptor;
   1.103 +    User::LeaveIfError(interface_ep0.GetInterfaceDescriptor(ifDescriptor));
   1.104 +    iUsbPrint.PrintDescriptor(ifDescriptor);
   1.105 +
   1.106 +    if (!IsDeviceMassStorage(ifDescriptor, devDescriptor))
   1.107 +        {
   1.108 +        RDebug::Print(_L("ATTACHED DEVICE IS NOT A MASS STORAGE DEVICE!\n"));
   1.109 +        User::Leave(KErrGeneral);
   1.110 +        }
   1.111 +
   1.112 +	TUint8 iProtocolId = ifDescriptor.InterfaceSubClass();
   1.113 +	TUint8 iTransportId = ifDescriptor.InterfaceProtocol();
   1.114 +
   1.115 +    interface_ep0.Close();
   1.116 +
   1.117 +    THostMassStorageConfig msConfig;
   1.118 +    msConfig.iInterfaceToken = token;
   1.119 +	msConfig.iProtocolId =iProtocolId;
   1.120 +	msConfig.iTransportId = iTransportId;
   1.121 +	msConfig.iStatusPollingInterval = 10; // 10 secs
   1.122 +
   1.123 +    TUint32 numLun;
   1.124 +
   1.125 +    TRequestStatus status;
   1.126 +    iUsbHostMsDevice.Add(msConfig, status);
   1.127 +	User::WaitForRequest(status);
   1.128 +	if (status.Int() != KErrNone)
   1.129 +        {
   1.130 +		__USBHOSTPRINT(_L("Add device failed"));
   1.131 +		User::Leave(status.Int());
   1.132 +        }
   1.133 +	TInt r = iUsbHostMsDevice.GetNumLun(numLun);
   1.134 +	if (r != KErrNone)
   1.135 +        {
   1.136 +		__USBHOSTPRINT(_L("GetNumLun failed"));
   1.137 +		User::Leave(r);
   1.138 +        }
   1.139 +
   1.140 +    if (numLun > KMaxLun)
   1.141 +        {
   1.142 +        __USBHOSTPRINT1(_L("Device MaxLun = %d. Error MaxLun > MAXLUN !"), numLun);
   1.143 +        User::Leave(KErrGeneral);
   1.144 +        }
   1.145 +
   1.146 +    __USBHOSTPRINT1(_L("MSC registered with %d Luns"), numLun);
   1.147 +
   1.148 +    iDeviceToken = token;
   1.149 +    iNumLuns = numLun;
   1.150 +    iDeviceHandle = aDeviceHandle;
   1.151 +    return token;
   1.152 +    }
   1.153 +
   1.154 +
   1.155 +void CDevice::CloseDeviceL()
   1.156 +    {
   1.157 +    __MSFNLOG
   1.158 +
   1.159 +    THostMassStorageConfig msConfig;
   1.160 +    msConfig.iInterfaceToken = iDeviceToken;
   1.161 +
   1.162 +    iUsbHostMsDevice.Remove();
   1.163 +    iUsbDevice.Close();
   1.164 +    }
   1.165 +
   1.166 +
   1.167 +void CDevice::MountLogicalUnitsL()
   1.168 +    {
   1.169 +    __MSFNLOG
   1.170 +    iExt.OpenL();
   1.171 +
   1.172 +    for (TInt lun = 0; lun < iNumLuns; lun++)
   1.173 +        {
   1.174 +        TDriveNumber driveNumber = iExt.GetDriveL();
   1.175 +        __PRINT2(_L("Mounting drive=%d lun=%d..."), driveNumber, lun);
   1.176 +        RDebug::Print(_L("Mounting drive=%d lun=%d..."), driveNumber, lun);
   1.177 +        TRAPD(err, iExt.MountL(iUsbHostMsDevice, driveNumber, iDeviceToken, lun));
   1.178 +        if (err == KErrNone || err == KErrNotReady || err == KErrCorrupt)
   1.179 +            {
   1.180 +            iLuList.Append(driveNumber);
   1.181 +            }
   1.182 +        __PRINT1(_L("%d"), err);
   1.183 +        RDebug::Print(_L("err=%d"), err);
   1.184 +        }
   1.185 +    }
   1.186 +
   1.187 +
   1.188 +void CDevice::DismountLogicalUnitsL()
   1.189 +    {
   1.190 +    __MSFNLOG
   1.191 +    for (TInt lun = 0; lun < iLuList.Count(); lun++)
   1.192 +        {
   1.193 +        TDriveNumber driveNumber = iLuList[lun];
   1.194 +        iExt.DismountL(iUsbHostMsDevice, driveNumber);
   1.195 +        }
   1.196 +    iLuList.Reset();
   1.197 +
   1.198 +    iExt.CloseL();
   1.199 +    }
   1.200 +
   1.201 +
   1.202 +TInt CDevice::GetEndpointAddress(RUsbInterface& aUsbInterface,
   1.203 +                                 TInt aInterfaceSetting,
   1.204 +                                 TUint8 aTransferType,
   1.205 +                                 TUint8 aDirection,
   1.206 +                                 TInt& aEndpointAddress) const
   1.207 +	{
   1.208 +    __MSFNSLOG
   1.209 +
   1.210 +	// Get the interface descriptor
   1.211 +	RDebug::Print(_L("GetEndpointAddress : Getting the interface descriptor for this alternate setting"));
   1.212 +
   1.213 +	TUsbInterfaceDescriptor alternateInterfaceDescriptor;
   1.214 +	TInt err = aUsbInterface.GetAlternateInterfaceDescriptor(aInterfaceSetting, alternateInterfaceDescriptor);
   1.215 +
   1.216 +	if (err)
   1.217 +		{
   1.218 +		RDebug::Print(_L("GetEndpointAddress : <Error %d> Unable to get alternate interface (%d) descriptor"),err,aInterfaceSetting);
   1.219 +		return err;
   1.220 +		}
   1.221 +
   1.222 +	// Parse the descriptor tree from the interface
   1.223 +	RDebug::Print(_L("Search the child descriptors for matching endpoint attributes"));
   1.224 +
   1.225 +	TUsbGenericDescriptor* descriptor = alternateInterfaceDescriptor.iFirstChild;
   1.226 +
   1.227 +	while (descriptor)
   1.228 +		{
   1.229 +		RDebug::Print(_L("GetEndpointAddress : Check descriptor type for endpoint"));
   1.230 +
   1.231 +		// Cast the descriptor to an endpoint descriptor
   1.232 +		TUsbEndpointDescriptor* endpoint = TUsbEndpointDescriptor::Cast(descriptor);
   1.233 +
   1.234 +		if (endpoint)
   1.235 +			{
   1.236 +			RDebug::Print(_L("GetEndpointAddress : Match attributes for transfer type"));
   1.237 +
   1.238 +			if ( (endpoint->Attributes() & aTransferType) == aTransferType)
   1.239 +				{
   1.240 +				RDebug::Print(_L("GetEndpointAddress : Match attributes for endpoint direction"));
   1.241 +
   1.242 +				if ( (endpoint->EndpointAddress() & 0x80) == aDirection)
   1.243 +					{
   1.244 +					aEndpointAddress = endpoint->EndpointAddress();
   1.245 +					RDebug::Print(_L("GetEndpointAddress : Endpoint address found"));
   1.246 +					return KErrNone;
   1.247 +					}
   1.248 +				}
   1.249 +			}
   1.250 +
   1.251 +		descriptor = descriptor->iNextPeer;
   1.252 +		}
   1.253 +
   1.254 +	// Unable to find the endpoint address
   1.255 +	RDebug::Print(_L("GetEndpointAddress : Unable to find endpoint address matching the specified attributes"));
   1.256 +
   1.257 +	return KErrNotFound;
   1.258 +	}
   1.259 +
   1.260 +
   1.261 +TBool CDevice::IsDeviceMassStorage(const TUsbInterfaceDescriptor& aInterfaceDesc,
   1.262 +                                   const TUsbDeviceDescriptor& aDeviceDesc) const
   1.263 +    {
   1.264 +    __MSFNSLOG
   1.265 +	/* check the interface descriptor */
   1.266 +	if(aInterfaceDesc.InterfaceClass() == 0x08 &&
   1.267 +		aInterfaceDesc.InterfaceSubClass() == 0x06 &&
   1.268 +		aInterfaceDesc.InterfaceProtocol() == 0x50)
   1.269 +        {
   1.270 +		if(aDeviceDesc.DeviceClass() == 0x00 &&
   1.271 +			aDeviceDesc.DeviceSubClass() == 0x00 &&
   1.272 +			aDeviceDesc.DeviceProtocol() == 0x00)
   1.273 +			return ETrue;
   1.274 +        }
   1.275 +
   1.276 +	return EFalse;
   1.277 +
   1.278 +    }
   1.279 +
   1.280 +
   1.281 +TLun CDevice::DriveMap(TDriveMap& aDriveMap) const
   1.282 +    {
   1.283 +    __MSFNSLOG
   1.284 +    TDriveNumber driveNumber;
   1.285 +    RDebug::Printf("LuList.Count=%d", iLuList.Count());
   1.286 +    for (TInt i = 0; i < iLuList.Count(); i++)
   1.287 +        {
   1.288 +        driveNumber = iLuList[i];
   1.289 +        aDriveMap[driveNumber] = iDeviceToken;
   1.290 +        RDebug::Printf("Device %d token=%d driveNumber=%d", i, iDeviceToken, driveNumber);
   1.291 +        }
   1.292 +
   1.293 +    return iNumLuns;
   1.294 +    }
   1.295 +
   1.296 +
   1.297 +TLun CDevice::DeviceMap(TDeviceMap& aDeviceMap) const
   1.298 +    {
   1.299 +    __MSFNSLOG
   1.300 +    TDriveNumber driveNumber;
   1.301 +    RDebug::Printf("LuList.Count=%d", iLuList.Count());
   1.302 +    for (TInt i = 0; i < iLuList.Count(); i++)
   1.303 +        {
   1.304 +        driveNumber = iLuList[i];
   1.305 +        aDeviceMap[i] = driveNumber;
   1.306 +        RDebug::Printf("CDevice LUN=%d driveNumber=%d", i, driveNumber);
   1.307 +        }
   1.308 +
   1.309 +    return iNumLuns;
   1.310 +    }
   1.311 +
   1.312 +
   1.313 +CUsbMsMountManager* CUsbMsMountManager::NewL()
   1.314 +    {
   1.315 +    __MSFNSLOG
   1.316 +	CUsbMsMountManager* r = new (ELeave) CUsbMsMountManager();
   1.317 +	CleanupStack::PushL(r);
   1.318 +
   1.319 +	r->ConstructL();
   1.320 +	CleanupStack::Pop();
   1.321 +	return r;
   1.322 +    }
   1.323 +
   1.324 +
   1.325 +void CUsbMsMountManager::ConstructL()
   1.326 +    {
   1.327 +    __MSFNLOG
   1.328 +    }
   1.329 +
   1.330 +
   1.331 +CUsbMsMountManager::CUsbMsMountManager()
   1.332 +    {
   1.333 +    __MSFNLOG
   1.334 +    }
   1.335 +
   1.336 +
   1.337 +CUsbMsMountManager::~CUsbMsMountManager()
   1.338 +    {
   1.339 +    __MSFNLOG
   1.340 +    iDeviceList.ResetAndDestroy();
   1.341 +    }
   1.342 +
   1.343 +
   1.344 +// adds new entry for this device
   1.345 +void CUsbMsMountManager::AddDeviceL(CDevice* aDevice)
   1.346 +    {
   1.347 +    __MSFNLOG
   1.348 +    iDeviceList.Append(aDevice);
   1.349 +    }
   1.350 +
   1.351 +
   1.352 +CDevice* CUsbMsMountManager::RemoveDeviceL(TUint aDeviceHandle)
   1.353 +    {
   1.354 +    __MSFNLOG
   1.355 +	TInt index = GetHandleIndexL(aDeviceHandle);
   1.356 +	CDevice* device = iDeviceList[index];
   1.357 +    iDeviceList.Remove(index);
   1.358 +    return device;
   1.359 +    }
   1.360 +
   1.361 +void CUsbMsMountManager::CloseAllDevicesL()
   1.362 +    {
   1.363 +    __MSFNLOG
   1.364 +    for (TInt i = 0; i < iDeviceList.Count(); i++)
   1.365 +        {
   1.366 +        iDeviceList[i]->CloseDeviceL();
   1.367 +        }
   1.368 +    }
   1.369 +
   1.370 +
   1.371 +TInt CUsbMsMountManager::GetDeviceIndexL(TToken aDeviceToken) const
   1.372 +    {
   1.373 +    __MSFNSLOG
   1.374 +    TInt index;
   1.375 +    for (index = 0; index < iDeviceList.Count(); index++)
   1.376 +        {
   1.377 +        if (aDeviceToken == iDeviceList[index]->DeviceToken())
   1.378 +            {
   1.379 +            break;
   1.380 +            }
   1.381 +        }
   1.382 +
   1.383 +    if (index == iDeviceList.Count())
   1.384 +        {
   1.385 +        User::Leave(KErrNotFound);
   1.386 +        }
   1.387 +
   1.388 +    return index;
   1.389 +    }
   1.390 +
   1.391 +
   1.392 +TInt CUsbMsMountManager::GetHandleIndexL(TUint aDeviceHandle) const
   1.393 +    {
   1.394 +    __MSFNSLOG
   1.395 +    TInt index;
   1.396 +    for (index = 0; index < iDeviceList.Count(); index++)
   1.397 +        {
   1.398 +        if (aDeviceHandle == iDeviceList[index]->DeviceHandle())
   1.399 +            {
   1.400 +            break;
   1.401 +            }
   1.402 +        }
   1.403 +
   1.404 +    if (index == iDeviceList.Count())
   1.405 +        {
   1.406 +        User::Leave(KErrNotFound);
   1.407 +        }
   1.408 +
   1.409 +    return index;
   1.410 +    }
   1.411 +
   1.412 +
   1.413 +
   1.414 +// mounts all LUNs for the device
   1.415 +void CUsbMsMountManager::MountDeviceL(TUint aDeviceHandle)
   1.416 +    {
   1.417 +    __MSFNLOG
   1.418 +    TInt index = GetHandleIndexL(aDeviceHandle);
   1.419 +    iDeviceList[index]->MountLogicalUnitsL();
   1.420 +    }
   1.421 +
   1.422 +
   1.423 +
   1.424 +// dismount all LUNs for this device
   1.425 +void CUsbMsMountManager::DismountDeviceL(TUint aDeviceHandle)
   1.426 +    {
   1.427 +    __MSFNLOG
   1.428 +    TInt index = GetHandleIndexL(aDeviceHandle);
   1.429 +    iDeviceList[index]->DismountLogicalUnitsL();
   1.430 +    }
   1.431 +
   1.432 +
   1.433 +// dismount all LUNs
   1.434 +void CUsbMsMountManager::DismountL()
   1.435 +    {
   1.436 +    __MSFNLOG
   1.437 +    for (TInt i = 0; i < iDeviceList.Count(); i++)
   1.438 +        {
   1.439 +        iDeviceList[i]->DismountLogicalUnitsL();
   1.440 +        }
   1.441 +    }
   1.442 +
   1.443 +
   1.444 +void CUsbMsMountManager::DriveMap(TDriveMap& aDriveMap) const
   1.445 +    {
   1.446 +    __MSFNSLOG
   1.447 +    TInt maxLun = 0;
   1.448 +    RDebug::Printf("DeviceList.Count=%d", iDeviceList.Count());
   1.449 +    for (TInt i = 0; i < iDeviceList.Count(); i++)
   1.450 +        {
   1.451 +        maxLun = iDeviceList[i]->DriveMap(aDriveMap);
   1.452 +        RDebug::Printf("%d %d", i, maxLun);
   1.453 +        }
   1.454 +    }
   1.455 +
   1.456 +
   1.457 +void CUsbMsMountManager::DeviceMap(TInt aDeviceIndex, TDeviceMap& aDeviceMap) const
   1.458 +    {
   1.459 +    __MSFNSLOG
   1.460 +    RDebug::Printf("Device=%d", aDeviceIndex);
   1.461 +
   1.462 +    __ASSERT_DEBUG(aDeviceIndex < iDeviceList.Count(), User::Invariant());
   1.463 +    iDeviceList[aDeviceIndex]->DeviceMap(aDeviceMap);
   1.464 +    }
   1.465 +
   1.466 +
   1.467 +
   1.468 +TUsbPrint::TUsbPrint()
   1.469 +:   iDebug(EFalse)
   1.470 +    {
   1.471 +    }
   1.472 +
   1.473 +void TUsbPrint::PrintTree(const TUsbGenericDescriptor& aDesc, TInt aDepth)
   1.474 +	{
   1.475 +    if (!iDebug)
   1.476 +        {
   1.477 +        return;
   1.478 +        }
   1.479 +
   1.480 +	TBuf<20> buf;
   1.481 +	for(TInt depth=aDepth;depth>=0;--depth)
   1.482 +		{
   1.483 +		buf.Append(_L("  "));
   1.484 +		}
   1.485 +	if(aDesc.iRecognisedAndParsed == TUsbGenericDescriptor::ERecognised)
   1.486 +		{
   1.487 +		RDebug::Print(_L("%S+0x%08x - %d 0x%02x"), &buf, &aDesc, aDesc.ibLength, aDesc.ibDescriptorType);
   1.488 +		}
   1.489 +	else
   1.490 +		{
   1.491 +		RDebug::Print(_L("%S-0x%08x - %d 0x%02x"), &buf, &aDesc, aDesc.ibLength, aDesc.ibDescriptorType);
   1.492 +		}
   1.493 +	HBufC* blob = HBufC::New(5*aDesc.iBlob.Length()); // 5* for " 0x" + 2*bytes for hex representation
   1.494 +	if(blob)
   1.495 +		{
   1.496 +		for(TInt i=0;i<aDesc.iBlob.Length();++i)
   1.497 +			{
   1.498 +			blob->Des().AppendFormat(_L("0x%02x "), aDesc.iBlob[i]);
   1.499 +			}
   1.500 +		RDebug::Print(_L("%S >%S"), &buf, blob);
   1.501 +		delete blob;
   1.502 +		}
   1.503 +	if(aDesc.iFirstChild)
   1.504 +		{
   1.505 +		RDebug::Print(_L("%S \\ "), &buf);
   1.506 +		PrintTree(*(aDesc.iFirstChild), aDepth+1);
   1.507 +		RDebug::Print(_L("%S / "), &buf);
   1.508 +		}
   1.509 +	if(aDesc.iNextPeer)
   1.510 +		{
   1.511 +		PrintTree(*(aDesc.iNextPeer), aDepth);
   1.512 +		}
   1.513 +	}
   1.514 +
   1.515 +
   1.516 +static TUint16 gLangId = 0x0000;
   1.517 +
   1.518 +void TUsbPrint::SetLanguageToPrintL(RUsbDevice& aDevice)
   1.519 +	{
   1.520 +    if (!iDebug) return;
   1.521 +
   1.522 +	// Try to set language to US Eng, otherwise take the first one listed.
   1.523 +	if(gLangId == 0x0000) // Only make the request if not been made before.
   1.524 +		{
   1.525 +		// Get string descriptor 0.
   1.526 +		TBuf8<256> stringBuf;
   1.527 +		TUsbStringDescriptor* stringDesc = NULL;
   1.528 +		User::LeaveIfError(aDevice.GetStringDescriptor(stringDesc, stringBuf, 0));
   1.529 +		CleanupStack::PushL(*stringDesc);
   1.530 +
   1.531 +		// Search for US English
   1.532 +		TBool usEngLang = EFalse;
   1.533 +		TInt langId = 0;
   1.534 +		TInt index = 0;
   1.535 +		const TUint16 KLangIdUsEng = 0x0409;
   1.536 +		while(!usEngLang && langId != KErrNotFound)
   1.537 +			{
   1.538 +			langId = stringDesc->GetLangId(index);
   1.539 +			usEngLang = (langId == KLangIdUsEng);
   1.540 +			index++;
   1.541 +			}
   1.542 +
   1.543 +		// Set the language appropriately
   1.544 +		if(usEngLang)
   1.545 +			{
   1.546 +        	gLangId = KLangIdUsEng;
   1.547 +			}
   1.548 +		else
   1.549 +			{
   1.550 +			gLangId = stringDesc->GetLangId(0);
   1.551 +			}
   1.552 +
   1.553 +		CleanupStack::PopAndDestroy(); // stringDesc
   1.554 +		}
   1.555 +	}
   1.556 +
   1.557 +
   1.558 +void TUsbPrint::PrintStringFromIndex(const TDesC& aFormatString,
   1.559 +                                     TInt aIndex,
   1.560 +                                     RUsbDevice* aDevice)
   1.561 +	{
   1.562 +    if (!iDebug) return;
   1.563 +
   1.564 +	// If we have no device handle, we cannot go and get any strings.
   1.565 +	// If we have index 0, this indicates we don't have a string for this entry.
   1.566 +	if(aDevice && aIndex != 0)
   1.567 +		{
   1.568 +		TRAPD(err, SetLanguageToPrintL(*aDevice));
   1.569 +		if(err == KErrNone)
   1.570 +			{
   1.571 +			TBuf8<255> stringBuf;
   1.572 +			TUsbStringDescriptor* stringDesc = NULL;
   1.573 +			err = aDevice->GetStringDescriptor(stringDesc, stringBuf, aIndex, gLangId);
   1.574 +			if(err == KErrNone)
   1.575 +				{
   1.576 +				TBuf<128> buf;
   1.577 +				stringDesc->StringData(buf);
   1.578 +				RDebug::Print(aFormatString, &buf);
   1.579 +				stringDesc->DestroyTree();
   1.580 +				}
   1.581 +			delete stringDesc;
   1.582 +			}
   1.583 +		else
   1.584 +			{
   1.585 +			RDebug::Print(_L("Error while Selecting Langauge %d\n"), err);
   1.586 +			}
   1.587 +		}
   1.588 +	}
   1.589 +
   1.590 +
   1.591 +void TUsbPrint::PrintDescriptor(const TUsbDeviceDescriptor& aDeviceDesc,
   1.592 +                                TInt /*aVariant*/,
   1.593 +                                RUsbDevice* aDevice)
   1.594 +	{
   1.595 +    if (!iDebug) return;
   1.596 +
   1.597 +	RDebug::Print(_L("USBBcd = 0x%04x\n"), aDeviceDesc.USBBcd());
   1.598 +	RDebug::Print(_L("DeviceClass = 0x%02x\n"), aDeviceDesc.DeviceClass());
   1.599 +	RDebug::Print(_L("DeviceSubClass = 0x%02x\n"), aDeviceDesc.DeviceSubClass());
   1.600 +	RDebug::Print(_L("DeviceProtocol = 0x%02x\n"), aDeviceDesc.DeviceProtocol());
   1.601 +	RDebug::Print(_L("MaxPacketSize0 = 0x%02x\n"), aDeviceDesc.MaxPacketSize0());
   1.602 +	RDebug::Print(_L("VendorId = 0x%04x\n"), aDeviceDesc.VendorId());
   1.603 +	RDebug::Print(_L("ProductId = 0x%04x\n"), aDeviceDesc.ProductId());
   1.604 +	RDebug::Print(_L("DeviceBcd = 0x%04x\n"), aDeviceDesc.DeviceBcd());
   1.605 +	RDebug::Print(_L("ManufacturerIndex = 0x%02x\n"), aDeviceDesc.ManufacturerIndex());
   1.606 +	PrintStringFromIndex(_L("ManufacturerString = %S\n"), aDeviceDesc.ManufacturerIndex(), aDevice);
   1.607 +	RDebug::Print(_L("ProductIndex = 0x%02x\n"), aDeviceDesc.ProductIndex());
   1.608 +	PrintStringFromIndex(_L("ProductString = %S\n"), aDeviceDesc.ProductIndex(), aDevice);
   1.609 +	RDebug::Print(_L("SerialNumberIndex = 0x%02x\n"), aDeviceDesc.SerialNumberIndex());
   1.610 +	PrintStringFromIndex(_L("SerialNumberString = %S\n"), aDeviceDesc.SerialNumberIndex(), aDevice);
   1.611 +	RDebug::Print(_L("NumConfigurations = 0x%02x\n"), aDeviceDesc.NumConfigurations());
   1.612 +	}
   1.613 +
   1.614 +
   1.615 +void TUsbPrint::PrintDescriptor(const TUsbConfigurationDescriptor& aConfigDesc,
   1.616 +                                TInt /*aVariant*/,
   1.617 +                                RUsbDevice* aDevice)
   1.618 +	{
   1.619 +    if (!iDebug) return;
   1.620 +	RDebug::Print(_L("TotalLength = 0x%04x\n"), aConfigDesc.TotalLength());
   1.621 +	RDebug::Print(_L("NumInterfaces = 0x%02x\n"), aConfigDesc.NumInterfaces());
   1.622 +	RDebug::Print(_L("ConfigurationValue = 0x%02x\n"), aConfigDesc.ConfigurationValue());
   1.623 +	RDebug::Print(_L("ConfigurationIndex = 0x%02x\n"), aConfigDesc.ConfigurationIndex());
   1.624 +	PrintStringFromIndex(_L("ConfigurationString = %S\n"), aConfigDesc.ConfigurationIndex(), aDevice);
   1.625 +	RDebug::Print(_L("Attributes = 0x%02x\n"), aConfigDesc.Attributes());
   1.626 +	RDebug::Print(_L("MaxPower = 0x%02x\n"), aConfigDesc.MaxPower());
   1.627 +	}
   1.628 +
   1.629 +
   1.630 +void TUsbPrint::PrintDescriptor(const TUsbEndpointDescriptor& aEndpointDesc,
   1.631 +                                TInt /*aVariant*/,
   1.632 +                                RUsbDevice* /*aDevice*/)
   1.633 +	{
   1.634 +    if (!iDebug) return;
   1.635 +	RDebug::Print(_L("EndpointAddress = 0x%02x\n"), aEndpointDesc.EndpointAddress());
   1.636 +	RDebug::Print(_L("Attributes = 0x%02x\n"), aEndpointDesc.Attributes());
   1.637 +	RDebug::Print(_L("MaxPacketSize = 0x%04x\n"), aEndpointDesc.MaxPacketSize());
   1.638 +	RDebug::Print(_L("Interval = 0x%02x\n"), aEndpointDesc.Interval());
   1.639 +	}
   1.640 +
   1.641 +
   1.642 +void TUsbPrint::PrintDescriptor(const TUsbInterfaceDescriptor& aInterfaceDesc,
   1.643 +                                TInt /*aVariant*/,
   1.644 +                                RUsbDevice* /*aDevice*/)
   1.645 +	{
   1.646 +    if (!iDebug) return;
   1.647 +	RDebug::Print(_L("InterfaceNumber = 0x%02x\n"), aInterfaceDesc.InterfaceNumber());
   1.648 +	RDebug::Print(_L("AlternateSetting = 0x%02x\n"), aInterfaceDesc.AlternateSetting());
   1.649 +	RDebug::Print(_L("NumEndpoints = 0x%02x\n"), aInterfaceDesc.NumEndpoints());
   1.650 +	RDebug::Print(_L("InterfaceClass = 0x%02x\n"), aInterfaceDesc.InterfaceClass());
   1.651 +	RDebug::Print(_L("InterfaceSubClass = 0x%02x\n"), aInterfaceDesc.InterfaceSubClass());
   1.652 +	RDebug::Print(_L("InterfaceProtocol = 0x%02x\n"), aInterfaceDesc.InterfaceProtocol());
   1.653 +	RDebug::Print(_L("Interface = 0x%02x\n"), aInterfaceDesc.Interface());
   1.654 +	}
   1.655 +
   1.656 +
   1.657 +void TUsbPrint::PrintDescriptor(const TUsbStringDescriptor& aStringDesc,
   1.658 +                                TInt aVariant,
   1.659 +                                RUsbDevice* /*aDevice*/)
   1.660 +	{
   1.661 +    if (!iDebug) return;
   1.662 +	if(aVariant == 0)
   1.663 +		{
   1.664 +		RDebug::Print(_L("String Descriptor Zero\n"));
   1.665 +		TInt index = 0;
   1.666 +		TInt langId = 0;
   1.667 +		while((langId = aStringDesc.GetLangId(index)) != KErrNotFound)
   1.668 +			{
   1.669 +			RDebug::Print(_L("  >0x%04x\n"), langId);
   1.670 +			++index;
   1.671 +			}
   1.672 +		}
   1.673 +	else
   1.674 +		{
   1.675 +		RDebug::Print(_L("Generic String Descriptor\n"));
   1.676 +		HBufC16* string = HBufC16::New(128);
   1.677 +		if(string)
   1.678 +			{
   1.679 +			TPtr16 stringPtr = string->Des();
   1.680 +			aStringDesc.StringData(stringPtr);
   1.681 +			RDebug::Print(_L("  >%S\n"), string);
   1.682 +			}
   1.683 +		delete string;
   1.684 +		}
   1.685 +	}