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