1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kerneltest/f32test/shostmassstorage/msman/src/cusbhost.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,368 @@
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 <d32usbdi_hubdriver.h>
1.20 +#include <d32usbdi.h>
1.21 +#include <d32otgdi.h>
1.22 +#include <d32usbdescriptors.h>
1.23 +#include <d32usbtransfers.h>
1.24 +#include <e32property.h>
1.25 +#include <f32file.h>
1.26 +
1.27 +#include "usbtypes.h"
1.28 +#include "rusbhostmsdevice.h"
1.29 +#include "rusbhostmslogicalunit.h"
1.30 +#include "rextfilesystem.h"
1.31 +#include "cusbmsmountmanager.h"
1.32 +
1.33 +#include "mdrivedisplay.h"
1.34 +#include "cusbhostao.h"
1.35 +#include "cusbhost.h"
1.36 +#include "tmslog.h"
1.37 +#include "debug.h"
1.38 +
1.39 +
1.40 +
1.41 +_LIT(KHubDriverLddFileName, "usbhubdriver");
1.42 +_LIT(KUsbdiLddFileName, "usbdi");
1.43 +
1.44 +
1.45 +CUsbHost* CUsbHost::NewL()
1.46 + {
1.47 + __MSFNSLOG
1.48 + CUsbHost* r = new (ELeave) CUsbHost();
1.49 + CleanupStack::PushL(r);
1.50 +
1.51 + r->ConstructL();
1.52 + CleanupStack::Pop();
1.53 + return r;
1.54 + }
1.55 +
1.56 +
1.57 +void CUsbHost::ConstructL()
1.58 + {
1.59 + __MSFNLOG
1.60 + OpenHubL();
1.61 + LoadFileSystemL();
1.62 +
1.63 + iUsbHostAo = CUsbHostAo::NewL(iHubDriver, iEvent, *this);
1.64 +
1.65 + iMountManager = CUsbMsMountManager::NewL();
1.66 + }
1.67 +
1.68 +
1.69 +CUsbHost::CUsbHost()
1.70 + {
1.71 + __MSFNLOG
1.72 + }
1.73 +
1.74 +
1.75 +CUsbHost::~CUsbHost()
1.76 + {
1.77 + __MSFNLOG
1.78 + delete iUsbHostAo;
1.79 +
1.80 + DismountAllFileSystemsL();
1.81 + CloseAllDevicesL();
1.82 + CloseHubL();
1.83 +
1.84 + delete iMountManager;
1.85 + }
1.86 +
1.87 +
1.88 +void CUsbHost::LoadFileSystemL()
1.89 + {
1.90 + __MSFNLOG
1.91 + RFs fs;
1.92 + User::LeaveIfError(fs.Connect());
1.93 + CleanupClosePushL(fs);
1.94 + _LIT(KFsNm, "elocal");
1.95 +
1.96 + TInt err;
1.97 + err = fs.AddFileSystem(KFsNm);
1.98 + if (err != KErrAlreadyExists)
1.99 + User::LeaveIfError(err);
1.100 +
1.101 + err = fs.AddFileSystem(_L("ELOCAL"));
1.102 + if (!(KErrAlreadyExists == err || KErrCorrupt == err))
1.103 + User::LeaveIfError(err);
1.104 +
1.105 + err = fs.AddProxyDrive(_L("usbhostms.pxy"));
1.106 + if (!(KErrAlreadyExists == err || KErrCorrupt == err))
1.107 + User::LeaveIfError(err);
1.108 +
1.109 + CleanupStack::PopAndDestroy(&fs);
1.110 + }
1.111 +
1.112 +
1.113 +void CUsbHost::OpenHubL()
1.114 + {
1.115 + __MSFNLOG
1.116 + TInt err;
1.117 + err = User::LoadLogicalDevice(KHubDriverLddFileName);
1.118 + if (err != KErrAlreadyExists)
1.119 + User::LeaveIfError(err);
1.120 +
1.121 + err = User::LoadLogicalDevice(KUsbdiLddFileName);
1.122 + if (err != KErrAlreadyExists)
1.123 + User::LeaveIfError(err);
1.124 +
1.125 + err = iHubDriver.Open();
1.126 + User::LeaveIfError(err);
1.127 + }
1.128 +
1.129 +
1.130 +void CUsbHost::CloseHubL()
1.131 + {
1.132 + __MSFNLOG
1.133 + iHubDriver.StopHost();
1.134 + iHubDriver.Close();
1.135 +
1.136 + TInt err1 = User::FreeLogicalDevice(KUsbdiLddFileName);
1.137 + __ASSERT_DEBUG(err1==KErrNone, User::Panic(KUsbdiLddFileName, err1));
1.138 +
1.139 + TInt err2 = User::FreeLogicalDevice(KHubDriverLddFileName);
1.140 + __ASSERT_DEBUG(err2==KErrNone, User::Panic(KHubDriverLddFileName, err2));
1.141 +
1.142 + User::LeaveIfError(err1);
1.143 + User::LeaveIfError(err2);
1.144 + }
1.145 +
1.146 +
1.147 +TToken CUsbHost::OpenDeviceL()
1.148 + {
1.149 + __MSFNLOG
1.150 + CDevice* device = CDevice::NewL();
1.151 +
1.152 + TToken token = 0;
1.153 + TRAPD(err, token = device->OpenDeviceL(iDeviceHandle, iHubDriver));
1.154 + if (err)
1.155 + {
1.156 + User::Leave(err);
1.157 + }
1.158 +
1.159 + iMountManager->AddDeviceL(device);
1.160 + return token;
1.161 + }
1.162 +
1.163 +
1.164 +void CUsbHost::CloseDeviceL()
1.165 + {
1.166 + __MSFNLOG
1.167 + CDevice* device = iMountManager->RemoveDeviceL(iDeviceHandle);
1.168 + device->CloseDeviceL();
1.169 + delete device;
1.170 + }
1.171 +
1.172 +
1.173 +void CUsbHost::CloseAllDevicesL()
1.174 + {
1.175 + __MSFNLOG
1.176 + iMountManager->CloseAllDevicesL();
1.177 + }
1.178 +
1.179 +
1.180 +void CUsbHost::MountDeviceL()
1.181 + {
1.182 + __MSFNLOG
1.183 + iMountManager->MountDeviceL(iDeviceHandle);
1.184 + }
1.185 +
1.186 +
1.187 +void CUsbHost::DismountDeviceL()
1.188 + {
1.189 + __MSFNLOG
1.190 + iMountManager->DismountDeviceL(iDeviceHandle);
1.191 + }
1.192 +
1.193 +
1.194 +void CUsbHost::DismountAllFileSystemsL()
1.195 + {
1.196 + __MSFNLOG
1.197 + iMountManager->DismountL();
1.198 + }
1.199 +
1.200 +
1.201 +void CUsbHost::Start()
1.202 + {
1.203 + __MSFNLOG
1.204 + iUsbHostAo->Wait();
1.205 + }
1.206 +
1.207 +
1.208 +void CUsbHost::ProcessBusEventL()
1.209 + {
1.210 + __MSFNLOG
1.211 +
1.212 + __USBHOSTPRINT2(_L(">> CUsbHost RUsbHubDriver Event[%d] Device Handle = %d"),
1.213 + iEvent.iEventType, iEvent.iDeviceHandle);
1.214 + __USBHOSTPRINT2(_L("Error = %d reason = %x"),
1.215 + iEvent.iError, iEvent.iReason);
1.216 +
1.217 + iDeviceHandle = iEvent.iDeviceHandle;
1.218 + RUsbHubDriver::TBusEvent::TEvent event = iEvent.iEventType;
1.219 +
1.220 + if (event == RUsbHubDriver::TBusEvent::EDeviceAttached)
1.221 + {
1.222 + /* Jungo stack has attached the device */
1.223 + TUint32 token = OpenDeviceL();
1.224 + MountDeviceL();
1.225 + __USBHOSTPRINT(_L("CUsbHost: device attached"));
1.226 + }
1.227 + else if (event == RUsbHubDriver::TBusEvent::EDeviceRemoved)
1.228 + {
1.229 + TRAPD(err, DismountDeviceL());
1.230 + CloseDeviceL();
1.231 + User::LeaveIfError(err);
1.232 + __USBHOSTPRINT(_L("CUsbHost: device removed"));
1.233 + }
1.234 +
1.235 + else
1.236 + {
1.237 + // nothing to do
1.238 + }
1.239 + }
1.240 +
1.241 +
1.242 +RUsbHubDriver::TBusEvent::TEvent CUsbHost::WaitForBusEvent()
1.243 + {
1.244 + __MSFNLOG
1.245 + TRequestStatus status;
1.246 + RUsbHubDriver::TBusEvent event;
1.247 + TBool eventReceived = EFalse;
1.248 + do
1.249 + {
1.250 + iHubDriver.WaitForBusEvent(event, status);
1.251 + __USBHOSTPRINT(_L("Waiting..."));
1.252 + User::WaitForRequest(status);
1.253 + __USBHOSTPRINT2(_L(">> CUsbHost RUsbHubDriver Event[%d] Device Handle = %d)"),
1.254 + iEvent.iEventType, iEvent.iDeviceHandle);
1.255 + __USBHOSTPRINT2(_L("Error = %d reason = %x"),
1.256 + iEvent.iError, iEvent.iReason);
1.257 +
1.258 + if (status != KErrNone)
1.259 + {
1.260 + __USBHOSTPRINT1(_L("Status error = %d"), status.Int());
1.261 + }
1.262 + iDeviceHandle = event.iDeviceHandle;
1.263 +
1.264 + switch (event.iEventType)
1.265 + {
1.266 + case RUsbHubDriver::TBusEvent::EDeviceAttached:
1.267 + case RUsbHubDriver::TBusEvent::EDeviceRemoved:
1.268 + eventReceived = ETrue;
1.269 + break;
1.270 + default:
1.271 + break;
1.272 + }
1.273 +
1.274 + } while (!eventReceived);
1.275 + return event.iEventType;
1.276 + }
1.277 +
1.278 +
1.279 +
1.280 +void CUsbHost::Cancel()
1.281 + {
1.282 + iHubDriver.CancelWaitForBusEvent();
1.283 + }
1.284 +
1.285 +
1.286 +void CUsbHost::DriveMap(TDriveMap& aDriveMap) const
1.287 + {
1.288 + __MSFNSLOG
1.289 + iMountManager->DriveMap(aDriveMap);
1.290 + }
1.291 +
1.292 +
1.293 +void CUsbHost::DeviceMap(TInt aDeviceIndex, TDeviceMap& aDeviceMap) const
1.294 + {
1.295 + __MSFNSLOG
1.296 + iMountManager->DeviceMap(aDeviceIndex, aDeviceMap);
1.297 + }
1.298 +
1.299 +
1.300 +TInt CUsbHost::DevicesNumber() const
1.301 + {
1.302 + return iMountManager->DevicesNumber();
1.303 + }
1.304 +
1.305 +
1.306 +CUsbHostDisp* CUsbHostDisp::NewL(MDriveDisplay& aDriveDisplay)
1.307 + {
1.308 + __MSFNSLOG
1.309 + CUsbHostDisp* r = new (ELeave) CUsbHostDisp(aDriveDisplay);
1.310 + CleanupStack::PushL(r);
1.311 + r->ConstructL();
1.312 + CleanupStack::Pop();
1.313 + return r;
1.314 + }
1.315 +
1.316 +
1.317 +void CUsbHostDisp::ConstructL()
1.318 + {
1.319 + __MSFNLOG
1.320 + CUsbHost::ConstructL();
1.321 + }
1.322 +
1.323 +
1.324 +CUsbHostDisp::CUsbHostDisp(MDriveDisplay& aDriveDisplay)
1.325 +: CUsbHost(),
1.326 + iDriveDisplay(aDriveDisplay)
1.327 + {
1.328 + __MSFNLOG
1.329 + }
1.330 +
1.331 +
1.332 +CUsbHostDisp::~CUsbHostDisp()
1.333 + {
1.334 + __MSFNLOG
1.335 + }
1.336 +
1.337 +void CUsbHostDisp::ProcessBusEventL()
1.338 + {
1.339 + CUsbHost::ProcessBusEventL();
1.340 +
1.341 + // update display
1.342 + iDriveDisplay.DriveListL();
1.343 +
1.344 + // Devices attached
1.345 + TInt devicesNumber = DevicesNumber();
1.346 + iDriveDisplay.DevicesNumber(devicesNumber);
1.347 +
1.348 + // LUNs for each device
1.349 + TDeviceMap deviceMap;
1.350 + TInt deviceIndex;
1.351 + TInt row;
1.352 + for (row = 0, deviceIndex = (devicesNumber - 1); deviceIndex >= 0 ; row++, deviceIndex--)
1.353 + {
1.354 + deviceMap.Reset();
1.355 + // get map
1.356 + DeviceMap(deviceIndex, deviceMap);
1.357 +
1.358 + // display
1.359 + iDriveDisplay.DeviceMapL(row, deviceIndex, deviceMap);
1.360 + }
1.361 +
1.362 + iDriveDisplay.DeviceMapClear(row);
1.363 +
1.364 + // Display all Drives
1.365 + TDriveMap driveMap;
1.366 + driveMap.Reset();
1.367 + DriveMap(driveMap);
1.368 + iDriveDisplay.DriveMapL(driveMap);
1.369 + }
1.370 +
1.371 +