]> git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOPMPowerStateQueue.cpp
xnu-792.18.15.tar.gz
[apple/xnu.git] / iokit / Kernel / IOPMPowerStateQueue.cpp
1 /*
2 * Copyright (c) 2001-2002 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include "IOPMPowerStateQueue.h"
30 #include "IOKit/IOLocks.h"
31 #undef super
32 #define super IOEventSource
33 OSDefineMetaClassAndStructors(IOPMPowerStateQueue, IOEventSource);
34
35 #ifdef __i386__ /* ppc does this right and doesn't need these routines */
36 static
37 void * OSDequeueAtomic(void ** inList, SInt32 inOffset)
38 {
39 void * oldListHead;
40 void * newListHead;
41
42 do {
43 oldListHead = *inList;
44 if (oldListHead == NULL) {
45 break;
46 }
47
48 newListHead = *(void **) (((char *) oldListHead) + inOffset);
49 } while (! OSCompareAndSwap((UInt32)oldListHead,
50 (UInt32)newListHead, (UInt32 *)inList));
51 return oldListHead;
52 }
53
54 static
55 void OSEnqueueAtomic(void ** inList, void * inNewLink, SInt32 inOffset)
56 {
57 void * oldListHead;
58 void * newListHead = inNewLink;
59 void ** newLinkNextPtr = (void **) (((char *) inNewLink) + inOffset);
60
61 do {
62 oldListHead = *inList;
63 *newLinkNextPtr = oldListHead;
64 } while (! OSCompareAndSwap((UInt32)oldListHead, (UInt32)newListHead,
65 (UInt32 *)inList));
66 }
67 #endif /* __i386__ */
68
69
70 IOPMPowerStateQueue *IOPMPowerStateQueue::PMPowerStateQueue(OSObject *inOwner)
71 {
72 IOPMPowerStateQueue *me = new IOPMPowerStateQueue;
73
74 if(me && !me->init(inOwner, 0) )
75 {
76 me->release();
77 return NULL;
78 }
79
80 return me;
81 }
82
83 bool IOPMPowerStateQueue::init(OSObject *owner, Action action)
84 {
85 if(!(super::init(owner, (IOEventSource::Action) action))) return false;
86
87 // Queue of powerstate changes
88 changes = NULL;
89 #ifdef __i386__
90 if (!(tmpLock = IOLockAlloc())) panic("IOPMPowerStateQueue::init can't alloc lock");
91 #endif
92 return true;
93 }
94
95
96 bool IOPMPowerStateQueue::unIdleOccurred(IOService *inTarget, unsigned long inState)
97 {
98 PowerChangeEntry *new_one = NULL;
99
100 new_one = (PowerChangeEntry *)IOMalloc(sizeof(PowerChangeEntry));
101 if(!new_one) return false;
102
103 new_one->actionType = IOPMPowerStateQueue::kUnIdle;
104 new_one->state = inState;
105 new_one->target = inTarget;
106
107 // Change to queue
108 #ifdef __i386__
109 IOLockLock(tmpLock);
110 #endif
111 OSEnqueueAtomic((void **)&changes, (void *)new_one, 0);
112 #ifdef __i386__
113 IOLockUnlock(tmpLock);
114 #endif
115 signalWorkAvailable();
116
117 return true;
118 }
119
120 // checkForWork() is called in a gated context
121 bool IOPMPowerStateQueue::checkForWork()
122 {
123 PowerChangeEntry *theNode;
124 int theState;
125 IOService *theTarget;
126 UInt16 theAction;
127
128 // Dequeue and process the state change request
129 #ifdef __i386__
130 IOLockLock(tmpLock);
131 #endif
132 if((theNode = (PowerChangeEntry *)OSDequeueAtomic((void **)&changes, 0)))
133 {
134 #ifdef __i386__
135 IOLockUnlock(tmpLock);
136 #endif
137 theState = theNode->state;
138 theTarget = theNode->target;
139 theAction = theNode->actionType;
140 IOFree((void *)theNode, sizeof(PowerChangeEntry));
141
142 switch (theAction)
143 {
144 case kUnIdle:
145 theTarget->command_received((void *)theState, 0, 0, 0);
146 break;
147 }
148 }
149 #ifdef __i386__
150 else {
151 IOLockUnlock(tmpLock);
152 }
153 #endif
154 // Return true if there's more work to be done
155 if(changes) return true;
156 else return false;
157 }