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