2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 #include <IOKit/system.h>
25 #include <IOKit/IOService.h>
26 #include <libkern/c++/OSContainers.h>
27 #include <libkern/c++/OSUnserialize.h>
28 #include <IOKit/IOCatalogue.h>
29 #include <IOKit/IODeviceMemory.h>
30 #include <IOKit/IOInterrupts.h>
31 #include <IOKit/IOInterruptController.h>
32 #include <IOKit/IOPlatformExpert.h>
33 #include <IOKit/IOMessage.h>
34 #include <IOKit/IOLib.h>
35 #include <IOKit/IOKitKeysPrivate.h>
36 #include <IOKit/IOBSD.h>
37 #include <IOKit/IOUserClient.h>
38 #include <IOKit/IOWorkLoop.h>
39 #include <mach/sync_policy.h>
40 #include <IOKit/assert.h>
41 #include <sys/errno.h>
46 #include "IOServicePrivate.h"
48 // take lockForArbitration before LOCKNOTIFY
50 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
52 #define super IORegistryEntry
54 OSDefineMetaClassAndStructors(IOService
, IORegistryEntry
)
56 OSDefineMetaClassAndStructors(_IOServiceNotifier
, IONotifier
)
58 OSDefineMetaClassAndStructors(_IOServiceInterestNotifier
, IONotifier
)
60 OSDefineMetaClassAndStructors(_IOConfigThread
, OSObject
)
62 OSDefineMetaClassAndStructors(_IOServiceJob
, OSObject
)
64 OSDefineMetaClassAndStructors(IOResources
, IOService
)
66 OSDefineMetaClassAndStructors(_IOOpenServiceIterator
, OSIterator
)
68 OSDefineMetaClassAndAbstractStructors(IONotifier
, OSObject
)
70 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
72 static IOPlatformExpert
* gIOPlatform
;
73 static class IOPMrootDomain
* gIOPMRootDomain
;
74 const IORegistryPlane
* gIOServicePlane
;
75 const IORegistryPlane
* gIOPowerPlane
;
76 const OSSymbol
* gIODeviceMemoryKey
;
77 const OSSymbol
* gIOInterruptControllersKey
;
78 const OSSymbol
* gIOInterruptSpecifiersKey
;
80 const OSSymbol
* gIOResourcesKey
;
81 const OSSymbol
* gIOResourceMatchKey
;
82 const OSSymbol
* gIOProviderClassKey
;
83 const OSSymbol
* gIONameMatchKey
;
84 const OSSymbol
* gIONameMatchedKey
;
85 const OSSymbol
* gIOPropertyMatchKey
;
86 const OSSymbol
* gIOLocationMatchKey
;
87 const OSSymbol
* gIOParentMatchKey
;
88 const OSSymbol
* gIOPathMatchKey
;
89 const OSSymbol
* gIOMatchCategoryKey
;
90 const OSSymbol
* gIODefaultMatchCategoryKey
;
91 const OSSymbol
* gIOMatchedServiceCountKey
;
93 const OSSymbol
* gIOUserClientClassKey
;
94 const OSSymbol
* gIOKitDebugKey
;
96 const OSSymbol
* gIOCommandPoolSizeKey
;
98 const OSSymbol
* gIOConsoleUsersKey
;
99 const OSSymbol
* gIOConsoleSessionUIDKey
;
100 const OSSymbol
* gIOConsoleUsersSeedKey
;
102 static int gIOResourceGenerationCount
;
104 const OSSymbol
* gIOServiceKey
;
105 const OSSymbol
* gIOPublishNotification
;
106 const OSSymbol
* gIOFirstPublishNotification
;
107 const OSSymbol
* gIOMatchedNotification
;
108 const OSSymbol
* gIOFirstMatchNotification
;
109 const OSSymbol
* gIOTerminatedNotification
;
111 const OSSymbol
* gIOGeneralInterest
;
112 const OSSymbol
* gIOBusyInterest
;
113 const OSSymbol
* gIOAppPowerStateInterest
;
114 const OSSymbol
* gIOPriorityPowerStateInterest
;
116 static OSDictionary
* gNotifications
;
117 static IORecursiveLock
* gNotificationLock
;
119 static IOService
* gIOResources
;
120 static IOService
* gIOServiceRoot
;
122 static OSOrderedSet
* gJobs
;
123 static semaphore_port_t gJobsSemaphore
;
124 static IOLock
* gJobsLock
;
125 static int gOutstandingJobs
;
126 static int gNumConfigThreads
;
127 static int gNumWaitingThreads
;
128 static IOLock
* gIOServiceBusyLock
;
130 static thread_t gIOTerminateThread
;
131 static UInt32 gIOTerminateWork
;
132 static OSArray
* gIOTerminatePhase2List
;
133 static OSArray
* gIOStopList
;
134 static OSArray
* gIOStopProviderList
;
135 static OSArray
* gIOFinalizeList
;
137 static SInt32 gIOConsoleUsersSeed
;
138 static OSData
* gIOConsoleUsersSeedValue
;
140 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
142 #define LOCKREADNOTIFY() \
143 IORecursiveLockLock( gNotificationLock )
144 #define LOCKWRITENOTIFY() \
145 IORecursiveLockLock( gNotificationLock )
146 #define LOCKWRITE2READNOTIFY()
147 #define UNLOCKNOTIFY() \
148 IORecursiveLockUnlock( gNotificationLock )
149 #define SLEEPNOTIFY(event) \
150 IORecursiveLockSleep( gNotificationLock, (void *)(event), THREAD_UNINT )
151 #define WAKEUPNOTIFY(event) \
152 IORecursiveLockWakeup( gNotificationLock, (void *)(event), /* wake one */ false )
154 #define randomDelay() \
155 int del = read_processor_clock(); \
156 del = (((int)IOThreadSelf()) ^ del ^ (del >> 10)) & 0x3ff; \
159 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
161 struct ArbitrationLockQueueElement
{
170 static queue_head_t gArbitrationLockQueueActive
;
171 static queue_head_t gArbitrationLockQueueWaiting
;
172 static queue_head_t gArbitrationLockQueueFree
;
173 static IOLock
* gArbitrationLockQueueLock
;
175 bool IOService::isInactive( void ) const
176 { return( 0 != (kIOServiceInactiveState
& getState())); }
178 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
180 void IOService::initialize( void )
184 gIOServicePlane
= IORegistryEntry::makePlane( kIOServicePlane
);
185 gIOPowerPlane
= IORegistryEntry::makePlane( kIOPowerPlane
);
187 gIOProviderClassKey
= OSSymbol::withCStringNoCopy( kIOProviderClassKey
);
188 gIONameMatchKey
= OSSymbol::withCStringNoCopy( kIONameMatchKey
);
189 gIONameMatchedKey
= OSSymbol::withCStringNoCopy( kIONameMatchedKey
);
190 gIOPropertyMatchKey
= OSSymbol::withCStringNoCopy( kIOPropertyMatchKey
);
191 gIOPathMatchKey
= OSSymbol::withCStringNoCopy( kIOPathMatchKey
);
192 gIOLocationMatchKey
= OSSymbol::withCStringNoCopy( kIOLocationMatchKey
);
193 gIOParentMatchKey
= OSSymbol::withCStringNoCopy( kIOParentMatchKey
);
195 gIOMatchCategoryKey
= OSSymbol::withCStringNoCopy( kIOMatchCategoryKey
);
196 gIODefaultMatchCategoryKey
= OSSymbol::withCStringNoCopy(
197 kIODefaultMatchCategoryKey
);
198 gIOMatchedServiceCountKey
= OSSymbol::withCStringNoCopy(
199 kIOMatchedServiceCountKey
);
201 gIOUserClientClassKey
= OSSymbol::withCStringNoCopy( kIOUserClientClassKey
);
203 gIOResourcesKey
= OSSymbol::withCStringNoCopy( kIOResourcesClass
);
204 gIOResourceMatchKey
= OSSymbol::withCStringNoCopy( kIOResourceMatchKey
);
206 gIODeviceMemoryKey
= OSSymbol::withCStringNoCopy( "IODeviceMemory" );
207 gIOInterruptControllersKey
208 = OSSymbol::withCStringNoCopy("IOInterruptControllers");
209 gIOInterruptSpecifiersKey
210 = OSSymbol::withCStringNoCopy("IOInterruptSpecifiers");
212 gIOKitDebugKey
= OSSymbol::withCStringNoCopy( kIOKitDebugKey
);
214 gIOCommandPoolSizeKey
= OSSymbol::withCStringNoCopy( kIOCommandPoolSizeKey
);
216 gIOGeneralInterest
= OSSymbol::withCStringNoCopy( kIOGeneralInterest
);
217 gIOBusyInterest
= OSSymbol::withCStringNoCopy( kIOBusyInterest
);
218 gIOAppPowerStateInterest
= OSSymbol::withCStringNoCopy( kIOAppPowerStateInterest
);
219 gIOPriorityPowerStateInterest
= OSSymbol::withCStringNoCopy( kIOPriorityPowerStateInterest
);
221 gNotifications
= OSDictionary::withCapacity( 1 );
222 gIOPublishNotification
= OSSymbol::withCStringNoCopy(
223 kIOPublishNotification
);
224 gIOFirstPublishNotification
= OSSymbol::withCStringNoCopy(
225 kIOFirstPublishNotification
);
226 gIOMatchedNotification
= OSSymbol::withCStringNoCopy(
227 kIOMatchedNotification
);
228 gIOFirstMatchNotification
= OSSymbol::withCStringNoCopy(
229 kIOFirstMatchNotification
);
230 gIOTerminatedNotification
= OSSymbol::withCStringNoCopy(
231 kIOTerminatedNotification
);
232 gIOServiceKey
= OSSymbol::withCStringNoCopy( kIOServiceClass
);
234 gIOConsoleUsersKey
= OSSymbol::withCStringNoCopy( kIOConsoleUsersKey
);
235 gIOConsoleSessionUIDKey
= OSSymbol::withCStringNoCopy( kIOConsoleSessionUIDKey
);
236 gIOConsoleUsersSeedKey
= OSSymbol::withCStringNoCopy( kIOConsoleUsersSeedKey
);
237 gIOConsoleUsersSeedValue
= OSData::withBytesNoCopy(&gIOConsoleUsersSeed
, sizeof(gIOConsoleUsersSeed
));
239 gNotificationLock
= IORecursiveLockAlloc();
241 assert( gIOServicePlane
&& gIODeviceMemoryKey
242 && gIOInterruptControllersKey
&& gIOInterruptSpecifiersKey
243 && gIOResourcesKey
&& gNotifications
&& gNotificationLock
244 && gIOProviderClassKey
&& gIONameMatchKey
&& gIONameMatchedKey
245 && gIOMatchCategoryKey
&& gIODefaultMatchCategoryKey
246 && gIOPublishNotification
&& gIOMatchedNotification
247 && gIOTerminatedNotification
&& gIOServiceKey
248 && gIOConsoleUsersKey
&& gIOConsoleSessionUIDKey
249 && gIOConsoleUsersSeedKey
&& gIOConsoleUsersSeedValue
);
251 gJobsLock
= IOLockAlloc();
252 gJobs
= OSOrderedSet::withCapacity( 10 );
254 gIOServiceBusyLock
= IOLockAlloc();
256 err
= semaphore_create(kernel_task
, &gJobsSemaphore
, SYNC_POLICY_FIFO
, 0);
258 assert( gIOServiceBusyLock
&& gJobs
&& gJobsLock
&& (err
== KERN_SUCCESS
) );
260 gIOResources
= IOResources::resources();
261 assert( gIOResources
);
263 gArbitrationLockQueueLock
= IOLockAlloc();
264 queue_init(&gArbitrationLockQueueActive
);
265 queue_init(&gArbitrationLockQueueWaiting
);
266 queue_init(&gArbitrationLockQueueFree
);
268 assert( gArbitrationLockQueueLock
);
270 gIOTerminatePhase2List
= OSArray::withCapacity( 2 );
271 gIOStopList
= OSArray::withCapacity( 16 );
272 gIOStopProviderList
= OSArray::withCapacity( 16 );
273 gIOFinalizeList
= OSArray::withCapacity( 16 );
274 assert( gIOTerminatePhase2List
&& gIOStopList
&& gIOStopProviderList
&& gIOFinalizeList
);
277 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
280 static UInt64
getDebugFlags( OSDictionary
* props
)
282 OSNumber
* debugProp
;
285 debugProp
= OSDynamicCast( OSNumber
,
286 props
->getObject( gIOKitDebugKey
));
288 debugFlags
= debugProp
->unsigned64BitValue();
290 debugFlags
= gIOKitDebug
;
292 return( debugFlags
);
296 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
298 // Probe a matched service and return an instance to be started.
299 // The default score is from the property table, & may be altered
300 // during probe to change the start order.
302 IOService
* IOService::probe( IOService
* provider
,
308 bool IOService::start( IOService
* provider
)
313 void IOService::stop( IOService
* provider
)
317 void IOService::free( void )
319 if( getPropertyTable())
320 unregisterAllInterest();
326 * Attach in service plane
328 bool IOService::attach( IOService
* provider
)
334 if( gIOKitDebug
& kIOLogAttach
)
335 LOG( "%s::attach(%s)\n", getName(),
336 provider
->getName());
338 provider
->lockForArbitration();
339 if( provider
->__state
[0] & kIOServiceInactiveState
)
342 ok
= attachToParent( provider
, gIOServicePlane
);
343 provider
->unlockForArbitration();
346 gIOServiceRoot
= this;
347 ok
= attachToParent( getRegistryRoot(), gIOServicePlane
);
353 IOService
* IOService::getServiceRoot( void )
355 return( gIOServiceRoot
);
358 void IOService::detach( IOService
* provider
)
360 IOService
* newProvider
= 0;
364 if( gIOKitDebug
& kIOLogAttach
)
365 LOG("%s::detach(%s)\n", getName(), provider
->getName());
367 lockForArbitration();
369 adjParent
= ((busy
= (__state
[1] & kIOServiceBusyStateMask
))
370 && (provider
== getProvider()));
372 detachFromParent( provider
, gIOServicePlane
);
375 newProvider
= getProvider();
376 if( busy
&& (__state
[1] & kIOServiceTermPhase3State
) && (0 == newProvider
))
377 _adjustBusy( -busy
);
380 unlockForArbitration();
383 newProvider
->lockForArbitration();
384 newProvider
->_adjustBusy(1);
385 newProvider
->unlockForArbitration();
388 // check for last client detach from a terminated service
389 if( provider
->lockForArbitration( true )) {
391 provider
->_adjustBusy( -1 );
392 if( (provider
->__state
[1] & kIOServiceTermPhase3State
)
393 && (0 == provider
->getClient())) {
394 provider
->scheduleFinalize();
396 provider
->unlockForArbitration();
401 * Register instance - publish it for matching
404 void IOService::registerService( IOOptionBits options
)
410 enum { kMaxPathLen
= 256 };
411 enum { kMaxChars
= 63 };
413 IORegistryEntry
* parent
= this;
414 IORegistryEntry
* root
= getRegistryRoot();
415 while( parent
&& (parent
!= root
))
416 parent
= parent
->getParentEntry( gIOServicePlane
);
418 if( parent
!= root
) {
419 IOLog("%s: not registry member at registerService()\n", getName());
423 // Allow the Platform Expert to adjust this node.
424 if( gIOPlatform
&& (!gIOPlatform
->platformAdjustService(this)))
427 if( (this != gIOResources
)
428 && (kIOLogRegister
& gIOKitDebug
)) {
430 pathBuf
= (char *) IOMalloc( kMaxPathLen
);
432 IOLog( "Registering: " );
435 if( pathBuf
&& getPath( pathBuf
, &len
, gIOServicePlane
)) {
438 if( len
> kMaxChars
) {
442 if( (skip
= strchr( path
, '/')))
448 IOLog( "%s\n", path
);
451 IOFree( pathBuf
, kMaxPathLen
);
454 startMatching( options
);
457 void IOService::startMatching( IOOptionBits options
)
459 IOService
* provider
;
462 bool needWake
= false;
467 lockForArbitration();
469 sync
= (options
& kIOServiceSynchronous
)
470 || ((provider
= getProvider())
471 && (provider
->__state
[1] & kIOServiceSynchronousState
));
474 needConfig
= (0 == (__state
[1] & (kIOServiceNeedConfigState
| kIOServiceConfigState
)))
475 && (0 == (__state
[0] & kIOServiceInactiveState
));
477 __state
[1] |= kIOServiceNeedConfigState
;
479 // __state[0] &= ~kIOServiceInactiveState;
481 // if( sync) LOG("OSKernelStackRemaining = %08x @ %s\n",
482 // OSKernelStackRemaining(), getName());
485 prevBusy
= _adjustBusy( 1 );
486 needWake
= (0 != (kIOServiceSyncPubState
& __state
[1]));
490 __state
[1] |= kIOServiceSynchronousState
;
492 __state
[1] &= ~kIOServiceSynchronousState
;
494 unlockForArbitration();
499 IOLockLock( gIOServiceBusyLock
);
500 thread_wakeup( (event_t
) this/*&__state[1]*/ );
501 IOLockUnlock( gIOServiceBusyLock
);
503 } else if( !sync
|| (kIOServiceAsynchronous
& options
)) {
505 ok
= (0 != _IOServiceJob::startJob( this, kMatchNubJob
, options
));
509 if( (__state
[1] & kIOServiceNeedConfigState
))
510 doServiceMatch( options
);
512 lockForArbitration();
513 IOLockLock( gIOServiceBusyLock
);
515 waitAgain
= (prevBusy
!= (__state
[1] & kIOServiceBusyStateMask
));
517 __state
[1] |= kIOServiceSyncPubState
| kIOServiceBusyWaiterState
;
519 __state
[1] &= ~kIOServiceSyncPubState
;
521 unlockForArbitration();
524 assert_wait( (event_t
) this/*&__state[1]*/, THREAD_UNINT
);
526 IOLockUnlock( gIOServiceBusyLock
);
528 thread_block(THREAD_CONTINUE_NULL
);
530 } while( waitAgain
);
534 IOReturn
IOService::catalogNewDrivers( OSOrderedSet
* newTables
)
536 OSDictionary
* table
;
545 while( (table
= (OSDictionary
*) newTables
->getFirstObject())) {
548 iter
= (OSIterator
*) getExistingServices( table
,
549 kIOServiceRegisteredState
);
552 while( (service
= (IOService
*) iter
->getNextObject())) {
553 service
->startMatching(kIOServiceAsynchronous
);
561 if( getDebugFlags( table
) & kIOLogMatch
)
562 LOG("Matching service count = %ld\n", count
);
564 newTables
->removeObject(table
);
567 newTables
->release();
569 return( kIOReturnSuccess
);
572 _IOServiceJob
* _IOServiceJob::startJob( IOService
* nub
, int type
,
573 IOOptionBits options
)
577 job
= new _IOServiceJob
;
578 if( job
&& !job
->init()) {
586 job
->options
= options
;
587 nub
->retain(); // thread will release()
595 * Called on a registered service to see if it matches
599 bool IOService::matchPropertyTable( OSDictionary
* table
, SInt32
* score
)
601 return( matchPropertyTable(table
) );
604 bool IOService::matchPropertyTable( OSDictionary
* table
)
610 * Called on a matched service to allocate resources
611 * before first driver is attached.
614 IOReturn
IOService::getResources( void )
616 return( kIOReturnSuccess
);
620 * Client/provider accessors
623 IOService
* IOService::getProvider( void ) const
625 IOService
* self
= (IOService
*) this;
630 generation
= getGenerationCount();
631 if( __providerGeneration
== generation
)
634 parent
= (IOService
*) getParentEntry( gIOServicePlane
);
635 if( parent
== IORegistryEntry::getRegistryRoot())
636 /* root is not an IOService */
639 self
->__provider
= parent
;
640 // save the count before getParentEntry()
641 self
->__providerGeneration
= generation
;
646 IOWorkLoop
* IOService::getWorkLoop() const
648 IOService
*provider
= getProvider();
651 return provider
->getWorkLoop();
656 OSIterator
* IOService::getProviderIterator( void ) const
658 return( getParentIterator( gIOServicePlane
));
661 IOService
* IOService::getClient( void ) const
663 return( (IOService
*) getChildEntry( gIOServicePlane
));
666 OSIterator
* IOService::getClientIterator( void ) const
668 return( getChildIterator( gIOServicePlane
));
671 OSIterator
* _IOOpenServiceIterator::iterator( OSIterator
* _iter
,
672 const IOService
* client
,
673 const IOService
* provider
)
675 _IOOpenServiceIterator
* inst
;
680 inst
= new _IOOpenServiceIterator
;
682 if( inst
&& !inst
->init()) {
688 inst
->client
= client
;
689 inst
->provider
= provider
;
695 void _IOOpenServiceIterator::free()
699 last
->unlockForArbitration();
703 OSObject
* _IOOpenServiceIterator::getNextObject()
708 last
->unlockForArbitration();
710 while( (next
= (IOService
*) iter
->getNextObject())) {
712 next
->lockForArbitration();
713 if( (client
&& (next
->isOpen( client
)))
714 || (provider
&& (provider
->isOpen( next
))) )
716 next
->unlockForArbitration();
724 bool _IOOpenServiceIterator::isValid()
726 return( iter
->isValid() );
729 void _IOOpenServiceIterator::reset()
732 last
->unlockForArbitration();
738 OSIterator
* IOService::getOpenProviderIterator( void ) const
740 return( _IOOpenServiceIterator::iterator( getProviderIterator(), this, 0 ));
743 OSIterator
* IOService::getOpenClientIterator( void ) const
745 return( _IOOpenServiceIterator::iterator( getClientIterator(), 0, this ));
749 IOReturn
IOService::callPlatformFunction( const OSSymbol
* functionName
,
750 bool waitForFunction
,
751 void *param1
, void *param2
,
752 void *param3
, void *param4
)
754 IOReturn result
= kIOReturnUnsupported
;
755 IOService
*provider
= getProvider();
758 result
= provider
->callPlatformFunction(functionName
, waitForFunction
,
759 param1
, param2
, param3
, param4
);
765 IOReturn
IOService::callPlatformFunction( const char * functionName
,
766 bool waitForFunction
,
767 void *param1
, void *param2
,
768 void *param3
, void *param4
)
770 IOReturn result
= kIOReturnNoMemory
;
771 const OSSymbol
*functionSymbol
= OSSymbol::withCString(functionName
);
773 if (functionSymbol
!= 0) {
774 result
= callPlatformFunction(functionSymbol
, waitForFunction
,
775 param1
, param2
, param3
, param4
);
776 functionSymbol
->release();
784 * Accessors for global services
787 IOPlatformExpert
* IOService::getPlatform( void )
789 return( gIOPlatform
);
792 class IOPMrootDomain
* IOService::getPMRootDomain( void )
794 return( gIOPMRootDomain
);
797 IOService
* IOService::getResourceService( void )
799 return( gIOResources
);
802 void IOService::setPlatform( IOPlatformExpert
* platform
)
804 gIOPlatform
= platform
;
805 gIOResources
->attachToParent( gIOServiceRoot
, gIOServicePlane
);
808 void IOService::setPMRootDomain( class IOPMrootDomain
* rootDomain
)
810 gIOPMRootDomain
= rootDomain
;
811 publishResource("IOKit");
818 bool IOService::lockForArbitration( bool isSuccessRequired
)
822 ArbitrationLockQueueElement
* element
;
823 ArbitrationLockQueueElement
* active
;
824 ArbitrationLockQueueElement
* waiting
;
826 enum { kPutOnFreeQueue
, kPutOnActiveQueue
, kPutOnWaitingQueue
} action
;
828 // lock global access
829 IOTakeLock( gArbitrationLockQueueLock
);
831 // obtain an unused queue element
832 if( !queue_empty( &gArbitrationLockQueueFree
)) {
833 queue_remove_first( &gArbitrationLockQueueFree
,
835 ArbitrationLockQueueElement
*,
838 element
= IONew( ArbitrationLockQueueElement
, 1 );
842 // prepare the queue element
843 element
->thread
= IOThreadSelf();
844 element
->service
= this;
846 element
->required
= isSuccessRequired
;
847 element
->aborted
= false;
849 // determine whether this object is already locked (ie. on active queue)
851 queue_iterate( &gArbitrationLockQueueActive
,
853 ArbitrationLockQueueElement
*,
856 if( active
->service
== element
->service
) {
862 if( found
) { // this object is already locked
864 // determine whether it is the same or a different thread trying to lock
865 if( active
->thread
!= element
->thread
) { // it is a different thread
867 ArbitrationLockQueueElement
* victim
= 0;
869 // before placing this new thread on the waiting queue, we look for
870 // a deadlock cycle...
873 // determine whether the active thread holding the object we
874 // want is waiting for another object to be unlocked
876 queue_iterate( &gArbitrationLockQueueWaiting
,
878 ArbitrationLockQueueElement
*,
881 if( waiting
->thread
== active
->thread
) {
882 assert( false == waiting
->aborted
);
888 if( found
) { // yes, active thread waiting for another object
890 // this may be a candidate for rejection if the required
891 // flag is not set, should we detect a deadlock later on
892 if( false == waiting
->required
)
895 // find the thread that is holding this other object, that
896 // is blocking the active thread from proceeding (fun :-)
898 queue_iterate( &gArbitrationLockQueueActive
,
899 active
, // (reuse active queue element)
900 ArbitrationLockQueueElement
*,
903 if( active
->service
== waiting
->service
) {
909 // someone must be holding it or it wouldn't be waiting
912 if( active
->thread
== element
->thread
) {
914 // doh, it's waiting for the thread that originated
915 // this whole lock (ie. current thread) -> deadlock
916 if( false == element
->required
) { // willing to fail?
918 // the originating thread doesn't have the required
919 // flag, so it can fail
920 success
= false; // (fail originating lock request)
921 break; // (out of while)
923 } else { // originating thread is not willing to fail
925 // see if we came across a waiting thread that did
926 // not have the 'required' flag set: we'll fail it
929 // we do have a willing victim, fail it's lock
930 victim
->aborted
= true;
932 // take the victim off the waiting queue
933 queue_remove( &gArbitrationLockQueueWaiting
,
935 ArbitrationLockQueueElement
*,
939 IOLockWakeup( gArbitrationLockQueueLock
,
941 /* one thread */ true );
943 // allow this thread to proceed (ie. wait)
944 success
= true; // (put request on wait queue)
945 break; // (out of while)
948 // all the waiting threads we came across in
949 // finding this loop had the 'required' flag
950 // set, so we've got a deadlock we can't avoid
951 panic("I/O Kit: Unrecoverable deadlock.");
955 // repeat while loop, redefining active thread to be the
956 // thread holding "this other object" (see above), and
957 // looking for threads waiting on it; note the active
958 // variable points to "this other object" already... so
959 // there nothing to do in this else clause.
961 } else { // no, active thread is not waiting for another object
963 success
= true; // (put request on wait queue)
964 break; // (out of while)
968 if( success
) { // put the request on the waiting queue?
969 kern_return_t wait_result
;
971 // place this thread on the waiting queue and put it to sleep;
972 // we place it at the tail of the queue...
973 queue_enter( &gArbitrationLockQueueWaiting
,
975 ArbitrationLockQueueElement
*,
978 // declare that this thread will wait for a given event
979 restart_sleep
: wait_result
= assert_wait( element
,
980 element
->required
? THREAD_UNINT
981 : THREAD_INTERRUPTIBLE
);
983 // unlock global access
984 IOUnlock( gArbitrationLockQueueLock
);
986 // put thread to sleep, waiting for our event to fire...
987 if (wait_result
== THREAD_WAITING
)
988 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
991 // ...and we've been woken up; we might be in one of two states:
992 // (a) we've been aborted and our queue element is not on
993 // any of the three queues, but is floating around
994 // (b) we're allowed to proceed with the lock and we have
995 // already been moved from the waiting queue to the
997 // ...plus a 3rd state, should the thread have been interrupted:
998 // (c) we're still on the waiting queue
1000 // determine whether we were interrupted out of our sleep
1001 if( THREAD_INTERRUPTED
== wait_result
) {
1003 // re-lock global access
1004 IOTakeLock( gArbitrationLockQueueLock
);
1006 // determine whether we're still on the waiting queue
1008 queue_iterate( &gArbitrationLockQueueWaiting
,
1009 waiting
, // (reuse waiting queue element)
1010 ArbitrationLockQueueElement
*,
1013 if( waiting
== element
) {
1019 if( found
) { // yes, we're still on the waiting queue
1021 // determine whether we're willing to fail
1022 if( false == element
->required
) {
1024 // mark us as aborted
1025 element
->aborted
= true;
1027 // take us off the waiting queue
1028 queue_remove( &gArbitrationLockQueueWaiting
,
1030 ArbitrationLockQueueElement
*,
1032 } else { // we are not willing to fail
1034 // ignore interruption, go back to sleep
1039 // unlock global access
1040 IOUnlock( gArbitrationLockQueueLock
);
1042 // proceed as though this were a normal wake up
1043 wait_result
= THREAD_AWAKENED
;
1046 assert( THREAD_AWAKENED
== wait_result
);
1048 // determine whether we've been aborted while we were asleep
1049 if( element
->aborted
) {
1050 assert( false == element
->required
);
1052 // re-lock global access
1053 IOTakeLock( gArbitrationLockQueueLock
);
1055 action
= kPutOnFreeQueue
;
1057 } else { // we weren't aborted, so we must be ready to go :-)
1059 // we've already been moved from waiting to active queue
1063 } else { // the lock request is to be failed
1065 // return unused queue element to queue
1066 action
= kPutOnFreeQueue
;
1068 } else { // it is the same thread, recursive access is allowed
1070 // add one level of recursion
1073 // return unused queue element to queue
1074 action
= kPutOnFreeQueue
;
1077 } else { // this object is not already locked, so let this thread through
1078 action
= kPutOnActiveQueue
;
1082 // put the new element on a queue
1083 if( kPutOnActiveQueue
== action
) {
1084 queue_enter( &gArbitrationLockQueueActive
,
1086 ArbitrationLockQueueElement
*,
1088 } else if( kPutOnFreeQueue
== action
) {
1089 queue_enter( &gArbitrationLockQueueFree
,
1091 ArbitrationLockQueueElement
*,
1094 assert( 0 ); // kPutOnWaitingQueue never occurs, handled specially above
1097 // unlock global access
1098 IOUnlock( gArbitrationLockQueueLock
);
1103 void IOService::unlockForArbitration( void )
1106 ArbitrationLockQueueElement
* element
;
1108 // lock global access
1109 IOTakeLock( gArbitrationLockQueueLock
);
1111 // find the lock element for this object (ie. on active queue)
1113 queue_iterate( &gArbitrationLockQueueActive
,
1115 ArbitrationLockQueueElement
*,
1118 if( element
->service
== this ) {
1126 // determine whether the lock has been taken recursively
1127 if( element
->count
> 1 ) {
1128 // undo one level of recursion
1133 // remove it from the active queue
1134 queue_remove( &gArbitrationLockQueueActive
,
1136 ArbitrationLockQueueElement
*,
1139 // put it on the free queue
1140 queue_enter( &gArbitrationLockQueueFree
,
1142 ArbitrationLockQueueElement
*,
1145 // determine whether a thread is waiting for object (head to tail scan)
1147 queue_iterate( &gArbitrationLockQueueWaiting
,
1149 ArbitrationLockQueueElement
*,
1152 if( element
->service
== this ) {
1158 if ( found
) { // we found an interested thread on waiting queue
1160 // remove it from the waiting queue
1161 queue_remove( &gArbitrationLockQueueWaiting
,
1163 ArbitrationLockQueueElement
*,
1166 // put it on the active queue
1167 queue_enter( &gArbitrationLockQueueActive
,
1169 ArbitrationLockQueueElement
*,
1172 // wake the waiting thread
1173 IOLockWakeup( gArbitrationLockQueueLock
,
1175 /* one thread */ true );
1179 // unlock global access
1180 IOUnlock( gArbitrationLockQueueLock
);
1183 void IOService::applyToProviders( IOServiceApplierFunction applier
,
1186 applyToParents( (IORegistryEntryApplierFunction
) applier
,
1187 context
, gIOServicePlane
);
1190 void IOService::applyToClients( IOServiceApplierFunction applier
,
1193 applyToChildren( (IORegistryEntryApplierFunction
) applier
,
1194 context
, gIOServicePlane
);
1203 // send a message to a client or interested party of this service
1204 IOReturn
IOService::messageClient( UInt32 type
, OSObject
* client
,
1205 void * argument
, vm_size_t argSize
)
1208 IOService
* service
;
1209 _IOServiceInterestNotifier
* notify
;
1211 if( (service
= OSDynamicCast( IOService
, client
)))
1212 ret
= service
->message( type
, this, argument
);
1214 else if( (notify
= OSDynamicCast( _IOServiceInterestNotifier
, client
))) {
1216 _IOServiceNotifierInvocation invocation
;
1219 invocation
.thread
= current_thread();
1222 willNotify
= (0 != (kIOServiceNotifyEnable
& notify
->state
));
1225 queue_enter( ¬ify
->handlerInvocations
, &invocation
,
1226 _IOServiceNotifierInvocation
*, link
);
1232 ret
= (*notify
->handler
)( notify
->target
, notify
->ref
,
1233 type
, this, argument
, argSize
);
1236 queue_remove( ¬ify
->handlerInvocations
, &invocation
,
1237 _IOServiceNotifierInvocation
*, link
);
1238 if( kIOServiceNotifyWaiter
& notify
->state
) {
1239 notify
->state
&= ~kIOServiceNotifyWaiter
;
1240 WAKEUPNOTIFY( notify
);
1245 ret
= kIOReturnSuccess
;
1248 ret
= kIOReturnBadArgument
;
1253 void IOService::applyToInterested( const OSSymbol
* typeOfInterest
,
1254 OSObjectApplierFunction applier
,
1260 OSArray
* copyArray
;
1262 applyToClients( (IOServiceApplierFunction
) applier
, context
);
1265 array
= OSDynamicCast( OSArray
, getProperty( typeOfInterest
));
1267 copyArray
= OSArray::withArray( array
);
1271 (next
= copyArray
->getObject( index
));
1273 (*applier
)(next
, context
);
1275 copyArray
->release();
1281 struct MessageClientsContext
{
1282 IOService
* service
;
1289 static void messageClientsApplier( OSObject
* object
, void * ctx
)
1292 MessageClientsContext
* context
= (MessageClientsContext
*) ctx
;
1294 ret
= context
->service
->messageClient( context
->type
,
1295 object
, context
->argument
, context
->argSize
);
1296 if( kIOReturnSuccess
!= ret
)
1300 // send a message to all clients
1301 IOReturn
IOService::messageClients( UInt32 type
,
1302 void * argument
, vm_size_t argSize
)
1304 MessageClientsContext context
;
1306 context
.service
= this;
1307 context
.type
= type
;
1308 context
.argument
= argument
;
1309 context
.argSize
= argSize
;
1310 context
.ret
= kIOReturnSuccess
;
1312 applyToInterested( gIOGeneralInterest
,
1313 &messageClientsApplier
, &context
);
1315 return( context
.ret
);
1318 IOReturn
IOService::acknowledgeNotification( IONotificationRef notification
,
1319 IOOptionBits response
)
1321 return( kIOReturnUnsupported
);
1324 IONotifier
* IOService::registerInterest( const OSSymbol
* typeOfInterest
,
1325 IOServiceInterestHandler handler
, void * target
, void * ref
)
1327 _IOServiceInterestNotifier
* notify
= 0;
1330 if( (typeOfInterest
!= gIOGeneralInterest
)
1331 && (typeOfInterest
!= gIOBusyInterest
)
1332 && (typeOfInterest
!= gIOAppPowerStateInterest
)
1333 && (typeOfInterest
!= gIOPriorityPowerStateInterest
))
1336 lockForArbitration();
1337 if( 0 == (__state
[0] & kIOServiceInactiveState
)) {
1339 notify
= new _IOServiceInterestNotifier
;
1340 if( notify
&& !notify
->init()) {
1346 notify
->handler
= handler
;
1347 notify
->target
= target
;
1349 notify
->state
= kIOServiceNotifyEnable
;
1350 queue_init( ¬ify
->handlerInvocations
);
1355 if( 0 == (set
= (OSArray
*) getProperty( typeOfInterest
))) {
1356 set
= OSArray::withCapacity( 1 );
1358 setProperty( typeOfInterest
, set
);
1362 notify
->whence
= set
;
1364 set
->setObject( notify
);
1368 unlockForArbitration();
1373 static void cleanInterestArray( OSObject
* object
)
1377 _IOServiceInterestNotifier
* next
;
1379 if( (array
= OSDynamicCast( OSArray
, object
))) {
1382 (next
= (_IOServiceInterestNotifier
*)
1383 array
->getObject( index
));
1391 void IOService::unregisterAllInterest( void )
1393 cleanInterestArray( getProperty( gIOGeneralInterest
));
1394 cleanInterestArray( getProperty( gIOBusyInterest
));
1395 cleanInterestArray( getProperty( gIOAppPowerStateInterest
));
1396 cleanInterestArray( getProperty( gIOPriorityPowerStateInterest
));
1400 * _IOServiceInterestNotifier
1403 // wait for all threads, other than the current one,
1404 // to exit the handler
1406 void _IOServiceInterestNotifier::wait()
1408 _IOServiceNotifierInvocation
* next
;
1413 queue_iterate( &handlerInvocations
, next
,
1414 _IOServiceNotifierInvocation
*, link
) {
1415 if( next
->thread
!= current_thread() ) {
1421 state
|= kIOServiceNotifyWaiter
;
1428 void _IOServiceInterestNotifier::free()
1430 assert( queue_empty( &handlerInvocations
));
1434 void _IOServiceInterestNotifier::remove()
1439 whence
->removeObject(whence
->getNextIndexOfObject(
1440 (OSObject
*) this, 0 ));
1444 state
&= ~kIOServiceNotifyEnable
;
1453 bool _IOServiceInterestNotifier::disable()
1459 ret
= (0 != (kIOServiceNotifyEnable
& state
));
1460 state
&= ~kIOServiceNotifyEnable
;
1469 void _IOServiceInterestNotifier::enable( bool was
)
1473 state
|= kIOServiceNotifyEnable
;
1475 state
&= ~kIOServiceNotifyEnable
;
1479 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1485 #define tailQ(o) setObject(o)
1486 #define headQ(o) setObject(0, o)
1487 #define TLOG(fmt, args...) { if(kIOLogYield & gIOKitDebug) IOLog(fmt, ## args); }
1489 inline void _workLoopAction( IOWorkLoop::Action action
,
1490 IOService
* service
,
1491 void * p0
= 0, void * p1
= 0,
1492 void * p2
= 0, void * p3
= 0 )
1496 if( (wl
= service
->getWorkLoop())) {
1498 wl
->runAction( action
, service
, p0
, p1
, p2
, p3
);
1501 (*action
)( service
, p0
, p1
, p2
, p3
);
1504 bool IOService::requestTerminate( IOService
* provider
, IOOptionBits options
)
1508 // if its our only provider
1509 ok
= isParent( provider
, gIOServicePlane
, true);
1513 provider
->terminateClient( this, options
| kIOServiceRecursing
);
1514 ok
= (0 != (__state
[1] & kIOServiceRecursing
));
1521 bool IOService::terminatePhase1( IOOptionBits options
)
1526 OSArray
* makeInactive
;
1529 bool startPhase2
= false;
1531 TLOG("%s::terminatePhase1(%08lx)\n", getName(), options
);
1534 if( options
& kIOServiceRecursing
) {
1535 __state
[1] |= kIOServiceRecursing
;
1540 makeInactive
= OSArray::withCapacity( 16 );
1549 didInactive
= victim
->lockForArbitration( true );
1551 didInactive
= (0 == (victim
->__state
[0] & kIOServiceInactiveState
));
1553 victim
->__state
[0] |= kIOServiceInactiveState
;
1554 victim
->__state
[0] &= ~(kIOServiceRegisteredState
| kIOServiceMatchedState
1555 | kIOServiceFirstPublishState
| kIOServiceFirstMatchState
);
1556 victim
->_adjustBusy( 1 );
1558 victim
->unlockForArbitration();
1561 startPhase2
= didInactive
;
1564 victim
->deliverNotification( gIOTerminatedNotification
, 0, 0xffffffff );
1565 IOUserClient::destroyUserReferences( victim
);
1566 victim
->unregisterAllInterest();
1568 iter
= victim
->getClientIterator();
1570 while( (client
= (IOService
*) iter
->getNextObject())) {
1571 TLOG("%s::requestTerminate(%s, %08lx)\n",
1572 client
->getName(), victim
->getName(), options
);
1573 ok
= client
->requestTerminate( victim
, options
);
1574 TLOG("%s::requestTerminate(%s, ok = %d)\n",
1575 client
->getName(), victim
->getName(), ok
);
1577 makeInactive
->setObject( client
);
1583 victim
= (IOService
*) makeInactive
->getObject(0);
1586 makeInactive
->removeObject(0);
1590 makeInactive
->release();
1593 scheduleTerminatePhase2( options
);
1598 void IOService::scheduleTerminatePhase2( IOOptionBits options
)
1600 AbsoluteTime deadline
;
1602 bool wait
, haveDeadline
= false;
1604 options
|= kIOServiceRequired
;
1608 IOLockLock( gJobsLock
);
1610 if( (options
& kIOServiceSynchronous
)
1611 && (current_thread() != gIOTerminateThread
)) {
1614 wait
= (gIOTerminateThread
!= 0);
1616 // wait to become the terminate thread
1617 IOLockSleep( gJobsLock
, &gIOTerminateThread
, THREAD_UNINT
);
1621 gIOTerminateThread
= current_thread();
1622 gIOTerminatePhase2List
->setObject( this );
1626 while( gIOTerminateWork
)
1627 terminateWorker( options
);
1628 wait
= (0 != (__state
[1] & kIOServiceBusyStateMask
));
1630 // wait for the victim to go non-busy
1631 if( !haveDeadline
) {
1632 clock_interval_to_deadline( 15, kSecondScale
, &deadline
);
1633 haveDeadline
= true;
1635 waitResult
= IOLockSleepDeadline( gJobsLock
, &gIOTerminateWork
,
1636 deadline
, THREAD_UNINT
);
1637 if( waitResult
== THREAD_TIMED_OUT
) {
1638 TLOG("%s::terminate(kIOServiceSynchronous) timeout", getName());
1640 thread_cancel_timer();
1642 } while(gIOTerminateWork
|| (wait
&& (waitResult
!= THREAD_TIMED_OUT
)));
1644 gIOTerminateThread
= 0;
1645 IOLockWakeup( gJobsLock
, (event_t
) &gIOTerminateThread
, /* one-thread */ false);
1648 // ! kIOServiceSynchronous
1650 gIOTerminatePhase2List
->setObject( this );
1651 if( 0 == gIOTerminateWork
++) {
1652 if( !gIOTerminateThread
)
1653 gIOTerminateThread
= IOCreateThread( &terminateThread
, (void *) options
);
1655 IOLockWakeup(gJobsLock
, (event_t
) &gIOTerminateWork
, /* one-thread */ false );
1659 IOLockUnlock( gJobsLock
);
1664 void IOService::terminateThread( void * arg
)
1666 IOLockLock( gJobsLock
);
1668 while (gIOTerminateWork
)
1669 terminateWorker( (IOOptionBits
) arg
);
1671 gIOTerminateThread
= 0;
1672 IOLockWakeup( gJobsLock
, (event_t
) &gIOTerminateThread
, /* one-thread */ false);
1674 IOLockUnlock( gJobsLock
);
1677 void IOService::scheduleStop( IOService
* provider
)
1679 TLOG("%s::scheduleStop(%s)\n", getName(), provider
->getName());
1681 IOLockLock( gJobsLock
);
1682 gIOStopList
->tailQ( this );
1683 gIOStopProviderList
->tailQ( provider
);
1685 if( 0 == gIOTerminateWork
++) {
1686 if( !gIOTerminateThread
)
1687 gIOTerminateThread
= IOCreateThread( &terminateThread
, (void *) 0 );
1689 IOLockWakeup(gJobsLock
, (event_t
) &gIOTerminateWork
, /* one-thread */ false );
1692 IOLockUnlock( gJobsLock
);
1695 void IOService::scheduleFinalize( void )
1697 TLOG("%s::scheduleFinalize\n", getName());
1699 IOLockLock( gJobsLock
);
1700 gIOFinalizeList
->tailQ( this );
1702 if( 0 == gIOTerminateWork
++) {
1703 if( !gIOTerminateThread
)
1704 gIOTerminateThread
= IOCreateThread( &terminateThread
, (void *) 0 );
1706 IOLockWakeup(gJobsLock
, (event_t
) &gIOTerminateWork
, /* one-thread */ false );
1709 IOLockUnlock( gJobsLock
);
1712 bool IOService::willTerminate( IOService
* provider
, IOOptionBits options
)
1717 bool IOService::didTerminate( IOService
* provider
, IOOptionBits options
, bool * defer
)
1719 if( false == *defer
) {
1721 if( lockForArbitration( true )) {
1722 if( false == provider
->handleIsOpen( this ))
1723 scheduleStop( provider
);
1726 message( kIOMessageServiceIsRequestingClose
, provider
, (void *) options
);
1727 if( false == provider
->handleIsOpen( this ))
1728 scheduleStop( provider
);
1731 unlockForArbitration();
1738 void IOService::actionWillTerminate( IOService
* victim
, IOOptionBits options
,
1739 OSArray
* doPhase2List
)
1745 iter
= victim
->getClientIterator();
1747 while( (client
= (IOService
*) iter
->getNextObject())) {
1748 TLOG("%s::willTerminate(%s, %08lx)\n",
1749 client
->getName(), victim
->getName(), options
);
1750 ok
= client
->willTerminate( victim
, options
);
1751 doPhase2List
->tailQ( client
);
1757 void IOService::actionDidTerminate( IOService
* victim
, IOOptionBits options
)
1763 victim
->messageClients( kIOMessageServiceIsTerminated
, (void *) options
);
1765 iter
= victim
->getClientIterator();
1767 while( (client
= (IOService
*) iter
->getNextObject())) {
1768 TLOG("%s::didTerminate(%s, %08lx)\n",
1769 client
->getName(), victim
->getName(), options
);
1770 client
->didTerminate( victim
, options
, &defer
);
1771 TLOG("%s::didTerminate(%s, defer %d)\n",
1772 client
->getName(), victim
->getName(), defer
);
1778 void IOService::actionFinalize( IOService
* victim
, IOOptionBits options
)
1780 TLOG("%s::finalize(%08lx)\n", victim
->getName(), options
);
1781 victim
->finalize( options
);
1784 void IOService::actionStop( IOService
* provider
, IOService
* client
)
1786 TLOG("%s::stop(%s)\n", client
->getName(), provider
->getName());
1787 client
->stop( provider
);
1788 if( provider
->isOpen( client
))
1789 provider
->close( client
);
1790 TLOG("%s::detach(%s)\n", client
->getName(), provider
->getName());
1791 client
->detach( provider
);
1794 void IOService::terminateWorker( IOOptionBits options
)
1796 OSArray
* doPhase2List
;
1797 OSArray
* didPhase2List
;
1802 IOService
* provider
;
1808 options
|= kIOServiceRequired
;
1810 doPhase2List
= OSArray::withCapacity( 16 );
1811 didPhase2List
= OSArray::withCapacity( 16 );
1812 freeList
= OSSet::withCapacity( 16 );
1813 if( (0 == doPhase2List
) || (0 == didPhase2List
) || (0 == freeList
))
1817 workDone
= gIOTerminateWork
;
1819 while( (victim
= (IOService
*) gIOTerminatePhase2List
->getObject(0) )) {
1822 gIOTerminatePhase2List
->removeObject(0);
1823 IOLockUnlock( gJobsLock
);
1827 doPhase2
= victim
->lockForArbitration( true );
1829 doPhase2
= (0 != (kIOServiceInactiveState
& victim
->__state
[0]));
1831 doPhase2
= (0 == (victim
->__state
[1] & kIOServiceTermPhase2State
))
1832 && (0 == (victim
->__state
[1] & kIOServiceConfigState
));
1834 victim
->__state
[1] |= kIOServiceTermPhase2State
;
1836 victim
->unlockForArbitration();
1839 if( 0 == victim
->getClient()) {
1840 // no clients - will go to finalize
1841 IOLockLock( gJobsLock
);
1842 gIOFinalizeList
->tailQ( victim
);
1843 IOLockUnlock( gJobsLock
);
1845 _workLoopAction( (IOWorkLoop::Action
) &actionWillTerminate
,
1846 victim
, (void *) options
, (void *) doPhase2List
);
1848 didPhase2List
->headQ( victim
);
1851 victim
= (IOService
*) doPhase2List
->getObject(0);
1854 doPhase2List
->removeObject(0);
1858 while( (victim
= (IOService
*) didPhase2List
->getObject(0)) ) {
1860 if( victim
->lockForArbitration( true )) {
1861 victim
->__state
[1] |= kIOServiceTermPhase3State
;
1862 victim
->unlockForArbitration();
1864 _workLoopAction( (IOWorkLoop::Action
) &actionDidTerminate
,
1865 victim
, (void *) options
);
1866 didPhase2List
->removeObject(0);
1868 IOLockLock( gJobsLock
);
1875 while( (victim
= (IOService
*) gIOFinalizeList
->getObject(0))) {
1877 IOLockUnlock( gJobsLock
);
1878 _workLoopAction( (IOWorkLoop::Action
) &actionFinalize
,
1879 victim
, (void *) options
);
1880 IOLockLock( gJobsLock
);
1882 freeList
->setObject( victim
);
1883 // safe if finalize list is append only
1884 gIOFinalizeList
->removeObject(0);
1888 (!doPhase3
) && (client
= (IOService
*) gIOStopList
->getObject(idx
)); ) {
1890 provider
= (IOService
*) gIOStopProviderList
->getObject(idx
);
1893 if( !provider
->isChild( client
, gIOServicePlane
)) {
1894 // may be multiply queued - nop it
1895 TLOG("%s::nop stop(%s)\n", client
->getName(), provider
->getName());
1897 // not ready for stop if it has clients, skip it
1898 if( (client
->__state
[1] & kIOServiceTermPhase3State
) && client
->getClient()) {
1899 TLOG("%s::defer stop(%s)\n", client
->getName(), provider
->getName());
1904 IOLockUnlock( gJobsLock
);
1905 _workLoopAction( (IOWorkLoop::Action
) &actionStop
,
1906 provider
, (void *) client
);
1907 IOLockLock( gJobsLock
);
1908 // check the finalize list now
1912 freeList
->setObject( client
);
1913 freeList
->setObject( provider
);
1915 // safe if stop list is append only
1916 gIOStopList
->removeObject( idx
);
1917 gIOStopProviderList
->removeObject( idx
);
1921 } while( doPhase3
);
1923 gIOTerminateWork
-= workDone
;
1924 moreToDo
= (gIOTerminateWork
!= 0);
1927 TLOG("iokit terminate done, %d stops remain\n", gIOStopList
->getCount());
1930 } while( moreToDo
);
1932 IOLockUnlock( gJobsLock
);
1934 freeList
->release();
1935 doPhase2List
->release();
1936 didPhase2List
->release();
1938 IOLockLock( gJobsLock
);
1941 bool IOService::finalize( IOOptionBits options
)
1944 IOService
* provider
;
1946 iter
= getProviderIterator();
1950 while( (provider
= (IOService
*) iter
->getNextObject())) {
1953 if( 0 == (__state
[1] & kIOServiceTermPhase3State
)) {
1954 /* we come down here on programmatic terminate */
1956 if( provider
->isOpen( this ))
1957 provider
->close( this );
1961 if( provider
->lockForArbitration( true )) {
1962 if( 0 == (provider
->__state
[1] & kIOServiceTermPhase3State
))
1963 scheduleStop( provider
);
1964 provider
->unlockForArbitration();
1981 void IOService::doServiceTerminate( IOOptionBits options
)
1985 // a method in case someone needs to override it
1986 bool IOService::terminateClient( IOService
* client
, IOOptionBits options
)
1990 if( client
->isParent( this, gIOServicePlane
, true))
1991 // we are the clients only provider
1992 ok
= client
->terminate( options
);
1999 bool IOService::terminate( IOOptionBits options
)
2001 options
|= kIOServiceTerminate
;
2003 return( terminatePhase1( options
));
2006 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2012 struct ServiceOpenMessageContext
2014 IOService
* service
;
2016 IOService
* excludeClient
;
2017 IOOptionBits options
;
2020 static void serviceOpenMessageApplier( OSObject
* object
, void * ctx
)
2022 ServiceOpenMessageContext
* context
= (ServiceOpenMessageContext
*) ctx
;
2024 if( object
!= context
->excludeClient
)
2025 context
->service
->messageClient( context
->type
, object
, (void *) context
->options
);
2028 bool IOService::open( IOService
* forClient
,
2029 IOOptionBits options
,
2033 ServiceOpenMessageContext context
;
2035 context
.service
= this;
2036 context
.type
= kIOMessageServiceIsAttemptingOpen
;
2037 context
.excludeClient
= forClient
;
2038 context
.options
= options
;
2040 applyToInterested( gIOGeneralInterest
,
2041 &serviceOpenMessageApplier
, &context
);
2043 if( false == lockForArbitration(false) )
2046 ok
= (0 == (__state
[0] & kIOServiceInactiveState
));
2048 ok
= handleOpen( forClient
, options
, arg
);
2050 unlockForArbitration();
2055 void IOService::close( IOService
* forClient
,
2056 IOOptionBits options
)
2061 lockForArbitration();
2063 wasClosed
= handleIsOpen( forClient
);
2065 handleClose( forClient
, options
);
2066 last
= (__state
[1] & kIOServiceTermPhase3State
);
2069 unlockForArbitration();
2072 forClient
->scheduleStop( this );
2074 else if( wasClosed
) {
2076 ServiceOpenMessageContext context
;
2078 context
.service
= this;
2079 context
.type
= kIOMessageServiceWasClosed
;
2080 context
.excludeClient
= forClient
;
2081 context
.options
= options
;
2083 applyToInterested( gIOGeneralInterest
,
2084 &serviceOpenMessageApplier
, &context
);
2088 bool IOService::isOpen( const IOService
* forClient
) const
2090 IOService
* self
= (IOService
*) this;
2093 self
->lockForArbitration();
2095 ok
= handleIsOpen( forClient
);
2097 self
->unlockForArbitration();
2102 bool IOService::handleOpen( IOService
* forClient
,
2103 IOOptionBits options
,
2108 ok
= (0 == __owner
);
2110 __owner
= forClient
;
2112 else if( options
& kIOServiceSeize
) {
2113 ok
= (kIOReturnSuccess
== messageClient( kIOMessageServiceIsRequestingClose
,
2114 __owner
, (void *) options
));
2115 if( ok
&& (0 == __owner
))
2116 __owner
= forClient
;
2123 void IOService::handleClose( IOService
* forClient
,
2124 IOOptionBits options
)
2126 if( __owner
== forClient
)
2130 bool IOService::handleIsOpen( const IOService
* forClient
) const
2133 return( __owner
== forClient
);
2135 return( __owner
!= forClient
);
2139 * Probing & starting
2141 static SInt32
IONotifyOrdering( const OSMetaClassBase
* inObj1
, const OSMetaClassBase
* inObj2
, void * ref
)
2143 const _IOServiceNotifier
* obj1
= (const _IOServiceNotifier
*) inObj1
;
2144 const _IOServiceNotifier
* obj2
= (const _IOServiceNotifier
*) inObj2
;
2152 val1
= obj1
->priority
;
2155 val2
= obj2
->priority
;
2157 return ( val1
- val2
);
2160 static SInt32
IOServiceObjectOrder( const OSObject
* entry
, void * ref
)
2162 OSDictionary
* dict
;
2163 IOService
* service
;
2164 _IOServiceNotifier
* notify
;
2165 OSSymbol
* key
= (OSSymbol
*) ref
;
2168 if( (notify
= OSDynamicCast( _IOServiceNotifier
, entry
)))
2169 return( notify
->priority
);
2171 else if( (service
= OSDynamicCast( IOService
, entry
)))
2172 offset
= OSDynamicCast(OSNumber
, service
->getProperty( key
));
2173 else if( (dict
= OSDynamicCast( OSDictionary
, entry
)))
2174 offset
= OSDynamicCast(OSNumber
, dict
->getObject( key
));
2181 return( (SInt32
) offset
->unsigned32BitValue());
2183 return( kIODefaultProbeScore
);
2186 SInt32
IOServiceOrdering( const OSMetaClassBase
* inObj1
, const OSMetaClassBase
* inObj2
, void * ref
)
2188 const OSObject
* obj1
= (const OSObject
*) inObj1
;
2189 const OSObject
* obj2
= (const OSObject
*) inObj2
;
2197 val1
= IOServiceObjectOrder( obj1
, ref
);
2200 val2
= IOServiceObjectOrder( obj2
, ref
);
2202 return ( val1
- val2
);
2205 IOService
* IOService::getClientWithCategory( const OSSymbol
* category
)
2207 IOService
* service
= 0;
2209 const OSSymbol
* nextCat
;
2211 iter
= getClientIterator();
2213 while( (service
= (IOService
*) iter
->getNextObject())) {
2214 if( kIOServiceInactiveState
& service
->__state
[0])
2216 nextCat
= (const OSSymbol
*) OSDynamicCast( OSSymbol
,
2217 service
->getProperty( gIOMatchCategoryKey
));
2218 if( category
== nextCat
)
2226 bool IOService::invokeNotifer( _IOServiceNotifier
* notify
)
2228 _IOServiceNotifierInvocation invocation
;
2232 invocation
.thread
= current_thread();
2235 willNotify
= (0 != (kIOServiceNotifyEnable
& notify
->state
));
2238 queue_enter( ¬ify
->handlerInvocations
, &invocation
,
2239 _IOServiceNotifierInvocation
*, link
);
2245 ret
= (*notify
->handler
)( notify
->target
, notify
->ref
, this );
2248 queue_remove( ¬ify
->handlerInvocations
, &invocation
,
2249 _IOServiceNotifierInvocation
*, link
);
2250 if( kIOServiceNotifyWaiter
& notify
->state
) {
2251 notify
->state
&= ~kIOServiceNotifyWaiter
;
2252 WAKEUPNOTIFY( notify
);
2261 * Alloc and probe matching classes,
2262 * called on the provider instance
2265 void IOService::probeCandidates( OSOrderedSet
* matches
)
2267 OSDictionary
* match
= 0;
2270 IOService
* newInst
;
2271 OSDictionary
* props
;
2274 OSOrderedSet
* familyMatches
= 0;
2275 OSOrderedSet
* startList
;
2276 OSDictionary
* startDict
= 0;
2277 const OSSymbol
* category
;
2279 _IOServiceNotifier
* notify
;
2280 OSObject
* nextMatch
= 0;
2282 bool needReloc
= false;
2288 while( !needReloc
&& (nextMatch
= matches
->getFirstObject())) {
2290 nextMatch
->retain();
2291 matches
->removeObject(nextMatch
);
2293 if( (notify
= OSDynamicCast( _IOServiceNotifier
, nextMatch
))) {
2295 lockForArbitration();
2296 if( 0 == (__state
[0] & kIOServiceInactiveState
))
2297 invokeNotifer( notify
);
2298 unlockForArbitration();
2299 nextMatch
->release();
2303 } else if( !(match
= OSDynamicCast( OSDictionary
, nextMatch
))) {
2304 nextMatch
->release();
2311 debugFlags
= getDebugFlags( match
);
2315 category
= OSDynamicCast( OSSymbol
,
2316 match
->getObject( gIOMatchCategoryKey
));
2318 category
= gIODefaultMatchCategoryKey
;
2320 if( getClientWithCategory( category
)) {
2322 if( debugFlags
& kIOLogMatch
)
2323 LOG("%s: match category %s exists\n", getName(),
2324 category
->getCStringNoCopy());
2326 nextMatch
->release();
2331 // create a copy now in case its modified during matching
2332 props
= OSDictionary::withDictionary( match
, match
->getCount());
2335 props
->setCapacityIncrement(1);
2337 // check the nub matches
2338 if( false == passiveMatch( props
, true ))
2341 // Check to see if driver reloc has been loaded.
2342 needReloc
= (false == gIOCatalogue
->isModuleLoaded( match
));
2345 if( debugFlags
& kIOLogCatalogue
)
2346 LOG("%s: stalling for module\n", getName());
2348 // If reloc hasn't been loaded, exit;
2349 // reprobing will occur after reloc has been loaded.
2353 // reorder on family matchPropertyTable score.
2354 if( 0 == familyMatches
)
2355 familyMatches
= OSOrderedSet::withCapacity( 1,
2356 IOServiceOrdering
, (void *) gIOProbeScoreKey
);
2358 familyMatches
->setObject( props
);
2363 nextMatch
->release();
2372 if( familyMatches
) {
2375 && (props
= (OSDictionary
*) familyMatches
->getFirstObject())) {
2378 familyMatches
->removeObject( props
);
2383 debugFlags
= getDebugFlags( props
);
2386 symbol
= OSDynamicCast( OSSymbol
,
2387 props
->getObject( gIOClassKey
));
2391 // alloc the driver instance
2392 inst
= (IOService
*) OSMetaClass::allocClassWithName( symbol
);
2395 IOLog("Couldn't alloc class \"%s\"\n",
2396 symbol
->getCStringNoCopy());
2400 // init driver instance
2401 if( !(inst
->init( props
))) {
2403 if( debugFlags
& kIOLogStart
)
2404 IOLog("%s::init fails\n", symbol
->getCStringNoCopy());
2408 if( __state
[1] & kIOServiceSynchronousState
)
2409 inst
->__state
[1] |= kIOServiceSynchronousState
;
2411 // give the driver the default match category if not specified
2412 category
= OSDynamicCast( OSSymbol
,
2413 props
->getObject( gIOMatchCategoryKey
));
2415 category
= gIODefaultMatchCategoryKey
;
2416 inst
->setProperty( gIOMatchCategoryKey
, (OSObject
*) category
);
2418 // attach driver instance
2419 if( !(inst
->attach( this )))
2422 // pass in score from property table
2423 score
= familyMatches
->orderObject( props
);
2425 // & probe the new driver instance
2427 if( debugFlags
& kIOLogProbe
)
2428 LOG("%s::probe(%s)\n",
2429 inst
->getMetaClass()->getClassName(), getName());
2432 newInst
= inst
->probe( this, &score
);
2433 inst
->detach( this );
2436 if( debugFlags
& kIOLogProbe
)
2437 IOLog("%s::probe fails\n", symbol
->getCStringNoCopy());
2443 newPri
= OSNumber::withNumber( score
, 32 );
2445 newInst
->setProperty( gIOProbeScoreKey
, newPri
);
2449 // add to start list for the match category
2451 startDict
= OSDictionary::withCapacity( 1 );
2452 assert( startDict
);
2453 startList
= (OSOrderedSet
*)
2454 startDict
->getObject( category
);
2455 if( 0 == startList
) {
2456 startList
= OSOrderedSet::withCapacity( 1,
2457 IOServiceOrdering
, (void *) gIOProbeScoreKey
);
2458 if( startDict
&& startList
) {
2459 startDict
->setObject( category
, startList
);
2460 startList
->release();
2463 assert( startList
);
2465 startList
->setObject( newInst
);
2473 familyMatches
->release();
2477 // start the best (until success) of each category
2479 iter
= OSCollectionIterator::withCollection( startDict
);
2481 while( (category
= (const OSSymbol
*) iter
->getNextObject())) {
2483 startList
= (OSOrderedSet
*) startDict
->getObject( category
);
2484 assert( startList
);
2489 while( true // (!started)
2490 && (inst
= (IOService
*)startList
->getFirstObject())) {
2493 startList
->removeObject(inst
);
2496 debugFlags
= getDebugFlags( inst
->getPropertyTable() );
2498 if( debugFlags
& kIOLogStart
) {
2500 LOG( "match category exists, skipping " );
2501 LOG( "%s::start(%s) <%d>\n", inst
->getName(),
2502 getName(), inst
->getRetainCount());
2505 if( false == started
)
2506 started
= startCandidate( inst
);
2508 if( (debugFlags
& kIOLogStart
) && (false == started
))
2509 LOG( "%s::start(%s) <%d> failed\n", inst
->getName(), getName(),
2510 inst
->getRetainCount());
2519 // adjust the busy count by -1 if matching is stalled for a module,
2520 // or +1 if a previously stalled matching is complete.
2521 lockForArbitration();
2524 adjBusy
= (__state
[1] & kIOServiceModuleStallState
) ? 0 : 1;
2526 __state
[1] |= kIOServiceModuleStallState
;
2528 } else if( __state
[1] & kIOServiceModuleStallState
) {
2529 __state
[1] &= ~kIOServiceModuleStallState
;
2533 _adjustBusy( adjBusy
);
2534 unlockForArbitration();
2537 startDict
->release();
2541 * Start a previously attached & probed instance,
2542 * called on exporting object instance
2545 bool IOService::startCandidate( IOService
* service
)
2549 ok
= service
->attach( this );
2552 // stall for any nub resources
2554 // stall for any driver resources
2555 service
->checkResources();
2557 AbsoluteTime startTime
;
2558 AbsoluteTime endTime
;
2561 if (kIOLogStart
& gIOKitDebug
)
2562 clock_get_uptime(&startTime
);
2564 ok
= service
->start(this);
2566 if (kIOLogStart
& gIOKitDebug
)
2568 clock_get_uptime(&endTime
);
2570 if (CMP_ABSOLUTETIME(&endTime
, &startTime
) > 0)
2572 SUB_ABSOLUTETIME(&endTime
, &startTime
);
2573 absolutetime_to_nanoseconds(endTime
, &nano
);
2574 if (nano
> 500000000ULL)
2575 IOLog("%s::start took %ld ms\n", service
->getName(), (UInt32
)(nano
/ 1000000ULL));
2579 service
->detach( this );
2584 IOService
* IOService::resources( void )
2586 return( gIOResources
);
2589 void IOService::publishResource( const char * key
, OSObject
* value
)
2591 const OSSymbol
* sym
;
2593 if( (sym
= OSSymbol::withCString( key
))) {
2594 publishResource( sym
, value
);
2599 void IOService::publishResource( const OSSymbol
* key
, OSObject
* value
)
2602 value
= (OSObject
*) gIOServiceKey
;
2604 gIOResources
->setProperty( key
, value
);
2606 if( IORecursiveLockHaveLock( gNotificationLock
))
2609 gIOResourceGenerationCount
++;
2610 gIOResources
->registerService();
2613 bool IOService::addNeededResource( const char * key
)
2615 OSObject
* resources
;
2620 resources
= getProperty( gIOResourceMatchKey
);
2622 newKey
= OSString::withCString( key
);
2623 if( (0 == resources
) || (0 == newKey
))
2626 set
= OSDynamicCast( OSSet
, resources
);
2628 set
= OSSet::withCapacity( 1 );
2630 set
->setObject( resources
);
2635 set
->setObject( newKey
);
2637 ret
= setProperty( gIOResourceMatchKey
, set
);
2643 bool IOService::checkResource( OSObject
* matching
)
2646 OSDictionary
* table
;
2648 if( (str
= OSDynamicCast( OSString
, matching
))) {
2649 if( gIOResources
->getProperty( str
))
2654 table
= resourceMatching( str
);
2655 else if( (table
= OSDynamicCast( OSDictionary
, matching
)))
2658 IOLog("%s: Can't match using: %s\n", getName(),
2659 matching
->getMetaClass()->getClassName());
2660 /* false would stall forever */
2664 if( gIOKitDebug
& kIOLogConfig
)
2665 LOG("config(%x): stalling %s\n", (int) IOThreadSelf(), getName());
2667 waitForService( table
);
2669 if( gIOKitDebug
& kIOLogConfig
)
2670 LOG("config(%x): waking\n", (int) IOThreadSelf() );
2675 bool IOService::checkResources( void )
2677 OSObject
* resources
;
2682 resources
= getProperty( gIOResourceMatchKey
);
2686 if( (set
= OSDynamicCast( OSSet
, resources
))) {
2688 iter
= OSCollectionIterator::withCollection( set
);
2690 while( ok
&& (resources
= iter
->getNextObject()) )
2691 ok
= checkResource( resources
);
2696 ok
= checkResource( resources
);
2702 _IOConfigThread
* _IOConfigThread::configThread( void )
2704 _IOConfigThread
* inst
;
2707 if( !(inst
= new _IOConfigThread
))
2711 if( !(inst
->thread
= IOCreateThread
2712 ( (IOThreadFunc
) &_IOConfigThread::main
, inst
)))
2725 void _IOConfigThread::free( void )
2730 void IOService::doServiceMatch( IOOptionBits options
)
2732 _IOServiceNotifier
* notify
;
2734 OSOrderedSet
* matches
;
2735 SInt32 catalogGeneration
;
2736 bool keepGuessing
= true;
2737 bool reRegistered
= true;
2739 // job->nub->deliverNotification( gIOPublishNotification,
2740 // kIOServiceRegisteredState, 0xffffffff );
2742 while( keepGuessing
) {
2744 matches
= gIOCatalogue
->findDrivers( this, &catalogGeneration
);
2745 // the matches list should always be created by findDrivers()
2748 lockForArbitration();
2749 if( 0 == (__state
[0] & kIOServiceFirstPublishState
))
2750 deliverNotification( gIOFirstPublishNotification
,
2751 kIOServiceFirstPublishState
, 0xffffffff );
2753 __state
[1] &= ~kIOServiceNeedConfigState
;
2754 __state
[1] |= kIOServiceConfigState
;
2755 __state
[0] |= kIOServiceRegisteredState
;
2757 if( reRegistered
&& (0 == (__state
[0] & kIOServiceInactiveState
))) {
2759 iter
= OSCollectionIterator::withCollection( (OSOrderedSet
*)
2760 gNotifications
->getObject( gIOPublishNotification
) );
2762 while((notify
= (_IOServiceNotifier
*)
2763 iter
->getNextObject())) {
2765 if( passiveMatch( notify
->matching
)
2766 && (kIOServiceNotifyEnable
& notify
->state
))
2767 matches
->setObject( notify
);
2774 unlockForArbitration();
2776 if( matches
->getCount() && (kIOReturnSuccess
== getResources()))
2777 probeCandidates( matches
);
2782 lockForArbitration();
2783 reRegistered
= (0 != (__state
[1] & kIOServiceNeedConfigState
));
2785 (reRegistered
|| (catalogGeneration
!=
2786 gIOCatalogue
->getGenerationCount()))
2787 && (0 == (__state
[0] & kIOServiceInactiveState
));
2790 unlockForArbitration();
2793 if( (0 == (__state
[0] & kIOServiceInactiveState
))
2794 && (0 == (__state
[1] & kIOServiceModuleStallState
)) ) {
2795 deliverNotification( gIOMatchedNotification
,
2796 kIOServiceMatchedState
, 0xffffffff );
2797 if( 0 == (__state
[0] & kIOServiceFirstMatchState
))
2798 deliverNotification( gIOFirstMatchNotification
,
2799 kIOServiceFirstMatchState
, 0xffffffff );
2802 __state
[1] &= ~kIOServiceConfigState
;
2803 if( __state
[0] & kIOServiceInactiveState
)
2804 scheduleTerminatePhase2();
2807 unlockForArbitration();
2810 UInt32
IOService::_adjustBusy( SInt32 delta
)
2815 bool wasQuiet
, nowQuiet
, needWake
;
2818 result
= __state
[1] & kIOServiceBusyStateMask
;
2822 next
->lockForArbitration();
2823 count
= next
->__state
[1] & kIOServiceBusyStateMask
;
2824 assert( count
< kIOServiceBusyMax
);
2825 wasQuiet
= (0 == count
);
2826 assert( (!wasQuiet
) || (delta
> 0));
2827 next
->__state
[1] += delta
;
2828 nowQuiet
= (0 == (next
->__state
[1] & kIOServiceBusyStateMask
));
2829 needWake
= (0 != (kIOServiceBusyWaiterState
& next
->__state
[1]));
2832 next
->__state
[1] &= ~kIOServiceBusyWaiterState
;
2833 IOLockLock( gIOServiceBusyLock
);
2834 thread_wakeup( (event_t
) next
);
2835 IOLockUnlock( gIOServiceBusyLock
);
2838 next
->unlockForArbitration();
2840 if( (wasQuiet
|| nowQuiet
) ) {
2843 OSObject
* interested
;
2845 array
= OSDynamicCast( OSArray
, next
->getProperty( gIOBusyInterest
));
2849 (interested
= array
->getObject( index
));
2851 next
->messageClient(kIOMessageServiceBusyStateChange
,
2852 interested
, (void *) wasQuiet
/* busy now */);
2857 if( nowQuiet
&& (next
== gIOServiceRoot
))
2858 OSMetaClass::considerUnloads();
2861 delta
= nowQuiet
? -1 : +1;
2863 } while( (wasQuiet
|| nowQuiet
) && (next
= next
->getProvider()));
2868 void IOService::adjustBusy( SInt32 delta
)
2870 lockForArbitration();
2871 _adjustBusy( delta
);
2872 unlockForArbitration();
2875 UInt32
IOService::getBusyState( void )
2877 return( __state
[1] & kIOServiceBusyStateMask
);
2880 IOReturn
IOService::waitForState( UInt32 mask
, UInt32 value
,
2881 mach_timespec_t
* timeout
)
2884 int waitResult
= THREAD_AWAKENED
;
2885 bool computeDeadline
= true;
2886 AbsoluteTime abstime
;
2889 lockForArbitration();
2890 IOLockLock( gIOServiceBusyLock
);
2891 wait
= (value
!= (__state
[1] & mask
));
2893 __state
[1] |= kIOServiceBusyWaiterState
;
2894 unlockForArbitration();
2895 assert_wait( (event_t
) this, THREAD_UNINT
);
2897 if( computeDeadline
) {
2898 AbsoluteTime nsinterval
;
2899 clock_interval_to_absolutetime_interval(
2900 timeout
->tv_sec
, kSecondScale
, &abstime
);
2901 clock_interval_to_absolutetime_interval(
2902 timeout
->tv_nsec
, kNanosecondScale
, &nsinterval
);
2903 ADD_ABSOLUTETIME( &abstime
, &nsinterval
);
2904 clock_absolutetime_interval_to_deadline(
2905 abstime
, &abstime
);
2906 computeDeadline
= false;
2908 thread_set_timer_deadline( abstime
);
2911 unlockForArbitration();
2912 IOLockUnlock( gIOServiceBusyLock
);
2914 waitResult
= thread_block(THREAD_CONTINUE_NULL
);
2915 if( timeout
&& (waitResult
!= THREAD_TIMED_OUT
))
2916 thread_cancel_timer();
2919 } while( wait
&& (waitResult
!= THREAD_TIMED_OUT
));
2921 if( waitResult
== THREAD_TIMED_OUT
)
2922 return( kIOReturnTimeout
);
2924 return( kIOReturnSuccess
);
2927 IOReturn
IOService::waitQuiet( mach_timespec_t
* timeout
)
2929 return( waitForState( kIOServiceBusyStateMask
, 0, timeout
));
2932 bool IOService::serializeProperties( OSSerialize
* s
) const
2935 ((IOService
*)this)->setProperty( ((IOService
*)this)->__state
,
2936 sizeof( __state
), "__state");
2938 return( super::serializeProperties(s
) );
2942 void _IOConfigThread::main( _IOConfigThread
* self
)
2944 _IOServiceJob
* job
;
2952 semaphore_wait( gJobsSemaphore
);
2954 IOTakeLock( gJobsLock
);
2955 job
= (_IOServiceJob
*) gJobs
->getFirstObject();
2957 gJobs
->removeObject(job
);
2960 // gNumConfigThreads--; // we're out of service
2961 gNumWaitingThreads
--; // we're out of service
2963 IOUnlock( gJobsLock
);
2969 if( gIOKitDebug
& kIOLogConfig
)
2970 LOG("config(%x): starting on %s, %d\n",
2971 (int) IOThreadSelf(), job
->nub
->getName(), job
->type
);
2973 switch( job
->type
) {
2976 nub
->doServiceMatch( job
->options
);
2980 LOG("config(%x): strange type (%d)\n",
2981 (int) IOThreadSelf(), job
->type
);
2988 IOTakeLock( gJobsLock
);
2989 alive
= (gOutstandingJobs
> gNumWaitingThreads
);
2991 gNumWaitingThreads
++; // back in service
2992 // gNumConfigThreads++;
2994 if( 0 == --gNumConfigThreads
) {
2995 // IOLog("MATCH IDLE\n");
2996 IOLockWakeup( gJobsLock
, (event_t
) &gNumConfigThreads
, /* one-thread */ false );
2999 IOUnlock( gJobsLock
);
3004 if( gIOKitDebug
& kIOLogConfig
)
3005 LOG("config(%x): terminating\n", (int) IOThreadSelf() );
3010 IOReturn
IOService::waitMatchIdle( UInt32 msToWait
)
3013 int waitResult
= THREAD_AWAKENED
;
3014 bool computeDeadline
= true;
3015 AbsoluteTime abstime
;
3017 IOLockLock( gJobsLock
);
3019 wait
= (0 != gNumConfigThreads
);
3022 if( computeDeadline
) {
3023 clock_interval_to_absolutetime_interval(
3024 msToWait
, kMillisecondScale
, &abstime
);
3025 clock_absolutetime_interval_to_deadline(
3026 abstime
, &abstime
);
3027 computeDeadline
= false;
3029 waitResult
= IOLockSleepDeadline( gJobsLock
, &gNumConfigThreads
,
3030 abstime
, THREAD_UNINT
);
3032 waitResult
= IOLockSleep( gJobsLock
, &gNumConfigThreads
,
3036 } while( wait
&& (waitResult
!= THREAD_TIMED_OUT
));
3037 IOLockUnlock( gJobsLock
);
3039 if( waitResult
== THREAD_TIMED_OUT
)
3040 return( kIOReturnTimeout
);
3042 return( kIOReturnSuccess
);
3045 void _IOServiceJob::pingConfig( _IOServiceJob
* job
)
3052 IOTakeLock( gJobsLock
);
3055 gJobs
->setLastObject( job
);
3057 count
= gNumWaitingThreads
;
3058 // if( gNumConfigThreads) count++;// assume we're called from a config thread
3060 create
= ( (gOutstandingJobs
> count
)
3061 && (gNumConfigThreads
< kMaxConfigThreads
) );
3063 gNumConfigThreads
++;
3064 gNumWaitingThreads
++;
3067 IOUnlock( gJobsLock
);
3072 if( gIOKitDebug
& kIOLogConfig
)
3073 LOG("config(%d): creating\n", gNumConfigThreads
- 1);
3074 _IOConfigThread::configThread();
3077 semaphore_signal( gJobsSemaphore
);
3081 // internal - call with gNotificationLock
3082 OSObject
* IOService::getExistingServices( OSDictionary
* matching
,
3083 IOOptionBits inState
, IOOptionBits options
)
3085 OSObject
* current
= 0;
3087 IOService
* service
;
3092 iter
= IORegistryIterator::iterateOver( gIOServicePlane
,
3093 kIORegistryIterateRecursively
);
3097 while( (service
= (IOService
*) iter
->getNextObject())) {
3098 if( (inState
== (service
->__state
[0] & inState
))
3099 && (0 == (service
->__state
[0] & kIOServiceInactiveState
))
3100 && service
->passiveMatch( matching
)) {
3102 if( options
& kIONotifyOnce
) {
3107 ((OSSet
*)current
)->setObject( service
);
3109 current
= OSSet::withObjects(
3110 (const OSObject
**) &service
, 1, 1 );
3113 } while( !service
&& !iter
->isValid());
3117 if( current
&& (0 == (options
& kIONotifyOnce
))) {
3118 iter
= OSCollectionIterator::withCollection( (OSSet
*)current
);
3127 OSIterator
* IOService::getMatchingServices( OSDictionary
* matching
)
3131 // is a lock even needed?
3134 iter
= (OSIterator
*) getExistingServices( matching
,
3135 kIOServiceMatchedState
);
3143 // internal - call with gNotificationLock
3144 IONotifier
* IOService::setNotification(
3145 const OSSymbol
* type
, OSDictionary
* matching
,
3146 IOServiceNotificationHandler handler
, void * target
, void * ref
,
3149 _IOServiceNotifier
* notify
= 0;
3155 notify
= new _IOServiceNotifier
;
3156 if( notify
&& !notify
->init()) {
3162 notify
->matching
= matching
;
3163 notify
->handler
= handler
;
3164 notify
->target
= target
;
3166 notify
->priority
= priority
;
3167 notify
->state
= kIOServiceNotifyEnable
;
3168 queue_init( ¬ify
->handlerInvocations
);
3172 if( 0 == (set
= (OSOrderedSet
*) gNotifications
->getObject( type
))) {
3173 set
= OSOrderedSet::withCapacity( 1,
3174 IONotifyOrdering
, 0 );
3176 gNotifications
->setObject( type
, set
);
3180 notify
->whence
= set
;
3182 set
->setObject( notify
);
3188 // internal - call with gNotificationLock
3189 IONotifier
* IOService::doInstallNotification(
3190 const OSSymbol
* type
, OSDictionary
* matching
,
3191 IOServiceNotificationHandler handler
,
3192 void * target
, void * ref
,
3193 SInt32 priority
, OSIterator
** existing
)
3196 IONotifier
* notify
;
3197 IOOptionBits inState
;
3202 if( type
== gIOPublishNotification
)
3203 inState
= kIOServiceRegisteredState
;
3205 else if( type
== gIOFirstPublishNotification
)
3206 inState
= kIOServiceFirstPublishState
;
3208 else if( (type
== gIOMatchedNotification
)
3209 || (type
== gIOFirstMatchNotification
))
3210 inState
= kIOServiceMatchedState
;
3211 else if( type
== gIOTerminatedNotification
)
3216 notify
= setNotification( type
, matching
, handler
, target
, ref
, priority
);
3219 // get the current set
3220 exist
= (OSIterator
*) getExistingServices( matching
, inState
);
3230 IONotifier
* IOService::installNotification(
3231 const OSSymbol
* type
, OSDictionary
* matching
,
3232 IOServiceNotificationHandler handler
,
3233 void * target
, void * ref
,
3234 SInt32 priority
, OSIterator
** existing
)
3236 IONotifier
* notify
;
3240 notify
= doInstallNotification( type
, matching
, handler
, target
, ref
,
3241 priority
, existing
);
3248 IONotifier
* IOService::addNotification(
3249 const OSSymbol
* type
, OSDictionary
* matching
,
3250 IOServiceNotificationHandler handler
,
3251 void * target
, void * ref
,
3254 OSIterator
* existing
;
3255 _IOServiceNotifier
* notify
;
3258 notify
= (_IOServiceNotifier
*) installNotification( type
, matching
,
3259 handler
, target
, ref
, priority
, &existing
);
3261 // send notifications for existing set
3264 notify
->retain(); // in case handler remove()s
3265 while( (next
= (IOService
*) existing
->getNextObject())) {
3267 next
->lockForArbitration();
3268 if( 0 == (next
->__state
[0] & kIOServiceInactiveState
))
3269 next
->invokeNotifer( notify
);
3270 next
->unlockForArbitration();
3273 existing
->release();
3279 struct SyncNotifyVars
{
3280 semaphore_port_t waitHere
;
3284 bool IOService::syncNotificationHandler(
3285 void * /* target */, void * ref
,
3286 IOService
* newService
)
3289 // result may get written more than once before the
3290 // notification is removed!
3291 ((SyncNotifyVars
*) ref
)->result
= newService
;
3292 semaphore_signal( ((SyncNotifyVars
*) ref
)->waitHere
);
3297 IOService
* IOService::waitForService( OSDictionary
* matching
,
3298 mach_timespec_t
* timeout
)
3300 IONotifier
* notify
= 0;
3301 // priority doesn't help us much since we need a thread wakeup
3302 SInt32 priority
= 0;
3303 SyncNotifyVars state
;
3304 kern_return_t err
= kIOReturnBadArgument
;
3316 state
.result
= (IOService
*) getExistingServices( matching
,
3317 kIOServiceMatchedState
, kIONotifyOnce
);
3321 err
= semaphore_create( kernel_task
, &state
.waitHere
,
3322 SYNC_POLICY_FIFO
, 0 );
3323 if( KERN_SUCCESS
!= err
)
3326 notify
= IOService::setNotification( gIOMatchedNotification
, matching
,
3327 &IOService::syncNotificationHandler
, (void *) 0,
3328 (void *) &state
, priority
);
3336 err
= semaphore_timedwait( state
.waitHere
, *timeout
);
3338 err
= semaphore_wait( state
.waitHere
);
3342 notify
->remove(); // dequeues
3344 matching
->release();
3346 semaphore_destroy( kernel_task
, state
.waitHere
);
3348 return( state
.result
);
3351 void IOService::deliverNotification( const OSSymbol
* type
,
3352 IOOptionBits orNewState
, IOOptionBits andNewState
)
3354 _IOServiceNotifier
* notify
;
3356 OSArray
* willSend
= 0;
3358 lockForArbitration();
3360 if( (0 == (__state
[0] & kIOServiceInactiveState
))
3361 || (type
== gIOTerminatedNotification
)) {
3365 iter
= OSCollectionIterator::withCollection( (OSOrderedSet
*)
3366 gNotifications
->getObject( type
) );
3369 while( (notify
= (_IOServiceNotifier
*) iter
->getNextObject())) {
3371 if( passiveMatch( notify
->matching
)
3372 && (kIOServiceNotifyEnable
& notify
->state
)) {
3374 willSend
= OSArray::withCapacity(8);
3376 willSend
->setObject( notify
);
3382 __state
[0] = (__state
[0] | orNewState
) & andNewState
;
3388 for( unsigned int idx
= 0;
3389 (notify
= (_IOServiceNotifier
*) willSend
->getObject(idx
));
3391 invokeNotifer( notify
);
3393 willSend
->release();
3395 unlockForArbitration();
3398 IOOptionBits
IOService::getState( void ) const
3400 return( __state
[0] );
3404 * Helpers to make matching objects for simple cases
3407 OSDictionary
* IOService::serviceMatching( const OSString
* name
,
3408 OSDictionary
* table
)
3411 table
= OSDictionary::withCapacity( 2 );
3413 table
->setObject(gIOProviderClassKey
, (OSObject
*)name
);
3418 OSDictionary
* IOService::serviceMatching( const char * name
,
3419 OSDictionary
* table
)
3421 const OSString
* str
;
3423 str
= OSSymbol::withCString( name
);
3427 table
= serviceMatching( str
, table
);
3432 OSDictionary
* IOService::nameMatching( const OSString
* name
,
3433 OSDictionary
* table
)
3436 table
= OSDictionary::withCapacity( 2 );
3438 table
->setObject( gIONameMatchKey
, (OSObject
*)name
);
3443 OSDictionary
* IOService::nameMatching( const char * name
,
3444 OSDictionary
* table
)
3446 const OSString
* str
;
3448 str
= OSSymbol::withCString( name
);
3452 table
= nameMatching( str
, table
);
3457 OSDictionary
* IOService::resourceMatching( const OSString
* str
,
3458 OSDictionary
* table
)
3460 table
= serviceMatching( gIOResourcesKey
, table
);
3462 table
->setObject( gIOResourceMatchKey
, (OSObject
*) str
);
3467 OSDictionary
* IOService::resourceMatching( const char * name
,
3468 OSDictionary
* table
)
3470 const OSSymbol
* str
;
3472 str
= OSSymbol::withCString( name
);
3476 table
= resourceMatching( str
, table
);
3483 * _IOServiceNotifier
3486 // wait for all threads, other than the current one,
3487 // to exit the handler
3489 void _IOServiceNotifier::wait()
3491 _IOServiceNotifierInvocation
* next
;
3496 queue_iterate( &handlerInvocations
, next
,
3497 _IOServiceNotifierInvocation
*, link
) {
3498 if( next
->thread
!= current_thread() ) {
3504 state
|= kIOServiceNotifyWaiter
;
3511 void _IOServiceNotifier::free()
3513 assert( queue_empty( &handlerInvocations
));
3517 void _IOServiceNotifier::remove()
3522 whence
->removeObject( (OSObject
*) this );
3526 matching
->release();
3530 state
&= ~kIOServiceNotifyEnable
;
3539 bool _IOServiceNotifier::disable()
3545 ret
= (0 != (kIOServiceNotifyEnable
& state
));
3546 state
&= ~kIOServiceNotifyEnable
;
3555 void _IOServiceNotifier::enable( bool was
)
3559 state
|= kIOServiceNotifyEnable
;
3561 state
&= ~kIOServiceNotifyEnable
;
3569 IOService
* IOResources::resources( void )
3573 inst
= new IOResources
;
3574 if( inst
&& !inst
->init()) {
3582 IOWorkLoop
* IOResources::getWorkLoop() const
3584 // If we are the resource root then bringe over to the
3585 // platform to get its workloop
3586 if (this == (IOResources
*) gIOResources
)
3587 return getPlatform()->getWorkLoop();
3589 return IOService::getWorkLoop();
3592 bool IOResources::matchPropertyTable( OSDictionary
* table
)
3600 prop
= table
->getObject( gIOResourceMatchKey
);
3601 str
= OSDynamicCast( OSString
, prop
);
3603 ok
= (0 != getProperty( str
));
3605 else if( (set
= OSDynamicCast( OSSet
, prop
))) {
3607 iter
= OSCollectionIterator::withCollection( set
);
3609 while( ok
&& (str
= OSDynamicCast( OSString
, iter
->getNextObject()) ))
3610 ok
= (0 != getProperty( str
));
3619 IOReturn
IOResources::setProperties( OSObject
* properties
)
3622 const OSSymbol
* key
;
3623 OSDictionary
* dict
;
3624 OSCollectionIterator
* iter
;
3626 err
= IOUserClient::clientHasPrivilege(current_task(), kIOClientPrivilegeAdministrator
);
3627 if ( kIOReturnSuccess
!= err
)
3630 dict
= OSDynamicCast(OSDictionary
, properties
);
3632 return( kIOReturnBadArgument
);
3634 iter
= OSCollectionIterator::withCollection( dict
);
3636 return( kIOReturnBadArgument
);
3638 while( (key
= OSDynamicCast(OSSymbol
, iter
->getNextObject()))) {
3640 if (gIOConsoleUsersKey
== key
)
3642 IORegistryEntry::getRegistryRoot()->setProperty(key
, dict
->getObject(key
));
3643 OSIncrementAtomic( &gIOConsoleUsersSeed
);
3644 publishResource( gIOConsoleUsersSeedKey
, gIOConsoleUsersSeedValue
);
3648 publishResource( key
, dict
->getObject(key
) );
3653 return( kIOReturnSuccess
);
3657 * Helpers for matching dictionaries.
3658 * Keys existing in matching are checked in properties.
3659 * Keys may be a string or OSCollection of IOStrings
3662 bool IOService::compareProperty( OSDictionary
* matching
,
3668 value
= matching
->getObject( key
);
3670 ok
= value
->isEqualTo( getProperty( key
));
3678 bool IOService::compareProperty( OSDictionary
* matching
,
3679 const OSString
* key
)
3684 value
= matching
->getObject( key
);
3686 ok
= value
->isEqualTo( getProperty( key
));
3693 bool IOService::compareProperties( OSDictionary
* matching
,
3694 OSCollection
* keys
)
3696 OSCollectionIterator
* iter
;
3697 const OSString
* key
;
3700 if( !matching
|| !keys
)
3703 iter
= OSCollectionIterator::withCollection( keys
);
3706 while( ok
&& (key
= OSDynamicCast( OSString
, iter
->getNextObject())))
3707 ok
= compareProperty( matching
, key
);
3711 keys
->release(); // !! consume a ref !!
3716 /* Helper to add a location matching dict to the table */
3718 OSDictionary
* IOService::addLocation( OSDictionary
* table
)
3720 OSDictionary
* dict
;
3725 dict
= OSDictionary::withCapacity( 1 );
3727 table
->setObject( gIOLocationMatchKey
, dict
);
3735 * Go looking for a provider to match a location dict.
3738 IOService
* IOService::matchLocation( IOService
* /* client */ )
3742 parent
= getProvider();
3745 parent
= parent
->matchLocation( this );
3750 bool IOService::passiveMatch( OSDictionary
* table
, bool changesOK
)
3756 IORegistryEntry
* entry
;
3761 bool matchParent
= false;
3772 str
= OSDynamicCast( OSString
, table
->getObject( gIOProviderClassKey
));
3775 match
= (0 != where
->metaCast( str
));
3780 obj
= table
->getObject( gIONameMatchKey
);
3783 match
= where
->compareNames( obj
, changesOK
? &matched
: 0 );
3786 if( changesOK
&& matched
) {
3787 // leave a hint as to which name matched
3788 table
->setObject( gIONameMatchedKey
, matched
);
3793 str
= OSDynamicCast( OSString
, table
->getObject( gIOLocationMatchKey
));
3796 const OSSymbol
* sym
;
3800 sym
= where
->copyLocation();
3802 match
= sym
->isEqualTo( str
);
3809 obj
= table
->getObject( gIOPropertyMatchKey
);
3812 OSDictionary
* dict
;
3813 OSDictionary
* nextDict
;
3818 dict
= where
->dictionaryWithProperties();
3820 nextDict
= OSDynamicCast( OSDictionary
, obj
);
3824 iter
= OSCollectionIterator::withCollection(
3825 OSDynamicCast(OSCollection
, obj
));
3828 || (iter
&& (0 != (nextDict
= OSDynamicCast(OSDictionary
,
3829 iter
->getNextObject()))))) {
3830 match
= dict
->isEqualTo( nextDict
, nextDict
);
3843 str
= OSDynamicCast( OSString
, table
->getObject( gIOPathMatchKey
));
3846 entry
= IORegistryEntry::fromPath( str
->getCStringNoCopy() );
3847 match
= (where
== entry
);
3854 num
= OSDynamicCast( OSNumber
, table
->getObject( gIOMatchedServiceCountKey
));
3858 IOService
* service
= 0;
3859 UInt32 serviceCount
= 0;
3862 iter
= where
->getClientIterator();
3864 while( (service
= (IOService
*) iter
->getNextObject())) {
3865 if( kIOServiceInactiveState
& service
->__state
[0])
3867 if( 0 == service
->getProperty( gIOMatchCategoryKey
))
3873 match
= (serviceCount
== num
->unsigned32BitValue());
3878 if( done
== table
->getCount()) {
3879 // don't call family if we've done all the entries in the table
3880 matchParent
= false;
3884 // pass in score from property table
3885 score
= IOServiceObjectOrder( table
, (void *) gIOProbeScoreKey
);
3887 // do family specific matching
3888 match
= where
->matchPropertyTable( table
, &score
);
3892 if( kIOLogMatch
& getDebugFlags( table
))
3893 LOG("%s: family specific matching fails\n", where
->getName());
3900 newPri
= OSNumber::withNumber( score
, 32 );
3902 table
->setObject( gIOProbeScoreKey
, newPri
);
3907 if( !(match
= where
->compareProperty( table
, kIOBSDNameKey
)))
3910 matchParent
= false;
3912 obj
= OSDynamicCast( OSDictionary
,
3913 table
->getObject( gIOParentMatchKey
));
3917 table
= (OSDictionary
*) obj
;
3921 table
= OSDynamicCast( OSDictionary
,
3922 table
->getObject( gIOLocationMatchKey
));
3925 where
= where
->getProvider();
3927 where
= where
->matchLocation( where
);
3930 } while( table
&& where
);
3932 } while( matchParent
&& (where
= where
->getProvider()) );
3934 if( kIOLogMatch
& gIOKitDebug
)
3936 LOG("match parent @ %s = %d\n",
3937 where
->getName(), match
);
3943 IOReturn
IOService::newUserClient( task_t owningTask
, void * securityID
,
3944 UInt32 type
, OSDictionary
* properties
,
3945 IOUserClient
** handler
)
3947 const OSSymbol
*userClientClass
= 0;
3948 IOUserClient
*client
;
3951 // First try my own properties for a user client class name
3952 temp
= getProperty(gIOUserClientClassKey
);
3954 if (OSDynamicCast(OSSymbol
, temp
))
3955 userClientClass
= (const OSSymbol
*) temp
;
3956 else if (OSDynamicCast(OSString
, temp
)) {
3957 userClientClass
= OSSymbol::withString((OSString
*) temp
);
3958 if (userClientClass
)
3959 setProperty(kIOUserClientClassKey
,
3960 (OSObject
*) userClientClass
);
3964 // Didn't find one so lets just bomb out now without further ado.
3965 if (!userClientClass
)
3966 return kIOReturnUnsupported
;
3968 temp
= OSMetaClass::allocClassWithName(userClientClass
);
3970 return kIOReturnNoMemory
;
3972 if (OSDynamicCast(IOUserClient
, temp
))
3973 client
= (IOUserClient
*) temp
;
3976 return kIOReturnUnsupported
;
3979 if ( !client
->initWithTask(owningTask
, securityID
, type
, properties
) ) {
3981 return kIOReturnBadArgument
;
3984 if ( !client
->attach(this) ) {
3986 return kIOReturnUnsupported
;
3989 if ( !client
->start(this) ) {
3990 client
->detach(this);
3992 return kIOReturnUnsupported
;
3996 return kIOReturnSuccess
;
3999 IOReturn
IOService::newUserClient( task_t owningTask
, void * securityID
,
4000 UInt32 type
, IOUserClient
** handler
)
4002 return( newUserClient( owningTask
, securityID
, type
, 0, handler
));
4005 IOReturn
IOService::requestProbe( IOOptionBits options
)
4007 return( kIOReturnUnsupported
);
4011 * Convert an IOReturn to text. Subclasses which add additional
4012 * IOReturn's should override this method and call
4013 * super::stringFromReturn if the desired value is not found.
4016 const char * IOService::stringFromReturn( IOReturn rtn
)
4018 static const IONamedValue IOReturn_values
[] = {
4019 {kIOReturnSuccess
, "success" },
4020 {kIOReturnError
, "general error" },
4021 {kIOReturnNoMemory
, "memory allocation error" },
4022 {kIOReturnNoResources
, "resource shortage" },
4023 {kIOReturnIPCError
, "Mach IPC failure" },
4024 {kIOReturnNoDevice
, "no such device" },
4025 {kIOReturnNotPrivileged
, "privilege violation" },
4026 {kIOReturnBadArgument
, "invalid argument" },
4027 {kIOReturnLockedRead
, "device is read locked" },
4028 {kIOReturnLockedWrite
, "device is write locked" },
4029 {kIOReturnExclusiveAccess
, "device is exclusive access" },
4030 {kIOReturnBadMessageID
, "bad IPC message ID" },
4031 {kIOReturnUnsupported
, "unsupported function" },
4032 {kIOReturnVMError
, "virtual memory error" },
4033 {kIOReturnInternalError
, "internal driver error" },
4034 {kIOReturnIOError
, "I/O error" },
4035 {kIOReturnCannotLock
, "cannot acquire lock" },
4036 {kIOReturnNotOpen
, "device is not open" },
4037 {kIOReturnNotReadable
, "device is not readable" },
4038 {kIOReturnNotWritable
, "device is not writeable" },
4039 {kIOReturnNotAligned
, "alignment error" },
4040 {kIOReturnBadMedia
, "media error" },
4041 {kIOReturnStillOpen
, "device is still open" },
4042 {kIOReturnRLDError
, "rld failure" },
4043 {kIOReturnDMAError
, "DMA failure" },
4044 {kIOReturnBusy
, "device is busy" },
4045 {kIOReturnTimeout
, "I/O timeout" },
4046 {kIOReturnOffline
, "device is offline" },
4047 {kIOReturnNotReady
, "device is not ready" },
4048 {kIOReturnNotAttached
, "device/channel is not attached" },
4049 {kIOReturnNoChannels
, "no DMA channels available" },
4050 {kIOReturnNoSpace
, "no space for data" },
4051 {kIOReturnPortExists
, "device port already exists" },
4052 {kIOReturnCannotWire
, "cannot wire physical memory" },
4053 {kIOReturnNoInterrupt
, "no interrupt attached" },
4054 {kIOReturnNoFrames
, "no DMA frames enqueued" },
4055 {kIOReturnMessageTooLarge
, "message is too large" },
4056 {kIOReturnNotPermitted
, "operation is not permitted" },
4057 {kIOReturnNoPower
, "device is without power" },
4058 {kIOReturnNoMedia
, "media is not present" },
4059 {kIOReturnUnformattedMedia
, "media is not formatted" },
4060 {kIOReturnUnsupportedMode
, "unsupported mode" },
4061 {kIOReturnUnderrun
, "data underrun" },
4062 {kIOReturnOverrun
, "data overrun" },
4063 {kIOReturnDeviceError
, "device error" },
4064 {kIOReturnNoCompletion
, "no completion routine" },
4065 {kIOReturnAborted
, "operation was aborted" },
4066 {kIOReturnNoBandwidth
, "bus bandwidth would be exceeded" },
4067 {kIOReturnNotResponding
, "device is not responding" },
4068 {kIOReturnInvalid
, "unanticipated driver error" },
4072 return IOFindNameForValue(rtn
, IOReturn_values
);
4076 * Convert an IOReturn to an errno.
4078 int IOService::errnoFromReturn( IOReturn rtn
)
4082 case kIOReturnSuccess
:
4084 case kIOReturnNoMemory
:
4086 case kIOReturnNoDevice
:
4088 case kIOReturnVMError
:
4090 case kIOReturnNotPermitted
:
4092 case kIOReturnNotPrivileged
:
4094 case kIOReturnIOError
:
4096 case kIOReturnNotWritable
:
4098 case kIOReturnBadArgument
:
4100 case kIOReturnUnsupported
:
4104 case kIOReturnNoPower
:
4106 case kIOReturnDeviceError
:
4108 case kIOReturnTimeout
:
4110 case kIOReturnMessageTooLarge
:
4112 case kIOReturnNoSpace
:
4114 case kIOReturnCannotLock
:
4118 case kIOReturnBadMessageID
:
4119 case kIOReturnNoCompletion
:
4120 case kIOReturnNotAligned
:
4122 case kIOReturnNotReady
:
4124 case kIOReturnRLDError
:
4126 case kIOReturnPortExists
:
4127 case kIOReturnStillOpen
:
4129 case kIOReturnExclusiveAccess
:
4130 case kIOReturnLockedRead
:
4131 case kIOReturnLockedWrite
:
4132 case kIOReturnNotAttached
:
4133 case kIOReturnNotOpen
:
4134 case kIOReturnNotReadable
:
4136 case kIOReturnCannotWire
:
4137 case kIOReturnNoResources
:
4139 case kIOReturnAborted
:
4140 case kIOReturnOffline
:
4141 case kIOReturnNotResponding
:
4143 case kIOReturnBadMedia
:
4144 case kIOReturnNoMedia
:
4145 case kIOReturnUnformattedMedia
:
4146 return(ENXIO
); // (media error)
4147 case kIOReturnDMAError
:
4148 case kIOReturnOverrun
:
4149 case kIOReturnUnderrun
:
4150 return(EIO
); // (transfer error)
4151 case kIOReturnNoBandwidth
:
4152 case kIOReturnNoChannels
:
4153 case kIOReturnNoFrames
:
4154 case kIOReturnNoInterrupt
:
4155 return(EIO
); // (hardware error)
4156 case kIOReturnError
:
4157 case kIOReturnInternalError
:
4158 case kIOReturnInvalid
:
4159 return(EIO
); // (generic error)
4160 case kIOReturnIPCError
:
4161 return(EIO
); // (ipc error)
4163 return(EIO
); // (all other errors)
4167 IOReturn
IOService::message( UInt32 type
, IOService
* provider
,
4171 * Generic entry point for calls from the provider. A return value of
4172 * kIOReturnSuccess indicates that the message was received, and where
4173 * applicable, that it was successful.
4176 return kIOReturnUnsupported
;
4183 IOItemCount
IOService::getDeviceMemoryCount( void )
4188 array
= OSDynamicCast( OSArray
, getProperty( gIODeviceMemoryKey
));
4190 count
= array
->getCount();
4197 IODeviceMemory
* IOService::getDeviceMemoryWithIndex( unsigned int index
)
4200 IODeviceMemory
* range
;
4202 array
= OSDynamicCast( OSArray
, getProperty( gIODeviceMemoryKey
));
4204 range
= (IODeviceMemory
*) array
->getObject( index
);
4211 IOMemoryMap
* IOService::mapDeviceMemoryWithIndex( unsigned int index
,
4212 IOOptionBits options
)
4214 IODeviceMemory
* range
;
4217 range
= getDeviceMemoryWithIndex( index
);
4219 map
= range
->map( options
);
4226 OSArray
* IOService::getDeviceMemory( void )
4228 return( OSDynamicCast( OSArray
, getProperty( gIODeviceMemoryKey
)));
4232 void IOService::setDeviceMemory( OSArray
* array
)
4234 setProperty( gIODeviceMemoryKey
, array
);
4241 IOReturn
IOService::resolveInterrupt(IOService
*nub
, int source
)
4243 IOInterruptController
*interruptController
;
4246 OSSymbol
*interruptControllerName
;
4248 IOInterruptSource
*interruptSources
;
4250 // Get the parents list from the nub.
4251 array
= OSDynamicCast(OSArray
, nub
->getProperty(gIOInterruptControllersKey
));
4252 if (array
== 0) return kIOReturnNoResources
;
4254 // Allocate space for the IOInterruptSources if needed... then return early.
4255 if (nub
->_interruptSources
== 0) {
4256 numSources
= array
->getCount();
4257 interruptSources
= (IOInterruptSource
*)IOMalloc(numSources
* sizeof(IOInterruptSource
));
4258 if (interruptSources
== 0) return kIOReturnNoMemory
;
4260 bzero(interruptSources
, numSources
* sizeof(IOInterruptSource
));
4262 nub
->_numInterruptSources
= numSources
;
4263 nub
->_interruptSources
= interruptSources
;
4264 return kIOReturnSuccess
;
4267 interruptControllerName
= OSDynamicCast(OSSymbol
,array
->getObject(source
));
4268 if (interruptControllerName
== 0) return kIOReturnNoResources
;
4270 interruptController
= getPlatform()->lookUpInterruptController(interruptControllerName
);
4271 if (interruptController
== 0) return kIOReturnNoResources
;
4273 // Get the interrupt numbers from the nub.
4274 array
= OSDynamicCast(OSArray
, nub
->getProperty(gIOInterruptSpecifiersKey
));
4275 if (array
== 0) return kIOReturnNoResources
;
4276 data
= OSDynamicCast(OSData
, array
->getObject(source
));
4277 if (data
== 0) return kIOReturnNoResources
;
4279 // Set the interruptController and interruptSource in the nub's table.
4280 interruptSources
= nub
->_interruptSources
;
4281 interruptSources
[source
].interruptController
= interruptController
;
4282 interruptSources
[source
].vectorData
= data
;
4284 return kIOReturnSuccess
;
4287 IOReturn
IOService::lookupInterrupt(int source
, bool resolve
, IOInterruptController
**interruptController
)
4291 /* Make sure the _interruptSources are set */
4292 if (_interruptSources
== 0) {
4293 ret
= resolveInterrupt(this, source
);
4294 if (ret
!= kIOReturnSuccess
) return ret
;
4297 /* Make sure the local source number is valid */
4298 if ((source
< 0) || (source
>= _numInterruptSources
))
4299 return kIOReturnNoInterrupt
;
4301 /* Look up the contoller for the local source */
4302 *interruptController
= _interruptSources
[source
].interruptController
;
4304 if (*interruptController
== NULL
) {
4305 if (!resolve
) return kIOReturnNoInterrupt
;
4307 /* Try to reslove the interrupt */
4308 ret
= resolveInterrupt(this, source
);
4309 if (ret
!= kIOReturnSuccess
) return ret
;
4311 *interruptController
= _interruptSources
[source
].interruptController
;
4314 return kIOReturnSuccess
;
4317 IOReturn
IOService::registerInterrupt(int source
, OSObject
*target
,
4318 IOInterruptAction handler
,
4321 IOInterruptController
*interruptController
;
4324 ret
= lookupInterrupt(source
, true, &interruptController
);
4325 if (ret
!= kIOReturnSuccess
) return ret
;
4327 /* Register the source */
4328 return interruptController
->registerInterrupt(this, source
, target
,
4329 (IOInterruptHandler
)handler
,
4333 IOReturn
IOService::unregisterInterrupt(int source
)
4335 IOInterruptController
*interruptController
;
4338 ret
= lookupInterrupt(source
, false, &interruptController
);
4339 if (ret
!= kIOReturnSuccess
) return ret
;
4341 /* Unregister the source */
4342 return interruptController
->unregisterInterrupt(this, source
);
4345 IOReturn
IOService::getInterruptType(int source
, int *interruptType
)
4347 IOInterruptController
*interruptController
;
4350 ret
= lookupInterrupt(source
, true, &interruptController
);
4351 if (ret
!= kIOReturnSuccess
) return ret
;
4353 /* Return the type */
4354 return interruptController
->getInterruptType(this, source
, interruptType
);
4357 IOReturn
IOService::enableInterrupt(int source
)
4359 IOInterruptController
*interruptController
;
4362 ret
= lookupInterrupt(source
, false, &interruptController
);
4363 if (ret
!= kIOReturnSuccess
) return ret
;
4365 /* Enable the source */
4366 return interruptController
->enableInterrupt(this, source
);
4369 IOReturn
IOService::disableInterrupt(int source
)
4371 IOInterruptController
*interruptController
;
4374 ret
= lookupInterrupt(source
, false, &interruptController
);
4375 if (ret
!= kIOReturnSuccess
) return ret
;
4377 /* Disable the source */
4378 return interruptController
->disableInterrupt(this, source
);
4381 IOReturn
IOService::causeInterrupt(int source
)
4383 IOInterruptController
*interruptController
;
4386 ret
= lookupInterrupt(source
, false, &interruptController
);
4387 if (ret
!= kIOReturnSuccess
) return ret
;
4389 /* Cause an interrupt for the source */
4390 return interruptController
->causeInterrupt(this, source
);
4393 OSMetaClassDefineReservedUsed(IOService
, 0);
4394 OSMetaClassDefineReservedUsed(IOService
, 1);
4395 OSMetaClassDefineReservedUsed(IOService
, 2);
4397 OSMetaClassDefineReservedUnused(IOService
, 3);
4398 OSMetaClassDefineReservedUnused(IOService
, 4);
4399 OSMetaClassDefineReservedUnused(IOService
, 5);
4400 OSMetaClassDefineReservedUnused(IOService
, 6);
4401 OSMetaClassDefineReservedUnused(IOService
, 7);
4402 OSMetaClassDefineReservedUnused(IOService
, 8);
4403 OSMetaClassDefineReservedUnused(IOService
, 9);
4404 OSMetaClassDefineReservedUnused(IOService
, 10);
4405 OSMetaClassDefineReservedUnused(IOService
, 11);
4406 OSMetaClassDefineReservedUnused(IOService
, 12);
4407 OSMetaClassDefineReservedUnused(IOService
, 13);
4408 OSMetaClassDefineReservedUnused(IOService
, 14);
4409 OSMetaClassDefineReservedUnused(IOService
, 15);
4410 OSMetaClassDefineReservedUnused(IOService
, 16);
4411 OSMetaClassDefineReservedUnused(IOService
, 17);
4412 OSMetaClassDefineReservedUnused(IOService
, 18);
4413 OSMetaClassDefineReservedUnused(IOService
, 19);
4414 OSMetaClassDefineReservedUnused(IOService
, 20);
4415 OSMetaClassDefineReservedUnused(IOService
, 21);
4416 OSMetaClassDefineReservedUnused(IOService
, 22);
4417 OSMetaClassDefineReservedUnused(IOService
, 23);
4418 OSMetaClassDefineReservedUnused(IOService
, 24);
4419 OSMetaClassDefineReservedUnused(IOService
, 25);
4420 OSMetaClassDefineReservedUnused(IOService
, 26);
4421 OSMetaClassDefineReservedUnused(IOService
, 27);
4422 OSMetaClassDefineReservedUnused(IOService
, 28);
4423 OSMetaClassDefineReservedUnused(IOService
, 29);
4424 OSMetaClassDefineReservedUnused(IOService
, 30);
4425 OSMetaClassDefineReservedUnused(IOService
, 31);
4426 OSMetaClassDefineReservedUnused(IOService
, 32);
4427 OSMetaClassDefineReservedUnused(IOService
, 33);
4428 OSMetaClassDefineReservedUnused(IOService
, 34);
4429 OSMetaClassDefineReservedUnused(IOService
, 35);
4430 OSMetaClassDefineReservedUnused(IOService
, 36);
4431 OSMetaClassDefineReservedUnused(IOService
, 37);
4432 OSMetaClassDefineReservedUnused(IOService
, 38);
4433 OSMetaClassDefineReservedUnused(IOService
, 39);
4434 OSMetaClassDefineReservedUnused(IOService
, 40);
4435 OSMetaClassDefineReservedUnused(IOService
, 41);
4436 OSMetaClassDefineReservedUnused(IOService
, 42);
4437 OSMetaClassDefineReservedUnused(IOService
, 43);
4438 OSMetaClassDefineReservedUnused(IOService
, 44);
4439 OSMetaClassDefineReservedUnused(IOService
, 45);
4440 OSMetaClassDefineReservedUnused(IOService
, 46);
4441 OSMetaClassDefineReservedUnused(IOService
, 47);
4442 OSMetaClassDefineReservedUnused(IOService
, 48);
4443 OSMetaClassDefineReservedUnused(IOService
, 49);
4444 OSMetaClassDefineReservedUnused(IOService
, 50);
4445 OSMetaClassDefineReservedUnused(IOService
, 51);
4446 OSMetaClassDefineReservedUnused(IOService
, 52);
4447 OSMetaClassDefineReservedUnused(IOService
, 53);
4448 OSMetaClassDefineReservedUnused(IOService
, 54);
4449 OSMetaClassDefineReservedUnused(IOService
, 55);
4450 OSMetaClassDefineReservedUnused(IOService
, 56);
4451 OSMetaClassDefineReservedUnused(IOService
, 57);
4452 OSMetaClassDefineReservedUnused(IOService
, 58);
4453 OSMetaClassDefineReservedUnused(IOService
, 59);
4454 OSMetaClassDefineReservedUnused(IOService
, 60);
4455 OSMetaClassDefineReservedUnused(IOService
, 61);
4456 OSMetaClassDefineReservedUnused(IOService
, 62);
4457 OSMetaClassDefineReservedUnused(IOService
, 63);