2 * Copyright (c) 1998-2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
32 * 12 Nov 98 sdouglas created.
36 #include <IOKit/IORegistryEntry.h>
37 #include <libkern/c++/OSContainers.h>
38 #include <IOKit/IOService.h>
39 #include <IOKit/IOKitKeys.h>
41 #include <IOKit/IOLib.h>
43 #include <IOKit/assert.h>
45 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
47 #define super OSObject
49 OSDefineMetaClassAndStructors(IORegistryEntry
, OSObject
)
51 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
53 #define kIORegPlaneParentSuffix "ParentLinks"
54 #define kIORegPlaneChildSuffix "ChildLinks"
55 #define kIORegPlaneNameSuffix "Name"
56 #define kIORegPlaneLocationSuffix "Location"
58 #define kIORegPlaneParentSuffixLen (sizeof(kIORegPlaneParentSuffix) - 1)
59 #define kIORegPlaneChildSuffixLen (sizeof(kIORegPlaneChildSuffix) - 1)
60 #define kIORegPlaneNameSuffixLen (sizeof(kIORegPlaneNameSuffix) - 1)
61 #define kIORegPlaneLocationSuffixLen (sizeof(kIORegPlaneLocationSuffix) - 1)
63 static IORegistryEntry
* gRegistryRoot
;
64 static OSDictionary
* gIORegistryPlanes
;
66 const OSSymbol
* gIONameKey
;
67 const OSSymbol
* gIOLocationKey
;
68 const OSSymbol
* gIORegistryEntryIDKey
;
79 enum { kIORegistryIDReserved
= (1ULL << 32) + 255 };
81 static uint64_t gIORegistryLastID
= kIORegistryIDReserved
;
83 class IORegistryPlane
: public OSObject
{
85 friend class IORegistryEntry
;
87 OSDeclareAbstractStructors(IORegistryPlane
)
89 const OSSymbol
* nameKey
;
90 const OSSymbol
* keys
[ kNumSetIndex
];
91 const OSSymbol
* pathNameKey
;
92 const OSSymbol
* pathLocationKey
;
96 virtual bool serialize(OSSerialize
*s
) const;
99 OSDefineMetaClassAndStructors(IORegistryPlane
, OSObject
)
102 static IORecursiveLock
* gPropertiesLock
;
103 static SInt32 gIORegistryGenerationCount
;
105 #define UNLOCK lck_rw_done( &gIORegistryLock )
106 #define RLOCK lck_rw_lock_shared( &gIORegistryLock )
107 #define WLOCK lck_rw_lock_exclusive( &gIORegistryLock ); \
108 gIORegistryGenerationCount++
111 #define PUNLOCK IORecursiveLockUnlock( gPropertiesLock )
112 #define PLOCK IORecursiveLockLock( gPropertiesLock )
114 #define IOREGSPLITTABLES
116 #ifdef IOREGSPLITTABLES
117 #define registryTable() fRegistryTable
119 #define registryTable() fPropertyTable
124 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
126 lck_rw_t gIORegistryLock
;
127 lck_grp_t
*gIORegistryLockGrp
;
128 lck_grp_attr_t
*gIORegistryLockGrpAttr
;
129 lck_attr_t
*gIORegistryLockAttr
;
132 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
134 IORegistryEntry
* IORegistryEntry::initialize( void )
138 if( !gRegistryRoot
) {
141 gIORegistryLockGrpAttr
= lck_grp_attr_alloc_init();
142 //lck_grp_attr_setstat(gIORegistryLockGrpAttr);
143 gIORegistryLockGrp
= lck_grp_alloc_init("IORegistryLock", gIORegistryLockGrpAttr
);
144 gIORegistryLockAttr
= lck_attr_alloc_init();
145 lck_attr_rw_shared_priority(gIORegistryLockAttr
);
146 //lck_attr_setdebug(gIORegistryLockAttr);
147 lck_rw_init( &gIORegistryLock
, gIORegistryLockGrp
, gIORegistryLockAttr
);
149 gRegistryRoot
= new IORegistryEntry
;
150 gPropertiesLock
= IORecursiveLockAlloc();
151 gIORegistryPlanes
= OSDictionary::withCapacity( 1 );
153 assert( gRegistryRoot
&& gPropertiesLock
154 && gIORegistryPlanes
);
155 ok
= gRegistryRoot
->init();
158 gRegistryRoot
->reserved
->fRegistryEntryID
= ++gIORegistryLastID
;
160 gIONameKey
= OSSymbol::withCStringNoCopy( "IOName" );
161 gIOLocationKey
= OSSymbol::withCStringNoCopy( "IOLocation" );
162 gIORegistryEntryIDKey
= OSSymbol::withCStringNoCopy( kIORegistryEntryIDKey
);
164 assert( ok
&& gIONameKey
&& gIOLocationKey
);
166 gRegistryRoot
->setName( "Root" );
167 gRegistryRoot
->setProperty( kIORegistryPlanesKey
, gIORegistryPlanes
);
170 return( gRegistryRoot
);
173 IORegistryEntry
* IORegistryEntry::getRegistryRoot( void )
175 return( gRegistryRoot
);
178 SInt32
IORegistryEntry::getGenerationCount( void )
180 return( gIORegistryGenerationCount
);
184 const IORegistryPlane
* IORegistryEntry::makePlane( const char * name
)
186 IORegistryPlane
* plane
;
187 const OSSymbol
* nameKey
;
188 const OSSymbol
* parentKey
;
189 const OSSymbol
* childKey
;
190 const OSSymbol
* pathNameKey
;
191 const OSSymbol
* pathLocationKey
;
192 char key
[ kIOMaxPlaneName
+ 16 ];
195 strlcpy( key
, name
, kIOMaxPlaneName
+ 1 );
196 end
= key
+ strlen( key
);
198 nameKey
= OSSymbol::withCString( key
);
200 strlcpy( end
, kIORegPlaneParentSuffix
, kIORegPlaneParentSuffixLen
+ 1 );
201 parentKey
= OSSymbol::withCString( key
);
203 strlcpy( end
, kIORegPlaneChildSuffix
, kIORegPlaneChildSuffixLen
+ 1 );
204 childKey
= OSSymbol::withCString( key
);
206 strlcpy( end
, kIORegPlaneNameSuffix
, kIORegPlaneNameSuffixLen
+ 1 );
207 pathNameKey
= OSSymbol::withCString( key
);
209 strlcpy( end
, kIORegPlaneLocationSuffix
, kIORegPlaneLocationSuffixLen
+ 1 );
210 pathLocationKey
= OSSymbol::withCString( key
);
212 plane
= new IORegistryPlane
;
214 if( plane
&& plane
->init()
215 && nameKey
&& parentKey
&& childKey
216 && pathNameKey
&& pathLocationKey
) {
218 plane
->nameKey
= nameKey
;
219 plane
->keys
[ kParentSetIndex
] = parentKey
;
220 plane
->keys
[ kChildSetIndex
] = childKey
;
221 plane
->pathNameKey
= pathNameKey
;
222 plane
->pathLocationKey
= pathLocationKey
;
225 gIORegistryPlanes
->setObject( nameKey
, plane
);
233 pathLocationKey
->release();
235 pathNameKey
->release();
237 parentKey
->release();
248 const IORegistryPlane
* IORegistryEntry::getPlane( const char * name
)
250 const IORegistryPlane
* plane
;
253 plane
= (const IORegistryPlane
*) gIORegistryPlanes
->getObject( name
);
259 bool IORegistryPlane::serialize(OSSerialize
*s
) const
261 return( nameKey
->serialize(s
) );
264 enum { kIORegCapacityIncrement
= 4 };
266 bool IORegistryEntry::init( OSDictionary
* dict
)
275 reserved
= IONew(ExpansionData
, 1);
278 bzero(reserved
, sizeof(ExpansionData
));
283 fPropertyTable
->release();
284 fPropertyTable
= dict
;
286 } else if( !fPropertyTable
) {
287 fPropertyTable
= OSDictionary::withCapacity( kIORegCapacityIncrement
);
289 fPropertyTable
->setCapacityIncrement( kIORegCapacityIncrement
);
295 #ifdef IOREGSPLITTABLES
296 if( !fRegistryTable
) {
297 fRegistryTable
= OSDictionary::withCapacity( kIORegCapacityIncrement
);
299 fRegistryTable
->setCapacityIncrement( kIORegCapacityIncrement
);
302 if( (prop
= OSDynamicCast( OSString
, getProperty( gIONameKey
)))) {
303 OSSymbol
* sym
= (OSSymbol
*)OSSymbol::withString( prop
);
304 // ok for OSSymbol too
309 #endif /* IOREGSPLITTABLES */
314 bool IORegistryEntry::init( IORegistryEntry
* old
,
315 const IORegistryPlane
* plane
)
318 IORegistryEntry
* next
;
326 reserved
= old
->reserved
;
327 old
->reserved
= NULL
;
329 fPropertyTable
= old
->getPropertyTable();
330 fPropertyTable
->retain();
331 #ifdef IOREGSPLITTABLES
332 fRegistryTable
= old
->fRegistryTable
;
333 old
->fRegistryTable
= OSDictionary::withDictionary( fRegistryTable
);
334 #endif /* IOREGSPLITTABLES */
336 old
->registryTable()->removeObject( plane
->keys
[ kParentSetIndex
] );
337 old
->registryTable()->removeObject( plane
->keys
[ kChildSetIndex
] );
339 all
= getParentSetReference( plane
);
340 if( all
) for( index
= 0;
341 (next
= (IORegistryEntry
*) all
->getObject(index
));
343 next
->makeLink( this, kChildSetIndex
, plane
);
344 next
->breakLink( old
, kChildSetIndex
, plane
);
347 all
= getChildSetReference( plane
);
348 if( all
) for( index
= 0;
349 (next
= (IORegistryEntry
*) all
->getObject(index
));
351 next
->makeLink( this, kParentSetIndex
, plane
);
352 next
->breakLink( old
, kParentSetIndex
, plane
);
360 void IORegistryEntry::free( void )
363 if( registryTable() && gIOServicePlane
) {
364 if( getParentSetReference( gIOServicePlane
)
365 || getChildSetReference( gIOServicePlane
)) {
366 panic("%s: attached at free()", getName());
371 if( getPropertyTable())
372 getPropertyTable()->release();
374 #ifdef IOREGSPLITTABLES
376 registryTable()->release();
377 #endif /* IOREGSPLITTABLES */
380 IODelete(reserved
, ExpansionData
, 1);
385 void IORegistryEntry::setPropertyTable( OSDictionary
* dict
)
388 fPropertyTable
->release();
391 fPropertyTable
= dict
;
394 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
396 /* Wrappers to synchronize property table */
398 #define wrap2(type, constant) \
400 IORegistryEntry::copyProperty( type * aKey) constant \
405 obj = getProperty( aKey ); \
413 #define wrap4(type,constant) \
415 IORegistryEntry::getProperty( type * aKey, \
416 const IORegistryPlane * plane, \
417 IOOptionBits options ) constant \
419 OSObject * obj = getProperty( aKey ); \
421 if ( (0 == obj) && plane && (options & kIORegistryIterateRecursively) ) { \
422 IORegistryEntry * entry = (IORegistryEntry *) this; \
423 IORegistryIterator * iter; \
424 iter = IORegistryIterator::iterateOver( entry, plane, options ); \
427 while ( (0 == obj) && (entry = iter->getNextObject()) ) { \
428 obj = entry->getProperty( aKey ); \
437 #define wrap5(type,constant) \
439 IORegistryEntry::copyProperty( type * aKey, \
440 const IORegistryPlane * plane, \
441 IOOptionBits options ) constant \
443 OSObject * obj = copyProperty( aKey ); \
445 if ( (0 == obj) && plane && (options & kIORegistryIterateRecursively) ) { \
446 IORegistryEntry * entry = (IORegistryEntry *) this; \
447 IORegistryIterator * iter; \
448 iter = IORegistryIterator::iterateOver( entry, plane, options ); \
451 while ( (0 == obj) && (entry = iter->getNextObject()) ) { \
452 obj = entry->copyProperty( aKey ); \
461 bool IORegistryEntry::serializeProperties( OSSerialize
* s
) const
463 // setProperty( getRetainCount(), 32, "__retain" );
466 OSCollection
*snapshotProperties
= getPropertyTable()->copyCollection();
469 bool ok
= snapshotProperties
->serialize( s
);
470 snapshotProperties
->release();
474 OSDictionary
* IORegistryEntry::dictionaryWithProperties( void ) const
479 dict
= OSDictionary::withDictionary( getPropertyTable(),
480 getPropertyTable()->getCapacity() );
486 IOReturn
IORegistryEntry::setProperties( OSObject
* properties
)
488 return( kIOReturnUnsupported
);
491 wrap2(const OSSymbol
, const) // copyProperty() definition
492 wrap2(const OSString
, const) // copyProperty() definition
493 wrap2(const char, const) // copyProperty() definition
495 wrap4(const OSSymbol
, const) // getProperty() w/plane definition
496 wrap4(const OSString
, const) // getProperty() w/plane definition
497 wrap4(const char, const) // getProperty() w/plane definition
499 wrap5(const OSSymbol
, const) // copyProperty() w/plane definition
500 wrap5(const OSString
, const) // copyProperty() w/plane definition
501 wrap5(const char, const) // copyProperty() w/plane definition
505 IORegistryEntry::getProperty( const OSSymbol
* aKey
) const
510 obj
= getPropertyTable()->getObject( aKey
);
517 IORegistryEntry::removeProperty( const OSSymbol
* aKey
)
520 getPropertyTable()->removeObject( aKey
);
525 IORegistryEntry::setProperty( const OSSymbol
* aKey
, OSObject
* anObject
)
529 // If we are inserting a collection class and the current entry
530 // is attached into the registry (inPlane()) then mark the collection
532 OSCollection
*coll
= OSDynamicCast(OSCollection
, anObject
);
533 bool makeImmutable
= (coll
&& inPlane());
537 coll
->setOptions( OSCollection::kMASK
, OSCollection::kImmutable
);
539 ret
= getPropertyTable()->setObject( aKey
, anObject
);
545 IOReturn
IORegistryEntry::
546 runPropertyAction(Action inAction
, OSObject
*target
,
547 void *arg0
, void *arg1
, void *arg2
, void *arg3
)
551 // closeGate is recursive so don't worry if we already hold the lock.
553 res
= (*inAction
)(target
, arg0
, arg1
, arg2
, arg3
);
560 IORegistryEntry::getProperty( const OSString
* aKey
) const
562 const OSSymbol
* tmpKey
= OSSymbol::withString( aKey
);
563 OSObject
* obj
= getProperty( tmpKey
);
570 IORegistryEntry::getProperty( const char * aKey
) const
572 const OSSymbol
* tmpKey
= OSSymbol::withCString( aKey
);
573 OSObject
* obj
= getProperty( tmpKey
);
581 IORegistryEntry::removeProperty( const OSString
* aKey
)
583 const OSSymbol
* tmpKey
= OSSymbol::withString( aKey
);
584 removeProperty( tmpKey
);
589 IORegistryEntry::removeProperty( const char * aKey
)
591 const OSSymbol
* tmpKey
= OSSymbol::withCString( aKey
);
592 removeProperty( tmpKey
);
597 IORegistryEntry::setProperty( const OSString
* aKey
, OSObject
* anObject
)
599 const OSSymbol
* tmpKey
= OSSymbol::withString( aKey
);
600 bool ret
= setProperty( tmpKey
, anObject
);
607 IORegistryEntry::setProperty( const char * aKey
, OSObject
* anObject
)
609 const OSSymbol
* tmpKey
= OSSymbol::withCString( aKey
);
610 bool ret
= setProperty( tmpKey
, anObject
);
617 IORegistryEntry::setProperty(const char * aKey
, const char * aString
)
620 OSSymbol
* aSymbol
= (OSSymbol
*) OSSymbol::withCString( aString
);
623 const OSSymbol
* tmpKey
= OSSymbol::withCString( aKey
);
624 ret
= setProperty( tmpKey
, aSymbol
);
633 IORegistryEntry::setProperty(const char * aKey
, bool aBoolean
)
636 OSBoolean
* aBooleanObj
= OSBoolean::withBoolean( aBoolean
);
639 const OSSymbol
* tmpKey
= OSSymbol::withCString( aKey
);
640 ret
= setProperty( tmpKey
, aBooleanObj
);
643 aBooleanObj
->release();
649 IORegistryEntry::setProperty( const char * aKey
,
650 unsigned long long aValue
,
651 unsigned int aNumberOfBits
)
654 OSNumber
* anOffset
= OSNumber::withNumber( aValue
, aNumberOfBits
);
657 const OSSymbol
* tmpKey
= OSSymbol::withCString( aKey
);
658 ret
= setProperty( tmpKey
, anOffset
);
667 IORegistryEntry::setProperty( const char * aKey
,
672 OSData
* data
= OSData::withBytes( bytes
, length
);
675 const OSSymbol
* tmpKey
= OSSymbol::withCString( aKey
);
676 ret
= setProperty( tmpKey
, data
);
684 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
686 /* Name, location, paths */
688 const char * IORegistryEntry::getName( const IORegistryPlane
* plane
) const
694 sym
= (OSSymbol
*) registryTable()->getObject( plane
->pathNameKey
);
696 sym
= (OSSymbol
*) registryTable()->getObject( gIONameKey
);
700 return( sym
->getCStringNoCopy());
702 return( (getMetaClass())->getClassName());
705 const OSSymbol
* IORegistryEntry::copyName(
706 const IORegistryPlane
* plane
) const
712 sym
= (OSSymbol
*) registryTable()->getObject( plane
->pathNameKey
);
714 sym
= (OSSymbol
*) registryTable()->getObject( gIONameKey
);
722 return( OSSymbol::withCString((getMetaClass())->getClassName()) );
725 const OSSymbol
* IORegistryEntry::copyLocation(
726 const IORegistryPlane
* plane
) const
732 sym
= (OSSymbol
*) registryTable()->getObject( plane
->pathLocationKey
);
734 sym
= (OSSymbol
*) registryTable()->getObject( gIOLocationKey
);
742 const char * IORegistryEntry::getLocation( const IORegistryPlane
* plane
) const
744 const OSSymbol
* sym
= copyLocation( plane
);
745 const char * result
= 0;
748 result
= sym
->getCStringNoCopy();
755 void IORegistryEntry::setName( const OSSymbol
* name
,
756 const IORegistryPlane
* plane
)
758 const OSSymbol
* key
;
762 key
= plane
->pathNameKey
;
767 registryTable()->setObject( key
, (OSObject
*) name
);
772 void IORegistryEntry::setName( const char * name
,
773 const IORegistryPlane
* plane
)
775 OSSymbol
* sym
= (OSSymbol
*)OSSymbol::withCString( name
);
777 setName( sym
, plane
);
782 void IORegistryEntry::setLocation( const OSSymbol
* location
,
783 const IORegistryPlane
* plane
)
785 const OSSymbol
* key
;
789 key
= plane
->pathLocationKey
;
791 key
= gIOLocationKey
;
794 registryTable()->setObject( key
, (OSObject
*) location
);
799 void IORegistryEntry::setLocation( const char * location
,
800 const IORegistryPlane
* plane
)
802 OSSymbol
* sym
= (OSSymbol
*)OSSymbol::withCString( location
);
804 setLocation( sym
, plane
);
810 IORegistryEntry::compareName( OSString
* name
, OSString
** matched
) const
812 const OSSymbol
* sym
= copyName();
815 isEqual
= sym
->isEqualTo( name
);
817 if( isEqual
&& matched
) {
829 IORegistryEntry::compareNames( OSObject
* names
, OSString
** matched
) const
832 OSCollection
* collection
;
833 OSIterator
* iter
= 0;
836 if( (collection
= OSDynamicCast( OSCollection
, names
))) {
837 iter
= OSCollectionIterator::withCollection( collection
);
840 string
= OSDynamicCast( OSString
, names
);
844 result
= compareName( string
, matched
);
846 } while( (false == result
)
847 && iter
&& (string
= OSDynamicCast( OSString
, iter
->getNextObject())));
856 bool IORegistryEntry::getPath( char * path
, int * length
,
857 const IORegistryPlane
* plane
) const
860 IORegistryEntry
* root
;
861 const IORegistryEntry
* entry
;
862 IORegistryEntry
* parent
;
863 const OSSymbol
* alias
;
865 int len
, maxLength
, compLen
, aliasLen
;
869 if( !path
|| !length
|| !plane
)
873 maxLength
= *length
- 2;
876 len
= plane
->nameKey
->getLength();
877 if( len
>= maxLength
)
879 strlcpy( nextComp
, plane
->nameKey
->getCStringNoCopy(), len
+ 1);
880 nextComp
[ len
++ ] = ':';
883 if( (alias
= hasAlias( plane
))) {
884 aliasLen
= alias
->getLength();
886 ok
= (maxLength
> len
);
889 strlcpy( nextComp
, alias
->getCStringNoCopy(), aliasLen
+ 1);
894 parent
= entry
->getParentEntry( plane
);
896 // Error if not attached in plane
899 stack
= OSArray::withCapacity( getDepth( plane
));
905 root
= gRegistryRoot
->getChildEntry( plane
);
906 while( parent
&& (entry
!= root
)) {
908 stack
->setObject( (OSObject
*) entry
);
910 parent
= entry
->getParentEntry( plane
);
913 index
= stack
->getCount();
922 } else while( ok
&& ((--index
) >= 0)) {
924 entry
= (IORegistryEntry
*) stack
->getObject((unsigned int) index
);
927 if( (alias
= entry
->hasAlias( plane
))) {
928 len
= plane
->nameKey
->getLength() + 1;
929 nextComp
= path
+ len
;
931 compLen
= alias
->getLength();
932 ok
= (maxLength
> (len
+ compLen
));
934 strlcpy( nextComp
, alias
->getCStringNoCopy(), compLen
+ 1);
936 compLen
= maxLength
- len
;
937 ok
= entry
->getPathComponent( nextComp
+ 1, &compLen
, plane
);
959 bool IORegistryEntry::getPathComponent( char * path
, int * length
,
960 const IORegistryPlane
* plane
) const
962 int len
, locLen
, maxLength
;
963 const char * compName
;
969 compName
= getName( plane
);
970 len
= strlen( compName
);
971 if( (loc
= getLocation( plane
)))
972 locLen
= 1 + strlen( loc
);
976 ok
= ((len
+ locLen
+ 1) < maxLength
);
978 strlcpy( path
, compName
, len
+ 1 );
983 strlcpy( path
, loc
, locLen
);
991 const char * IORegistryEntry::matchPathLocation( const char * cmp
,
992 const IORegistryPlane
* plane
)
995 const char * result
= 0;
997 char lastPathChar
, lastLocationChar
;
999 str
= getLocation( plane
);
1001 lastPathChar
= cmp
[0];
1002 lastLocationChar
= str
[0];
1005 num1
= strtouq( cmp
, (char **) &cmp
, 16 );
1006 lastPathChar
= *cmp
++;
1010 if( lastLocationChar
) {
1011 num2
= strtouq( str
, (char **) &str
, 16 );
1012 lastLocationChar
= *str
++;
1019 if (!lastPathChar
&& !lastLocationChar
) {
1024 if( (',' != lastPathChar
) && (':' != lastPathChar
))
1027 if (lastPathChar
&& lastLocationChar
&& (lastPathChar
!= lastLocationChar
))
1036 IORegistryEntry
* IORegistryEntry::getChildFromComponent( const char ** opath
,
1037 const IORegistryPlane
* plane
)
1039 IORegistryEntry
* entry
= 0;
1043 const char * cmp
= 0;
1048 set
= getChildSetReference( plane
);
1054 (entry
= (IORegistryEntry
*) set
->getObject(index
));
1060 str
= entry
->getName( plane
);
1061 len
= strlen( str
);
1062 if( strncmp( str
, cmp
, len
))
1067 if( (c
== 0) || (c
== '/') || (c
== ':'))
1073 if( (cmp
= entry
->matchPathLocation( cmp
, plane
)))
1083 const OSSymbol
* IORegistryEntry::hasAlias( const IORegistryPlane
* plane
,
1084 char * opath
, int * length
) const
1086 IORegistryEntry
* entry
;
1087 IORegistryEntry
* entry2
;
1088 const OSSymbol
* key
;
1089 const OSSymbol
* bestKey
= 0;
1092 const char * path
= "/aliases";
1094 entry
= IORegistryEntry::fromPath( path
, plane
);
1097 if( (iter
= OSCollectionIterator::withCollection(
1098 entry
->getPropertyTable() ))) {
1100 while( (key
= (OSSymbol
*) iter
->getNextObject())) {
1102 data
= (OSData
*) entry
->getProperty( key
);
1103 path
= (const char *) data
->getBytesNoCopy();
1104 if( (entry2
= IORegistryEntry::fromPath( path
, plane
,
1106 if( this == entry2
) {
1108 || (bestKey
->getLength() > key
->getLength()))
1109 // pick the smallest alias
1123 const char * IORegistryEntry::dealiasPath(
1124 const char ** opath
,
1125 const IORegistryPlane
* plane
)
1127 IORegistryEntry
* entry
;
1129 const char * path
= *opath
;
1130 const char * rpath
= 0;
1133 char temp
[ kIOMaxPlaneName
+ 1 ];
1140 while( (c
= *end
++) && (c
!= '/') && (c
!= ':'))
1143 if( (end
- path
) < kIOMaxPlaneName
) {
1144 strlcpy( temp
, path
, end
- path
+ 1 );
1147 entry
= IORegistryEntry::fromPath( "/aliases", plane
);
1149 data
= (OSData
*) entry
->getProperty( temp
);
1151 rpath
= (const char *) data
->getBytesNoCopy();
1163 IORegistryEntry
* IORegistryEntry::fromPath(
1165 const IORegistryPlane
* plane
,
1168 IORegistryEntry
* fromEntry
)
1170 IORegistryEntry
* where
= 0;
1171 IORegistryEntry
* aliasEntry
= 0;
1172 IORegistryEntry
* next
;
1178 char temp
[ kIOMaxPlaneName
+ 1 ];
1185 end
= strchr( path
, ':' );
1186 if( end
&& ((end
- path
) < kIOMaxPlaneName
)) {
1187 strlcpy( temp
, path
, end
- path
+ 1 );
1188 plane
= getPlane( temp
);
1197 if( (alias
= dealiasPath( &end
, plane
))) {
1200 aliasEntry
= IORegistryEntry::fromPath( alias
, plane
,
1201 opath
, &len
, fromEntry
);
1213 if( (0 == fromEntry
) && (*path
++ == '/'))
1214 fromEntry
= gRegistryRoot
->getChildEntry( plane
);
1221 if( c
&& (c
!= ':')) // check valid terminator
1226 next
= where
->getChildFromComponent( &path
, plane
);
1232 // check residual path
1233 if( where
!= fromEntry
)
1236 if( opath
&& length
) {
1237 // copy out residual path
1238 len2
= strlen( path
);
1239 if( (len
+ len2
) < *length
)
1240 strlcpy( opath
+ len
, path
, len2
+ 1 );
1241 *length
= (len
+ len2
);
1244 // no residual path => must be no tail for success
1251 aliasEntry
->release();
1258 IORegistryEntry
* IORegistryEntry::childFromPath(
1260 const IORegistryPlane
* plane
,
1264 return( IORegistryEntry::fromPath( path
, plane
, opath
, len
, this ));
1267 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1269 #define IOLinkIterator OSCollectionIterator
1272 #define super OSObject
1274 inline bool IORegistryEntry::arrayMember( OSArray
* set
,
1275 const IORegistryEntry
* member
,
1276 unsigned int * index
) const
1279 OSObject
* probeObject
;
1281 for( i
= 0; (probeObject
= set
->getObject(i
)); i
++) {
1282 if (probeObject
== (OSObject
*) member
) {
1291 bool IORegistryEntry::makeLink( IORegistryEntry
* to
,
1292 unsigned int relation
,
1293 const IORegistryPlane
* plane
) const
1296 bool result
= false;
1298 if( (links
= (OSArray
*)
1299 registryTable()->getObject( plane
->keys
[ relation
] ))) {
1301 result
= arrayMember( links
, to
);
1303 result
= links
->setObject( to
);
1307 links
= OSArray::withObjects( (const OSObject
**) &to
, 1, 1 );
1308 result
= (links
!= 0);
1310 result
= registryTable()->setObject( plane
->keys
[ relation
],
1319 void IORegistryEntry::breakLink( IORegistryEntry
* to
,
1320 unsigned int relation
,
1321 const IORegistryPlane
* plane
) const
1326 if( (links
= (OSArray
*)
1327 registryTable()->getObject( plane
->keys
[ relation
]))) {
1329 if( arrayMember( links
, to
, &index
)) {
1330 links
->removeObject( index
);
1331 if( 0 == links
->getCount())
1332 registryTable()->removeObject( plane
->keys
[ relation
]);
1338 OSArray
* IORegistryEntry::getParentSetReference(
1339 const IORegistryPlane
* plane
) const
1342 return( (OSArray
*) registryTable()->getObject(
1343 plane
->keys
[ kParentSetIndex
]));
1348 OSIterator
* IORegistryEntry::getParentIterator(
1349 const IORegistryPlane
* plane
) const
1358 links
= getParentSetReference( plane
);
1360 links
= OSArray::withCapacity( 1 );
1362 links
= OSArray::withArray( links
, links
->getCount() );
1365 iter
= IOLinkIterator::withCollection( links
);
1373 IORegistryEntry
* IORegistryEntry::copyParentEntry( const IORegistryPlane
* plane
) const
1375 IORegistryEntry
* entry
= 0;
1380 if( (links
= getParentSetReference( plane
))) {
1381 entry
= (IORegistryEntry
*) links
->getObject( 0 );
1390 IORegistryEntry
* IORegistryEntry::getParentEntry( const IORegistryPlane
* plane
) const
1392 IORegistryEntry
* entry
;
1394 entry
= copyParentEntry( plane
);
1401 OSArray
* IORegistryEntry::getChildSetReference( const IORegistryPlane
* plane
) const
1404 return( (OSArray
*) registryTable()->getObject(
1405 plane
->keys
[ kChildSetIndex
]));
1410 OSIterator
* IORegistryEntry::getChildIterator( const IORegistryPlane
* plane
) const
1419 links
= getChildSetReference( plane
);
1421 links
= OSArray::withCapacity( 1 );
1423 links
= OSArray::withArray( links
, links
->getCount() );
1426 iter
= IOLinkIterator::withCollection( links
);
1435 IORegistryEntry
* IORegistryEntry::copyChildEntry(
1436 const IORegistryPlane
* plane
) const
1438 IORegistryEntry
* entry
= 0;
1443 if( (links
= getChildSetReference( plane
))) {
1444 entry
= (IORegistryEntry
*) links
->getObject( 0 );
1453 IORegistryEntry
* IORegistryEntry::getChildEntry(
1454 const IORegistryPlane
* plane
) const
1456 IORegistryEntry
* entry
;
1458 entry
= copyChildEntry( plane
);
1465 void IORegistryEntry::applyToChildren( IORegistryEntryApplierFunction applier
,
1467 const IORegistryPlane
* plane
) const
1471 IORegistryEntry
* next
;
1477 array
= OSArray::withArray( getChildSetReference( plane
));
1481 (next
= (IORegistryEntry
*) array
->getObject( index
));
1483 (*applier
)(next
, context
);
1488 void IORegistryEntry::applyToParents( IORegistryEntryApplierFunction applier
,
1490 const IORegistryPlane
* plane
) const
1494 IORegistryEntry
* next
;
1500 array
= OSArray::withArray( getParentSetReference( plane
));
1504 (next
= (IORegistryEntry
*) array
->getObject( index
));
1506 (*applier
)(next
, context
);
1511 bool IORegistryEntry::isChild( IORegistryEntry
* child
,
1512 const IORegistryPlane
* plane
,
1513 bool onlyChild
) const
1520 if( (links
= getChildSetReference( plane
))) {
1521 if( (!onlyChild
) || (1 == links
->getCount()))
1522 ret
= arrayMember( links
, child
);
1524 if( ret
&& (links
= child
->getParentSetReference( plane
)))
1525 ret
= arrayMember( links
, this );
1532 bool IORegistryEntry::isParent( IORegistryEntry
* parent
,
1533 const IORegistryPlane
* plane
,
1534 bool onlyParent
) const
1542 if( (links
= getParentSetReference( plane
))) {
1543 if( (!onlyParent
) || (1 == links
->getCount()))
1544 ret
= arrayMember( links
, parent
);
1546 if( ret
&& (links
= parent
->getChildSetReference( plane
)))
1547 ret
= arrayMember( links
, this );
1554 bool IORegistryEntry::inPlane( const IORegistryPlane
* plane
) const
1561 ret
= (0 != getParentSetReference( plane
));
1564 // Check to see if this is in any plane. If it is in a plane
1565 // then the registryTable will contain a key with the ParentLinks
1566 // suffix. When we iterate over the keys looking for that suffix
1569 OSCollectionIterator
*iter
=
1570 OSCollectionIterator::withCollection( registryTable());
1572 const OSSymbol
*key
;
1574 while( (key
= (OSSymbol
*) iter
->getNextObject()) ) {
1577 // Get a pointer to this keys suffix
1578 keysuffix
= key
->getLength();
1579 if (keysuffix
<= kIORegPlaneParentSuffixLen
)
1581 keysuffix
-= kIORegPlaneParentSuffixLen
;
1582 if( !strncmp(key
->getCStringNoCopy() + keysuffix
,
1583 kIORegPlaneParentSuffix
,
1584 kIORegPlaneParentSuffixLen
+ 1) ) {
1598 bool IORegistryEntry::attachToParent( IORegistryEntry
* parent
,
1599 const IORegistryPlane
* plane
)
1610 if (!reserved
->fRegistryEntryID
)
1611 reserved
->fRegistryEntryID
= ++gIORegistryLastID
;
1613 ret
= makeLink( parent
, kParentSetIndex
, plane
);
1615 if( (links
= parent
->getChildSetReference( plane
)))
1616 needParent
= (false == arrayMember( links
, this ));
1624 // Mark any collections in the property list as immutable
1625 OSDictionary
*ptable
= getPropertyTable();
1626 OSCollectionIterator
*iter
=
1627 OSCollectionIterator::withCollection( ptable
);
1629 const OSSymbol
*key
;
1631 while( (key
= (OSSymbol
*) iter
->getNextObject( ))) {
1632 // Is object for key a collection?
1633 OSCollection
*coll
=
1634 OSDynamicCast( OSCollection
, ptable
->getObject( key
));
1637 // Yup so mark it as immutable
1638 coll
->setOptions( OSCollection::kMASK
,
1639 OSCollection::kImmutable
);
1648 ret
&= parent
->attachToChild( this, plane
);
1653 uint64_t IORegistryEntry::getRegistryEntryID( void )
1656 return (reserved
->fRegistryEntryID
);
1661 bool IORegistryEntry::attachToChild( IORegistryEntry
* child
,
1662 const IORegistryPlane
* plane
)
1673 ret
= makeLink( child
, kChildSetIndex
, plane
);
1675 if( (links
= child
->getParentSetReference( plane
)))
1676 needChild
= (false == arrayMember( links
, this ));
1683 ret
&= child
->attachToParent( this, plane
);
1688 void IORegistryEntry::detachFromParent( IORegistryEntry
* parent
,
1689 const IORegistryPlane
* plane
)
1698 breakLink( parent
, kParentSetIndex
, plane
);
1700 if( (links
= parent
->getChildSetReference( plane
)))
1701 needParent
= arrayMember( links
, this );
1705 // parent->breakLink( this, kChildSetIndex, plane );
1710 parent
->detachFromChild( this, plane
);
1715 void IORegistryEntry::detachFromChild( IORegistryEntry
* child
,
1716 const IORegistryPlane
* plane
)
1725 breakLink( child
, kChildSetIndex
, plane
);
1727 if( (links
= child
->getParentSetReference( plane
)))
1728 needChild
= arrayMember( links
, this );
1735 child
->detachFromParent( this, plane
);
1740 void IORegistryEntry::detachAbove( const IORegistryPlane
* plane
)
1742 IORegistryEntry
* parent
;
1745 while( (parent
= getParentEntry( plane
)))
1746 detachFromParent( parent
, plane
);
1750 void IORegistryEntry::detachAll( const IORegistryPlane
* plane
)
1753 IORegistryEntry
* next
;
1754 IORegistryIterator
* regIter
;
1756 regIter
= IORegistryIterator::iterateOver( this, plane
, true );
1759 all
= regIter
->iterateAll();
1762 detachAbove( plane
);
1764 while( (next
= (IORegistryEntry
*) all
->getLastObject())) {
1767 all
->removeObject(next
);
1769 next
->detachAbove( plane
);
1776 unsigned int IORegistryEntry::getDepth( const IORegistryPlane
* plane
) const
1778 unsigned int depth
= 1;
1780 unsigned int oneDepth
, maxParentDepth
, count
;
1781 IORegistryEntry
* one
;
1782 const IORegistryEntry
* next
;
1788 while( (parents
= next
->getParentSetReference( plane
))) {
1790 count
= parents
->getCount();
1795 next
= (IORegistryEntry
*) parents
->getObject( 0 );
1800 (one
= (IORegistryEntry
*) parents
->getObject( index
));
1802 oneDepth
= one
->getDepth( plane
);
1803 if( oneDepth
> maxParentDepth
)
1804 maxParentDepth
= oneDepth
;
1806 depth
+= maxParentDepth
;
1816 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1819 #define super OSIterator
1821 OSDefineMetaClassAndStructors(IORegistryIterator
, OSIterator
)
1823 enum { kIORegistryIteratorInvalidFlag
= 0x80000000 };
1825 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1827 IORegistryIterator
*
1828 IORegistryIterator::iterateOver( IORegistryEntry
* root
,
1829 const IORegistryPlane
* plane
,
1830 IOOptionBits options
)
1832 IORegistryIterator
* create
;
1839 create
= new IORegistryIterator
;
1841 if( create
->init()) {
1844 create
->root
= root
;
1845 create
->where
= &create
->start
;
1846 create
->start
.current
= root
;
1847 create
->plane
= plane
;
1848 create
->options
= options
& ~kIORegistryIteratorInvalidFlag
;
1858 IORegistryIterator
*
1859 IORegistryIterator::iterateOver( const IORegistryPlane
* plane
,
1860 IOOptionBits options
)
1862 return( iterateOver( gRegistryRoot
, plane
, options
));
1865 bool IORegistryIterator::isValid( void )
1874 ok
= (0 == (kIORegistryIteratorInvalidFlag
& options
));
1876 while( ok
&& next
) {
1878 ok
= where
->iter
->isValid();
1886 void IORegistryIterator::enterEntry( const IORegistryPlane
* enterPlane
)
1891 where
= (IORegCursor
*) IOMalloc( sizeof(IORegCursor
));
1897 where
->current
= prev
->current
;
1902 void IORegistryIterator::enterEntry( void )
1904 enterEntry( plane
);
1907 bool IORegistryIterator::exitEntry( void )
1912 where
->iter
->release();
1914 if( where
->current
)// && (where != &start))
1915 where
->current
->release();
1918 if( where
!= &start
) {
1921 IOFree( gone
, sizeof(IORegCursor
));
1928 void IORegistryIterator::reset( void )
1938 where
->current
= root
;
1939 options
&= ~kIORegistryIteratorInvalidFlag
;
1942 void IORegistryIterator::free( void )
1953 IORegistryEntry
* IORegistryIterator::getNextObjectFlat( void )
1955 IORegistryEntry
* next
= 0;
1956 OSArray
* links
= 0;
1960 if( (0 == where
->iter
)) {
1961 // just entered - create new iter
1964 && (links
= ( (options
& kIORegistryIterateParents
) ?
1965 where
->current
->getParentSetReference( plane
) :
1966 where
->current
->getChildSetReference( plane
) )) )
1968 where
->iter
= OSCollectionIterator::withCollection( links
);
1971 // next sibling - release current
1973 where
->current
->release();
1977 next
= (IORegistryEntry
*) where
->iter
->getNextObject();
1981 else if( !where
->iter
->isValid())
1982 options
|= kIORegistryIteratorInvalidFlag
;
1985 where
->current
= next
;
1992 IORegistryEntry
* IORegistryIterator::getNextObjectRecursive( void )
1994 IORegistryEntry
* next
;
1997 next
= getNextObjectFlat();
1998 while( (0 == next
) && exitEntry());
2002 done
= OSOrderedSet::withCapacity( 10 );
2003 if( done
->setObject((OSObject
*) next
)) {
2004 // done set didn't contain this one, so recurse
2011 IORegistryEntry
* IORegistryIterator::getNextObject( void )
2013 if( options
& kIORegistryIterateRecursively
)
2014 return( getNextObjectRecursive());
2016 return( getNextObjectFlat());
2019 IORegistryEntry
* IORegistryIterator::getCurrentEntry( void )
2022 return( where
->current
);
2027 OSOrderedSet
* IORegistryIterator::iterateAll( void )
2030 while( getNextObjectRecursive())
2038 OSMetaClassDefineReservedUnused(IORegistryEntry
, 0);
2039 OSMetaClassDefineReservedUnused(IORegistryEntry
, 1);
2040 OSMetaClassDefineReservedUnused(IORegistryEntry
, 2);
2041 OSMetaClassDefineReservedUnused(IORegistryEntry
, 3);
2042 OSMetaClassDefineReservedUnused(IORegistryEntry
, 4);
2043 OSMetaClassDefineReservedUnused(IORegistryEntry
, 5);
2045 OSMetaClassDefineReservedUsed(IORegistryEntry
, 0);
2046 OSMetaClassDefineReservedUsed(IORegistryEntry
, 1);
2047 OSMetaClassDefineReservedUsed(IORegistryEntry
, 2);
2048 OSMetaClassDefineReservedUsed(IORegistryEntry
, 3);
2049 OSMetaClassDefineReservedUsed(IORegistryEntry
, 4);
2050 OSMetaClassDefineReservedUsed(IORegistryEntry
, 5);
2052 OSMetaClassDefineReservedUnused(IORegistryEntry
, 6);
2053 OSMetaClassDefineReservedUnused(IORegistryEntry
, 7);
2054 OSMetaClassDefineReservedUnused(IORegistryEntry
, 8);
2055 OSMetaClassDefineReservedUnused(IORegistryEntry
, 9);
2056 OSMetaClassDefineReservedUnused(IORegistryEntry
, 10);
2057 OSMetaClassDefineReservedUnused(IORegistryEntry
, 11);
2058 OSMetaClassDefineReservedUnused(IORegistryEntry
, 12);
2059 OSMetaClassDefineReservedUnused(IORegistryEntry
, 13);
2060 OSMetaClassDefineReservedUnused(IORegistryEntry
, 14);
2061 OSMetaClassDefineReservedUnused(IORegistryEntry
, 15);
2062 OSMetaClassDefineReservedUnused(IORegistryEntry
, 16);
2063 OSMetaClassDefineReservedUnused(IORegistryEntry
, 17);
2064 OSMetaClassDefineReservedUnused(IORegistryEntry
, 18);
2065 OSMetaClassDefineReservedUnused(IORegistryEntry
, 19);
2066 OSMetaClassDefineReservedUnused(IORegistryEntry
, 20);
2067 OSMetaClassDefineReservedUnused(IORegistryEntry
, 21);
2068 OSMetaClassDefineReservedUnused(IORegistryEntry
, 22);
2069 OSMetaClassDefineReservedUnused(IORegistryEntry
, 23);
2070 OSMetaClassDefineReservedUnused(IORegistryEntry
, 24);
2071 OSMetaClassDefineReservedUnused(IORegistryEntry
, 25);
2072 OSMetaClassDefineReservedUnused(IORegistryEntry
, 26);
2073 OSMetaClassDefineReservedUnused(IORegistryEntry
, 27);
2074 OSMetaClassDefineReservedUnused(IORegistryEntry
, 28);
2075 OSMetaClassDefineReservedUnused(IORegistryEntry
, 29);
2076 OSMetaClassDefineReservedUnused(IORegistryEntry
, 30);
2077 OSMetaClassDefineReservedUnused(IORegistryEntry
, 31);
2079 /* inline function implementation */
2080 OSDictionary
* IORegistryEntry::getPropertyTable( void ) const
2081 { return(fPropertyTable
); }