os/kernelhwsrv/kerneltest/f32test/shostmassstorage/msman/src/cusbmsmountmanager.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
#include <f32file.h>
sl@0
    17
#include <d32usbdi_hubdriver.h>
sl@0
    18
#include <d32usbdi.h>
sl@0
    19
#include <d32otgdi.h>
sl@0
    20
#include <d32usbdescriptors.h>
sl@0
    21
#include <d32usbtransfers.h>
sl@0
    22
sl@0
    23
#include <shared.h>
sl@0
    24
sl@0
    25
#include "rusbhostmsdevice.h"
sl@0
    26
sl@0
    27
#include "rextfilesystem.h"
sl@0
    28
#include "usbtypes.h"
sl@0
    29
#include "cusbmsmountmanager.h"
sl@0
    30
#include "tmslog.h"
sl@0
    31
#include "debug.h"
sl@0
    32
sl@0
    33
sl@0
    34
CDevice* CDevice::NewL()
sl@0
    35
    {
sl@0
    36
    __MSFNSLOG
sl@0
    37
	CDevice* r = new (ELeave) CDevice();
sl@0
    38
	CleanupStack::PushL(r);
sl@0
    39
sl@0
    40
	r->ConstructL();
sl@0
    41
	CleanupStack::Pop();
sl@0
    42
	return r;
sl@0
    43
    }
sl@0
    44
sl@0
    45
sl@0
    46
void CDevice::ConstructL()
sl@0
    47
    {
sl@0
    48
    __MSFNLOG
sl@0
    49
    }
sl@0
    50
sl@0
    51
sl@0
    52
CDevice::CDevice()
sl@0
    53
    {
sl@0
    54
    __MSFNLOG
sl@0
    55
    }
sl@0
    56
sl@0
    57
sl@0
    58
CDevice::~CDevice()
sl@0
    59
    {
sl@0
    60
    __MSFNLOG
sl@0
    61
    __USBHOSTPRINT1(_L("~CDevice Token=%d"), iDeviceToken);
sl@0
    62
    }
sl@0
    63
sl@0
    64
sl@0
    65
TToken CDevice::OpenDeviceL(TUint aDeviceHandle, RUsbHubDriver& aHub)
sl@0
    66
    {
sl@0
    67
    __MSFNLOG
sl@0
    68
    __USBHOSTPRINT1(_L("CDevice::OpenDeviceL Handle=%d"), aDeviceHandle);
sl@0
    69
sl@0
    70
    TInt err = iUsbDevice.Open(aHub, aDeviceHandle);
sl@0
    71
    __USBHOSTPRINT1(_L(" - returned %d\n"), err);
sl@0
    72
    User::LeaveIfError(err);
sl@0
    73
sl@0
    74
    /* Retrieve the device descriptor */
sl@0
    75
    TUsbDeviceDescriptor devDescriptor;
sl@0
    76
    User::LeaveIfError(iUsbDevice.GetDeviceDescriptor(devDescriptor));
sl@0
    77
    iUsbPrint.PrintDescriptor(devDescriptor, 0, &iUsbDevice);
sl@0
    78
sl@0
    79
    iUsbPrint.PrintTree(devDescriptor);
sl@0
    80
sl@0
    81
    /* Retrieve the configuration descriptor */
sl@0
    82
    TUsbConfigurationDescriptor configDescriptor;
sl@0
    83
    User::LeaveIfError(iUsbDevice.GetConfigurationDescriptor(configDescriptor));
sl@0
    84
    iUsbPrint.PrintDescriptor(configDescriptor, 0, &iUsbDevice);
sl@0
    85
sl@0
    86
    /* Get the token for interface 0 */
sl@0
    87
    TUint32 token;
sl@0
    88
    err = iUsbDevice.GetTokenForInterface(0, token);
sl@0
    89
    __USBHOSTPRINT2(_L("RUsbDevice::GetTokenForInterface returned error %d, token %08x"), err, token);
sl@0
    90
    User::LeaveIfError(err);
sl@0
    91
sl@0
    92
    /* open the interface */
sl@0
    93
    RUsbInterface interface_ep0;
sl@0
    94
    err = interface_ep0.Open(token);
sl@0
    95
    __USBHOSTPRINT1(_L("RUsbInterface::Open returned error %d"), err);
sl@0
    96
    User::LeaveIfError(err);
sl@0
    97
sl@0
    98
    /* Retrieve the interface and device descriptors */
sl@0
    99
    TUsbInterfaceDescriptor ifDescriptor;
sl@0
   100
    User::LeaveIfError(interface_ep0.GetInterfaceDescriptor(ifDescriptor));
sl@0
   101
    iUsbPrint.PrintDescriptor(ifDescriptor);
sl@0
   102
sl@0
   103
    if (!IsDeviceMassStorage(ifDescriptor, devDescriptor))
sl@0
   104
        {
sl@0
   105
        RDebug::Print(_L("ATTACHED DEVICE IS NOT A MASS STORAGE DEVICE!\n"));
sl@0
   106
        User::Leave(KErrGeneral);
sl@0
   107
        }
sl@0
   108
sl@0
   109
	TUint8 iProtocolId = ifDescriptor.InterfaceSubClass();
sl@0
   110
	TUint8 iTransportId = ifDescriptor.InterfaceProtocol();
sl@0
   111
sl@0
   112
    interface_ep0.Close();
sl@0
   113
sl@0
   114
    THostMassStorageConfig msConfig;
sl@0
   115
    msConfig.iInterfaceToken = token;
sl@0
   116
	msConfig.iProtocolId =iProtocolId;
sl@0
   117
	msConfig.iTransportId = iTransportId;
sl@0
   118
	msConfig.iStatusPollingInterval = 10; // 10 secs
sl@0
   119
sl@0
   120
    TUint32 numLun;
sl@0
   121
sl@0
   122
    TRequestStatus status;
sl@0
   123
    iUsbHostMsDevice.Add(msConfig, status);
sl@0
   124
	User::WaitForRequest(status);
sl@0
   125
	if (status.Int() != KErrNone)
sl@0
   126
        {
sl@0
   127
		__USBHOSTPRINT(_L("Add device failed"));
sl@0
   128
		User::Leave(status.Int());
sl@0
   129
        }
sl@0
   130
	TInt r = iUsbHostMsDevice.GetNumLun(numLun);
sl@0
   131
	if (r != KErrNone)
sl@0
   132
        {
sl@0
   133
		__USBHOSTPRINT(_L("GetNumLun failed"));
sl@0
   134
		User::Leave(r);
sl@0
   135
        }
sl@0
   136
sl@0
   137
    if (numLun > KMaxLun)
sl@0
   138
        {
sl@0
   139
        __USBHOSTPRINT1(_L("Device MaxLun = %d. Error MaxLun > MAXLUN !"), numLun);
sl@0
   140
        User::Leave(KErrGeneral);
sl@0
   141
        }
sl@0
   142
sl@0
   143
    __USBHOSTPRINT1(_L("MSC registered with %d Luns"), numLun);
sl@0
   144
sl@0
   145
    iDeviceToken = token;
sl@0
   146
    iNumLuns = numLun;
sl@0
   147
    iDeviceHandle = aDeviceHandle;
sl@0
   148
    return token;
sl@0
   149
    }
sl@0
   150
sl@0
   151
sl@0
   152
void CDevice::CloseDeviceL()
sl@0
   153
    {
sl@0
   154
    __MSFNLOG
sl@0
   155
sl@0
   156
    THostMassStorageConfig msConfig;
sl@0
   157
    msConfig.iInterfaceToken = iDeviceToken;
sl@0
   158
sl@0
   159
    iUsbHostMsDevice.Remove();
sl@0
   160
    iUsbDevice.Close();
sl@0
   161
    }
sl@0
   162
sl@0
   163
sl@0
   164
void CDevice::MountLogicalUnitsL()
sl@0
   165
    {
sl@0
   166
    __MSFNLOG
sl@0
   167
    iExt.OpenL();
sl@0
   168
sl@0
   169
    for (TInt lun = 0; lun < iNumLuns; lun++)
sl@0
   170
        {
sl@0
   171
        TDriveNumber driveNumber = iExt.GetDriveL();
sl@0
   172
        __PRINT2(_L("Mounting drive=%d lun=%d..."), driveNumber, lun);
sl@0
   173
        RDebug::Print(_L("Mounting drive=%d lun=%d..."), driveNumber, lun);
sl@0
   174
        TRAPD(err, iExt.MountL(iUsbHostMsDevice, driveNumber, iDeviceToken, lun));
sl@0
   175
        if (err == KErrNone || err == KErrNotReady || err == KErrCorrupt)
sl@0
   176
            {
sl@0
   177
            iLuList.Append(driveNumber);
sl@0
   178
            }
sl@0
   179
        __PRINT1(_L("%d"), err);
sl@0
   180
        RDebug::Print(_L("err=%d"), err);
sl@0
   181
        }
sl@0
   182
    }
sl@0
   183
sl@0
   184
sl@0
   185
void CDevice::DismountLogicalUnitsL()
sl@0
   186
    {
sl@0
   187
    __MSFNLOG
sl@0
   188
    for (TInt lun = 0; lun < iLuList.Count(); lun++)
sl@0
   189
        {
sl@0
   190
        TDriveNumber driveNumber = iLuList[lun];
sl@0
   191
        iExt.DismountL(iUsbHostMsDevice, driveNumber);
sl@0
   192
        }
sl@0
   193
    iLuList.Reset();
sl@0
   194
sl@0
   195
    iExt.CloseL();
sl@0
   196
    }
sl@0
   197
sl@0
   198
sl@0
   199
TInt CDevice::GetEndpointAddress(RUsbInterface& aUsbInterface,
sl@0
   200
                                 TInt aInterfaceSetting,
sl@0
   201
                                 TUint8 aTransferType,
sl@0
   202
                                 TUint8 aDirection,
sl@0
   203
                                 TInt& aEndpointAddress) const
sl@0
   204
	{
sl@0
   205
    __MSFNSLOG
sl@0
   206
sl@0
   207
	// Get the interface descriptor
sl@0
   208
	RDebug::Print(_L("GetEndpointAddress : Getting the interface descriptor for this alternate setting"));
sl@0
   209
sl@0
   210
	TUsbInterfaceDescriptor alternateInterfaceDescriptor;
sl@0
   211
	TInt err = aUsbInterface.GetAlternateInterfaceDescriptor(aInterfaceSetting, alternateInterfaceDescriptor);
sl@0
   212
sl@0
   213
	if (err)
sl@0
   214
		{
sl@0
   215
		RDebug::Print(_L("GetEndpointAddress : <Error %d> Unable to get alternate interface (%d) descriptor"),err,aInterfaceSetting);
sl@0
   216
		return err;
sl@0
   217
		}
sl@0
   218
sl@0
   219
	// Parse the descriptor tree from the interface
sl@0
   220
	RDebug::Print(_L("Search the child descriptors for matching endpoint attributes"));
sl@0
   221
sl@0
   222
	TUsbGenericDescriptor* descriptor = alternateInterfaceDescriptor.iFirstChild;
sl@0
   223
sl@0
   224
	while (descriptor)
sl@0
   225
		{
sl@0
   226
		RDebug::Print(_L("GetEndpointAddress : Check descriptor type for endpoint"));
sl@0
   227
sl@0
   228
		// Cast the descriptor to an endpoint descriptor
sl@0
   229
		TUsbEndpointDescriptor* endpoint = TUsbEndpointDescriptor::Cast(descriptor);
sl@0
   230
sl@0
   231
		if (endpoint)
sl@0
   232
			{
sl@0
   233
			RDebug::Print(_L("GetEndpointAddress : Match attributes for transfer type"));
sl@0
   234
sl@0
   235
			if ( (endpoint->Attributes() & aTransferType) == aTransferType)
sl@0
   236
				{
sl@0
   237
				RDebug::Print(_L("GetEndpointAddress : Match attributes for endpoint direction"));
sl@0
   238
sl@0
   239
				if ( (endpoint->EndpointAddress() & 0x80) == aDirection)
sl@0
   240
					{
sl@0
   241
					aEndpointAddress = endpoint->EndpointAddress();
sl@0
   242
					RDebug::Print(_L("GetEndpointAddress : Endpoint address found"));
sl@0
   243
					return KErrNone;
sl@0
   244
					}
sl@0
   245
				}
sl@0
   246
			}
sl@0
   247
sl@0
   248
		descriptor = descriptor->iNextPeer;
sl@0
   249
		}
sl@0
   250
sl@0
   251
	// Unable to find the endpoint address
sl@0
   252
	RDebug::Print(_L("GetEndpointAddress : Unable to find endpoint address matching the specified attributes"));
sl@0
   253
sl@0
   254
	return KErrNotFound;
sl@0
   255
	}
sl@0
   256
sl@0
   257
sl@0
   258
TBool CDevice::IsDeviceMassStorage(const TUsbInterfaceDescriptor& aInterfaceDesc,
sl@0
   259
                                   const TUsbDeviceDescriptor& aDeviceDesc) const
sl@0
   260
    {
sl@0
   261
    __MSFNSLOG
sl@0
   262
	/* check the interface descriptor */
sl@0
   263
	if(aInterfaceDesc.InterfaceClass() == 0x08 &&
sl@0
   264
		aInterfaceDesc.InterfaceSubClass() == 0x06 &&
sl@0
   265
		aInterfaceDesc.InterfaceProtocol() == 0x50)
sl@0
   266
        {
sl@0
   267
		if(aDeviceDesc.DeviceClass() == 0x00 &&
sl@0
   268
			aDeviceDesc.DeviceSubClass() == 0x00 &&
sl@0
   269
			aDeviceDesc.DeviceProtocol() == 0x00)
sl@0
   270
			return ETrue;
sl@0
   271
        }
sl@0
   272
sl@0
   273
	return EFalse;
sl@0
   274
sl@0
   275
    }
sl@0
   276
sl@0
   277
sl@0
   278
TLun CDevice::DriveMap(TDriveMap& aDriveMap) const
sl@0
   279
    {
sl@0
   280
    __MSFNSLOG
sl@0
   281
    TDriveNumber driveNumber;
sl@0
   282
    RDebug::Printf("LuList.Count=%d", iLuList.Count());
sl@0
   283
    for (TInt i = 0; i < iLuList.Count(); i++)
sl@0
   284
        {
sl@0
   285
        driveNumber = iLuList[i];
sl@0
   286
        aDriveMap[driveNumber] = iDeviceToken;
sl@0
   287
        RDebug::Printf("Device %d token=%d driveNumber=%d", i, iDeviceToken, driveNumber);
sl@0
   288
        }
sl@0
   289
sl@0
   290
    return iNumLuns;
sl@0
   291
    }
sl@0
   292
sl@0
   293
sl@0
   294
TLun CDevice::DeviceMap(TDeviceMap& aDeviceMap) const
sl@0
   295
    {
sl@0
   296
    __MSFNSLOG
sl@0
   297
    TDriveNumber driveNumber;
sl@0
   298
    RDebug::Printf("LuList.Count=%d", iLuList.Count());
sl@0
   299
    for (TInt i = 0; i < iLuList.Count(); i++)
sl@0
   300
        {
sl@0
   301
        driveNumber = iLuList[i];
sl@0
   302
        aDeviceMap[i] = driveNumber;
sl@0
   303
        RDebug::Printf("CDevice LUN=%d driveNumber=%d", i, driveNumber);
sl@0
   304
        }
sl@0
   305
sl@0
   306
    return iNumLuns;
sl@0
   307
    }
sl@0
   308
sl@0
   309
sl@0
   310
CUsbMsMountManager* CUsbMsMountManager::NewL()
sl@0
   311
    {
sl@0
   312
    __MSFNSLOG
sl@0
   313
	CUsbMsMountManager* r = new (ELeave) CUsbMsMountManager();
sl@0
   314
	CleanupStack::PushL(r);
sl@0
   315
sl@0
   316
	r->ConstructL();
sl@0
   317
	CleanupStack::Pop();
sl@0
   318
	return r;
sl@0
   319
    }
sl@0
   320
sl@0
   321
sl@0
   322
void CUsbMsMountManager::ConstructL()
sl@0
   323
    {
sl@0
   324
    __MSFNLOG
sl@0
   325
    }
sl@0
   326
sl@0
   327
sl@0
   328
CUsbMsMountManager::CUsbMsMountManager()
sl@0
   329
    {
sl@0
   330
    __MSFNLOG
sl@0
   331
    }
sl@0
   332
sl@0
   333
sl@0
   334
CUsbMsMountManager::~CUsbMsMountManager()
sl@0
   335
    {
sl@0
   336
    __MSFNLOG
sl@0
   337
    iDeviceList.ResetAndDestroy();
sl@0
   338
    }
sl@0
   339
sl@0
   340
sl@0
   341
// adds new entry for this device
sl@0
   342
void CUsbMsMountManager::AddDeviceL(CDevice* aDevice)
sl@0
   343
    {
sl@0
   344
    __MSFNLOG
sl@0
   345
    iDeviceList.Append(aDevice);
sl@0
   346
    }
sl@0
   347
sl@0
   348
sl@0
   349
CDevice* CUsbMsMountManager::RemoveDeviceL(TUint aDeviceHandle)
sl@0
   350
    {
sl@0
   351
    __MSFNLOG
sl@0
   352
	TInt index = GetHandleIndexL(aDeviceHandle);
sl@0
   353
	CDevice* device = iDeviceList[index];
sl@0
   354
    iDeviceList.Remove(index);
sl@0
   355
    return device;
sl@0
   356
    }
sl@0
   357
sl@0
   358
void CUsbMsMountManager::CloseAllDevicesL()
sl@0
   359
    {
sl@0
   360
    __MSFNLOG
sl@0
   361
    for (TInt i = 0; i < iDeviceList.Count(); i++)
sl@0
   362
        {
sl@0
   363
        iDeviceList[i]->CloseDeviceL();
sl@0
   364
        }
sl@0
   365
    }
sl@0
   366
sl@0
   367
sl@0
   368
TInt CUsbMsMountManager::GetDeviceIndexL(TToken aDeviceToken) const
sl@0
   369
    {
sl@0
   370
    __MSFNSLOG
sl@0
   371
    TInt index;
sl@0
   372
    for (index = 0; index < iDeviceList.Count(); index++)
sl@0
   373
        {
sl@0
   374
        if (aDeviceToken == iDeviceList[index]->DeviceToken())
sl@0
   375
            {
sl@0
   376
            break;
sl@0
   377
            }
sl@0
   378
        }
sl@0
   379
sl@0
   380
    if (index == iDeviceList.Count())
sl@0
   381
        {
sl@0
   382
        User::Leave(KErrNotFound);
sl@0
   383
        }
sl@0
   384
sl@0
   385
    return index;
sl@0
   386
    }
sl@0
   387
sl@0
   388
sl@0
   389
TInt CUsbMsMountManager::GetHandleIndexL(TUint aDeviceHandle) const
sl@0
   390
    {
sl@0
   391
    __MSFNSLOG
sl@0
   392
    TInt index;
sl@0
   393
    for (index = 0; index < iDeviceList.Count(); index++)
sl@0
   394
        {
sl@0
   395
        if (aDeviceHandle == iDeviceList[index]->DeviceHandle())
sl@0
   396
            {
sl@0
   397
            break;
sl@0
   398
            }
sl@0
   399
        }
sl@0
   400
sl@0
   401
    if (index == iDeviceList.Count())
sl@0
   402
        {
sl@0
   403
        User::Leave(KErrNotFound);
sl@0
   404
        }
sl@0
   405
sl@0
   406
    return index;
sl@0
   407
    }
sl@0
   408
sl@0
   409
sl@0
   410
sl@0
   411
// mounts all LUNs for the device
sl@0
   412
void CUsbMsMountManager::MountDeviceL(TUint aDeviceHandle)
sl@0
   413
    {
sl@0
   414
    __MSFNLOG
sl@0
   415
    TInt index = GetHandleIndexL(aDeviceHandle);
sl@0
   416
    iDeviceList[index]->MountLogicalUnitsL();
sl@0
   417
    }
sl@0
   418
sl@0
   419
sl@0
   420
sl@0
   421
// dismount all LUNs for this device
sl@0
   422
void CUsbMsMountManager::DismountDeviceL(TUint aDeviceHandle)
sl@0
   423
    {
sl@0
   424
    __MSFNLOG
sl@0
   425
    TInt index = GetHandleIndexL(aDeviceHandle);
sl@0
   426
    iDeviceList[index]->DismountLogicalUnitsL();
sl@0
   427
    }
sl@0
   428
sl@0
   429
sl@0
   430
// dismount all LUNs
sl@0
   431
void CUsbMsMountManager::DismountL()
sl@0
   432
    {
sl@0
   433
    __MSFNLOG
sl@0
   434
    for (TInt i = 0; i < iDeviceList.Count(); i++)
sl@0
   435
        {
sl@0
   436
        iDeviceList[i]->DismountLogicalUnitsL();
sl@0
   437
        }
sl@0
   438
    }
sl@0
   439
sl@0
   440
sl@0
   441
void CUsbMsMountManager::DriveMap(TDriveMap& aDriveMap) const
sl@0
   442
    {
sl@0
   443
    __MSFNSLOG
sl@0
   444
    TInt maxLun = 0;
sl@0
   445
    RDebug::Printf("DeviceList.Count=%d", iDeviceList.Count());
sl@0
   446
    for (TInt i = 0; i < iDeviceList.Count(); i++)
sl@0
   447
        {
sl@0
   448
        maxLun = iDeviceList[i]->DriveMap(aDriveMap);
sl@0
   449
        RDebug::Printf("%d %d", i, maxLun);
sl@0
   450
        }
sl@0
   451
    }
sl@0
   452
sl@0
   453
sl@0
   454
void CUsbMsMountManager::DeviceMap(TInt aDeviceIndex, TDeviceMap& aDeviceMap) const
sl@0
   455
    {
sl@0
   456
    __MSFNSLOG
sl@0
   457
    RDebug::Printf("Device=%d", aDeviceIndex);
sl@0
   458
sl@0
   459
    __ASSERT_DEBUG(aDeviceIndex < iDeviceList.Count(), User::Invariant());
sl@0
   460
    iDeviceList[aDeviceIndex]->DeviceMap(aDeviceMap);
sl@0
   461
    }
sl@0
   462
sl@0
   463
sl@0
   464
sl@0
   465
TUsbPrint::TUsbPrint()
sl@0
   466
:   iDebug(EFalse)
sl@0
   467
    {
sl@0
   468
    }
sl@0
   469
sl@0
   470
void TUsbPrint::PrintTree(const TUsbGenericDescriptor& aDesc, TInt aDepth)
sl@0
   471
	{
sl@0
   472
    if (!iDebug)
sl@0
   473
        {
sl@0
   474
        return;
sl@0
   475
        }
sl@0
   476
sl@0
   477
	TBuf<20> buf;
sl@0
   478
	for(TInt depth=aDepth;depth>=0;--depth)
sl@0
   479
		{
sl@0
   480
		buf.Append(_L("  "));
sl@0
   481
		}
sl@0
   482
	if(aDesc.iRecognisedAndParsed == TUsbGenericDescriptor::ERecognised)
sl@0
   483
		{
sl@0
   484
		RDebug::Print(_L("%S+0x%08x - %d 0x%02x"), &buf, &aDesc, aDesc.ibLength, aDesc.ibDescriptorType);
sl@0
   485
		}
sl@0
   486
	else
sl@0
   487
		{
sl@0
   488
		RDebug::Print(_L("%S-0x%08x - %d 0x%02x"), &buf, &aDesc, aDesc.ibLength, aDesc.ibDescriptorType);
sl@0
   489
		}
sl@0
   490
	HBufC* blob = HBufC::New(5*aDesc.iBlob.Length()); // 5* for " 0x" + 2*bytes for hex representation
sl@0
   491
	if(blob)
sl@0
   492
		{
sl@0
   493
		for(TInt i=0;i<aDesc.iBlob.Length();++i)
sl@0
   494
			{
sl@0
   495
			blob->Des().AppendFormat(_L("0x%02x "), aDesc.iBlob[i]);
sl@0
   496
			}
sl@0
   497
		RDebug::Print(_L("%S >%S"), &buf, blob);
sl@0
   498
		delete blob;
sl@0
   499
		}
sl@0
   500
	if(aDesc.iFirstChild)
sl@0
   501
		{
sl@0
   502
		RDebug::Print(_L("%S \\ "), &buf);
sl@0
   503
		PrintTree(*(aDesc.iFirstChild), aDepth+1);
sl@0
   504
		RDebug::Print(_L("%S / "), &buf);
sl@0
   505
		}
sl@0
   506
	if(aDesc.iNextPeer)
sl@0
   507
		{
sl@0
   508
		PrintTree(*(aDesc.iNextPeer), aDepth);
sl@0
   509
		}
sl@0
   510
	}
sl@0
   511
sl@0
   512
sl@0
   513
static TUint16 gLangId = 0x0000;
sl@0
   514
sl@0
   515
void TUsbPrint::SetLanguageToPrintL(RUsbDevice& aDevice)
sl@0
   516
	{
sl@0
   517
    if (!iDebug) return;
sl@0
   518
sl@0
   519
	// Try to set language to US Eng, otherwise take the first one listed.
sl@0
   520
	if(gLangId == 0x0000) // Only make the request if not been made before.
sl@0
   521
		{
sl@0
   522
		// Get string descriptor 0.
sl@0
   523
		TBuf8<256> stringBuf;
sl@0
   524
		TUsbStringDescriptor* stringDesc = NULL;
sl@0
   525
		User::LeaveIfError(aDevice.GetStringDescriptor(stringDesc, stringBuf, 0));
sl@0
   526
		CleanupStack::PushL(*stringDesc);
sl@0
   527
sl@0
   528
		// Search for US English
sl@0
   529
		TBool usEngLang = EFalse;
sl@0
   530
		TInt langId = 0;
sl@0
   531
		TInt index = 0;
sl@0
   532
		const TUint16 KLangIdUsEng = 0x0409;
sl@0
   533
		while(!usEngLang && langId != KErrNotFound)
sl@0
   534
			{
sl@0
   535
			langId = stringDesc->GetLangId(index);
sl@0
   536
			usEngLang = (langId == KLangIdUsEng);
sl@0
   537
			index++;
sl@0
   538
			}
sl@0
   539
sl@0
   540
		// Set the language appropriately
sl@0
   541
		if(usEngLang)
sl@0
   542
			{
sl@0
   543
        	gLangId = KLangIdUsEng;
sl@0
   544
			}
sl@0
   545
		else
sl@0
   546
			{
sl@0
   547
			gLangId = stringDesc->GetLangId(0);
sl@0
   548
			}
sl@0
   549
sl@0
   550
		CleanupStack::PopAndDestroy(); // stringDesc
sl@0
   551
		}
sl@0
   552
	}
sl@0
   553
sl@0
   554
sl@0
   555
void TUsbPrint::PrintStringFromIndex(const TDesC& aFormatString,
sl@0
   556
                                     TInt aIndex,
sl@0
   557
                                     RUsbDevice* aDevice)
sl@0
   558
	{
sl@0
   559
    if (!iDebug) return;
sl@0
   560
sl@0
   561
	// If we have no device handle, we cannot go and get any strings.
sl@0
   562
	// If we have index 0, this indicates we don't have a string for this entry.
sl@0
   563
	if(aDevice && aIndex != 0)
sl@0
   564
		{
sl@0
   565
		TRAPD(err, SetLanguageToPrintL(*aDevice));
sl@0
   566
		if(err == KErrNone)
sl@0
   567
			{
sl@0
   568
			TBuf8<255> stringBuf;
sl@0
   569
			TUsbStringDescriptor* stringDesc = NULL;
sl@0
   570
			err = aDevice->GetStringDescriptor(stringDesc, stringBuf, aIndex, gLangId);
sl@0
   571
			if(err == KErrNone)
sl@0
   572
				{
sl@0
   573
				TBuf<128> buf;
sl@0
   574
				stringDesc->StringData(buf);
sl@0
   575
				RDebug::Print(aFormatString, &buf);
sl@0
   576
				stringDesc->DestroyTree();
sl@0
   577
				}
sl@0
   578
			delete stringDesc;
sl@0
   579
			}
sl@0
   580
		else
sl@0
   581
			{
sl@0
   582
			RDebug::Print(_L("Error while Selecting Langauge %d\n"), err);
sl@0
   583
			}
sl@0
   584
		}
sl@0
   585
	}
sl@0
   586
sl@0
   587
sl@0
   588
void TUsbPrint::PrintDescriptor(const TUsbDeviceDescriptor& aDeviceDesc,
sl@0
   589
                                TInt /*aVariant*/,
sl@0
   590
                                RUsbDevice* aDevice)
sl@0
   591
	{
sl@0
   592
    if (!iDebug) return;
sl@0
   593
sl@0
   594
	RDebug::Print(_L("USBBcd = 0x%04x\n"), aDeviceDesc.USBBcd());
sl@0
   595
	RDebug::Print(_L("DeviceClass = 0x%02x\n"), aDeviceDesc.DeviceClass());
sl@0
   596
	RDebug::Print(_L("DeviceSubClass = 0x%02x\n"), aDeviceDesc.DeviceSubClass());
sl@0
   597
	RDebug::Print(_L("DeviceProtocol = 0x%02x\n"), aDeviceDesc.DeviceProtocol());
sl@0
   598
	RDebug::Print(_L("MaxPacketSize0 = 0x%02x\n"), aDeviceDesc.MaxPacketSize0());
sl@0
   599
	RDebug::Print(_L("VendorId = 0x%04x\n"), aDeviceDesc.VendorId());
sl@0
   600
	RDebug::Print(_L("ProductId = 0x%04x\n"), aDeviceDesc.ProductId());
sl@0
   601
	RDebug::Print(_L("DeviceBcd = 0x%04x\n"), aDeviceDesc.DeviceBcd());
sl@0
   602
	RDebug::Print(_L("ManufacturerIndex = 0x%02x\n"), aDeviceDesc.ManufacturerIndex());
sl@0
   603
	PrintStringFromIndex(_L("ManufacturerString = %S\n"), aDeviceDesc.ManufacturerIndex(), aDevice);
sl@0
   604
	RDebug::Print(_L("ProductIndex = 0x%02x\n"), aDeviceDesc.ProductIndex());
sl@0
   605
	PrintStringFromIndex(_L("ProductString = %S\n"), aDeviceDesc.ProductIndex(), aDevice);
sl@0
   606
	RDebug::Print(_L("SerialNumberIndex = 0x%02x\n"), aDeviceDesc.SerialNumberIndex());
sl@0
   607
	PrintStringFromIndex(_L("SerialNumberString = %S\n"), aDeviceDesc.SerialNumberIndex(), aDevice);
sl@0
   608
	RDebug::Print(_L("NumConfigurations = 0x%02x\n"), aDeviceDesc.NumConfigurations());
sl@0
   609
	}
sl@0
   610
sl@0
   611
sl@0
   612
void TUsbPrint::PrintDescriptor(const TUsbConfigurationDescriptor& aConfigDesc,
sl@0
   613
                                TInt /*aVariant*/,
sl@0
   614
                                RUsbDevice* aDevice)
sl@0
   615
	{
sl@0
   616
    if (!iDebug) return;
sl@0
   617
	RDebug::Print(_L("TotalLength = 0x%04x\n"), aConfigDesc.TotalLength());
sl@0
   618
	RDebug::Print(_L("NumInterfaces = 0x%02x\n"), aConfigDesc.NumInterfaces());
sl@0
   619
	RDebug::Print(_L("ConfigurationValue = 0x%02x\n"), aConfigDesc.ConfigurationValue());
sl@0
   620
	RDebug::Print(_L("ConfigurationIndex = 0x%02x\n"), aConfigDesc.ConfigurationIndex());
sl@0
   621
	PrintStringFromIndex(_L("ConfigurationString = %S\n"), aConfigDesc.ConfigurationIndex(), aDevice);
sl@0
   622
	RDebug::Print(_L("Attributes = 0x%02x\n"), aConfigDesc.Attributes());
sl@0
   623
	RDebug::Print(_L("MaxPower = 0x%02x\n"), aConfigDesc.MaxPower());
sl@0
   624
	}
sl@0
   625
sl@0
   626
sl@0
   627
void TUsbPrint::PrintDescriptor(const TUsbEndpointDescriptor& aEndpointDesc,
sl@0
   628
                                TInt /*aVariant*/,
sl@0
   629
                                RUsbDevice* /*aDevice*/)
sl@0
   630
	{
sl@0
   631
    if (!iDebug) return;
sl@0
   632
	RDebug::Print(_L("EndpointAddress = 0x%02x\n"), aEndpointDesc.EndpointAddress());
sl@0
   633
	RDebug::Print(_L("Attributes = 0x%02x\n"), aEndpointDesc.Attributes());
sl@0
   634
	RDebug::Print(_L("MaxPacketSize = 0x%04x\n"), aEndpointDesc.MaxPacketSize());
sl@0
   635
	RDebug::Print(_L("Interval = 0x%02x\n"), aEndpointDesc.Interval());
sl@0
   636
	}
sl@0
   637
sl@0
   638
sl@0
   639
void TUsbPrint::PrintDescriptor(const TUsbInterfaceDescriptor& aInterfaceDesc,
sl@0
   640
                                TInt /*aVariant*/,
sl@0
   641
                                RUsbDevice* /*aDevice*/)
sl@0
   642
	{
sl@0
   643
    if (!iDebug) return;
sl@0
   644
	RDebug::Print(_L("InterfaceNumber = 0x%02x\n"), aInterfaceDesc.InterfaceNumber());
sl@0
   645
	RDebug::Print(_L("AlternateSetting = 0x%02x\n"), aInterfaceDesc.AlternateSetting());
sl@0
   646
	RDebug::Print(_L("NumEndpoints = 0x%02x\n"), aInterfaceDesc.NumEndpoints());
sl@0
   647
	RDebug::Print(_L("InterfaceClass = 0x%02x\n"), aInterfaceDesc.InterfaceClass());
sl@0
   648
	RDebug::Print(_L("InterfaceSubClass = 0x%02x\n"), aInterfaceDesc.InterfaceSubClass());
sl@0
   649
	RDebug::Print(_L("InterfaceProtocol = 0x%02x\n"), aInterfaceDesc.InterfaceProtocol());
sl@0
   650
	RDebug::Print(_L("Interface = 0x%02x\n"), aInterfaceDesc.Interface());
sl@0
   651
	}
sl@0
   652
sl@0
   653
sl@0
   654
void TUsbPrint::PrintDescriptor(const TUsbStringDescriptor& aStringDesc,
sl@0
   655
                                TInt aVariant,
sl@0
   656
                                RUsbDevice* /*aDevice*/)
sl@0
   657
	{
sl@0
   658
    if (!iDebug) return;
sl@0
   659
	if(aVariant == 0)
sl@0
   660
		{
sl@0
   661
		RDebug::Print(_L("String Descriptor Zero\n"));
sl@0
   662
		TInt index = 0;
sl@0
   663
		TInt langId = 0;
sl@0
   664
		while((langId = aStringDesc.GetLangId(index)) != KErrNotFound)
sl@0
   665
			{
sl@0
   666
			RDebug::Print(_L("  >0x%04x\n"), langId);
sl@0
   667
			++index;
sl@0
   668
			}
sl@0
   669
		}
sl@0
   670
	else
sl@0
   671
		{
sl@0
   672
		RDebug::Print(_L("Generic String Descriptor\n"));
sl@0
   673
		HBufC16* string = HBufC16::New(128);
sl@0
   674
		if(string)
sl@0
   675
			{
sl@0
   676
			TPtr16 stringPtr = string->Des();
sl@0
   677
			aStringDesc.StringData(stringPtr);
sl@0
   678
			RDebug::Print(_L("  >%S\n"), string);
sl@0
   679
			}
sl@0
   680
		delete string;
sl@0
   681
		}
sl@0
   682
	}