kerneltest/f32test/shostmassstorage/msman/src/cusbhost.cpp
changeset 0 a41df078684a
child 22 2f92ad2dc5db
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of the License "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include <d32usbdi_hubdriver.h>
       
    17 #include <d32usbdi.h>
       
    18 #include <d32otgdi.h>
       
    19 #include <d32usbdescriptors.h>
       
    20 #include <d32usbtransfers.h>
       
    21 #include <e32property.h>
       
    22 #include <f32file.h>
       
    23 
       
    24 #include "usbtypes.h"
       
    25 #include "rusbhostmsdevice.h"
       
    26 #include "rusbhostmslogicalunit.h"
       
    27 #include "rextfilesystem.h"
       
    28 #include "cusbmsmountmanager.h"
       
    29 
       
    30 #include "mdrivedisplay.h"
       
    31 #include "cusbhostao.h"
       
    32 #include "cusbhost.h"
       
    33 #include "tmslog.h"
       
    34 #include "debug.h"
       
    35 
       
    36 
       
    37 
       
    38 _LIT(KHubDriverLddFileName, "usbhubdriver");
       
    39 _LIT(KUsbdiLddFileName, "usbdi");
       
    40 
       
    41 
       
    42 CUsbHost* CUsbHost::NewL()
       
    43     {
       
    44     __MSFNSLOG
       
    45 	CUsbHost* r = new (ELeave) CUsbHost();
       
    46 	CleanupStack::PushL(r);
       
    47 
       
    48 	r->ConstructL();
       
    49 	CleanupStack::Pop();
       
    50 	return r;
       
    51     }
       
    52 
       
    53 
       
    54 void CUsbHost::ConstructL()
       
    55     {
       
    56     __MSFNLOG
       
    57     OpenHubL();
       
    58 	LoadFileSystemL();
       
    59 
       
    60     iUsbHostAo = CUsbHostAo::NewL(iHubDriver, iEvent, *this);
       
    61 
       
    62     iMountManager = CUsbMsMountManager::NewL();
       
    63     }
       
    64 
       
    65 
       
    66 CUsbHost::CUsbHost()
       
    67     {
       
    68     __MSFNLOG
       
    69     }
       
    70 
       
    71 
       
    72 CUsbHost::~CUsbHost()
       
    73     {
       
    74     __MSFNLOG
       
    75     delete iUsbHostAo;
       
    76 
       
    77     DismountAllFileSystemsL();
       
    78     CloseAllDevicesL();
       
    79     CloseHubL();
       
    80 
       
    81     delete iMountManager;
       
    82     }
       
    83 
       
    84 
       
    85 void CUsbHost::LoadFileSystemL()
       
    86     {
       
    87     __MSFNLOG
       
    88     RFs fs;
       
    89     User::LeaveIfError(fs.Connect());
       
    90     CleanupClosePushL(fs);
       
    91 	_LIT(KFsNm, "elocal");
       
    92 
       
    93     TInt err;
       
    94     err = fs.AddFileSystem(KFsNm);
       
    95     if (err != KErrAlreadyExists)
       
    96         User::LeaveIfError(err);
       
    97 
       
    98     err = fs.AddFileSystem(_L("ELOCAL"));
       
    99     if (!(KErrAlreadyExists == err || KErrCorrupt == err))
       
   100         User::LeaveIfError(err);
       
   101 
       
   102     err = fs.AddProxyDrive(_L("usbhostms.pxy"));
       
   103     if (!(KErrAlreadyExists == err || KErrCorrupt == err))
       
   104         User::LeaveIfError(err);
       
   105 
       
   106     CleanupStack::PopAndDestroy(&fs);
       
   107     }
       
   108 
       
   109 
       
   110 void CUsbHost::OpenHubL()
       
   111     {
       
   112     __MSFNLOG
       
   113     TInt err;
       
   114     err = User::LoadLogicalDevice(KHubDriverLddFileName);
       
   115     if (err != KErrAlreadyExists)
       
   116         User::LeaveIfError(err);
       
   117 
       
   118     err = User::LoadLogicalDevice(KUsbdiLddFileName);
       
   119     if (err != KErrAlreadyExists)
       
   120         User::LeaveIfError(err);
       
   121 
       
   122     err = iHubDriver.Open();
       
   123     User::LeaveIfError(err);
       
   124     }
       
   125 
       
   126 
       
   127 void CUsbHost::CloseHubL()
       
   128     {
       
   129     __MSFNLOG
       
   130 	iHubDriver.StopHost();
       
   131 	iHubDriver.Close();
       
   132 
       
   133 	TInt err1 = User::FreeLogicalDevice(KUsbdiLddFileName);
       
   134 	__ASSERT_DEBUG(err1==KErrNone, User::Panic(KUsbdiLddFileName, err1));
       
   135 
       
   136 	TInt err2 = User::FreeLogicalDevice(KHubDriverLddFileName);
       
   137 	__ASSERT_DEBUG(err2==KErrNone, User::Panic(KHubDriverLddFileName, err2));
       
   138 
       
   139 	User::LeaveIfError(err1);
       
   140 	User::LeaveIfError(err2);
       
   141     }
       
   142 
       
   143 
       
   144 TToken CUsbHost::OpenDeviceL()
       
   145     {
       
   146     __MSFNLOG
       
   147     CDevice* device = CDevice::NewL();
       
   148 
       
   149     TToken token = 0;
       
   150     TRAPD(err, token = device->OpenDeviceL(iDeviceHandle, iHubDriver));
       
   151     if (err)
       
   152         {
       
   153         User::Leave(err);
       
   154         }
       
   155 
       
   156     iMountManager->AddDeviceL(device);
       
   157     return token;
       
   158     }
       
   159 
       
   160 
       
   161 void CUsbHost::CloseDeviceL()
       
   162     {
       
   163     __MSFNLOG
       
   164     CDevice* device = iMountManager->RemoveDeviceL(iDeviceHandle);
       
   165     device->CloseDeviceL();
       
   166     delete device;
       
   167     }
       
   168 
       
   169 
       
   170 void CUsbHost::CloseAllDevicesL()
       
   171     {
       
   172     __MSFNLOG
       
   173     iMountManager->CloseAllDevicesL();
       
   174     }
       
   175 
       
   176 
       
   177 void CUsbHost::MountDeviceL()
       
   178     {
       
   179     __MSFNLOG
       
   180     iMountManager->MountDeviceL(iDeviceHandle);
       
   181     }
       
   182 
       
   183 
       
   184 void CUsbHost::DismountDeviceL()
       
   185     {
       
   186     __MSFNLOG
       
   187     iMountManager->DismountDeviceL(iDeviceHandle);
       
   188     }
       
   189 
       
   190 
       
   191 void CUsbHost::DismountAllFileSystemsL()
       
   192     {
       
   193     __MSFNLOG
       
   194     iMountManager->DismountL();
       
   195     }
       
   196 
       
   197 
       
   198 void CUsbHost::Start()
       
   199     {
       
   200     __MSFNLOG
       
   201     iUsbHostAo->Wait();
       
   202     }
       
   203 
       
   204 
       
   205 void CUsbHost::ProcessBusEventL()
       
   206     {
       
   207     __MSFNLOG
       
   208 
       
   209     __USBHOSTPRINT2(_L(">> CUsbHost RUsbHubDriver Event[%d] Device Handle = %d"),
       
   210                     iEvent.iEventType, iEvent.iDeviceHandle);
       
   211     __USBHOSTPRINT2(_L("Error = %d reason = %x"),
       
   212                     iEvent.iError, iEvent.iReason);
       
   213 
       
   214     iDeviceHandle = iEvent.iDeviceHandle;
       
   215     RUsbHubDriver::TBusEvent::TEvent event = iEvent.iEventType;
       
   216 
       
   217     if (event == RUsbHubDriver::TBusEvent::EDeviceAttached)
       
   218         {
       
   219         /* Jungo stack has attached the device */
       
   220         TUint32 token = OpenDeviceL();
       
   221         MountDeviceL();
       
   222 	    __USBHOSTPRINT(_L("CUsbHost: device attached"));
       
   223         }
       
   224     else if (event == RUsbHubDriver::TBusEvent::EDeviceRemoved)
       
   225         {
       
   226 		TRAPD(err, DismountDeviceL());
       
   227 	    CloseDeviceL();
       
   228         User::LeaveIfError(err);
       
   229         __USBHOSTPRINT(_L("CUsbHost: device removed"));
       
   230         }
       
   231 
       
   232     else
       
   233         {
       
   234         // nothing to do
       
   235         }
       
   236     }
       
   237 
       
   238 
       
   239 RUsbHubDriver::TBusEvent::TEvent CUsbHost::WaitForBusEvent()
       
   240     {
       
   241     __MSFNLOG
       
   242     TRequestStatus status;
       
   243     RUsbHubDriver::TBusEvent event;
       
   244     TBool eventReceived = EFalse;
       
   245     do
       
   246         {
       
   247         iHubDriver.WaitForBusEvent(event, status);
       
   248         __USBHOSTPRINT(_L("Waiting..."));
       
   249         User::WaitForRequest(status);
       
   250         __USBHOSTPRINT2(_L(">> CUsbHost RUsbHubDriver Event[%d] Device Handle = %d)"),
       
   251                         iEvent.iEventType, iEvent.iDeviceHandle);
       
   252         __USBHOSTPRINT2(_L("Error = %d reason = %x"),
       
   253                         iEvent.iError, iEvent.iReason);
       
   254 
       
   255         if (status != KErrNone)
       
   256             {
       
   257             __USBHOSTPRINT1(_L("Status error = %d"), status.Int());
       
   258             }
       
   259         iDeviceHandle = event.iDeviceHandle;
       
   260 
       
   261         switch (event.iEventType)
       
   262             {
       
   263             case RUsbHubDriver::TBusEvent::EDeviceAttached:
       
   264             case RUsbHubDriver::TBusEvent::EDeviceRemoved:
       
   265                 eventReceived = ETrue;
       
   266                 break;
       
   267             default:
       
   268                 break;
       
   269             }
       
   270 
       
   271         } while (!eventReceived);
       
   272     return event.iEventType;
       
   273     }
       
   274 
       
   275 
       
   276 
       
   277 void CUsbHost::Cancel()
       
   278     {
       
   279     iHubDriver.CancelWaitForBusEvent();
       
   280     }
       
   281 
       
   282 
       
   283 void CUsbHost::DriveMap(TDriveMap& aDriveMap) const
       
   284     {
       
   285     __MSFNSLOG
       
   286     iMountManager->DriveMap(aDriveMap);
       
   287     }
       
   288 
       
   289 
       
   290 void CUsbHost::DeviceMap(TInt aDeviceIndex, TDeviceMap& aDeviceMap) const
       
   291     {
       
   292     __MSFNSLOG
       
   293     iMountManager->DeviceMap(aDeviceIndex, aDeviceMap);
       
   294     }
       
   295 
       
   296 
       
   297 TInt CUsbHost::DevicesNumber() const
       
   298     {
       
   299     return iMountManager->DevicesNumber();
       
   300     }
       
   301 
       
   302 
       
   303 CUsbHostDisp* CUsbHostDisp::NewL(MDriveDisplay& aDriveDisplay)
       
   304     {
       
   305     __MSFNSLOG
       
   306 	CUsbHostDisp* r = new (ELeave) CUsbHostDisp(aDriveDisplay);
       
   307 	CleanupStack::PushL(r);
       
   308 	r->ConstructL();
       
   309 	CleanupStack::Pop();
       
   310 	return r;
       
   311     }
       
   312 
       
   313 
       
   314 void CUsbHostDisp::ConstructL()
       
   315     {
       
   316     __MSFNLOG
       
   317     CUsbHost::ConstructL();
       
   318     }
       
   319 
       
   320 
       
   321 CUsbHostDisp::CUsbHostDisp(MDriveDisplay& aDriveDisplay)
       
   322 :   CUsbHost(),
       
   323     iDriveDisplay(aDriveDisplay)
       
   324     {
       
   325     __MSFNLOG
       
   326     }
       
   327 
       
   328 
       
   329 CUsbHostDisp::~CUsbHostDisp()
       
   330     {
       
   331     __MSFNLOG
       
   332     }
       
   333 
       
   334 void CUsbHostDisp::ProcessBusEventL()
       
   335     {
       
   336     CUsbHost::ProcessBusEventL();
       
   337 
       
   338     // update display
       
   339     iDriveDisplay.DriveListL();
       
   340 
       
   341     // Devices attached
       
   342     TInt devicesNumber = DevicesNumber();
       
   343     iDriveDisplay.DevicesNumber(devicesNumber);
       
   344 
       
   345     // LUNs for each device
       
   346     TDeviceMap deviceMap;
       
   347     TInt deviceIndex;
       
   348     TInt row;
       
   349     for (row = 0, deviceIndex = (devicesNumber - 1); deviceIndex >= 0 ; row++, deviceIndex--)
       
   350         {
       
   351         deviceMap.Reset();
       
   352         // get map
       
   353         DeviceMap(deviceIndex, deviceMap);
       
   354 
       
   355         // display
       
   356         iDriveDisplay.DeviceMapL(row, deviceIndex, deviceMap);
       
   357         }
       
   358 
       
   359     iDriveDisplay.DeviceMapClear(row);
       
   360 
       
   361     // Display all Drives
       
   362     TDriveMap driveMap;
       
   363     driveMap.Reset();
       
   364     DriveMap(driveMap);
       
   365     iDriveDisplay.DriveMapL(driveMap);
       
   366     }
       
   367 
       
   368