os/kernelhwsrv/userlibandfileserver/fileserver/shostmassstorage/client/rusbhostmsdevice.cpp
First public contribution.
1 // Copyright (c) 2008-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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
24 #include "rusbhostmsdevice.h"
26 #include "msgservice.h"
28 _LIT(KFileSystem, "FAT");
30 TVersion RUsbHostMsDevice::Version() const
32 __FNLOG("RUsbHostMsDevice::Version");
33 return(TVersion(KUsbHostMsSrvMajorVersionNumber,
34 KUsbHostMsSrvMinorVersionNumber,
35 KUsbHostMsSrvBuildVersionNumber));
39 TInt RUsbHostMsDevice::StartServer()
41 __FNLOG("RUsbHostMsDevice::StartServer");
45 const TUid KServerUid3={0x10286A83};
46 const TUidType serverUid(KNullUid,KNullUid,KServerUid3);
48 // Create the server process
49 if((r=server.Create(KUsbHostMsServerName,KNullDesC,serverUid)) != KErrNone)
51 __PRINT1(_L("Server process create = %d\n"), r);
55 // Create the rendezvous request with the server process
57 server.Rendezvous(stat);
58 if (stat!=KRequestPending)
60 server.Kill(0); // If the outstanding request is not pending then kill the server
64 server.SetPriority(EPriorityHigh);
65 server.Resume(); // start the server
68 // Test whether the process has ended and if it has ended, return how it ended.
69 User::WaitForRequest(stat);
70 r=(server.ExitType()==EExitPanic) ? KErrGeneral : stat.Int();
76 EXPORT_C RUsbHostMsDevice::RUsbHostMsDevice()
78 // Intentionally left blank
86 Add the Mass Storage device to the MSC server. This API is asynchronous, upon
87 completion one could find the number of logical units present in the added
88 device by calling GetNumLun API. Calling the Remove API before completing this
89 asynchronous API will complete the pending request notification with KErrCancel.
91 @param aConfig [In] A constant reference object to
92 THostMassStorageConfig containing the confiquration values of
93 the massstorage device requested to add to the MSC
94 @param aStatus [In] A reference to TRequestStatus to be used for asynchronous
97 EXPORT_C void RUsbHostMsDevice::Add(const THostMassStorageConfig& aConfig, TRequestStatus& aStatus)
99 __FNLOG("RUsbHostMsDevice::Add");
102 err = CreateSession(KUsbHostMsServerName, Version(), 128, EIpcSession_GlobalSharable);
104 // Being a transient server, the first session creation would fail with if
105 // the server is not running
108 // Find whether the session creation failed because server was not
110 if (err==KErrNotFound || err==KErrServerTerminated)
116 // Try session creation again
117 err = CreateSession(KUsbHostMsServerName, Version(), 128, EIpcSession_GlobalSharable);
122 TRequestStatus* statusPtr = &aStatus;
125 // Create a session handle that can be passed via IPC to another process
126 // (also being shared by other threads in the current process)
127 err = ShareProtected();
130 // synchronous call to register the interface
131 TPckg<THostMassStorageConfig> pckg(aConfig);
132 err = SendReceive(EUsbHostMsRegisterInterface, TIpcArgs(&pckg));
135 User::RequestComplete(statusPtr, err);
139 // Asynchronous call to initialise the interface
140 SendReceive(EUsbHostMsInitialiseInterface, TIpcArgs(NULL), aStatus);
145 Close(); // Close the session handle
146 __PRINT1(_L("Could not create a sharable session handle %d\n"), err);
147 User::RequestComplete(statusPtr, err);
152 // Check whether the error is in starting the server or in creating the
154 __PRINT1(_L("Creating server/session failed with %d\n"), err);
155 User::RequestComplete(statusPtr, err);
164 Remove the Mass Storage device from the MSC server.
166 EXPORT_C void RUsbHostMsDevice::Remove()
168 // Note: Here, at present we use only the interface token. But we still take
169 // THostMassStorageConfig as parameter for future needs
170 __FNLOG("RUsbHostMsDevice::Remove");
171 _LIT(KUsbHostMsClientPanicCat, "usbhostmsclient");
173 TInt r = SendReceive(EUsbHostMsUnRegisterInterface);
175 r = SendReceive(EUsbHostMsFinalCleanup);
178 User::Panic(KUsbHostMsClientPanicCat ,KErrCouldNotDisconnect);
180 Close(); // Close the session handle
188 Get the number of logical units suppoted by the device.
190 @param aNumLuns [Out] Outputs the number of logical units found in the
191 added Mass Storage device. A value of 'n' represents there are
192 'n' LUNs present in the device, where "n > 0"
196 EXPORT_C TInt RUsbHostMsDevice::GetNumLun(TUint32& aNumLuns)
198 __FNLOG("RUsbHostMsDevice::GetNumLun");
199 TPckg<TUint32> pckg(aNumLuns);
200 return SendReceive(EUsbHostMsGetNumLun,TIpcArgs(&pckg));
204 EXPORT_C TInt RUsbHostMsDevice::MountLun(TUint32 aLunId, TInt aDriveNum)
206 __FNLOG("RUsbHostMsDevice::MountLun");
208 TInt r = TheFs.Connect();
211 TPckgBuf<TMassStorageUnitInfo> unitPkg;
212 unitPkg().iLunID = aLunId;
214 r = TheFs.MountProxyDrive(aDriveNum, _L("usbhostms"), &unitPkg, *this);
217 r = TheFs.MountFileSystem(KFileSystem, aDriveNum);
219 if(r != KErrNone && r != KErrNotReady && r != KErrCorrupt)
221 TheFs.DismountFileSystem(KFileSystem, aDriveNum);
222 TheFs.DismountProxyDrive(aDriveNum);
230 EXPORT_C TInt RUsbHostMsDevice::DismountLun(TInt aDriveNum)
232 __FNLOG("RUsbHostMsDevice::DismountLun");
238 r = TheFs.DismountFileSystem(KFileSystem, aDriveNum);
241 // dismount failed - attempt a forced dismount
243 TheFs.NotifyDismount(aDriveNum, stat, EFsDismountForceDismount);
244 User::WaitForRequest(stat);
249 r = TheFs.DismountProxyDrive(aDriveNum);