]> git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOPMPowerStateQueue.cpp
1fd0f6b9019b7b9a3dff7c696a82ecad152ebd49
[apple/xnu.git] / iokit / Kernel / IOPMPowerStateQueue.cpp
1 /*
2 * Copyright (c) 2001-2002 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #include "IOPMPowerStateQueue.h"
25 #undef super
26 #define super IOEventSource
27 OSDefineMetaClassAndStructors(IOPMPowerStateQueue, IOEventSource);
28
29 IOPMPowerStateQueue *IOPMPowerStateQueue::PMPowerStateQueue(OSObject *inOwner)
30 {
31 IOPMPowerStateQueue *me = new IOPMPowerStateQueue;
32
33 if(me && !me->init(inOwner, 0) )
34 {
35 me->release();
36 return NULL;
37 }
38
39 return me;
40 }
41
42 bool IOPMPowerStateQueue::init(OSObject *owner, Action action)
43 {
44 if(!(super::init(owner, (IOEventSource::Action) action))) return false;
45
46 // Queue of powerstate changes
47 changes = NULL;
48
49 return true;
50 }
51
52
53 bool IOPMPowerStateQueue::unIdleOccurred(IOService *inTarget, unsigned long inState)
54 {
55 PowerChangeEntry *new_one = NULL;
56
57 new_one = (PowerChangeEntry *)IOMalloc(sizeof(PowerChangeEntry));
58 if(!new_one) return false;
59
60 new_one->actionType = IOPMPowerStateQueue::kUnIdle;
61 new_one->state = inState;
62 new_one->target = inTarget;
63
64 // Change to queue
65 OSEnqueueAtomic((void **)&changes, (void *)new_one, 0);
66
67 signalWorkAvailable();
68
69 return true;
70 }
71
72 // checkForWork() is called in a gated context
73 bool IOPMPowerStateQueue::checkForWork()
74 {
75 PowerChangeEntry *theNode;
76 int theState;
77 IOService *theTarget;
78 UInt16 theAction;
79
80 // Dequeue and process the state change request
81 if((theNode = (PowerChangeEntry *)OSDequeueAtomic((void **)&changes, 0)))
82 {
83 theState = theNode->state;
84 theTarget = theNode->target;
85 theAction = theNode->actionType;
86 IOFree((void *)theNode, sizeof(PowerChangeEntry));
87
88 switch (theAction)
89 {
90 case kUnIdle:
91 theTarget->command_received(theState, 0, 0, 0);
92 break;
93 }
94 }
95
96 // Return true if there's more work to be done
97 if(changes) return true;
98 else return false;
99 }