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
));
281 if (OSCollection::kImmutable
& dict
->setOptions(0, 0)) {
282 dict
= (OSDictionary
*) dict
->copyCollection();
288 fPropertyTable
->release();
289 fPropertyTable
= dict
;
291 } else if( !fPropertyTable
) {
292 fPropertyTable
= OSDictionary::withCapacity( kIORegCapacityIncrement
);
294 fPropertyTable
->setCapacityIncrement( kIORegCapacityIncrement
);
300 #ifdef IOREGSPLITTABLES
301 if( !fRegistryTable
) {
302 fRegistryTable
= OSDictionary::withCapacity( kIORegCapacityIncrement
);
304 fRegistryTable
->setCapacityIncrement( kIORegCapacityIncrement
);
307 if( (prop
= OSDynamicCast( OSString
, getProperty( gIONameKey
)))) {
308 OSSymbol
* sym
= (OSSymbol
*)OSSymbol::withString( prop
);
309 // ok for OSSymbol too
314 #endif /* IOREGSPLITTABLES */
319 bool IORegistryEntry::init( IORegistryEntry
* old
,
320 const IORegistryPlane
* plane
)
323 IORegistryEntry
* next
;
331 reserved
= old
->reserved
;
332 old
->reserved
= NULL
;
334 fPropertyTable
= old
->getPropertyTable();
335 fPropertyTable
->retain();
336 #ifdef IOREGSPLITTABLES
337 fRegistryTable
= old
->fRegistryTable
;
338 old
->fRegistryTable
= OSDictionary::withDictionary( fRegistryTable
);
339 #endif /* IOREGSPLITTABLES */
341 old
->registryTable()->removeObject( plane
->keys
[ kParentSetIndex
] );
342 old
->registryTable()->removeObject( plane
->keys
[ kChildSetIndex
] );
344 all
= getParentSetReference( plane
);
345 if( all
) for( index
= 0;
346 (next
= (IORegistryEntry
*) all
->getObject(index
));
348 next
->makeLink( this, kChildSetIndex
, plane
);
349 next
->breakLink( old
, kChildSetIndex
, plane
);
352 all
= getChildSetReference( plane
);
353 if( all
) for( index
= 0;
354 (next
= (IORegistryEntry
*) all
->getObject(index
));
356 next
->makeLink( this, kParentSetIndex
, plane
);
357 next
->breakLink( old
, kParentSetIndex
, plane
);
365 void IORegistryEntry::free( void )
368 if( registryTable() && gIOServicePlane
) {
369 if( getParentSetReference( gIOServicePlane
)
370 || getChildSetReference( gIOServicePlane
)) {
371 panic("%s: attached at free()", getName());
376 if( getPropertyTable())
377 getPropertyTable()->release();
379 #ifdef IOREGSPLITTABLES
381 registryTable()->release();
382 #endif /* IOREGSPLITTABLES */
385 IODelete(reserved
, ExpansionData
, 1);
390 void IORegistryEntry::setPropertyTable( OSDictionary
* dict
)
393 fPropertyTable
->release();
396 fPropertyTable
= dict
;
399 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
401 /* Wrappers to synchronize property table */
403 #define wrap2(type, constant) \
405 IORegistryEntry::copyProperty( type * aKey) constant \
410 obj = getProperty( aKey ); \
418 #define wrap4(type,constant) \
420 IORegistryEntry::getProperty( type * aKey, \
421 const IORegistryPlane * plane, \
422 IOOptionBits options ) constant \
424 OSObject * obj = getProperty( aKey ); \
426 if ( (0 == obj) && plane && (options & kIORegistryIterateRecursively) ) { \
427 IORegistryEntry * entry = (IORegistryEntry *) this; \
428 IORegistryIterator * iter; \
429 iter = IORegistryIterator::iterateOver( entry, plane, options ); \
432 while ( (0 == obj) && (entry = iter->getNextObject()) ) { \
433 obj = entry->getProperty( aKey ); \
442 #define wrap5(type,constant) \
444 IORegistryEntry::copyProperty( type * aKey, \
445 const IORegistryPlane * plane, \
446 IOOptionBits options ) constant \
448 OSObject * obj = copyProperty( aKey ); \
450 if ( (0 == obj) && plane && (options & kIORegistryIterateRecursively) ) { \
451 IORegistryEntry * entry = (IORegistryEntry *) this; \
452 IORegistryIterator * iter; \
453 iter = IORegistryIterator::iterateOver( entry, plane, options ); \
456 while ( (0 == obj) && (entry = iter->getNextObject()) ) { \
457 obj = entry->copyProperty( aKey ); \
466 bool IORegistryEntry::serializeProperties( OSSerialize
* s
) const
468 // setProperty( getRetainCount(), 32, "__retain" );
471 OSCollection
*snapshotProperties
= getPropertyTable()->copyCollection();
474 bool ok
= snapshotProperties
->serialize( s
);
475 snapshotProperties
->release();
479 OSDictionary
* IORegistryEntry::dictionaryWithProperties( void ) const
484 dict
= OSDictionary::withDictionary( getPropertyTable(),
485 getPropertyTable()->getCapacity() );
491 IOReturn
IORegistryEntry::setProperties( OSObject
* properties
)
493 return( kIOReturnUnsupported
);
496 wrap2(const OSSymbol
, const) // copyProperty() definition
497 wrap2(const OSString
, const) // copyProperty() definition
498 wrap2(const char, const) // copyProperty() definition
500 wrap4(const OSSymbol
, const) // getProperty() w/plane definition
501 wrap4(const OSString
, const) // getProperty() w/plane definition
502 wrap4(const char, const) // getProperty() w/plane definition
504 wrap5(const OSSymbol
, const) // copyProperty() w/plane definition
505 wrap5(const OSString
, const) // copyProperty() w/plane definition
506 wrap5(const char, const) // copyProperty() w/plane definition
510 IORegistryEntry::getProperty( const OSSymbol
* aKey
) const
515 obj
= getPropertyTable()->getObject( aKey
);
522 IORegistryEntry::removeProperty( const OSSymbol
* aKey
)
525 getPropertyTable()->removeObject( aKey
);
530 IORegistryEntry::setProperty( const OSSymbol
* aKey
, OSObject
* anObject
)
534 // If we are inserting a collection class and the current entry
535 // is attached into the registry (inPlane()) then mark the collection
537 OSCollection
*coll
= OSDynamicCast(OSCollection
, anObject
);
538 bool makeImmutable
= (coll
&& inPlane());
542 coll
->setOptions( OSCollection::kMASK
, OSCollection::kImmutable
);
544 ret
= getPropertyTable()->setObject( aKey
, anObject
);
550 IOReturn
IORegistryEntry::
551 runPropertyAction(Action inAction
, OSObject
*target
,
552 void *arg0
, void *arg1
, void *arg2
, void *arg3
)
556 // closeGate is recursive so don't worry if we already hold the lock.
558 res
= (*inAction
)(target
, arg0
, arg1
, arg2
, arg3
);
565 IORegistryEntry::getProperty( const OSString
* aKey
) const
567 const OSSymbol
* tmpKey
= OSSymbol::withString( aKey
);
568 OSObject
* obj
= getProperty( tmpKey
);
575 IORegistryEntry::getProperty( const char * aKey
) const
577 const OSSymbol
* tmpKey
= OSSymbol::withCString( aKey
);
578 OSObject
* obj
= getProperty( tmpKey
);
586 IORegistryEntry::removeProperty( const OSString
* aKey
)
588 const OSSymbol
* tmpKey
= OSSymbol::withString( aKey
);
589 removeProperty( tmpKey
);
594 IORegistryEntry::removeProperty( const char * aKey
)
596 const OSSymbol
* tmpKey
= OSSymbol::withCString( aKey
);
597 removeProperty( tmpKey
);
602 IORegistryEntry::setProperty( const OSString
* aKey
, OSObject
* anObject
)
604 const OSSymbol
* tmpKey
= OSSymbol::withString( aKey
);
605 bool ret
= setProperty( tmpKey
, anObject
);
612 IORegistryEntry::setProperty( const char * aKey
, OSObject
* anObject
)
614 const OSSymbol
* tmpKey
= OSSymbol::withCString( aKey
);
615 bool ret
= setProperty( tmpKey
, anObject
);
622 IORegistryEntry::setProperty(const char * aKey
, const char * aString
)
625 OSSymbol
* aSymbol
= (OSSymbol
*) OSSymbol::withCString( aString
);
628 const OSSymbol
* tmpKey
= OSSymbol::withCString( aKey
);
629 ret
= setProperty( tmpKey
, aSymbol
);
638 IORegistryEntry::setProperty(const char * aKey
, bool aBoolean
)
641 OSBoolean
* aBooleanObj
= OSBoolean::withBoolean( aBoolean
);
644 const OSSymbol
* tmpKey
= OSSymbol::withCString( aKey
);
645 ret
= setProperty( tmpKey
, aBooleanObj
);
648 aBooleanObj
->release();
654 IORegistryEntry::setProperty( const char * aKey
,
655 unsigned long long aValue
,
656 unsigned int aNumberOfBits
)
659 OSNumber
* anOffset
= OSNumber::withNumber( aValue
, aNumberOfBits
);
662 const OSSymbol
* tmpKey
= OSSymbol::withCString( aKey
);
663 ret
= setProperty( tmpKey
, anOffset
);
672 IORegistryEntry::setProperty( const char * aKey
,
677 OSData
* data
= OSData::withBytes( bytes
, length
);
680 const OSSymbol
* tmpKey
= OSSymbol::withCString( aKey
);
681 ret
= setProperty( tmpKey
, data
);
689 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
691 /* Name, location, paths */
693 const char * IORegistryEntry::getName( const IORegistryPlane
* plane
) const
699 sym
= (OSSymbol
*) registryTable()->getObject( plane
->pathNameKey
);
701 sym
= (OSSymbol
*) registryTable()->getObject( gIONameKey
);
705 return( sym
->getCStringNoCopy());
707 return( (getMetaClass())->getClassName());
710 const OSSymbol
* IORegistryEntry::copyName(
711 const IORegistryPlane
* plane
) const
717 sym
= (OSSymbol
*) registryTable()->getObject( plane
->pathNameKey
);
719 sym
= (OSSymbol
*) registryTable()->getObject( gIONameKey
);
727 return( OSSymbol::withCString((getMetaClass())->getClassName()) );
730 const OSSymbol
* IORegistryEntry::copyLocation(
731 const IORegistryPlane
* plane
) const
737 sym
= (OSSymbol
*) registryTable()->getObject( plane
->pathLocationKey
);
739 sym
= (OSSymbol
*) registryTable()->getObject( gIOLocationKey
);
747 const char * IORegistryEntry::getLocation( const IORegistryPlane
* plane
) const
749 const OSSymbol
* sym
= copyLocation( plane
);
750 const char * result
= 0;
753 result
= sym
->getCStringNoCopy();
760 void IORegistryEntry::setName( const OSSymbol
* name
,
761 const IORegistryPlane
* plane
)
763 const OSSymbol
* key
;
767 key
= plane
->pathNameKey
;
772 registryTable()->setObject( key
, (OSObject
*) name
);
777 void IORegistryEntry::setName( const char * name
,
778 const IORegistryPlane
* plane
)
780 OSSymbol
* sym
= (OSSymbol
*)OSSymbol::withCString( name
);
782 setName( sym
, plane
);
787 void IORegistryEntry::setLocation( const OSSymbol
* location
,
788 const IORegistryPlane
* plane
)
790 const OSSymbol
* key
;
794 key
= plane
->pathLocationKey
;
796 key
= gIOLocationKey
;
799 registryTable()->setObject( key
, (OSObject
*) location
);
804 void IORegistryEntry::setLocation( const char * location
,
805 const IORegistryPlane
* plane
)
807 OSSymbol
* sym
= (OSSymbol
*)OSSymbol::withCString( location
);
809 setLocation( sym
, plane
);
815 IORegistryEntry::compareName( OSString
* name
, OSString
** matched
) const
817 const OSSymbol
* sym
= copyName();
820 isEqual
= sym
->isEqualTo( name
);
822 if( isEqual
&& matched
) {
834 IORegistryEntry::compareNames( OSObject
* names
, OSString
** matched
) const
837 OSCollection
* collection
;
838 OSIterator
* iter
= 0;
841 if( (collection
= OSDynamicCast( OSCollection
, names
))) {
842 iter
= OSCollectionIterator::withCollection( collection
);
845 string
= OSDynamicCast( OSString
, names
);
849 result
= compareName( string
, matched
);
851 } while( (false == result
)
852 && iter
&& (string
= OSDynamicCast( OSString
, iter
->getNextObject())));
861 bool IORegistryEntry::getPath( char * path
, int * length
,
862 const IORegistryPlane
* plane
) const
865 IORegistryEntry
* root
;
866 const IORegistryEntry
* entry
;
867 IORegistryEntry
* parent
;
868 const OSSymbol
* alias
;
870 int len
, maxLength
, compLen
, aliasLen
;
874 if( !path
|| !length
|| !plane
)
878 maxLength
= *length
- 2;
881 len
= plane
->nameKey
->getLength();
882 if( len
>= maxLength
)
884 strlcpy( nextComp
, plane
->nameKey
->getCStringNoCopy(), len
+ 1);
885 nextComp
[ len
++ ] = ':';
888 if( (alias
= hasAlias( plane
))) {
889 aliasLen
= alias
->getLength();
891 ok
= (maxLength
> len
);
894 strlcpy( nextComp
, alias
->getCStringNoCopy(), aliasLen
+ 1);
899 parent
= entry
->getParentEntry( plane
);
901 // Error if not attached in plane
904 stack
= OSArray::withCapacity( getDepth( plane
));
910 root
= gRegistryRoot
->getChildEntry( plane
);
911 while( parent
&& (entry
!= root
)) {
913 stack
->setObject( (OSObject
*) entry
);
915 parent
= entry
->getParentEntry( plane
);
918 index
= stack
->getCount();
927 } else while( ok
&& ((--index
) >= 0)) {
929 entry
= (IORegistryEntry
*) stack
->getObject((unsigned int) index
);
932 if( (alias
= entry
->hasAlias( plane
))) {
933 len
= plane
->nameKey
->getLength() + 1;
934 nextComp
= path
+ len
;
936 compLen
= alias
->getLength();
937 ok
= (maxLength
> (len
+ compLen
));
939 strlcpy( nextComp
, alias
->getCStringNoCopy(), compLen
+ 1);
941 compLen
= maxLength
- len
;
942 ok
= entry
->getPathComponent( nextComp
+ 1, &compLen
, plane
);
964 bool IORegistryEntry::getPathComponent( char * path
, int * length
,
965 const IORegistryPlane
* plane
) const
967 int len
, locLen
, maxLength
;
968 const char * compName
;
974 compName
= getName( plane
);
975 len
= strlen( compName
);
976 if( (loc
= getLocation( plane
)))
977 locLen
= 1 + strlen( loc
);
981 ok
= ((len
+ locLen
+ 1) < maxLength
);
983 strlcpy( path
, compName
, len
+ 1 );
988 strlcpy( path
, loc
, locLen
);
996 const char * IORegistryEntry::matchPathLocation( const char * cmp
,
997 const IORegistryPlane
* plane
)
1000 const char * result
= 0;
1001 u_quad_t num1
, num2
;
1002 char lastPathChar
, lastLocationChar
;
1004 str
= getLocation( plane
);
1006 lastPathChar
= cmp
[0];
1007 lastLocationChar
= str
[0];
1010 num1
= strtouq( cmp
, (char **) &cmp
, 16 );
1011 lastPathChar
= *cmp
++;
1015 if( lastLocationChar
) {
1016 num2
= strtouq( str
, (char **) &str
, 16 );
1017 lastLocationChar
= *str
++;
1024 if (!lastPathChar
&& !lastLocationChar
) {
1029 if( (',' != lastPathChar
) && (':' != lastPathChar
))
1032 if (lastPathChar
&& lastLocationChar
&& (lastPathChar
!= lastLocationChar
))
1041 IORegistryEntry
* IORegistryEntry::getChildFromComponent( const char ** opath
,
1042 const IORegistryPlane
* plane
)
1044 IORegistryEntry
* entry
= 0;
1048 const char * cmp
= 0;
1053 set
= getChildSetReference( plane
);
1059 (entry
= (IORegistryEntry
*) set
->getObject(index
));
1065 str
= entry
->getName( plane
);
1066 len
= strlen( str
);
1067 if( strncmp( str
, cmp
, len
))
1072 if( (c
== 0) || (c
== '/') || (c
== ':'))
1078 if( (cmp
= entry
->matchPathLocation( cmp
, plane
)))
1088 const OSSymbol
* IORegistryEntry::hasAlias( const IORegistryPlane
* plane
,
1089 char * opath
, int * length
) const
1091 IORegistryEntry
* entry
;
1092 IORegistryEntry
* entry2
;
1093 const OSSymbol
* key
;
1094 const OSSymbol
* bestKey
= 0;
1097 const char * path
= "/aliases";
1099 entry
= IORegistryEntry::fromPath( path
, plane
);
1102 if( (iter
= OSCollectionIterator::withCollection(
1103 entry
->getPropertyTable() ))) {
1105 while( (key
= (OSSymbol
*) iter
->getNextObject())) {
1107 data
= (OSData
*) entry
->getProperty( key
);
1108 path
= (const char *) data
->getBytesNoCopy();
1109 if( (entry2
= IORegistryEntry::fromPath( path
, plane
,
1111 if( this == entry2
) {
1113 || (bestKey
->getLength() > key
->getLength()))
1114 // pick the smallest alias
1128 const char * IORegistryEntry::dealiasPath(
1129 const char ** opath
,
1130 const IORegistryPlane
* plane
)
1132 IORegistryEntry
* entry
;
1134 const char * path
= *opath
;
1135 const char * rpath
= 0;
1138 char temp
[ kIOMaxPlaneName
+ 1 ];
1145 while( (c
= *end
++) && (c
!= '/') && (c
!= ':'))
1148 if( (end
- path
) < kIOMaxPlaneName
) {
1149 strlcpy( temp
, path
, end
- path
+ 1 );
1152 entry
= IORegistryEntry::fromPath( "/aliases", plane
);
1154 data
= (OSData
*) entry
->getProperty( temp
);
1156 rpath
= (const char *) data
->getBytesNoCopy();
1168 IORegistryEntry
* IORegistryEntry::fromPath(
1170 const IORegistryPlane
* plane
,
1173 IORegistryEntry
* fromEntry
)
1175 IORegistryEntry
* where
= 0;
1176 IORegistryEntry
* aliasEntry
= 0;
1177 IORegistryEntry
* next
;
1183 char temp
[ kIOMaxPlaneName
+ 1 ];
1190 end
= strchr( path
, ':' );
1191 if( end
&& ((end
- path
) < kIOMaxPlaneName
)) {
1192 strlcpy( temp
, path
, end
- path
+ 1 );
1193 plane
= getPlane( temp
);
1202 if( (alias
= dealiasPath( &end
, plane
))) {
1205 aliasEntry
= IORegistryEntry::fromPath( alias
, plane
,
1206 opath
, &len
, fromEntry
);
1218 if( (0 == fromEntry
) && (*path
++ == '/'))
1219 fromEntry
= gRegistryRoot
->getChildEntry( plane
);
1226 if( c
&& (c
!= ':')) // check valid terminator
1231 next
= where
->getChildFromComponent( &path
, plane
);
1237 // check residual path
1238 if( where
!= fromEntry
)
1241 if( opath
&& length
) {
1242 // copy out residual path
1243 len2
= strlen( path
);
1244 if( (len
+ len2
) < *length
)
1245 strlcpy( opath
+ len
, path
, len2
+ 1 );
1246 *length
= (len
+ len2
);
1249 // no residual path => must be no tail for success
1256 aliasEntry
->release();
1263 IORegistryEntry
* IORegistryEntry::childFromPath(
1265 const IORegistryPlane
* plane
,
1269 return( IORegistryEntry::fromPath( path
, plane
, opath
, len
, this ));
1272 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1274 #define IOLinkIterator OSCollectionIterator
1277 #define super OSObject
1279 inline bool IORegistryEntry::arrayMember( OSArray
* set
,
1280 const IORegistryEntry
* member
,
1281 unsigned int * index
) const
1284 OSObject
* probeObject
;
1286 for( i
= 0; (probeObject
= set
->getObject(i
)); i
++) {
1287 if (probeObject
== (OSObject
*) member
) {
1296 bool IORegistryEntry::makeLink( IORegistryEntry
* to
,
1297 unsigned int relation
,
1298 const IORegistryPlane
* plane
) const
1301 bool result
= false;
1303 if( (links
= (OSArray
*)
1304 registryTable()->getObject( plane
->keys
[ relation
] ))) {
1306 result
= arrayMember( links
, to
);
1308 result
= links
->setObject( to
);
1312 links
= OSArray::withObjects( (const OSObject
**) &to
, 1, 1 );
1313 result
= (links
!= 0);
1315 result
= registryTable()->setObject( plane
->keys
[ relation
],
1324 void IORegistryEntry::breakLink( IORegistryEntry
* to
,
1325 unsigned int relation
,
1326 const IORegistryPlane
* plane
) const
1331 if( (links
= (OSArray
*)
1332 registryTable()->getObject( plane
->keys
[ relation
]))) {
1334 if( arrayMember( links
, to
, &index
)) {
1335 links
->removeObject( index
);
1336 if( 0 == links
->getCount())
1337 registryTable()->removeObject( plane
->keys
[ relation
]);
1343 OSArray
* IORegistryEntry::getParentSetReference(
1344 const IORegistryPlane
* plane
) const
1347 return( (OSArray
*) registryTable()->getObject(
1348 plane
->keys
[ kParentSetIndex
]));
1353 OSIterator
* IORegistryEntry::getParentIterator(
1354 const IORegistryPlane
* plane
) const
1363 links
= getParentSetReference( plane
);
1365 links
= OSArray::withCapacity( 1 );
1367 links
= OSArray::withArray( links
, links
->getCount() );
1370 iter
= IOLinkIterator::withCollection( links
);
1378 IORegistryEntry
* IORegistryEntry::copyParentEntry( const IORegistryPlane
* plane
) const
1380 IORegistryEntry
* entry
= 0;
1385 if( (links
= getParentSetReference( plane
))) {
1386 entry
= (IORegistryEntry
*) links
->getObject( 0 );
1395 IORegistryEntry
* IORegistryEntry::getParentEntry( const IORegistryPlane
* plane
) const
1397 IORegistryEntry
* entry
;
1399 entry
= copyParentEntry( plane
);
1406 OSArray
* IORegistryEntry::getChildSetReference( const IORegistryPlane
* plane
) const
1409 return( (OSArray
*) registryTable()->getObject(
1410 plane
->keys
[ kChildSetIndex
]));
1415 OSIterator
* IORegistryEntry::getChildIterator( const IORegistryPlane
* plane
) const
1424 links
= getChildSetReference( plane
);
1426 links
= OSArray::withCapacity( 1 );
1428 links
= OSArray::withArray( links
, links
->getCount() );
1431 iter
= IOLinkIterator::withCollection( links
);
1440 IORegistryEntry
* IORegistryEntry::copyChildEntry(
1441 const IORegistryPlane
* plane
) const
1443 IORegistryEntry
* entry
= 0;
1448 if( (links
= getChildSetReference( plane
))) {
1449 entry
= (IORegistryEntry
*) links
->getObject( 0 );
1458 IORegistryEntry
* IORegistryEntry::getChildEntry(
1459 const IORegistryPlane
* plane
) const
1461 IORegistryEntry
* entry
;
1463 entry
= copyChildEntry( plane
);
1470 void IORegistryEntry::applyToChildren( IORegistryEntryApplierFunction applier
,
1472 const IORegistryPlane
* plane
) const
1476 IORegistryEntry
* next
;
1482 array
= OSArray::withArray( getChildSetReference( plane
));
1486 (next
= (IORegistryEntry
*) array
->getObject( index
));
1488 (*applier
)(next
, context
);
1493 void IORegistryEntry::applyToParents( IORegistryEntryApplierFunction applier
,
1495 const IORegistryPlane
* plane
) const
1499 IORegistryEntry
* next
;
1505 array
= OSArray::withArray( getParentSetReference( plane
));
1509 (next
= (IORegistryEntry
*) array
->getObject( index
));
1511 (*applier
)(next
, context
);
1516 bool IORegistryEntry::isChild( IORegistryEntry
* child
,
1517 const IORegistryPlane
* plane
,
1518 bool onlyChild
) const
1525 if( (links
= getChildSetReference( plane
))) {
1526 if( (!onlyChild
) || (1 == links
->getCount()))
1527 ret
= arrayMember( links
, child
);
1529 if( ret
&& (links
= child
->getParentSetReference( plane
)))
1530 ret
= arrayMember( links
, this );
1537 bool IORegistryEntry::isParent( IORegistryEntry
* parent
,
1538 const IORegistryPlane
* plane
,
1539 bool onlyParent
) const
1547 if( (links
= getParentSetReference( plane
))) {
1548 if( (!onlyParent
) || (1 == links
->getCount()))
1549 ret
= arrayMember( links
, parent
);
1551 if( ret
&& (links
= parent
->getChildSetReference( plane
)))
1552 ret
= arrayMember( links
, this );
1559 bool IORegistryEntry::inPlane( const IORegistryPlane
* plane
) const
1566 ret
= (0 != getParentSetReference( plane
));
1569 // Check to see if this is in any plane. If it is in a plane
1570 // then the registryTable will contain a key with the ParentLinks
1571 // suffix. When we iterate over the keys looking for that suffix
1574 OSCollectionIterator
*iter
=
1575 OSCollectionIterator::withCollection( registryTable());
1577 const OSSymbol
*key
;
1579 while( (key
= (OSSymbol
*) iter
->getNextObject()) ) {
1582 // Get a pointer to this keys suffix
1583 keysuffix
= key
->getLength();
1584 if (keysuffix
<= kIORegPlaneParentSuffixLen
)
1586 keysuffix
-= kIORegPlaneParentSuffixLen
;
1587 if( !strncmp(key
->getCStringNoCopy() + keysuffix
,
1588 kIORegPlaneParentSuffix
,
1589 kIORegPlaneParentSuffixLen
+ 1) ) {
1603 bool IORegistryEntry::attachToParent( IORegistryEntry
* parent
,
1604 const IORegistryPlane
* plane
)
1615 if (!reserved
->fRegistryEntryID
)
1616 reserved
->fRegistryEntryID
= ++gIORegistryLastID
;
1618 ret
= makeLink( parent
, kParentSetIndex
, plane
);
1620 if( (links
= parent
->getChildSetReference( plane
)))
1621 needParent
= (false == arrayMember( links
, this ));
1629 // Mark any collections in the property list as immutable
1630 OSDictionary
*ptable
= getPropertyTable();
1631 OSCollectionIterator
*iter
=
1632 OSCollectionIterator::withCollection( ptable
);
1634 const OSSymbol
*key
;
1636 while( (key
= (OSSymbol
*) iter
->getNextObject( ))) {
1637 // Is object for key a collection?
1638 OSCollection
*coll
=
1639 OSDynamicCast( OSCollection
, ptable
->getObject( key
));
1642 // Yup so mark it as immutable
1643 coll
->setOptions( OSCollection::kMASK
,
1644 OSCollection::kImmutable
);
1653 ret
&= parent
->attachToChild( this, plane
);
1658 uint64_t IORegistryEntry::getRegistryEntryID( void )
1661 return (reserved
->fRegistryEntryID
);
1666 bool IORegistryEntry::attachToChild( IORegistryEntry
* child
,
1667 const IORegistryPlane
* plane
)
1678 ret
= makeLink( child
, kChildSetIndex
, plane
);
1680 if( (links
= child
->getParentSetReference( plane
)))
1681 needChild
= (false == arrayMember( links
, this ));
1688 ret
&= child
->attachToParent( this, plane
);
1693 void IORegistryEntry::detachFromParent( IORegistryEntry
* parent
,
1694 const IORegistryPlane
* plane
)
1703 breakLink( parent
, kParentSetIndex
, plane
);
1705 if( (links
= parent
->getChildSetReference( plane
)))
1706 needParent
= arrayMember( links
, this );
1710 // parent->breakLink( this, kChildSetIndex, plane );
1715 parent
->detachFromChild( this, plane
);
1720 void IORegistryEntry::detachFromChild( IORegistryEntry
* child
,
1721 const IORegistryPlane
* plane
)
1730 breakLink( child
, kChildSetIndex
, plane
);
1732 if( (links
= child
->getParentSetReference( plane
)))
1733 needChild
= arrayMember( links
, this );
1740 child
->detachFromParent( this, plane
);
1745 void IORegistryEntry::detachAbove( const IORegistryPlane
* plane
)
1747 IORegistryEntry
* parent
;
1750 while( (parent
= getParentEntry( plane
)))
1751 detachFromParent( parent
, plane
);
1755 void IORegistryEntry::detachAll( const IORegistryPlane
* plane
)
1758 IORegistryEntry
* next
;
1759 IORegistryIterator
* regIter
;
1761 regIter
= IORegistryIterator::iterateOver( this, plane
, true );
1764 all
= regIter
->iterateAll();
1767 detachAbove( plane
);
1769 while( (next
= (IORegistryEntry
*) all
->getLastObject())) {
1772 all
->removeObject(next
);
1774 next
->detachAbove( plane
);
1781 unsigned int IORegistryEntry::getDepth( const IORegistryPlane
* plane
) const
1783 unsigned int depth
= 1;
1785 unsigned int oneDepth
, maxParentDepth
, count
;
1786 IORegistryEntry
* one
;
1787 const IORegistryEntry
* next
;
1793 while( (parents
= next
->getParentSetReference( plane
))) {
1795 count
= parents
->getCount();
1800 next
= (IORegistryEntry
*) parents
->getObject( 0 );
1805 (one
= (IORegistryEntry
*) parents
->getObject( index
));
1807 oneDepth
= one
->getDepth( plane
);
1808 if( oneDepth
> maxParentDepth
)
1809 maxParentDepth
= oneDepth
;
1811 depth
+= maxParentDepth
;
1821 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1824 #define super OSIterator
1826 OSDefineMetaClassAndStructors(IORegistryIterator
, OSIterator
)
1828 enum { kIORegistryIteratorInvalidFlag
= 0x80000000 };
1830 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1832 IORegistryIterator
*
1833 IORegistryIterator::iterateOver( IORegistryEntry
* root
,
1834 const IORegistryPlane
* plane
,
1835 IOOptionBits options
)
1837 IORegistryIterator
* create
;
1844 create
= new IORegistryIterator
;
1846 if( create
->init()) {
1849 create
->root
= root
;
1850 create
->where
= &create
->start
;
1851 create
->start
.current
= root
;
1852 create
->plane
= plane
;
1853 create
->options
= options
& ~kIORegistryIteratorInvalidFlag
;
1863 IORegistryIterator
*
1864 IORegistryIterator::iterateOver( const IORegistryPlane
* plane
,
1865 IOOptionBits options
)
1867 return( iterateOver( gRegistryRoot
, plane
, options
));
1870 bool IORegistryIterator::isValid( void )
1879 ok
= (0 == (kIORegistryIteratorInvalidFlag
& options
));
1881 while( ok
&& next
) {
1883 ok
= where
->iter
->isValid();
1891 void IORegistryIterator::enterEntry( const IORegistryPlane
* enterPlane
)
1896 where
= (IORegCursor
*) IOMalloc( sizeof(IORegCursor
));
1902 where
->current
= prev
->current
;
1907 void IORegistryIterator::enterEntry( void )
1909 enterEntry( plane
);
1912 bool IORegistryIterator::exitEntry( void )
1917 where
->iter
->release();
1919 if( where
->current
)// && (where != &start))
1920 where
->current
->release();
1923 if( where
!= &start
) {
1926 IOFree( gone
, sizeof(IORegCursor
));
1933 void IORegistryIterator::reset( void )
1943 where
->current
= root
;
1944 options
&= ~kIORegistryIteratorInvalidFlag
;
1947 void IORegistryIterator::free( void )
1958 IORegistryEntry
* IORegistryIterator::getNextObjectFlat( void )
1960 IORegistryEntry
* next
= 0;
1961 OSArray
* links
= 0;
1965 if( (0 == where
->iter
)) {
1966 // just entered - create new iter
1969 && (links
= ( (options
& kIORegistryIterateParents
) ?
1970 where
->current
->getParentSetReference( plane
) :
1971 where
->current
->getChildSetReference( plane
) )) )
1973 where
->iter
= OSCollectionIterator::withCollection( links
);
1976 // next sibling - release current
1978 where
->current
->release();
1982 next
= (IORegistryEntry
*) where
->iter
->getNextObject();
1986 else if( !where
->iter
->isValid())
1987 options
|= kIORegistryIteratorInvalidFlag
;
1990 where
->current
= next
;
1997 IORegistryEntry
* IORegistryIterator::getNextObjectRecursive( void )
1999 IORegistryEntry
* next
;
2002 next
= getNextObjectFlat();
2003 while( (0 == next
) && exitEntry());
2007 done
= OSOrderedSet::withCapacity( 10 );
2008 if( done
->setObject((OSObject
*) next
)) {
2009 // done set didn't contain this one, so recurse
2016 IORegistryEntry
* IORegistryIterator::getNextObject( void )
2018 if( options
& kIORegistryIterateRecursively
)
2019 return( getNextObjectRecursive());
2021 return( getNextObjectFlat());
2024 IORegistryEntry
* IORegistryIterator::getCurrentEntry( void )
2027 return( where
->current
);
2032 OSOrderedSet
* IORegistryIterator::iterateAll( void )
2035 while( getNextObjectRecursive())
2043 OSMetaClassDefineReservedUnused(IORegistryEntry
, 0);
2044 OSMetaClassDefineReservedUnused(IORegistryEntry
, 1);
2045 OSMetaClassDefineReservedUnused(IORegistryEntry
, 2);
2046 OSMetaClassDefineReservedUnused(IORegistryEntry
, 3);
2047 OSMetaClassDefineReservedUnused(IORegistryEntry
, 4);
2048 OSMetaClassDefineReservedUnused(IORegistryEntry
, 5);
2050 OSMetaClassDefineReservedUsed(IORegistryEntry
, 0);
2051 OSMetaClassDefineReservedUsed(IORegistryEntry
, 1);
2052 OSMetaClassDefineReservedUsed(IORegistryEntry
, 2);
2053 OSMetaClassDefineReservedUsed(IORegistryEntry
, 3);
2054 OSMetaClassDefineReservedUsed(IORegistryEntry
, 4);
2055 OSMetaClassDefineReservedUsed(IORegistryEntry
, 5);
2057 OSMetaClassDefineReservedUnused(IORegistryEntry
, 6);
2058 OSMetaClassDefineReservedUnused(IORegistryEntry
, 7);
2059 OSMetaClassDefineReservedUnused(IORegistryEntry
, 8);
2060 OSMetaClassDefineReservedUnused(IORegistryEntry
, 9);
2061 OSMetaClassDefineReservedUnused(IORegistryEntry
, 10);
2062 OSMetaClassDefineReservedUnused(IORegistryEntry
, 11);
2063 OSMetaClassDefineReservedUnused(IORegistryEntry
, 12);
2064 OSMetaClassDefineReservedUnused(IORegistryEntry
, 13);
2065 OSMetaClassDefineReservedUnused(IORegistryEntry
, 14);
2066 OSMetaClassDefineReservedUnused(IORegistryEntry
, 15);
2067 OSMetaClassDefineReservedUnused(IORegistryEntry
, 16);
2068 OSMetaClassDefineReservedUnused(IORegistryEntry
, 17);
2069 OSMetaClassDefineReservedUnused(IORegistryEntry
, 18);
2070 OSMetaClassDefineReservedUnused(IORegistryEntry
, 19);
2071 OSMetaClassDefineReservedUnused(IORegistryEntry
, 20);
2072 OSMetaClassDefineReservedUnused(IORegistryEntry
, 21);
2073 OSMetaClassDefineReservedUnused(IORegistryEntry
, 22);
2074 OSMetaClassDefineReservedUnused(IORegistryEntry
, 23);
2075 OSMetaClassDefineReservedUnused(IORegistryEntry
, 24);
2076 OSMetaClassDefineReservedUnused(IORegistryEntry
, 25);
2077 OSMetaClassDefineReservedUnused(IORegistryEntry
, 26);
2078 OSMetaClassDefineReservedUnused(IORegistryEntry
, 27);
2079 OSMetaClassDefineReservedUnused(IORegistryEntry
, 28);
2080 OSMetaClassDefineReservedUnused(IORegistryEntry
, 29);
2081 OSMetaClassDefineReservedUnused(IORegistryEntry
, 30);
2082 OSMetaClassDefineReservedUnused(IORegistryEntry
, 31);
2084 /* inline function implementation */
2085 OSDictionary
* IORegistryEntry::getPropertyTable( void ) const
2086 { return(fPropertyTable
); }