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