]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
0a7de745 | 5 | * |
2d21ac55 A |
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. | |
0a7de745 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 17 | * |
2d21ac55 A |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
0a7de745 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b | 27 | */ |
1c79356b A |
28 | #include <IOKit/pwr_mgt/IOPM.h> |
29 | #include <IOKit/pwr_mgt/IOPMinformeeList.h> | |
30 | #include <IOKit/pwr_mgt/IOPMinformee.h> | |
31 | ||
32 | #define super OSObject | |
0a7de745 | 33 | OSDefineMetaClassAndStructors(IOPMinformeeList, OSObject) |
1c79356b A |
34 | |
35 | //********************************************************************************* | |
36 | // init | |
37 | // | |
38 | //********************************************************************************* | |
0a7de745 A |
39 | void |
40 | IOPMinformeeList::initialize( void ) | |
1c79356b | 41 | { |
0a7de745 A |
42 | firstItem = NULL; |
43 | length = 0; | |
1c79356b A |
44 | } |
45 | ||
0c530ab8 A |
46 | //****************************************************************************** |
47 | // getSharedRecursiveLock | |
48 | // | |
49 | //****************************************************************************** | |
0a7de745 A |
50 | IORecursiveLock * |
51 | IOPMinformeeList::getSharedRecursiveLock( void ) | |
0c530ab8 | 52 | { |
0a7de745 A |
53 | static IORecursiveLock *sharedListLock = NULL; |
54 | ||
55 | /* A running system could have 50-60+ instances of IOPMInformeeList. | |
56 | * They'll share this lock, since list insertion and removal is relatively | |
57 | * rare, and generally tied to major events like device discovery. | |
58 | * | |
59 | * getSharedRecursiveLock() is called from IOStartIOKit to initialize | |
60 | * the sharedListLock before any IOPMinformeeLists are instantiated. | |
61 | * | |
62 | * The IOPMinformeeList class will be around for the lifetime of the system, | |
63 | * we don't worry about freeing this lock. | |
64 | */ | |
65 | ||
66 | if (NULL == sharedListLock) { | |
67 | sharedListLock = IORecursiveLockAlloc(); | |
68 | } | |
69 | return sharedListLock; | |
0c530ab8 A |
70 | } |
71 | ||
0a7de745 | 72 | //********************************************************************************* |
2d21ac55 | 73 | // appendNewInformee |
0a7de745 A |
74 | // |
75 | //********************************************************************************* | |
76 | IOPMinformee * | |
77 | IOPMinformeeList::appendNewInformee( IOService * newObject ) | |
2d21ac55 | 78 | { |
0a7de745 A |
79 | IOPMinformee * newInformee; |
80 | ||
81 | if (!newObject) { | |
82 | return NULL; | |
83 | } | |
84 | ||
85 | newInformee = IOPMinformee::withObject(newObject); | |
86 | ||
87 | if (!newInformee) { | |
88 | return NULL; | |
89 | } | |
90 | ||
91 | if (IOPMNoErr == addToList(newInformee)) { | |
92 | return newInformee; | |
93 | } else { | |
94 | return NULL; | |
95 | } | |
2d21ac55 A |
96 | } |
97 | ||
98 | ||
1c79356b A |
99 | //********************************************************************************* |
100 | // addToList | |
2d21ac55 A |
101 | // *OBSOLETE* do not call from outside of this file. |
102 | // Try appendNewInformee() instead | |
1c79356b | 103 | //********************************************************************************* |
0a7de745 A |
104 | IOReturn |
105 | IOPMinformeeList::addToList( IOPMinformee * newInformee ) | |
1c79356b | 106 | { |
0a7de745 A |
107 | IOPMinformee * nextInformee; |
108 | IORecursiveLock *listLock = getSharedRecursiveLock(); | |
109 | ||
110 | if (!listLock) { | |
111 | return kIOReturnError; | |
112 | } | |
113 | ||
114 | IORecursiveLockLock(listLock); | |
115 | nextInformee = firstItem; | |
116 | ||
117 | // Is new object already in the list? | |
118 | while (nextInformee != NULL) { | |
119 | if (nextInformee->whatObject == newInformee->whatObject) { | |
120 | // object is present; just exit | |
121 | goto unlock_and_exit; | |
122 | } | |
123 | nextInformee = nextInList(nextInformee); | |
124 | } | |
125 | ||
126 | // add it to the front of the list | |
127 | newInformee->nextInList = firstItem; | |
128 | firstItem = newInformee; | |
129 | length++; | |
0c530ab8 A |
130 | |
131 | unlock_and_exit: | |
0a7de745 A |
132 | IORecursiveLockUnlock(listLock); |
133 | return IOPMNoErr; | |
0c530ab8 A |
134 | } |
135 | ||
136 | ||
137 | //********************************************************************************* | |
138 | // removeFromList | |
139 | // | |
140 | // Find the item in the list, unlink it, and free it. | |
141 | //********************************************************************************* | |
142 | ||
0a7de745 A |
143 | IOReturn |
144 | IOPMinformeeList::removeFromList( IOService * theItem ) | |
0c530ab8 | 145 | { |
0a7de745 A |
146 | IOPMinformee * item = firstItem; |
147 | IOPMinformee * temp; | |
148 | IORecursiveLock *listLock = getSharedRecursiveLock(); | |
149 | ||
150 | if (NULL == item) { | |
151 | return IOPMNoErr; | |
152 | } | |
153 | if (!listLock) { | |
154 | return kIOReturnError; | |
155 | } | |
156 | ||
157 | IORecursiveLockLock( listLock ); | |
158 | ||
159 | if (item->whatObject == theItem) { | |
160 | firstItem = item->nextInList; | |
161 | length--; | |
162 | item->release(); | |
163 | goto unlock_and_exit; | |
164 | } | |
165 | ||
166 | while (item->nextInList != NULL) { | |
167 | if (item->nextInList->whatObject == theItem) { | |
168 | temp = item->nextInList; | |
169 | item->nextInList = temp->nextInList; | |
170 | length--; | |
171 | temp->release(); | |
172 | goto unlock_and_exit; | |
173 | } | |
174 | item = item->nextInList; | |
175 | } | |
0c530ab8 A |
176 | |
177 | unlock_and_exit: | |
0a7de745 A |
178 | IORecursiveLockUnlock(listLock); |
179 | return IOPMNoErr; | |
1c79356b A |
180 | } |
181 | ||
182 | ||
183 | //********************************************************************************* | |
184 | // firstInList | |
185 | // | |
186 | //********************************************************************************* | |
187 | ||
0a7de745 A |
188 | IOPMinformee * |
189 | IOPMinformeeList::firstInList( void ) | |
1c79356b | 190 | { |
0a7de745 | 191 | return firstItem; |
1c79356b A |
192 | } |
193 | ||
194 | //********************************************************************************* | |
195 | // nextInList | |
196 | // | |
197 | //********************************************************************************* | |
198 | ||
0a7de745 A |
199 | IOPMinformee * |
200 | IOPMinformeeList::nextInList( IOPMinformee * currentItem ) | |
1c79356b | 201 | { |
0a7de745 A |
202 | if (currentItem != NULL) { |
203 | return currentItem->nextInList; | |
204 | } | |
205 | return NULL; | |
1c79356b A |
206 | } |
207 | ||
208 | //********************************************************************************* | |
209 | // numberOfItems | |
210 | // | |
211 | //********************************************************************************* | |
212 | ||
0a7de745 A |
213 | unsigned long |
214 | IOPMinformeeList::numberOfItems( void ) | |
1c79356b | 215 | { |
0a7de745 | 216 | return length; |
1c79356b A |
217 | } |
218 | ||
219 | //********************************************************************************* | |
220 | // findItem | |
221 | // | |
222 | // Look through the list for the one which points to the object identified | |
223 | // by the parameter. Return a pointer to the list item or NULL. | |
224 | //********************************************************************************* | |
225 | ||
0a7de745 A |
226 | IOPMinformee * |
227 | IOPMinformeeList::findItem( IOService * driverOrChild ) | |
1c79356b | 228 | { |
0a7de745 A |
229 | IOPMinformee * nextObject; |
230 | ||
231 | nextObject = firstInList(); | |
232 | while (nextObject != NULL) { | |
233 | if (nextObject->whatObject == driverOrChild) { | |
234 | return nextObject; | |
235 | } | |
236 | nextObject = nextInList(nextObject); | |
237 | } | |
238 | return NULL; | |
1c79356b A |
239 | } |
240 | ||
241 | ||
1c79356b A |
242 | |
243 | //********************************************************************************* | |
244 | // free | |
245 | // | |
246 | // Free all items in the list, and then free the list itself | |
247 | //********************************************************************************* | |
248 | ||
0a7de745 A |
249 | void |
250 | IOPMinformeeList::free(void ) | |
1c79356b | 251 | { |
0a7de745 A |
252 | IOPMinformee * next = firstItem; |
253 | ||
254 | while (next != NULL) { | |
255 | firstItem = next->nextInList; | |
256 | length--; | |
257 | next->release(); | |
258 | next = firstItem; | |
259 | } | |
260 | super::free(); | |
1c79356b | 261 | } |