sl@0: // Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of the License "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #include "usbtypes.h" sl@0: #include "rusbhostmsdevice.h" sl@0: #include "rusbhostmslogicalunit.h" sl@0: #include "rextfilesystem.h" sl@0: #include "cusbmsmountmanager.h" sl@0: sl@0: #include "mdrivedisplay.h" sl@0: #include "cusbhostao.h" sl@0: #include "cusbhost.h" sl@0: #include "tmslog.h" sl@0: #include "debug.h" sl@0: sl@0: sl@0: sl@0: _LIT(KHubDriverLddFileName, "usbhubdriver"); sl@0: _LIT(KUsbdiLddFileName, "usbdi"); sl@0: sl@0: sl@0: CUsbHost* CUsbHost::NewL() sl@0: { sl@0: __MSFNSLOG sl@0: CUsbHost* r = new (ELeave) CUsbHost(); sl@0: CleanupStack::PushL(r); sl@0: sl@0: r->ConstructL(); sl@0: CleanupStack::Pop(); sl@0: return r; sl@0: } sl@0: sl@0: sl@0: void CUsbHost::ConstructL() sl@0: { sl@0: __MSFNLOG sl@0: OpenHubL(); sl@0: LoadFileSystemL(); sl@0: sl@0: iUsbHostAo = CUsbHostAo::NewL(iHubDriver, iEvent, *this); sl@0: sl@0: iMountManager = CUsbMsMountManager::NewL(); sl@0: } sl@0: sl@0: sl@0: CUsbHost::CUsbHost() sl@0: { sl@0: __MSFNLOG sl@0: } sl@0: sl@0: sl@0: CUsbHost::~CUsbHost() sl@0: { sl@0: __MSFNLOG sl@0: delete iUsbHostAo; sl@0: sl@0: DismountAllFileSystemsL(); sl@0: CloseAllDevicesL(); sl@0: CloseHubL(); sl@0: sl@0: delete iMountManager; sl@0: } sl@0: sl@0: sl@0: void CUsbHost::LoadFileSystemL() sl@0: { sl@0: __MSFNLOG sl@0: RFs fs; sl@0: User::LeaveIfError(fs.Connect()); sl@0: CleanupClosePushL(fs); sl@0: _LIT(KFsNm, "elocal"); sl@0: sl@0: TInt err; sl@0: err = fs.AddFileSystem(KFsNm); sl@0: if (err != KErrAlreadyExists) sl@0: User::LeaveIfError(err); sl@0: sl@0: err = fs.AddFileSystem(_L("ELOCAL")); sl@0: if (!(KErrAlreadyExists == err || KErrCorrupt == err)) sl@0: User::LeaveIfError(err); sl@0: sl@0: err = fs.AddProxyDrive(_L("usbhostms.pxy")); sl@0: if (!(KErrAlreadyExists == err || KErrCorrupt == err)) sl@0: User::LeaveIfError(err); sl@0: sl@0: CleanupStack::PopAndDestroy(&fs); sl@0: } sl@0: sl@0: sl@0: void CUsbHost::OpenHubL() sl@0: { sl@0: __MSFNLOG sl@0: TInt err; sl@0: err = User::LoadLogicalDevice(KHubDriverLddFileName); sl@0: if (err != KErrAlreadyExists) sl@0: User::LeaveIfError(err); sl@0: sl@0: err = User::LoadLogicalDevice(KUsbdiLddFileName); sl@0: if (err != KErrAlreadyExists) sl@0: User::LeaveIfError(err); sl@0: sl@0: err = iHubDriver.Open(); sl@0: User::LeaveIfError(err); sl@0: } sl@0: sl@0: sl@0: void CUsbHost::CloseHubL() sl@0: { sl@0: __MSFNLOG sl@0: iHubDriver.StopHost(); sl@0: iHubDriver.Close(); sl@0: sl@0: TInt err1 = User::FreeLogicalDevice(KUsbdiLddFileName); sl@0: __ASSERT_DEBUG(err1==KErrNone, User::Panic(KUsbdiLddFileName, err1)); sl@0: sl@0: TInt err2 = User::FreeLogicalDevice(KHubDriverLddFileName); sl@0: __ASSERT_DEBUG(err2==KErrNone, User::Panic(KHubDriverLddFileName, err2)); sl@0: sl@0: User::LeaveIfError(err1); sl@0: User::LeaveIfError(err2); sl@0: } sl@0: sl@0: sl@0: TToken CUsbHost::OpenDeviceL() sl@0: { sl@0: __MSFNLOG sl@0: CDevice* device = CDevice::NewL(); sl@0: sl@0: TToken token = 0; sl@0: TRAPD(err, token = device->OpenDeviceL(iDeviceHandle, iHubDriver)); sl@0: if (err) sl@0: { sl@0: User::Leave(err); sl@0: } sl@0: sl@0: iMountManager->AddDeviceL(device); sl@0: return token; sl@0: } sl@0: sl@0: sl@0: void CUsbHost::CloseDeviceL() sl@0: { sl@0: __MSFNLOG sl@0: CDevice* device = iMountManager->RemoveDeviceL(iDeviceHandle); sl@0: device->CloseDeviceL(); sl@0: delete device; sl@0: } sl@0: sl@0: sl@0: void CUsbHost::CloseAllDevicesL() sl@0: { sl@0: __MSFNLOG sl@0: iMountManager->CloseAllDevicesL(); sl@0: } sl@0: sl@0: sl@0: void CUsbHost::MountDeviceL() sl@0: { sl@0: __MSFNLOG sl@0: iMountManager->MountDeviceL(iDeviceHandle); sl@0: } sl@0: sl@0: sl@0: void CUsbHost::DismountDeviceL() sl@0: { sl@0: __MSFNLOG sl@0: iMountManager->DismountDeviceL(iDeviceHandle); sl@0: } sl@0: sl@0: sl@0: void CUsbHost::DismountAllFileSystemsL() sl@0: { sl@0: __MSFNLOG sl@0: iMountManager->DismountL(); sl@0: } sl@0: sl@0: sl@0: void CUsbHost::Start() sl@0: { sl@0: __MSFNLOG sl@0: iUsbHostAo->Wait(); sl@0: } sl@0: sl@0: sl@0: void CUsbHost::ProcessBusEventL() sl@0: { sl@0: __MSFNLOG sl@0: sl@0: __USBHOSTPRINT2(_L(">> CUsbHost RUsbHubDriver Event[%d] Device Handle = %d"), sl@0: iEvent.iEventType, iEvent.iDeviceHandle); sl@0: __USBHOSTPRINT2(_L("Error = %d reason = %x"), sl@0: iEvent.iError, iEvent.iReason); sl@0: sl@0: iDeviceHandle = iEvent.iDeviceHandle; sl@0: RUsbHubDriver::TBusEvent::TEvent event = iEvent.iEventType; sl@0: sl@0: if (event == RUsbHubDriver::TBusEvent::EDeviceAttached) sl@0: { sl@0: /* Jungo stack has attached the device */ sl@0: TUint32 token = OpenDeviceL(); sl@0: MountDeviceL(); sl@0: __USBHOSTPRINT(_L("CUsbHost: device attached")); sl@0: } sl@0: else if (event == RUsbHubDriver::TBusEvent::EDeviceRemoved) sl@0: { sl@0: TRAPD(err, DismountDeviceL()); sl@0: CloseDeviceL(); sl@0: User::LeaveIfError(err); sl@0: __USBHOSTPRINT(_L("CUsbHost: device removed")); sl@0: } sl@0: sl@0: else sl@0: { sl@0: // nothing to do sl@0: } sl@0: } sl@0: sl@0: sl@0: RUsbHubDriver::TBusEvent::TEvent CUsbHost::WaitForBusEvent() sl@0: { sl@0: __MSFNLOG sl@0: TRequestStatus status; sl@0: RUsbHubDriver::TBusEvent event; sl@0: TBool eventReceived = EFalse; sl@0: do sl@0: { sl@0: iHubDriver.WaitForBusEvent(event, status); sl@0: __USBHOSTPRINT(_L("Waiting...")); sl@0: User::WaitForRequest(status); sl@0: __USBHOSTPRINT2(_L(">> CUsbHost RUsbHubDriver Event[%d] Device Handle = %d)"), sl@0: iEvent.iEventType, iEvent.iDeviceHandle); sl@0: __USBHOSTPRINT2(_L("Error = %d reason = %x"), sl@0: iEvent.iError, iEvent.iReason); sl@0: sl@0: if (status != KErrNone) sl@0: { sl@0: __USBHOSTPRINT1(_L("Status error = %d"), status.Int()); sl@0: } sl@0: iDeviceHandle = event.iDeviceHandle; sl@0: sl@0: switch (event.iEventType) sl@0: { sl@0: case RUsbHubDriver::TBusEvent::EDeviceAttached: sl@0: case RUsbHubDriver::TBusEvent::EDeviceRemoved: sl@0: eventReceived = ETrue; sl@0: break; sl@0: default: sl@0: break; sl@0: } sl@0: sl@0: } while (!eventReceived); sl@0: return event.iEventType; sl@0: } sl@0: sl@0: sl@0: sl@0: void CUsbHost::Cancel() sl@0: { sl@0: iHubDriver.CancelWaitForBusEvent(); sl@0: } sl@0: sl@0: sl@0: void CUsbHost::DriveMap(TDriveMap& aDriveMap) const sl@0: { sl@0: __MSFNSLOG sl@0: iMountManager->DriveMap(aDriveMap); sl@0: } sl@0: sl@0: sl@0: void CUsbHost::DeviceMap(TInt aDeviceIndex, TDeviceMap& aDeviceMap) const sl@0: { sl@0: __MSFNSLOG sl@0: iMountManager->DeviceMap(aDeviceIndex, aDeviceMap); sl@0: } sl@0: sl@0: sl@0: TInt CUsbHost::DevicesNumber() const sl@0: { sl@0: return iMountManager->DevicesNumber(); sl@0: } sl@0: sl@0: sl@0: CUsbHostDisp* CUsbHostDisp::NewL(MDriveDisplay& aDriveDisplay) sl@0: { sl@0: __MSFNSLOG sl@0: CUsbHostDisp* r = new (ELeave) CUsbHostDisp(aDriveDisplay); sl@0: CleanupStack::PushL(r); sl@0: r->ConstructL(); sl@0: CleanupStack::Pop(); sl@0: return r; sl@0: } sl@0: sl@0: sl@0: void CUsbHostDisp::ConstructL() sl@0: { sl@0: __MSFNLOG sl@0: CUsbHost::ConstructL(); sl@0: } sl@0: sl@0: sl@0: CUsbHostDisp::CUsbHostDisp(MDriveDisplay& aDriveDisplay) sl@0: : CUsbHost(), sl@0: iDriveDisplay(aDriveDisplay) sl@0: { sl@0: __MSFNLOG sl@0: } sl@0: sl@0: sl@0: CUsbHostDisp::~CUsbHostDisp() sl@0: { sl@0: __MSFNLOG sl@0: } sl@0: sl@0: void CUsbHostDisp::ProcessBusEventL() sl@0: { sl@0: CUsbHost::ProcessBusEventL(); sl@0: sl@0: // update display sl@0: iDriveDisplay.DriveListL(); sl@0: sl@0: // Devices attached sl@0: TInt devicesNumber = DevicesNumber(); sl@0: iDriveDisplay.DevicesNumber(devicesNumber); sl@0: sl@0: // LUNs for each device sl@0: TDeviceMap deviceMap; sl@0: TInt deviceIndex; sl@0: TInt row; sl@0: for (row = 0, deviceIndex = (devicesNumber - 1); deviceIndex >= 0 ; row++, deviceIndex--) sl@0: { sl@0: deviceMap.Reset(); sl@0: // get map sl@0: DeviceMap(deviceIndex, deviceMap); sl@0: sl@0: // display sl@0: iDriveDisplay.DeviceMapL(row, deviceIndex, deviceMap); sl@0: } sl@0: sl@0: iDriveDisplay.DeviceMapClear(row); sl@0: sl@0: // Display all Drives sl@0: TDriveMap driveMap; sl@0: driveMap.Reset(); sl@0: DriveMap(driveMap); sl@0: iDriveDisplay.DriveMapL(driveMap); sl@0: } sl@0: sl@0: