1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kerneltest/f32test/shostmassstorage/msman/src/cusbotg.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,270 @@
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 <e32cmn.h>
1.20 +#include <e32base.h>
1.21 +#include <d32otgdi.h>
1.22 +#include <e32property.h>
1.23 +
1.24 +#include "cusbotg.h"
1.25 +#include "cusbotgwatcher.h"
1.26 +#include "rusbmspublisher.h"
1.27 +
1.28 +
1.29 +#include "tmslog.h"
1.30 +#include "debug.h"
1.31 +
1.32 +_LIT(KOtgdiLddFileName, "otgdi");
1.33 +
1.34 +
1.35 +ROtgStateChangeNotifier::ROtgStateChangeNotifier()
1.36 +: iRegistered(EFalse)
1.37 + {
1.38 + __MSFNSLOG
1.39 + }
1.40 +
1.41 +
1.42 +ROtgStateChangeNotifier::~ROtgStateChangeNotifier()
1.43 + {
1.44 + __MSFNSLOG
1.45 + if (iRegistered)
1.46 + iMessage.Complete(KErrDisconnected);
1.47 + }
1.48 +
1.49 +/**
1.50 +Initialise notifier to enable media change notfications.
1.51 +
1.52 +@param aMessage The message to commplete the notification
1.53 +*/
1.54 +void ROtgStateChangeNotifier::Register(const RMessage2& aMessage)
1.55 + {
1.56 + __MSFNLOG
1.57 + iRegistered = ETrue;
1.58 + iMessage = aMessage;
1.59 + }
1.60 +
1.61 +
1.62 +void ROtgStateChangeNotifier::DoNotifyL()
1.63 + {
1.64 + __MSFNLOG
1.65 + CompleteNotifierL(KErrNone);
1.66 + }
1.67 +
1.68 +
1.69 +void ROtgStateChangeNotifier::DoCancelL()
1.70 + {
1.71 + __MSFNLOG
1.72 + CompleteNotifierL(KErrCancel);
1.73 + }
1.74 +
1.75 +
1.76 +void ROtgStateChangeNotifier::CompleteNotifierL(TInt aReason)
1.77 + {
1.78 + __MSFNLOG
1.79 + if (iRegistered)
1.80 + {
1.81 + TBool changed = ETrue;
1.82 + TPckgBuf<TBool> p(changed);
1.83 + iMessage.WriteL(0, p);
1.84 + iMessage.Complete(aReason);
1.85 + iRegistered = EFalse;
1.86 + }
1.87 + }
1.88 +
1.89 +
1.90 +CUsbOtg* CUsbOtg::NewL()
1.91 + {
1.92 + __MSFNSLOG
1.93 + CUsbOtg* self = new (ELeave) CUsbOtg();
1.94 + CleanupStack::PushL(self);
1.95 + self->ConstructL();
1.96 + CleanupStack::Pop(self);
1.97 + return self;
1.98 + }
1.99 +
1.100 +
1.101 +void CUsbOtg::ConstructL()
1.102 + {
1.103 + __MSFNLOG
1.104 +
1.105 + TInt r = User::LoadLogicalDevice(KOtgdiLddFileName);
1.106 +
1.107 + if (r != KErrNone && r != KErrAlreadyExists)
1.108 + {
1.109 + __USBOTGPRINT1(_L(" LoadLogicalDevice(KOtgdiLddFileName) error = %d"), r);
1.110 + User::Leave(r);
1.111 + }
1.112 +
1.113 + r = iUsbOtgDriver.Open();
1.114 + if (r != KErrNone && r != KErrAlreadyExists)
1.115 + {
1.116 + __USBOTGPRINT1(_L(" otg.Open fails %d"), r);
1.117 + User::FreeLogicalDevice(RUsbOtgDriver::Name());
1.118 + User::Leave(r);
1.119 + }
1.120 +
1.121 + StartL();
1.122 +
1.123 + r = iUsbOtgDriver.StartStacks();
1.124 + if (r != KErrNone)
1.125 + {
1.126 + __USBOTGPRINT1(_L(" otg.StartStacks fails %d"), r);
1.127 + User::FreeLogicalDevice(RUsbOtgDriver::Name());
1.128 + User::Leave(r);
1.129 + }
1.130 +
1.131 + __USBOTGPRINT(_L(" otg stacks successfully started"));
1.132 + }
1.133 +
1.134 +
1.135 +CUsbOtg::CUsbOtg()
1.136 +: iOtgState(KOtgStateStart)
1.137 + {
1.138 + __MSFNLOG
1.139 + }
1.140 +
1.141 +
1.142 +CUsbOtg::~CUsbOtg()
1.143 + {
1.144 + __MSFNLOG
1.145 + Stop();
1.146 +
1.147 + TInt r = iUsbOtgDriver.BusDrop();
1.148 +
1.149 + // Unload OTGDI components if it was ever started
1.150 +
1.151 + if (iUsbOtgDriver.Handle())
1.152 + {
1.153 + iUsbOtgDriver.StopStacks();
1.154 + iUsbOtgDriver.Close();
1.155 +
1.156 + }
1.157 +
1.158 + TInt err = User::FreeLogicalDevice(RUsbOtgDriver::Name());
1.159 + }
1.160 +
1.161 +
1.162 +void CUsbOtg::StartL()
1.163 + {
1.164 + __MSFNLOG
1.165 + // Request Otg notifications
1.166 + iOtgEventWatcher = CUsbOtgEventWatcher::NewL(iUsbOtgDriver, *this);
1.167 + iOtgEventWatcher->Start();
1.168 +
1.169 + iRequestSessionWatcher = CRequestSessionWatcher::NewL(*this);
1.170 + }
1.171 +
1.172 +
1.173 +void CUsbOtg::Stop()
1.174 +/**
1.175 + * Stop the USB OTG events watcher
1.176 + */
1.177 + {
1.178 + __MSFNLOG
1.179 +
1.180 + if (iOtgEventWatcher)
1.181 + {
1.182 + iOtgEventWatcher->Cancel();
1.183 + delete iOtgEventWatcher;
1.184 + iOtgEventWatcher = NULL;
1.185 + }
1.186 +
1.187 + if (iRequestSessionWatcher)
1.188 + {
1.189 + delete iRequestSessionWatcher;
1.190 + iRequestSessionWatcher = NULL;
1.191 + }
1.192 + }
1.193 +
1.194 +
1.195 +void CUsbOtg::BusRequestL()
1.196 + {
1.197 + __MSFNLOG
1.198 + if (iOtgState == KOtgStateAPlugInserted)
1.199 + {
1.200 + TInt err = iUsbOtgDriver.BusRequest();
1.201 + if (err)
1.202 + {
1.203 + __USBOTGPRINT1(_L("OTG::BusRequest[%d] failed !"), err);
1.204 + }
1.205 + User::LeaveIfError(err);
1.206 + iOtgState = KOtgStateSessionOpen;
1.207 + }
1.208 +
1.209 + }
1.210 +
1.211 +
1.212 +void CUsbOtg::HandleUsbOtgEvent(RUsbOtgDriver::TOtgEvent aEvent)
1.213 + {
1.214 + __MSFNLOG
1.215 +
1.216 + switch (aEvent)
1.217 + {
1.218 + case RUsbOtgDriver::EEventAPlugInserted:
1.219 + {
1.220 + __USBOTGPRINT(_L(">> UsbOtgEvent[EEventAPlugInserted]"));
1.221 + /*
1.222 + RUsbOtgEventPublisher eventPublisher;
1.223 + eventPublisher.PublishEvent(aEvent);
1.224 + */
1.225 + iNotifier.DoNotifyL();
1.226 + iOtgState = KOtgStateAPlugInserted;
1.227 + }
1.228 + break;
1.229 +
1.230 + case RUsbOtgDriver::EEventBusConnectionBusy:
1.231 + {
1.232 + RUsbManConnectionStatePublisher publisher;
1.233 + publisher.PublishEvent(ETrue);
1.234 + }
1.235 + break;
1.236 + case RUsbOtgDriver::EEventBusConnectionIdle:
1.237 + {
1.238 + RUsbManConnectionStatePublisher publisher;
1.239 + publisher.PublishEvent(EFalse);
1.240 + }
1.241 + break;
1.242 + default:
1.243 + __USBOTGPRINT1(_L(">> UsbOtgEvent[%x]"), aEvent);
1.244 + break;
1.245 + }
1.246 + }
1.247 +
1.248 +
1.249 +TBool CUsbOtg::DeviceInserted()
1.250 + {
1.251 + __MSFNLOG
1.252 + return iOtgState == KOtgStateAPlugInserted ? ETrue : EFalse;
1.253 + }
1.254 +
1.255 +void CUsbOtg::NotifyChange(const RMessage2& aMessage)
1.256 + {
1.257 + __MSFNLOG
1.258 + iNotifier.Register(aMessage);
1.259 + }
1.260 +
1.261 +
1.262 +void CUsbOtg::NotifyChangeCancel()
1.263 + {
1.264 + __MSFNLOG
1.265 + iNotifier.DoCancelL();
1.266 + }
1.267 +
1.268 +
1.269 +TInt CUsbOtg::BusDrop()
1.270 + {
1.271 + __MSFNLOG
1.272 + return iUsbOtgDriver.BusDrop();
1.273 + }