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