author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Thu, 02 Sep 2010 21:54:16 +0300 | |
changeset 259 | 57b9594f5772 |
parent 152 | 657f875b013e |
child 280 | 2bfb1feef9de |
permissions | -rw-r--r-- |
259
57b9594f5772
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
1 |
// Copyright (c) 1998-2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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". |
|
7 |
// |
|
8 |
// Initial Contributors: |
|
9 |
// Nokia Corporation - initial contribution. |
|
10 |
// |
|
11 |
// Contributors: |
|
12 |
// |
|
13 |
// Description: |
|
14 |
// e32\kernel\power.cpp |
|
15 |
// Generic power management |
|
16 |
// |
|
17 |
// |
|
18 |
||
19 |
#include <kernel/kpower.h> |
|
20 |
#include <kernel/kern_priv.h> |
|
21 |
#include "execs.h" |
|
22 |
#include "msgqueue.h" |
|
23 |
||
24 |
// #define _DEBUG_POWER |
|
25 |
||
26 |
||
27 |
/****************************************************************************** |
|
28 |
* Power Manager - a Power Model implementation |
|
29 |
******************************************************************************/ |
|
30 |
#ifndef __X86__ |
|
31 |
DPowerController* TPowerController::ThePowerController = 0; |
|
32 |
#endif |
|
33 |
||
34 |
class DPowerManager : public DPowerModel |
|
35 |
{ |
|
36 |
public: |
|
37 |
// from DPowerModel |
|
38 |
void CpuIdle(); |
|
39 |
void RegisterUserActivity(const TRawEvent& anEvent); |
|
40 |
TInt PowerHalFunction(TInt aFunction, TAny* a1, TAny* a2); |
|
41 |
void AbsoluteTimerExpired(); |
|
42 |
void SystemTimeChanged(TInt anOldTime, TInt aNewTime); |
|
43 |
TSupplyStatus MachinePowerStatus(); |
|
44 |
||
45 |
public: |
|
46 |
||
47 |
static DPowerManager* New(); |
|
48 |
||
49 |
TInt EnableWakeupEvents(TPowerState); |
|
50 |
void DisableWakeupEvents(); |
|
51 |
void RequestWakeupEventNotification(TRequestStatus*); |
|
52 |
void CancelWakeupEventNotification(); |
|
53 |
TInt PowerDown(); |
|
54 |
||
55 |
void AppendHandler(DPowerHandler*); |
|
56 |
void RemoveHandler(DPowerHandler*); |
|
57 |
void WakeupEvent(); |
|
58 |
||
59 |
DPowerController* iPowerController; |
|
60 |
DBatteryMonitor* iBatteryMonitor; |
|
61 |
DPowerHal* iPowerHal; |
|
62 |
||
63 |
TInt iTotalCurrent; |
|
64 |
||
65 |
private: |
|
66 |
||
67 |
DPowerManager(); |
|
68 |
||
69 |
// Acquire the feature lock |
|
70 |
// Called in CS |
|
71 |
void Lock() |
|
72 |
{ |
|
73 |
Kern::MutexWait(*iFeatureLock); |
|
74 |
} |
|
75 |
||
76 |
// Release the feature lock |
|
77 |
// Called in CS |
|
78 |
void Unlock() |
|
79 |
{ |
|
80 |
Kern::MutexSignal(*iFeatureLock); |
|
81 |
} |
|
82 |
// |
|
83 |
// Currently not used |
|
84 |
// |
|
85 |
// #ifdef _DEBUG |
|
86 |
// TBool IsLocked() |
|
87 |
// used in assertions only |
|
88 |
// { return iFeatureLock->iCleanup.iThread == TheCurrentThread; } |
|
89 |
// #endif |
|
90 |
||
91 |
void NotifyWakeupEvent(TInt aReason); |
|
92 |
||
93 |
DMutex* iFeatureLock; |
|
94 |
DThread* iClient; // protected by the system lock |
|
95 |
TClientRequest* iRequest; // protected by the system lock |
|
96 |
DPowerHandler* iHandlers; // protected by the feature lock |
|
97 |
}; |
|
98 |
||
99 |
static DPowerManager* PowerManager; |
|
100 |
||
101 |
class DummyPowerController : public DPowerController |
|
102 |
// Used when the plaform doesn't support power management |
|
103 |
{ |
|
104 |
public: // from DPowerController |
|
105 |
void CpuIdle() {} |
|
106 |
void EnableWakeupEvents() { WakeupEvent(); } |
|
107 |
void DisableWakeupEvents() {} |
|
108 |
void AbsoluteTimerExpired() {} |
|
109 |
void PowerDown(TTimeK /* aWakeupTime */) { if (iTargetState == EPwOff) __PM_PANIC("Can't power off"); } |
|
110 |
}; |
|
111 |
||
112 |
TInt PowerModelInit() |
|
113 |
{ |
|
114 |
#ifdef _DEBUG_POWER |
|
115 |
TInt maskIndex = KPOWER / 32; |
|
116 |
TUint32 bitMask = 1 << (KPOWER % 32); |
|
117 |
TheSuperPage().iDebugMask[maskIndex] |= bitMask; |
|
118 |
#endif |
|
119 |
__KTRACE_OPT2(KBOOT,KPOWER,Kern::Printf("PowerModelInit()")); |
|
120 |
DPowerManager* pm = DPowerManager::New(); |
|
121 |
if (!pm) |
|
122 |
return KErrNoMemory; |
|
123 |
PowerManager = pm; |
|
124 |
return KErrNone; |
|
125 |
} |
|
126 |
||
127 |
_LIT(KPowerMgrMutexName, "PwrMgrLock"); |
|
128 |
||
129 |
DPowerManager* DPowerManager::New() |
|
130 |
{ |
|
131 |
DPowerManager* self = new DPowerManager(); |
|
132 |
if (!self) |
|
133 |
return NULL; |
|
134 |
if (Kern::MutexCreate(self->iFeatureLock, KPowerMgrMutexName, KMutexOrdPowerMgr) != KErrNone) |
|
135 |
return NULL; // Do not cleanup since kernel will panic eventually |
|
136 |
self->iPowerController = new DummyPowerController(); |
|
137 |
if (self->iPowerController == NULL) |
|
138 |
return NULL; // Do not cleanup since kernel will panic eventually |
|
139 |
TInt r = Kern::CreateClientRequest(self->iRequest); |
|
140 |
if (r != KErrNone) |
|
141 |
return NULL; // Do not cleanup since kernel will panic eventually |
|
142 |
return self; |
|
143 |
} |
|
144 |
||
145 |
DPowerManager::DPowerManager() |
|
146 |
{ |
|
147 |
} |
|
148 |
||
149 |
void DPowerManager::CpuIdle() |
|
150 |
{ // from DPowerModel |
|
151 |
__PM_ASSERT(iPowerController); |
|
152 |
iPowerController->CpuIdle(); |
|
153 |
} |
|
154 |
||
155 |
void DPowerManager::RegisterUserActivity(const TRawEvent&) |
|
156 |
{ // from DPowerModel |
|
157 |
} |
|
158 |
||
159 |
void DPowerManager::SystemTimeChanged(TInt aOldTime, TInt aNewTime) |
|
160 |
{ // from DPowerModel |
|
161 |
if (iBatteryMonitor) |
|
162 |
iBatteryMonitor->SystemTimeChanged(aOldTime, aNewTime); |
|
163 |
} |
|
164 |
||
165 |
TSupplyStatus DPowerManager::MachinePowerStatus() |
|
166 |
{ // from DPowerModel |
|
167 |
TSupplyStatus s; |
|
168 |
if (iBatteryMonitor) |
|
169 |
s = iBatteryMonitor->MachinePowerStatus(); |
|
170 |
else |
|
171 |
s = EGood; |
|
172 |
return s; |
|
173 |
} |
|
174 |
||
175 |
TInt DPowerManager::PowerHalFunction(TInt aFunction, TAny* a1, TAny* a2) |
|
176 |
{ // from DPowerModel |
|
177 |
__KTRACE_OPT(KPOWER,Kern::Printf(">DPowerManager::PowerHalFunction() func=0x%x, a1=0x%x, a2=0x%x", aFunction, a1, a2)); |
|
178 |
TInt r; |
|
179 |
if (iPowerHal) |
|
180 |
r = iPowerHal->PowerHalFunction(aFunction, a1, a2); |
|
181 |
else |
|
182 |
r = KErrNotSupported; |
|
183 |
||
184 |
__KTRACE_OPT(KPOWER,Kern::Printf("<DPowerManager::PowerHalFunction() return 0x%x", r)); |
|
185 |
return r; |
|
186 |
} |
|
187 |
||
188 |
void DPowerManager::AbsoluteTimerExpired() |
|
189 |
{ // from DPowerModel |
|
190 |
if (iPowerController->iTargetState != EPwActive) |
|
191 |
iPowerController->AbsoluteTimerExpired(); |
|
192 |
} |
|
193 |
||
194 |
// Called in CS |
|
195 |
void DPowerManager::AppendHandler(DPowerHandler* aPh) |
|
196 |
{ // called by drivers (power handler) |
|
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
197 |
__KTRACE_OPT(KPOWER,Kern::Printf("PowerManger::AppendHandler('%S')", &aPh->iName)); |
0 | 198 |
__ASSERT_CRITICAL; |
199 |
//Check to ensure that handler is not added multiple times and not part of any other list |
|
200 |
__PM_ASSERT(!(aPh->iPrev) && !(aPh->iNext)); |
|
201 |
Lock(); |
|
202 |
//Create circular doubly linked power handler list |
|
203 |
if(iHandlers == NULL) |
|
204 |
{ //Empty list, create first entry |
|
205 |
iHandlers = aPh; |
|
206 |
aPh->iNext = iHandlers; |
|
207 |
aPh->iPrev = iHandlers; |
|
208 |
} |
|
209 |
else |
|
210 |
{ //Append to end of the list. |
|
211 |
aPh->iNext = iHandlers->iNext; |
|
212 |
iHandlers->iNext->iPrev = aPh; |
|
213 |
aPh->iPrev = iHandlers; |
|
214 |
iHandlers->iNext = aPh; |
|
215 |
iHandlers = aPh; |
|
216 |
} |
|
217 |
Unlock(); |
|
218 |
} |
|
219 |
||
220 |
// Called in CS |
|
221 |
void DPowerManager::RemoveHandler(DPowerHandler* aPh) |
|
222 |
{ // called by drivers (power handler) |
|
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
223 |
__KTRACE_OPT(KPOWER,Kern::Printf("PowerManger::RemoveHandler('%S')", &aPh->iName)); |
0 | 224 |
__ASSERT_CRITICAL; |
225 |
Lock(); |
|
226 |
__PM_ASSERT(aPh); |
|
227 |
||
228 |
if ((iHandlers == NULL) //If the list is empty |
|
229 |
|| (aPh->iNext == NULL || aPh->iPrev == NULL)) //or entry already removed then return immediately |
|
230 |
{ |
|
231 |
aPh->iPrev = aPh->iNext = NULL; |
|
232 |
Unlock(); |
|
233 |
return; |
|
234 |
} |
|
235 |
||
236 |
DPowerHandler *temp = aPh->iPrev; |
|
237 |
if(aPh == temp) //Only one node in the list. |
|
238 |
iHandlers = NULL; |
|
239 |
else |
|
240 |
{ //Remove the entry from list |
|
241 |
temp->iNext = aPh->iNext; |
|
242 |
temp = aPh->iNext; |
|
243 |
temp->iPrev = aPh->iPrev; |
|
244 |
if(aPh == iHandlers) //If the node is the last node in the list then make iHandler point to last one. |
|
245 |
iHandlers = aPh->iPrev; |
|
246 |
} |
|
247 |
//Make next and prev pointers to NULL |
|
248 |
aPh->iPrev = aPh->iNext = NULL; |
|
249 |
Unlock(); |
|
250 |
} |
|
251 |
||
252 |
void DPowerManager::WakeupEvent() |
|
253 |
{ // called by power controller |
|
254 |
__KTRACE_OPT(KPOWER,Kern::Printf("PowerManger::WakeupEvent()")); |
|
255 |
if (iPowerController->iTargetState != EPwActive) |
|
256 |
{ |
|
257 |
NKern::ThreadEnterCS(); |
|
258 |
NotifyWakeupEvent(KErrNone); |
|
259 |
NKern::ThreadLeaveCS(); |
|
260 |
} |
|
261 |
} |
|
262 |
||
263 |
TInt DPowerManager::EnableWakeupEvents(TPowerState aState) |
|
264 |
{ // called by ExecHandler |
|
265 |
__KTRACE_OPT(KPOWER,Kern::Printf("PowerManger::EnableWakeupEvents(0x%x)", aState)); |
|
266 |
if ((aState < 0) || (EPwLimit <= aState)) |
|
267 |
return KErrArgument; |
|
268 |
if (aState == EPwActive) |
|
269 |
return KErrArgument; |
|
270 |
Lock(); |
|
271 |
if (iPowerController->iTargetState != EPwActive) |
|
272 |
iPowerController->DisableWakeupEvents(); |
|
273 |
iPowerController->iTargetState = aState; |
|
274 |
iPowerController->EnableWakeupEvents(); |
|
275 |
Unlock(); |
|
276 |
return KErrNone; |
|
277 |
} |
|
278 |
||
279 |
void DPowerManager::DisableWakeupEvents() |
|
280 |
{ // called by ExecHandler |
|
281 |
__KTRACE_OPT(KPOWER,Kern::Printf("PowerManger::DisableWakeupEvents()")); |
|
282 |
Lock(); |
|
283 |
if (iPowerController->iTargetState != EPwActive) |
|
284 |
{ |
|
285 |
iPowerController->iTargetState = EPwActive; |
|
286 |
iPowerController->DisableWakeupEvents(); |
|
287 |
} |
|
288 |
Unlock(); |
|
289 |
} |
|
290 |
||
291 |
void DPowerManager::RequestWakeupEventNotification(TRequestStatus* aStatus) |
|
292 |
{ // called by ExecHandler |
|
293 |
__KTRACE_OPT(KPOWER,Kern::Printf("PowerManger::RequestWakeupEventNotification()")); |
|
294 |
Lock(); // we aquire this lock to avoid new requests while in PowerDown |
|
295 |
NKern::LockSystem(); |
|
296 |
if (iClient || iRequest->SetStatus(aStatus) != KErrNone) |
|
297 |
Kern::RequestComplete(aStatus, KErrInUse); |
|
298 |
else |
|
299 |
{ |
|
300 |
iClient = TheCurrentThread; |
|
301 |
iClient->Open(); |
|
302 |
} |
|
303 |
NKern::UnlockSystem(); |
|
304 |
Unlock(); |
|
305 |
__KTRACE_OPT(KPOWER,Kern::Printf("<PowerManger::RequestWakeupEventNotification()")); |
|
306 |
} |
|
307 |
||
308 |
void DPowerManager::NotifyWakeupEvent(TInt aReason) |
|
309 |
{ // private |
|
310 |
NKern::LockSystem(); |
|
311 |
DThread* client = iClient; |
|
312 |
if (!client) |
|
313 |
{ |
|
314 |
NKern::UnlockSystem(); |
|
315 |
return; |
|
316 |
} |
|
317 |
iClient = NULL; |
|
318 |
Kern::QueueRequestComplete(client, iRequest, aReason); |
|
319 |
NKern::UnlockSystem(); |
|
320 |
client->Close(NULL); |
|
321 |
} |
|
322 |
||
323 |
void DPowerManager::CancelWakeupEventNotification() |
|
324 |
{ // called by ExecHandler |
|
325 |
__KTRACE_OPT(KPOWER,Kern::Printf("PowerManger::CancelWakeupEventNotification()")); |
|
326 |
NKern::ThreadEnterCS(); |
|
327 |
NotifyWakeupEvent(KErrCancel); |
|
328 |
NKern::ThreadLeaveCS(); |
|
329 |
} |
|
330 |
||
331 |
// Called in CS |
|
332 |
TInt DPowerManager::PowerDown() |
|
333 |
{ // called by ExecHandler |
|
334 |
__KTRACE_OPT(KPOWER,Kern::Printf(">PowerManger::PowerDown(0x%x) Enter", iPowerController->iTargetState)); |
|
335 |
__ASSERT_CRITICAL; |
|
336 |
||
337 |
Lock(); |
|
338 |
||
339 |
||
340 |
if (iPowerController->iTargetState == EPwActive) |
|
341 |
{ |
|
342 |
Unlock(); |
|
343 |
return KErrNotReady; |
|
344 |
} |
|
345 |
||
346 |
__PM_ASSERT(iHandlers); |
|
347 |
NFastSemaphore sem(0); |
|
348 |
DPowerHandler* ph = iHandlers; |
|
349 |
//Power down in reverse order of handle registration. |
|
350 |
do |
|
351 |
{ |
|
352 |
#ifdef _DEBUG_POWER |
|
353 |
__PM_ASSERT(!(ph->iStatus & DPowerHandler::EDone)); |
|
354 |
#endif |
|
355 |
ph->iSem = &sem; |
|
356 |
ph->PowerDown(iPowerController->iTargetState); |
|
357 |
#ifdef _DEBUG_POWER |
|
358 |
__PM_ASSERT(!(ph->iStatus & DPowerHandler::EDone)); |
|
359 |
NKern::FSWait(&sem); // power down drivers one after another to simplify debug |
|
360 |
__PM_ASSERT(!ph->iSem); |
|
361 |
__PM_ASSERT(ph->iStatus & EDone); |
|
362 |
ph->iStatus &= ~EDone; |
|
363 |
#endif |
|
364 |
ph = ph->iPrev; |
|
365 |
}while(ph != iHandlers); |
|
366 |
||
367 |
#ifndef _DEBUG_POWER |
|
368 |
ph = iHandlers; |
|
369 |
do |
|
370 |
{ |
|
371 |
NKern::FSWait(&sem); |
|
372 |
ph = ph->iPrev; |
|
373 |
}while(ph != iHandlers); |
|
374 |
#endif |
|
375 |
||
376 |
TTickQ::Wait(); |
|
377 |
||
378 |
iPowerController->PowerDown(K::SecondQ->WakeupTime()); |
|
379 |
__PM_ASSERT(iPowerController->iTargetState != EPwOff); |
|
380 |
iPowerController->iTargetState = EPwActive; |
|
381 |
||
382 |
K::SecondQ->WakeUp(); |
|
383 |
TTickQ::Signal(); |
|
384 |
||
385 |
ph = iHandlers->iNext; |
|
386 |
//Power up in same order of handle registration. |
|
387 |
do |
|
388 |
{ |
|
389 |
#ifdef _DEBUG_POWER |
|
390 |
__PM_ASSERT(!(ph->iStatus & DPowerHandler::EDone)); |
|
391 |
#endif |
|
392 |
ph->iSem = &sem; |
|
393 |
ph->PowerUp(); |
|
394 |
#ifdef _DEBUG_POWER |
|
395 |
__PM_ASSERT(!(ph->iStatus & DPowerHandler::EDone)); |
|
396 |
NKern::FSWait(&sem); // power down drivers one after another to simplify debug |
|
397 |
__PM_ASSERT(!ph->iSem); |
|
398 |
__PM_ASSERT(ph->iStatus & EDone); |
|
399 |
ph->iStatus &= ~EDone; |
|
400 |
#endif |
|
401 |
ph = ph->iNext; |
|
402 |
}while(ph != iHandlers->iNext); |
|
403 |
||
404 |
#ifndef _DEBUG_POWER |
|
405 |
ph = iHandlers->iNext; |
|
406 |
do |
|
407 |
{ |
|
408 |
NKern::FSWait(&sem); |
|
409 |
ph = ph->iNext; |
|
410 |
}while(ph != iHandlers->iNext); |
|
411 |
#endif |
|
412 |
||
413 |
// complete wakeup notification request if any |
|
414 |
NotifyWakeupEvent(KErrNone); |
|
415 |
||
416 |
Unlock(); |
|
417 |
||
418 |
__KTRACE_OPT(KPOWER,Kern::Printf("<PowerManger::PowerDown() Leave")); |
|
419 |
||
420 |
return KErrNone; |
|
421 |
} |
|
422 |
||
423 |
/****************************************************************************** |
|
424 |
* Power Handlers |
|
425 |
******************************************************************************/ |
|
426 |
||
427 |
||
428 |
||
429 |
/** |
|
430 |
Constructor. |
|
431 |
||
432 |
@param aName The power handler's debug-purpose symbolic name. |
|
433 |
||
434 |
@pre Calling thread must be in a critical section. |
|
435 |
@pre No fast mutex can be held. |
|
436 |
@pre Call in a thread context. |
|
437 |
@pre Kernel must be unlocked |
|
438 |
@pre interrupts enabled |
|
439 |
*/ |
|
440 |
EXPORT_C DPowerHandler::DPowerHandler(const TDesC& aName) : iName(aName) |
|
441 |
{ |
|
442 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"DPowerHandler::DPowerHandler"); |
|
443 |
} |
|
444 |
||
445 |
||
446 |
||
447 |
/** |
|
448 |
Destructor. |
|
449 |
||
450 |
@pre Calling thread must be in a critical section. |
|
451 |
@pre No fast mutex can be held. |
|
452 |
@pre Call in a thread context. |
|
453 |
@pre Kernel must be unlocked |
|
454 |
@pre interrupts enabled |
|
455 |
*/ |
|
456 |
EXPORT_C DPowerHandler::~DPowerHandler() |
|
457 |
{ |
|
458 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"DPowerHandler::~DPowerHandler"); |
|
459 |
SetCurrentConsumption(0); |
|
460 |
} |
|
461 |
||
462 |
||
463 |
||
464 |
/** |
|
465 |
Registers the power handler with the power manager. |
|
466 |
||
467 |
After registration, the power manager notifies the device driver about |
|
468 |
power state transitions by calling PowerDown() & PowerUp() in this power |
|
469 |
handler's interface. |
|
470 |
||
471 |
Note that the implementation of Add() acquires an internal lock (a DMutex), |
|
472 |
which is also held when the power manager calls PowerDown() and PowerUp(). |
|
473 |
This means that the device driver cannot hold a lock over Add() calls if |
|
474 |
the same lock is acquired by PowerDown() and PowerUp() implementations. |
|
475 |
||
476 |
You can find an example of synchronization between outgoing Add() |
|
477 |
and incoming PowerDown() & PowerUp() in e32/drivers/ecomm/d_comm.cpp. |
|
478 |
||
479 |
@see DPowerHandler::Remove() |
|
480 |
||
481 |
@pre Calling thread must be in a critical section. |
|
482 |
@pre No fast mutex can be held. |
|
483 |
@pre Call in a thread context. |
|
484 |
@pre Kernel must be unlocked |
|
485 |
@pre interrupts enabled |
|
486 |
*/ |
|
487 |
EXPORT_C void DPowerHandler::Add() |
|
488 |
{ |
|
489 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"DPowerHandler::Add"); |
|
490 |
PowerManager->AppendHandler(this); |
|
491 |
} |
|
492 |
||
493 |
||
494 |
||
495 |
||
496 |
/** |
|
497 |
De-registers the power handler. |
|
498 |
||
499 |
After the call to this function completes, the power manager stops notifying |
|
500 |
the device driver about kernel power state transitions. |
|
501 |
||
502 |
Note that the implementation of Remove() acquires an internal lock (a DMutex), |
|
503 |
which is also held when the power manager calls PowerDown() and PowerUp(). |
|
504 |
This means that the device driver cannot hold a lock over Remove() calls if |
|
505 |
the same lock is acquired by PowerDown() and PowerUp() implementations. |
|
506 |
||
507 |
You can find an example of synchronization between outgoing Remove() |
|
508 |
and incoming PowerDown() & PowerUp() in e32/drivers/ecomm/d_comm.cpp. |
|
509 |
||
510 |
@see DPowerHandler::Add() |
|
511 |
||
512 |
@pre Calling thread must be in a critical section. |
|
513 |
@pre No fast mutex can be held. |
|
514 |
@pre Call in a thread context. |
|
515 |
@pre Kernel must be unlocked |
|
516 |
@pre interrupts enabled |
|
517 |
*/ |
|
518 |
EXPORT_C void DPowerHandler::Remove() |
|
519 |
{ |
|
520 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"DPowerHandler::Remove"); |
|
521 |
PowerManager->RemoveHandler(this); |
|
522 |
} |
|
523 |
||
524 |
||
525 |
void DPowerHandler::Done() |
|
526 |
{ // private |
|
527 |
#ifdef _DEBUG_POWER |
|
528 |
__PM_ASSERT(!(iStatus & EDone)); |
|
529 |
iStatus |= EDone; |
|
530 |
#endif |
|
531 |
NKern::Lock(); |
|
152
657f875b013e
Revision: 201023
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
532 |
__KTRACE_OPT(KPOWER,Kern::Printf("DPowerHandler::Done('%S') sem=0x%x", &iName, iSem)); |
0 | 533 |
NFastSemaphore* sem = (NFastSemaphore*)__e32_atomic_swp_ord_ptr(&iSem, 0); |
534 |
if (sem) |
|
535 |
sem->Signal(); |
|
536 |
NKern::Unlock(); |
|
537 |
} |
|
538 |
||
539 |
||
540 |
||
541 |
||
542 |
/** |
|
543 |
Signals power up completion. |
|
544 |
||
545 |
A device driver calls this function precisely once in response to a PowerUp() call. |
|
546 |
It can be called by any thread at any time after PowerUp() has been entered. |
|
547 |
||
548 |
@pre No fast mutex can be held. |
|
549 |
@pre Call in a thread context. |
|
550 |
@pre Kernel must be unlocked |
|
551 |
@pre interrupts enabled |
|
552 |
*/ |
|
553 |
EXPORT_C void DPowerHandler::PowerUpDone() |
|
554 |
{ |
|
555 |
CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"DPowerHandler::PowerUpDone"); |
|
556 |
Done(); |
|
557 |
} |
|
558 |
||
559 |
||
560 |
||
561 |
/** |
|
562 |
Signals power down completion. |
|
563 |
||
564 |
A device driver calls this function precisely once in response to a PowerDown() call. |
|
565 |
It can be called by any thread at any time after PowerDown() has been entered. |
|
566 |
||
567 |
@pre No fast mutex can be held. |
|
568 |
@pre Call in a thread context. |
|
569 |
@pre Kernel must be unlocked |
|
570 |
@pre interrupts enabled |
|
571 |
*/ |
|
572 |
EXPORT_C void DPowerHandler::PowerDownDone() |
|
573 |
{ |
|
574 |
CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"DPowerHandler::PowerDownDone"); |
|
575 |
Done(); |
|
576 |
} |
|
577 |
||
578 |
||
579 |
||
580 |
/** @deprecated, no replacement */ |
|
581 |
EXPORT_C void DPowerHandler::DeltaCurrentConsumption(TInt aDelta) |
|
582 |
{ |
|
583 |
__e32_atomic_add_ord32(&iCurrent, aDelta); |
|
584 |
__PM_ASSERT(iCurrent >= 0); |
|
585 |
__e32_atomic_add_ord32(&PowerManager->iTotalCurrent, aDelta); |
|
586 |
__PM_ASSERT(PowerManager->iTotalCurrent >= 0); |
|
587 |
} |
|
588 |
||
589 |
/** @deprecated, no replacement */ |
|
590 |
EXPORT_C void DPowerHandler::SetCurrentConsumption(TInt aCurrent) |
|
591 |
{ |
|
592 |
__PM_ASSERT(aCurrent >= 0); |
|
593 |
TInt old = (TInt)__e32_atomic_swp_ord32(&iCurrent, aCurrent); |
|
594 |
TInt delta = aCurrent - old; |
|
595 |
__e32_atomic_add_ord32(&PowerManager->iTotalCurrent, delta); |
|
596 |
__PM_ASSERT(PowerManager->iTotalCurrent >= 0); |
|
597 |
} |
|
598 |
||
599 |
/****************************************************************************** |
|
600 |
* Power Controller |
|
601 |
******************************************************************************/ |
|
602 |
||
603 |
||
604 |
||
605 |
||
606 |
/** |
|
607 |
Default constructor. |
|
608 |
||
609 |
Typically a variant implementation creates a power controller object at |
|
610 |
variant initialization time. |
|
611 |
||
612 |
Only one power controller can be created. |
|
613 |
||
614 |
The object can never be destroyed. |
|
615 |
||
616 |
@pre Calling thread must be in a critical section. |
|
617 |
@pre No fast mutex can be held. |
|
618 |
@pre Call in a thread context. |
|
619 |
@pre Kernel must be unlocked |
|
620 |
@pre interrupts enabled |
|
621 |
*/ |
|
622 |
EXPORT_C DPowerController::DPowerController() |
|
623 |
{ |
|
624 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"DPowerController::DPowerController"); |
|
625 |
iTargetState = EPwActive; |
|
626 |
#ifndef __X86__ |
|
627 |
iResourceControllerData.iResourceController = NULL; |
|
628 |
iResourceControllerData.iClientId = -1; |
|
629 |
#endif |
|
630 |
} |
|
631 |
||
632 |
/** |
|
633 |
Registers this power controller object with the power manager. |
|
634 |
||
635 |
The power manager can only use the power controller after registration. |
|
636 |
||
637 |
@pre Calling thread must be in a critical section. |
|
638 |
@pre No fast mutex can be held. |
|
639 |
@pre Call in a thread context. |
|
640 |
@pre Kernel must be unlocked |
|
641 |
@pre interrupts enabled |
|
642 |
*/ |
|
643 |
EXPORT_C void DPowerController::Register() |
|
644 |
{ |
|
645 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"DPowerController::Register"); |
|
646 |
__KTRACE_OPT2(KBOOT,KPOWER,Kern::Printf("DPowerController::Register()")); |
|
647 |
delete PowerManager->iPowerController; |
|
648 |
PowerManager->iPowerController = this; |
|
649 |
#ifndef __X86__ |
|
650 |
TPowerController::ThePowerController = this; |
|
651 |
#endif |
|
652 |
K::PowerModel = PowerManager; |
|
653 |
} |
|
654 |
||
655 |
#ifndef __X86__ |
|
656 |
/** |
|
657 |
Registers resource controller with power controller |
|
658 |
This function is called only by resource controller to register itself with power controller |
|
659 |
*/ |
|
660 |
EXPORT_C TInt DPowerController::RegisterResourceController(DBase* aController, TInt aClientId) |
|
661 |
{ |
|
662 |
__KTRACE_OPT(KRESMANAGER, Kern::Printf("DPowerController::Register()")); |
|
663 |
if(iResourceControllerData.iResourceController) |
|
664 |
return KErrAlreadyExists; |
|
665 |
//Store the Resource controller pointer |
|
666 |
iResourceControllerData.iResourceController = aController; |
|
667 |
//Store the client Id generated by Resource controller for Power controller to use. |
|
668 |
iResourceControllerData.iClientId = aClientId; |
|
259
57b9594f5772
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
669 |
|
57b9594f5772
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
670 |
// Perform variant specific operations e.g. trigger registration of resources for idle with Resource Manager |
57b9594f5772
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
152
diff
changeset
|
671 |
return DoRegisterResourceController(); |
0 | 672 |
} |
673 |
||
674 |
/** |
|
675 |
Interface class to give access to the power controller to base port components. |
|
676 |
*/ |
|
677 |
EXPORT_C DPowerController* TPowerController::PowerController() |
|
678 |
{ |
|
679 |
return TPowerController::ThePowerController; |
|
680 |
} |
|
681 |
#endif |
|
682 |
||
683 |
/** |
|
684 |
Signals a wakeup event. |
|
685 |
||
686 |
The power controller must signal wakeup events when enabled, |
|
687 |
through EnableWakeupEvents() and DisableWakeupEvents(), and |
|
688 |
when iTargetState is not equal to EPwActive. |
|
689 |
||
690 |
@see DPowerController::iTargetState |
|
691 |
||
692 |
@pre Calling thread must be in a critical section. |
|
693 |
@pre No fast mutex can be held. |
|
694 |
@pre Call in a thread context. |
|
695 |
@pre Kernel must be unlocked |
|
696 |
@pre interrupts enabled |
|
697 |
*/ |
|
698 |
EXPORT_C void DPowerController::WakeupEvent() |
|
699 |
{ |
|
700 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"DPowerController::WakeupEvent"); |
|
701 |
PowerManager->WakeupEvent(); |
|
702 |
} |
|
703 |
||
704 |
||
705 |
/****************************************************************************** |
|
706 |
* Battery Monitor |
|
707 |
******************************************************************************/ |
|
708 |
||
709 |
EXPORT_C DBatteryMonitor::DBatteryMonitor() |
|
710 |
{ |
|
711 |
} |
|
712 |
||
713 |
EXPORT_C void DBatteryMonitor::Register() |
|
714 |
{ |
|
715 |
__KTRACE_OPT2(KBOOT,KPOWER,Kern::Printf("DBatteryMonitor::Register()")); |
|
716 |
PowerManager->iBatteryMonitor = this; |
|
717 |
} |
|
718 |
||
719 |
/****************************************************************************** |
|
720 |
* Power Hal |
|
721 |
******************************************************************************/ |
|
722 |
||
723 |
EXPORT_C DPowerHal::DPowerHal() |
|
724 |
{ |
|
725 |
} |
|
726 |
||
727 |
EXPORT_C void DPowerHal::Register() |
|
728 |
{ |
|
729 |
__KTRACE_OPT2(KBOOT,KPOWER,Kern::Printf("DPowerHal::Register()")); |
|
730 |
PowerManager->iPowerHal = this; |
|
731 |
} |
|
732 |
||
733 |
/****************************************************************************** |
|
734 |
* Kern |
|
735 |
******************************************************************************/ |
|
736 |
||
737 |
/** Checks power status |
|
738 |
@return ETrue if status is good, EFalse otherwise |
|
739 |
*/ |
|
740 |
||
741 |
EXPORT_C TBool Kern::PowerGood() |
|
742 |
{ |
|
743 |
return K::PowerGood; |
|
744 |
} |
|
745 |
||
746 |
/****************************************************************************** |
|
747 |
* Exec Handlers |
|
748 |
******************************************************************************/ |
|
749 |
||
750 |
TInt ExecHandler::PowerEnableWakeupEvents(TPowerState aState) |
|
751 |
{ |
|
752 |
if(!Kern::CurrentThreadHasCapability(ECapabilityPowerMgmt,__PLATSEC_DIAGNOSTIC_STRING("Checked by Power::EnableWakeupEvents"))) |
|
753 |
K::UnlockedPlatformSecurityPanic(); |
|
754 |
NKern::ThreadEnterCS(); |
|
755 |
TInt r = PowerManager->EnableWakeupEvents(aState); |
|
756 |
NKern::ThreadLeaveCS(); |
|
757 |
return r; |
|
758 |
} |
|
759 |
||
760 |
void ExecHandler::PowerDisableWakeupEvents() |
|
761 |
{ |
|
762 |
if(!Kern::CurrentThreadHasCapability(ECapabilityPowerMgmt,__PLATSEC_DIAGNOSTIC_STRING("Checked by Power::DisableWakeupEvents"))) |
|
763 |
K::UnlockedPlatformSecurityPanic(); |
|
764 |
NKern::ThreadEnterCS(); |
|
765 |
PowerManager->DisableWakeupEvents(); |
|
766 |
NKern::ThreadLeaveCS(); |
|
767 |
} |
|
768 |
||
769 |
void ExecHandler::PowerRequestWakeupEventNotification(TRequestStatus* aStatus) |
|
770 |
{ |
|
771 |
if(!Kern::CurrentThreadHasCapability(ECapabilityPowerMgmt,__PLATSEC_DIAGNOSTIC_STRING("Checked by Power::RequestWakeupEventNotification"))) |
|
772 |
K::UnlockedPlatformSecurityPanic(); |
|
773 |
NKern::ThreadEnterCS(); |
|
774 |
PowerManager->RequestWakeupEventNotification(aStatus); |
|
775 |
NKern::ThreadLeaveCS(); |
|
776 |
} |
|
777 |
||
778 |
void ExecHandler::PowerCancelWakeupEventNotification() |
|
779 |
{ |
|
780 |
if(!Kern::CurrentThreadHasCapability(ECapabilityPowerMgmt,__PLATSEC_DIAGNOSTIC_STRING("Checked by Power::CancelWakeupEventNotification"))) |
|
781 |
K::UnlockedPlatformSecurityPanic(); |
|
782 |
PowerManager->CancelWakeupEventNotification(); |
|
783 |
} |
|
784 |
||
785 |
TInt ExecHandler::PowerDown() |
|
786 |
{ |
|
787 |
if(!Kern::CurrentThreadHasCapability(ECapabilityPowerMgmt,__PLATSEC_DIAGNOSTIC_STRING("Checked by Power::PowerDown"))) |
|
788 |
K::UnlockedPlatformSecurityPanic(); |
|
789 |
NKern::ThreadEnterCS(); |
|
790 |
TInt r = PowerManager->PowerDown(); |
|
791 |
NKern::ThreadLeaveCS(); |
|
792 |
return r; |
|
793 |
} |
|
794 |