2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
29 * 12 Nov 98 sdouglas created.
33 #include <IOKit/IORegistryEntry.h>
34 #include <libkern/c++/OSContainers.h>
35 #include <IOKit/IOService.h>
36 #include <IOKit/IOKitKeys.h>
38 #include <IOKit/IOLib.h>
40 #include <IOKit/assert.h>
42 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
44 #define super OSObject
46 OSDefineMetaClassAndStructors(IORegistryEntry
, OSObject
)
48 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
50 static IORegistryEntry
* gRegistryRoot
;
51 static OSDictionary
* gIORegistryPlanes
;
53 const OSSymbol
* gIONameKey
;
54 const OSSymbol
* gIOLocationKey
;
65 class IORegistryPlane
: public OSObject
{
67 friend class IORegistryEntry
;
69 OSDeclareAbstractStructors(IORegistryPlane
)
71 const OSSymbol
* nameKey
;
72 const OSSymbol
* keys
[ kNumSetIndex
];
73 const OSSymbol
* pathNameKey
;
74 const OSSymbol
* pathLocationKey
;
78 virtual bool serialize(OSSerialize
*s
) const;
81 OSDefineMetaClassAndStructors(IORegistryPlane
, OSObject
)
84 static IORecursiveLock
* gPropertiesLock
;
85 static SInt32 gIORegistryGenerationCount
;
87 #define UNLOCK s_lock_done( &gIORegistryLock )
88 #define RLOCK s_lock_read( &gIORegistryLock )
89 #define WLOCK s_lock_write( &gIORegistryLock ); \
90 gIORegistryGenerationCount++
93 #define PUNLOCK IORecursiveLockUnlock( gPropertiesLock )
94 #define PLOCK IORecursiveLockLock( gPropertiesLock )
96 #define IOREGSPLITTABLES
98 #ifdef IOREGSPLITTABLES
99 #define registryTable() fRegistryTable
101 #define registryTable() fPropertyTable
106 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
109 decl_simple_lock_data(,interlock
) /* "hardware" interlock field */
110 volatile unsigned int
111 read_count
:16, /* No. of accepted readers */
112 want_upgrade
:1, /* Read-to-write upgrade waiting */
113 want_write
:1, /* Writer is waiting, or
115 waiting
:1, /* Someone is sleeping on lock */
116 can_sleep
:1; /* Can attempts to lock go to sleep? */
119 static struct s_lock_t gIORegistryLock
;
121 /* Time we loop without holding the interlock.
122 * The former is for when we cannot sleep, the latter
123 * for when our thread can go to sleep (loop less)
124 * we shouldn't retake the interlock at all frequently
125 * if we cannot go to sleep, since it interferes with
126 * any other processors. In particular, 100 is too small
127 * a number for powerpc MP systems because of cache
128 * coherency issues and differing lock fetch times between
131 static unsigned int lock_wait_time
[2] = { (unsigned int)-1, 100 } ;
138 (void) memset((void *) l
, 0, sizeof(s_lock_t
));
140 simple_lock_init(&l
->interlock
, 0);
141 l
->want_write
= FALSE
;
142 l
->want_upgrade
= FALSE
;
144 l
->can_sleep
= can_sleep
;
149 register s_lock_t
* l
)
153 simple_lock(&l
->interlock
);
156 * Try to acquire the want_write bit.
158 while (l
->want_write
) {
160 i
= lock_wait_time
[l
->can_sleep
? 1 : 0];
162 simple_unlock(&l
->interlock
);
163 while (--i
!= 0 && l
->want_write
)
165 simple_lock(&l
->interlock
);
168 if (l
->can_sleep
&& l
->want_write
) {
170 thread_sleep_simple_lock((event_t
) l
,
171 simple_lock_addr(l
->interlock
),
173 /* interlock relocked */
176 l
->want_write
= TRUE
;
178 /* Wait for readers (and upgrades) to finish */
180 while ((l
->read_count
!= 0) || l
->want_upgrade
) {
182 i
= lock_wait_time
[l
->can_sleep
? 1 : 0];
184 simple_unlock(&l
->interlock
);
185 while (--i
!= 0 && (l
->read_count
!= 0 ||
188 simple_lock(&l
->interlock
);
191 if (l
->can_sleep
&& (l
->read_count
!= 0 || l
->want_upgrade
)) {
193 thread_sleep_simple_lock((event_t
) l
,
194 simple_lock_addr(l
->interlock
),
196 /* interlock relocked */
200 simple_unlock(&l
->interlock
);
205 register s_lock_t
* l
)
207 boolean_t do_wakeup
= FALSE
;
209 simple_lock(&l
->interlock
);
211 if (l
->read_count
!= 0) {
215 if (l
->want_upgrade
) {
216 l
->want_upgrade
= FALSE
;
219 l
->want_write
= FALSE
;
224 * There is no reason to wakeup a waiting thread
225 * if the read-count is non-zero. Consider:
226 * we must be dropping a read lock
227 * threads are waiting only if one wants a write lock
228 * if there are still readers, they can't proceed
230 if (l
->waiting
&& (l
->read_count
== 0)) {
235 simple_unlock(&l
->interlock
);
238 thread_wakeup((event_t
) l
);
243 register s_lock_t
* l
)
247 simple_lock(&l
->interlock
);
249 while ( l
->want_upgrade
|| ((0 == l
->read_count
) && l
->want_write
)) {
251 i
= lock_wait_time
[l
->can_sleep
? 1 : 0];
254 simple_unlock(&l
->interlock
);
256 (l
->want_upgrade
|| ((0 == l
->read_count
) && l
->want_write
)))
258 simple_lock(&l
->interlock
);
262 (l
->want_upgrade
|| ((0 == l
->read_count
) && l
->want_write
))) {
264 thread_sleep_simple_lock((event_t
) l
,
265 simple_lock_addr(l
->interlock
),
267 /* interlock relocked */
272 simple_unlock(&l
->interlock
);
276 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
278 IORegistryEntry
* IORegistryEntry::initialize( void )
282 if( !gRegistryRoot
) {
284 s_lock_init( &gIORegistryLock
, true );
285 gRegistryRoot
= new IORegistryEntry
;
286 gPropertiesLock
= IORecursiveLockAlloc();
287 gIORegistryPlanes
= OSDictionary::withCapacity( 1 );
289 assert( gRegistryRoot
&& gPropertiesLock
290 && gIORegistryPlanes
);
291 ok
= gRegistryRoot
->init();
293 gIONameKey
= OSSymbol::withCStringNoCopy( "IOName" );
294 gIOLocationKey
= OSSymbol::withCStringNoCopy( "IOLocation" );
296 assert( ok
&& gIONameKey
&& gIOLocationKey
);
298 gRegistryRoot
->setName( "Root" );
299 gRegistryRoot
->setProperty( kIORegistryPlanesKey
, gIORegistryPlanes
);
302 return( gRegistryRoot
);
305 IORegistryEntry
* IORegistryEntry::getRegistryRoot( void )
307 return( gRegistryRoot
);
310 SInt32
IORegistryEntry::getGenerationCount( void )
312 return( gIORegistryGenerationCount
);
316 const IORegistryPlane
* IORegistryEntry::makePlane( const char * name
)
318 IORegistryPlane
* plane
;
319 const OSSymbol
* nameKey
;
320 const OSSymbol
* parentKey
;
321 const OSSymbol
* childKey
;
322 const OSSymbol
* pathNameKey
;
323 const OSSymbol
* pathLocationKey
;
324 char key
[ kIOMaxPlaneName
+ 16 ];
327 strncpy( key
, name
, kIOMaxPlaneName
);
328 key
[ kIOMaxPlaneName
] = 0;
329 end
= key
+ strlen( name
);
331 nameKey
= OSSymbol::withCString( key
);
333 strcpy( end
, "ParentLinks" );
334 parentKey
= OSSymbol::withCString( key
);
336 strcpy( end
, "ChildLinks" );
337 childKey
= OSSymbol::withCString( key
);
339 strcpy( end
, "Name" );
340 pathNameKey
= OSSymbol::withCString( key
);
342 strcpy( end
, "Location" );
343 pathLocationKey
= OSSymbol::withCString( key
);
345 plane
= new IORegistryPlane
;
347 if( plane
&& plane
->init()
348 && nameKey
&& parentKey
&& childKey
349 && pathNameKey
&& pathLocationKey
) {
351 plane
->nameKey
= nameKey
;
352 plane
->keys
[ kParentSetIndex
] = parentKey
;
353 plane
->keys
[ kChildSetIndex
] = childKey
;
354 plane
->pathNameKey
= pathNameKey
;
355 plane
->pathLocationKey
= pathLocationKey
;
358 gIORegistryPlanes
->setObject( nameKey
, plane
);
366 pathLocationKey
->release();
368 pathNameKey
->release();
370 parentKey
->release();
381 const IORegistryPlane
* IORegistryEntry::getPlane( const char * name
)
383 const IORegistryPlane
* plane
;
386 plane
= (const IORegistryPlane
*) gIORegistryPlanes
->getObject( name
);
392 bool IORegistryPlane::serialize(OSSerialize
*s
) const
394 return( nameKey
->serialize(s
) );
397 enum { kIORegCapacityIncrement
= 4 };
399 bool IORegistryEntry::init( OSDictionary
* dict
)
409 fPropertyTable
->release();
410 fPropertyTable
= dict
;
412 } else if( !fPropertyTable
) {
413 fPropertyTable
= OSDictionary::withCapacity( kIORegCapacityIncrement
);
415 fPropertyTable
->setCapacityIncrement( kIORegCapacityIncrement
);
421 #ifdef IOREGSPLITTABLES
422 if( !fRegistryTable
) {
423 fRegistryTable
= OSDictionary::withCapacity( kIORegCapacityIncrement
);
425 fRegistryTable
->setCapacityIncrement( kIORegCapacityIncrement
);
428 if( (prop
= OSDynamicCast( OSString
, getProperty( gIONameKey
)))) {
429 OSSymbol
* sym
= (OSSymbol
*)OSSymbol::withString( prop
);
430 // ok for OSSymbol too
435 #endif /* IOREGSPLITTABLES */
440 bool IORegistryEntry::init( IORegistryEntry
* old
,
441 const IORegistryPlane
* plane
)
444 IORegistryEntry
* next
;
452 fPropertyTable
= old
->getPropertyTable();
453 fPropertyTable
->retain();
454 #ifdef IOREGSPLITTABLES
455 fRegistryTable
= old
->fRegistryTable
;
456 old
->fRegistryTable
= OSDictionary::withDictionary( fRegistryTable
);
457 #endif /* IOREGSPLITTABLES */
459 old
->registryTable()->removeObject( plane
->keys
[ kParentSetIndex
] );
460 old
->registryTable()->removeObject( plane
->keys
[ kChildSetIndex
] );
462 all
= getParentSetReference( plane
);
463 if( all
) for( index
= 0;
464 (next
= (IORegistryEntry
*) all
->getObject(index
));
466 next
->makeLink( this, kChildSetIndex
, plane
);
467 next
->breakLink( old
, kChildSetIndex
, plane
);
470 all
= getChildSetReference( plane
);
471 if( all
) for( index
= 0;
472 (next
= (IORegistryEntry
*) all
->getObject(index
));
474 next
->makeLink( this, kParentSetIndex
, plane
);
475 next
->breakLink( old
, kParentSetIndex
, plane
);
483 void IORegistryEntry::free( void )
487 #define msg ": attached at free()"
488 char buf
[ strlen(msg
) + 40 ];
490 if( registryTable() && gIOServicePlane
) {
491 if( getParentSetReference( gIOServicePlane
)
492 || getChildSetReference( gIOServicePlane
)) {
494 strncpy( buf
, getName(), 32);
502 if( getPropertyTable())
503 getPropertyTable()->release();
505 #ifdef IOREGSPLITTABLES
507 registryTable()->release();
508 #endif /* IOREGSPLITTABLES */
513 void IORegistryEntry::setPropertyTable( OSDictionary
* dict
)
516 fPropertyTable
->release();
519 fPropertyTable
= dict
;
522 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
524 /* Wrappers to synchronize property table */
526 #define wrap2(type, constant) \
528 IORegistryEntry::copyProperty( type * aKey) constant \
533 obj = getProperty( aKey ); \
541 #define wrap4(type,constant) \
543 IORegistryEntry::getProperty( type * aKey, \
544 const IORegistryPlane * plane, \
545 IOOptionBits options ) constant \
547 OSObject * obj = getProperty( aKey ); \
549 if ( (0 == obj) && plane && (options & kIORegistryIterateRecursively) ) { \
550 IORegistryEntry * entry = (IORegistryEntry *) this; \
551 IORegistryIterator * iter; \
552 iter = IORegistryIterator::iterateOver( entry, plane, options ); \
555 while ( (0 == obj) && (entry = iter->getNextObject()) ) { \
556 obj = entry->getProperty( aKey ); \
565 #define wrap5(type,constant) \
567 IORegistryEntry::copyProperty( type * aKey, \
568 const IORegistryPlane * plane, \
569 IOOptionBits options ) constant \
571 OSObject * obj = copyProperty( aKey ); \
573 if ( (0 == obj) && plane && (options & kIORegistryIterateRecursively) ) { \
574 IORegistryEntry * entry = (IORegistryEntry *) this; \
575 IORegistryIterator * iter; \
576 iter = IORegistryIterator::iterateOver( entry, plane, options ); \
579 while ( (0 == obj) && (entry = iter->getNextObject()) ) { \
580 obj = entry->copyProperty( aKey ); \
589 bool IORegistryEntry::serializeProperties( OSSerialize
* s
) const
593 // setProperty( getRetainCount(), 32, "__retain" );
596 ok
= getPropertyTable()->serialize( s
);
602 OSDictionary
* IORegistryEntry::dictionaryWithProperties( void ) const
607 dict
= OSDictionary::withDictionary( getPropertyTable(),
608 getPropertyTable()->getCapacity() );
614 IOReturn
IORegistryEntry::setProperties( OSObject
* properties
)
616 return( kIOReturnUnsupported
);
619 wrap2(const OSSymbol
, const) // copyProperty() definition
620 wrap2(const OSString
, const) // copyProperty() definition
621 wrap2(const char, const) // copyProperty() definition
623 wrap4(const OSSymbol
, const) // getProperty() w/plane definition
624 wrap4(const OSString
, const) // getProperty() w/plane definition
625 wrap4(const char, const) // getProperty() w/plane definition
627 wrap5(const OSSymbol
, const) // copyProperty() w/plane definition
628 wrap5(const OSString
, const) // copyProperty() w/plane definition
629 wrap5(const char, const) // copyProperty() w/plane definition
633 IORegistryEntry::getProperty( const OSSymbol
* aKey
) const
638 obj
= getPropertyTable()->getObject( aKey
);
645 IORegistryEntry::getProperty( const OSString
* aKey
) const
647 const OSSymbol
* tmpKey
= OSSymbol::withString( aKey
);
648 OSObject
* obj
= getProperty( tmpKey
);
655 IORegistryEntry::getProperty( const char * aKey
) const
657 const OSSymbol
* tmpKey
= OSSymbol::withCString( aKey
);
658 OSObject
* obj
= getProperty( tmpKey
);
665 IORegistryEntry::removeProperty( const OSSymbol
* aKey
)
668 getPropertyTable()->removeObject( aKey
);
673 IORegistryEntry::removeProperty( const OSString
* aKey
)
675 const OSSymbol
* tmpKey
= OSSymbol::withString( aKey
);
676 removeProperty( tmpKey
);
681 IORegistryEntry::removeProperty( const char * aKey
)
683 const OSSymbol
* tmpKey
= OSSymbol::withCString( aKey
);
684 removeProperty( tmpKey
);
689 IORegistryEntry::setProperty( const OSSymbol
* aKey
, OSObject
* anObject
)
693 ret
= getPropertyTable()->setObject( aKey
, anObject
);
700 IORegistryEntry::setProperty( const OSString
* aKey
, OSObject
* anObject
)
702 const OSSymbol
* tmpKey
= OSSymbol::withString( aKey
);
703 bool ret
= setProperty( tmpKey
, anObject
);
710 IORegistryEntry::setProperty( const char * aKey
, OSObject
* anObject
)
712 const OSSymbol
* tmpKey
= OSSymbol::withCString( aKey
);
713 bool ret
= setProperty( tmpKey
, anObject
);
720 IORegistryEntry::setProperty(const char * aKey
, const char * aString
)
723 OSSymbol
* aSymbol
= (OSSymbol
*) OSSymbol::withCString( aString
);
726 const OSSymbol
* tmpKey
= OSSymbol::withCString( aKey
);
727 ret
= setProperty( tmpKey
, aSymbol
);
736 IORegistryEntry::setProperty(const char * aKey
, bool aBoolean
)
739 OSBoolean
* aBooleanObj
= OSBoolean::withBoolean( aBoolean
);
742 const OSSymbol
* tmpKey
= OSSymbol::withCString( aKey
);
743 ret
= setProperty( tmpKey
, aBooleanObj
);
746 aBooleanObj
->release();
752 IORegistryEntry::setProperty( const char * aKey
,
753 unsigned long long aValue
,
754 unsigned int aNumberOfBits
)
757 OSNumber
* anOffset
= OSNumber::withNumber( aValue
, aNumberOfBits
);
760 const OSSymbol
* tmpKey
= OSSymbol::withCString( aKey
);
761 ret
= setProperty( tmpKey
, anOffset
);
770 IORegistryEntry::setProperty( const char * aKey
,
775 OSData
* data
= OSData::withBytes( bytes
, length
);
778 const OSSymbol
* tmpKey
= OSSymbol::withCString( aKey
);
779 ret
= setProperty( tmpKey
, data
);
787 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
789 /* Name, location, paths */
791 const char * IORegistryEntry::getName( const IORegistryPlane
* plane
) const
797 sym
= (OSSymbol
*) registryTable()->getObject( plane
->pathNameKey
);
799 sym
= (OSSymbol
*) registryTable()->getObject( gIONameKey
);
803 return( sym
->getCStringNoCopy());
805 return( (getMetaClass())->getClassName());
808 const OSSymbol
* IORegistryEntry::copyName(
809 const IORegistryPlane
* plane
) const
815 sym
= (OSSymbol
*) registryTable()->getObject( plane
->pathNameKey
);
817 sym
= (OSSymbol
*) registryTable()->getObject( gIONameKey
);
825 return( OSSymbol::withCString((getMetaClass())->getClassName()) );
828 const OSSymbol
* IORegistryEntry::copyLocation(
829 const IORegistryPlane
* plane
) const
835 sym
= (OSSymbol
*) registryTable()->getObject( plane
->pathLocationKey
);
837 sym
= (OSSymbol
*) registryTable()->getObject( gIOLocationKey
);
845 const char * IORegistryEntry::getLocation( const IORegistryPlane
* plane
) const
847 const OSSymbol
* sym
= copyLocation( plane
);
848 const char * result
= 0;
851 result
= sym
->getCStringNoCopy();
858 void IORegistryEntry::setName( const OSSymbol
* name
,
859 const IORegistryPlane
* plane
)
861 const OSSymbol
* key
;
865 key
= plane
->pathNameKey
;
870 registryTable()->setObject( key
, (OSObject
*) name
);
875 void IORegistryEntry::setName( const char * name
,
876 const IORegistryPlane
* plane
)
878 OSSymbol
* sym
= (OSSymbol
*)OSSymbol::withCString( name
);
880 setName( sym
, plane
);
885 void IORegistryEntry::setLocation( const OSSymbol
* location
,
886 const IORegistryPlane
* plane
)
888 const OSSymbol
* key
;
892 key
= plane
->pathLocationKey
;
894 key
= gIOLocationKey
;
897 registryTable()->setObject( key
, (OSObject
*) location
);
902 void IORegistryEntry::setLocation( const char * location
,
903 const IORegistryPlane
* plane
)
905 OSSymbol
* sym
= (OSSymbol
*)OSSymbol::withCString( location
);
907 setLocation( sym
, plane
);
913 IORegistryEntry::compareName( OSString
* name
, OSString
** matched
) const
915 const OSSymbol
* sym
= copyName();
918 isEqual
= sym
->isEqualTo( name
);
920 if( isEqual
&& matched
) {
932 IORegistryEntry::compareNames( OSObject
* names
, OSString
** matched
) const
935 OSCollection
* collection
;
936 OSIterator
* iter
= 0;
939 if( (collection
= OSDynamicCast( OSCollection
, names
))) {
940 iter
= OSCollectionIterator::withCollection( collection
);
943 string
= OSDynamicCast( OSString
, names
);
947 result
= compareName( string
, matched
);
949 } while( (false == result
)
950 && iter
&& (string
= OSDynamicCast( OSString
, iter
->getNextObject())));
959 bool IORegistryEntry::getPath( char * path
, int * length
,
960 const IORegistryPlane
* plane
) const
963 IORegistryEntry
* root
;
964 const IORegistryEntry
* entry
;
965 IORegistryEntry
* parent
;
966 const OSSymbol
* alias
;
968 int len
, maxLength
, compLen
;
972 if( !path
|| !length
|| !plane
)
976 maxLength
= *length
- 2;
979 len
= plane
->nameKey
->getLength();
980 if( len
>= maxLength
)
982 strcpy( nextComp
, plane
->nameKey
->getCStringNoCopy());
983 nextComp
[ len
++ ] = ':';
986 if( (alias
= hasAlias( plane
))) {
987 len
+= alias
->getLength();
988 ok
= (maxLength
> len
);
991 strcpy( nextComp
, alias
->getCStringNoCopy());
996 parent
= entry
->getParentEntry( plane
);
998 // Error if not attached in plane
1001 stack
= OSArray::withCapacity( getDepth( plane
));
1007 root
= gRegistryRoot
->getChildEntry( plane
);
1008 while( parent
&& (entry
!= root
)) {
1010 stack
->setObject( (OSObject
*) entry
);
1012 parent
= entry
->getParentEntry( plane
);
1015 index
= stack
->getCount();
1024 } else while( ok
&& ((--index
) >= 0)) {
1026 entry
= (IORegistryEntry
*) stack
->getObject((unsigned int) index
);
1029 if( (alias
= entry
->hasAlias( plane
))) {
1030 len
= plane
->nameKey
->getLength() + 1;
1031 nextComp
= path
+ len
;
1033 compLen
= alias
->getLength();
1034 ok
= (maxLength
> len
+ compLen
);
1036 strcpy( nextComp
, alias
->getCStringNoCopy());
1038 compLen
= maxLength
- len
;
1039 ok
= entry
->getPathComponent( nextComp
+ 1, &compLen
, plane
);
1041 if( ok
&& compLen
) {
1049 nextComp
+= compLen
;
1061 bool IORegistryEntry::getPathComponent( char * path
, int * length
,
1062 const IORegistryPlane
* plane
) const
1064 int len
, locLen
, maxLength
;
1065 const char * compName
;
1069 maxLength
= *length
;
1071 compName
= getName( plane
);
1072 len
= strlen( compName
);
1073 if( (loc
= getLocation( plane
)))
1074 locLen
= 1 + strlen( loc
);
1078 ok
= ((len
+ locLen
) < maxLength
);
1080 strcpy( path
, compName
);
1085 strcpy( path
, loc
);
1093 const char * IORegistryEntry::matchPathLocation( const char * cmp
,
1094 const IORegistryPlane
* plane
)
1097 const char * result
= 0;
1098 u_quad_t num1
, num2
;
1099 char lastPathChar
, lastLocationChar
;
1101 str
= getLocation( plane
);
1103 lastPathChar
= cmp
[0];
1104 lastLocationChar
= str
[0];
1107 num1
= strtouq( cmp
, (char **) &cmp
, 16 );
1108 lastPathChar
= *cmp
++;
1112 if( lastLocationChar
) {
1113 num2
= strtouq( str
, (char **) &str
, 16 );
1114 lastLocationChar
= *str
++;
1121 if (!lastPathChar
&& !lastLocationChar
) {
1126 if( (',' != lastPathChar
) && (':' != lastPathChar
))
1129 if (lastPathChar
&& lastLocationChar
&& (lastPathChar
!= lastLocationChar
))
1138 IORegistryEntry
* IORegistryEntry::getChildFromComponent( const char ** opath
,
1139 const IORegistryPlane
* plane
)
1141 IORegistryEntry
* entry
= 0;
1145 const char * cmp
= 0;
1150 set
= getChildSetReference( plane
);
1156 (entry
= (IORegistryEntry
*) set
->getObject(index
));
1162 str
= entry
->getName( plane
);
1163 len
= strlen( str
);
1164 if( strncmp( str
, cmp
, len
))
1169 if( (c
== 0) || (c
== '/') || (c
== ':'))
1175 if( (cmp
= entry
->matchPathLocation( cmp
, plane
)))
1185 const OSSymbol
* IORegistryEntry::hasAlias( const IORegistryPlane
* plane
,
1186 char * opath
, int * length
) const
1188 IORegistryEntry
* entry
;
1189 IORegistryEntry
* entry2
;
1190 const OSSymbol
* key
;
1191 const OSSymbol
* bestKey
= 0;
1194 const char * path
= "/aliases";
1196 entry
= IORegistryEntry::fromPath( path
, plane
);
1199 if( (iter
= OSCollectionIterator::withCollection(
1200 entry
->getPropertyTable() ))) {
1202 while( (key
= (OSSymbol
*) iter
->getNextObject())) {
1204 data
= (OSData
*) entry
->getProperty( key
);
1205 path
= (const char *) data
->getBytesNoCopy();
1206 if( (entry2
= IORegistryEntry::fromPath( path
, plane
,
1208 if( this == entry2
) {
1210 || (bestKey
->getLength() > key
->getLength()))
1211 // pick the smallest alias
1225 const char * IORegistryEntry::dealiasPath(
1226 const char ** opath
,
1227 const IORegistryPlane
* plane
)
1229 IORegistryEntry
* entry
;
1231 const char * path
= *opath
;
1232 const char * rpath
= 0;
1235 char temp
[ kIOMaxPlaneName
+ 1 ];
1242 while( (c
= *end
++) && (c
!= '/') && (c
!= ':'))
1245 if( (end
- path
) < kIOMaxPlaneName
) {
1246 strncpy( temp
, path
, end
- path
);
1247 temp
[ end
- path
] = 0;
1250 entry
= IORegistryEntry::fromPath( "/aliases", plane
);
1252 data
= (OSData
*) entry
->getProperty( temp
);
1254 rpath
= (const char *) data
->getBytesNoCopy();
1266 IORegistryEntry
* IORegistryEntry::fromPath(
1268 const IORegistryPlane
* plane
,
1271 IORegistryEntry
* fromEntry
)
1273 IORegistryEntry
* where
= 0;
1274 IORegistryEntry
* aliasEntry
= 0;
1275 IORegistryEntry
* next
;
1281 char temp
[ kIOMaxPlaneName
+ 1 ];
1288 end
= strchr( path
, ':' );
1289 if( end
&& ((end
- path
) < kIOMaxPlaneName
)) {
1290 strncpy( temp
, path
, end
- path
);
1291 temp
[ end
- path
] = 0;
1292 plane
= getPlane( temp
);
1301 if( (alias
= dealiasPath( &end
, plane
))) {
1304 aliasEntry
= IORegistryEntry::fromPath( alias
, plane
,
1305 opath
, &len
, fromEntry
);
1317 if( (0 == fromEntry
) && (*path
++ == '/'))
1318 fromEntry
= gRegistryRoot
->getChildEntry( plane
);
1325 if( c
&& (c
!= ':')) // check valid terminator
1330 next
= where
->getChildFromComponent( &path
, plane
);
1336 // check residual path
1337 if( where
!= fromEntry
)
1340 if( opath
&& length
) {
1341 // copy out residual path
1342 len2
= len
+ strlen( path
);
1344 strcpy( opath
+ len
, path
);
1348 // no residual path => must be no tail for success
1355 aliasEntry
->release();
1362 IORegistryEntry
* IORegistryEntry::childFromPath(
1364 const IORegistryPlane
* plane
,
1368 return( IORegistryEntry::fromPath( path
, plane
, opath
, len
, this ));
1371 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1373 #define IOLinkIterator OSCollectionIterator
1376 #define super OSObject
1378 inline bool IORegistryEntry::arrayMember( OSArray
* set
,
1379 const IORegistryEntry
* member
,
1380 unsigned int * index
) const
1383 OSObject
* probeObject
;
1385 for( i
= 0; (probeObject
= set
->getObject(i
)); i
++) {
1386 if (probeObject
== (OSObject
*) member
) {
1395 bool IORegistryEntry::makeLink( IORegistryEntry
* to
,
1396 unsigned int relation
,
1397 const IORegistryPlane
* plane
) const
1400 bool result
= false;
1402 if( (links
= (OSArray
*)
1403 registryTable()->getObject( plane
->keys
[ relation
] ))) {
1405 result
= arrayMember( links
, to
);
1407 result
= links
->setObject( to
);
1411 links
= OSArray::withObjects( (const OSObject
**) &to
, 1, 1 );
1412 result
= (links
!= 0);
1414 result
= registryTable()->setObject( plane
->keys
[ relation
],
1423 void IORegistryEntry::breakLink( IORegistryEntry
* to
,
1424 unsigned int relation
,
1425 const IORegistryPlane
* plane
) const
1430 if( (links
= (OSArray
*)
1431 registryTable()->getObject( plane
->keys
[ relation
]))) {
1433 if( arrayMember( links
, to
, &index
)) {
1434 links
->removeObject( index
);
1435 if( 0 == links
->getCount())
1436 registryTable()->removeObject( plane
->keys
[ relation
]);
1442 OSArray
* IORegistryEntry::getParentSetReference(
1443 const IORegistryPlane
* plane
) const
1446 return( (OSArray
*) registryTable()->getObject(
1447 plane
->keys
[ kParentSetIndex
]));
1452 OSIterator
* IORegistryEntry::getParentIterator(
1453 const IORegistryPlane
* plane
) const
1462 links
= getParentSetReference( plane
);
1464 links
= OSArray::withCapacity( 1 );
1466 links
= OSArray::withArray( links
, links
->getCount() );
1469 iter
= IOLinkIterator::withCollection( links
);
1477 IORegistryEntry
* IORegistryEntry::copyParentEntry( const IORegistryPlane
* plane
) const
1479 IORegistryEntry
* entry
= 0;
1484 if( (links
= getParentSetReference( plane
))) {
1485 entry
= (IORegistryEntry
*) links
->getObject( 0 );
1494 IORegistryEntry
* IORegistryEntry::getParentEntry( const IORegistryPlane
* plane
) const
1496 IORegistryEntry
* entry
;
1498 entry
= copyParentEntry( plane
);
1505 OSArray
* IORegistryEntry::getChildSetReference( const IORegistryPlane
* plane
) const
1508 return( (OSArray
*) registryTable()->getObject(
1509 plane
->keys
[ kChildSetIndex
]));
1514 OSIterator
* IORegistryEntry::getChildIterator( const IORegistryPlane
* plane
) const
1523 links
= getChildSetReference( plane
);
1525 links
= OSArray::withCapacity( 1 );
1527 links
= OSArray::withArray( links
, links
->getCount() );
1530 iter
= IOLinkIterator::withCollection( links
);
1539 IORegistryEntry
* IORegistryEntry::copyChildEntry(
1540 const IORegistryPlane
* plane
) const
1542 IORegistryEntry
* entry
= 0;
1547 if( (links
= getChildSetReference( plane
))) {
1548 entry
= (IORegistryEntry
*) links
->getObject( 0 );
1557 IORegistryEntry
* IORegistryEntry::getChildEntry(
1558 const IORegistryPlane
* plane
) const
1560 IORegistryEntry
* entry
;
1562 entry
= copyChildEntry( plane
);
1569 void IORegistryEntry::applyToChildren( IORegistryEntryApplierFunction applier
,
1571 const IORegistryPlane
* plane
) const
1575 IORegistryEntry
* next
;
1581 array
= OSArray::withArray( getChildSetReference( plane
));
1585 (next
= (IORegistryEntry
*) array
->getObject( index
));
1587 (*applier
)(next
, context
);
1592 void IORegistryEntry::applyToParents( IORegistryEntryApplierFunction applier
,
1594 const IORegistryPlane
* plane
) const
1598 IORegistryEntry
* next
;
1604 array
= OSArray::withArray( getParentSetReference( plane
));
1608 (next
= (IORegistryEntry
*) array
->getObject( index
));
1610 (*applier
)(next
, context
);
1615 bool IORegistryEntry::isChild( IORegistryEntry
* child
,
1616 const IORegistryPlane
* plane
,
1617 bool onlyChild
) const
1624 if( (links
= getChildSetReference( plane
))) {
1625 if( (!onlyChild
) || (1 == links
->getCount()))
1626 ret
= arrayMember( links
, child
);
1628 if( ret
&& (links
= child
->getParentSetReference( plane
)))
1629 ret
= arrayMember( links
, this );
1636 bool IORegistryEntry::isParent( IORegistryEntry
* parent
,
1637 const IORegistryPlane
* plane
,
1638 bool onlyParent
) const
1646 if( (links
= getParentSetReference( plane
))) {
1647 if( (!onlyParent
) || (1 == links
->getCount()))
1648 ret
= arrayMember( links
, parent
);
1650 if( ret
&& (links
= parent
->getChildSetReference( plane
)))
1651 ret
= arrayMember( links
, this );
1658 bool IORegistryEntry::inPlane( const IORegistryPlane
* plane
) const
1664 ret
= (0 != getParentSetReference( plane
));
1671 bool IORegistryEntry::attachToParent( IORegistryEntry
* parent
,
1672 const IORegistryPlane
* plane
)
1683 ret
= makeLink( parent
, kParentSetIndex
, plane
);
1685 if( (links
= parent
->getChildSetReference( plane
)))
1686 needParent
= (false == arrayMember( links
, this ));
1690 // ret &= parent->makeLink( this, kChildSetIndex, plane );
1695 ret
&= parent
->attachToChild( this, plane
);
1700 bool IORegistryEntry::attachToChild( IORegistryEntry
* child
,
1701 const IORegistryPlane
* plane
)
1712 ret
= makeLink( child
, kChildSetIndex
, plane
);
1714 if( (links
= child
->getParentSetReference( plane
)))
1715 needChild
= (false == arrayMember( links
, this ));
1722 ret
&= child
->attachToParent( this, plane
);
1727 void IORegistryEntry::detachFromParent( IORegistryEntry
* parent
,
1728 const IORegistryPlane
* plane
)
1737 breakLink( parent
, kParentSetIndex
, plane
);
1739 if( (links
= parent
->getChildSetReference( plane
)))
1740 needParent
= arrayMember( links
, this );
1744 // parent->breakLink( this, kChildSetIndex, plane );
1749 parent
->detachFromChild( this, plane
);
1754 void IORegistryEntry::detachFromChild( IORegistryEntry
* child
,
1755 const IORegistryPlane
* plane
)
1764 breakLink( child
, kChildSetIndex
, plane
);
1766 if( (links
= child
->getParentSetReference( plane
)))
1767 needChild
= arrayMember( links
, this );
1774 child
->detachFromParent( this, plane
);
1779 void IORegistryEntry::detachAbove( const IORegistryPlane
* plane
)
1781 IORegistryEntry
* parent
;
1784 while( (parent
= getParentEntry( plane
)))
1785 detachFromParent( parent
, plane
);
1789 void IORegistryEntry::detachAll( const IORegistryPlane
* plane
)
1792 IORegistryEntry
* next
;
1793 IORegistryIterator
* regIter
;
1795 regIter
= IORegistryIterator::iterateOver( this, plane
, true );
1798 all
= regIter
->iterateAll();
1801 detachAbove( plane
);
1803 while( (next
= (IORegistryEntry
*) all
->getLastObject())) {
1806 all
->removeObject(next
);
1808 next
->detachAbove( plane
);
1815 unsigned int IORegistryEntry::getDepth( const IORegistryPlane
* plane
) const
1817 unsigned int depth
= 1;
1819 unsigned int oneDepth
, maxParentDepth
, count
;
1820 IORegistryEntry
* one
;
1821 const IORegistryEntry
* next
;
1827 while( (parents
= next
->getParentSetReference( plane
))) {
1829 count
= parents
->getCount();
1834 next
= (IORegistryEntry
*) parents
->getObject( 0 );
1839 (one
= (IORegistryEntry
*) parents
->getObject( index
));
1841 oneDepth
= one
->getDepth( plane
);
1842 if( oneDepth
> maxParentDepth
)
1843 maxParentDepth
= oneDepth
;
1845 depth
+= maxParentDepth
;
1855 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1858 #define super OSIterator
1860 OSDefineMetaClassAndStructors(IORegistryIterator
, OSIterator
)
1862 enum { kIORegistryIteratorInvalidFlag
= 0x80000000 };
1864 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1866 IORegistryIterator
*
1867 IORegistryIterator::iterateOver( IORegistryEntry
* root
,
1868 const IORegistryPlane
* plane
,
1869 IOOptionBits options
)
1871 IORegistryIterator
* create
;
1878 create
= new IORegistryIterator
;
1880 if( create
->init()) {
1883 create
->root
= root
;
1884 create
->where
= &create
->start
;
1885 create
->start
.current
= root
;
1886 create
->plane
= plane
;
1887 create
->options
= options
& ~kIORegistryIteratorInvalidFlag
;
1897 IORegistryIterator
*
1898 IORegistryIterator::iterateOver( const IORegistryPlane
* plane
,
1899 IOOptionBits options
)
1901 return( iterateOver( gRegistryRoot
, plane
, options
));
1904 bool IORegistryIterator::isValid( void )
1913 ok
= (0 == (kIORegistryIteratorInvalidFlag
& options
));
1915 while( ok
&& next
) {
1917 ok
= where
->iter
->isValid();
1925 void IORegistryIterator::enterEntry( const IORegistryPlane
* enterPlane
)
1930 where
= (IORegCursor
*) IOMalloc( sizeof( IORegCursor
));
1936 where
->current
= prev
->current
;
1941 void IORegistryIterator::enterEntry( void )
1943 enterEntry( plane
);
1946 bool IORegistryIterator::exitEntry( void )
1951 where
->iter
->release();
1953 if( where
->current
)// && (where != &start))
1954 where
->current
->release();
1957 if( where
!= &start
) {
1960 IOFree( gone
, sizeof( IORegCursor
));
1967 void IORegistryIterator::reset( void )
1977 where
->current
= root
;
1978 options
&= ~kIORegistryIteratorInvalidFlag
;
1981 void IORegistryIterator::free( void )
1992 IORegistryEntry
* IORegistryIterator::getNextObjectFlat( void )
1994 IORegistryEntry
* next
= 0;
1995 OSArray
* links
= 0;
1999 if( (0 == where
->iter
)) {
2000 // just entered - create new iter
2003 && (links
= ( (options
& kIORegistryIterateParents
) ?
2004 where
->current
->getParentSetReference( plane
) :
2005 where
->current
->getChildSetReference( plane
) )) )
2007 where
->iter
= OSCollectionIterator::withCollection( links
);
2010 // next sibling - release current
2012 where
->current
->release();
2016 next
= (IORegistryEntry
*) where
->iter
->getNextObject();
2020 else if( !where
->iter
->isValid())
2021 options
|= kIORegistryIteratorInvalidFlag
;
2024 where
->current
= next
;
2031 IORegistryEntry
* IORegistryIterator::getNextObjectRecursive( void )
2033 IORegistryEntry
* next
;
2036 next
= getNextObjectFlat();
2037 while( (0 == next
) && exitEntry());
2041 done
= OSOrderedSet::withCapacity( 10 );
2042 if( done
->setObject((OSObject
*) next
)) {
2043 // done set didn't contain this one, so recurse
2050 IORegistryEntry
* IORegistryIterator::getNextObject( void )
2052 if( options
& kIORegistryIterateRecursively
)
2053 return( getNextObjectRecursive());
2055 return( getNextObjectFlat());
2058 IORegistryEntry
* IORegistryIterator::getCurrentEntry( void )
2061 return( where
->current
);
2066 OSOrderedSet
* IORegistryIterator::iterateAll( void )
2069 while( getNextObjectRecursive())
2076 OSMetaClassDefineReservedUsed(IORegistryEntry
, 0);
2077 OSMetaClassDefineReservedUsed(IORegistryEntry
, 1);
2078 OSMetaClassDefineReservedUsed(IORegistryEntry
, 2);
2079 OSMetaClassDefineReservedUsed(IORegistryEntry
, 3);
2080 OSMetaClassDefineReservedUsed(IORegistryEntry
, 4);
2082 OSMetaClassDefineReservedUnused(IORegistryEntry
, 5);
2083 OSMetaClassDefineReservedUnused(IORegistryEntry
, 6);
2084 OSMetaClassDefineReservedUnused(IORegistryEntry
, 7);
2085 OSMetaClassDefineReservedUnused(IORegistryEntry
, 8);
2086 OSMetaClassDefineReservedUnused(IORegistryEntry
, 9);
2087 OSMetaClassDefineReservedUnused(IORegistryEntry
, 10);
2088 OSMetaClassDefineReservedUnused(IORegistryEntry
, 11);
2089 OSMetaClassDefineReservedUnused(IORegistryEntry
, 12);
2090 OSMetaClassDefineReservedUnused(IORegistryEntry
, 13);
2091 OSMetaClassDefineReservedUnused(IORegistryEntry
, 14);
2092 OSMetaClassDefineReservedUnused(IORegistryEntry
, 15);
2093 OSMetaClassDefineReservedUnused(IORegistryEntry
, 16);
2094 OSMetaClassDefineReservedUnused(IORegistryEntry
, 17);
2095 OSMetaClassDefineReservedUnused(IORegistryEntry
, 18);
2096 OSMetaClassDefineReservedUnused(IORegistryEntry
, 19);
2097 OSMetaClassDefineReservedUnused(IORegistryEntry
, 20);
2098 OSMetaClassDefineReservedUnused(IORegistryEntry
, 21);
2099 OSMetaClassDefineReservedUnused(IORegistryEntry
, 22);
2100 OSMetaClassDefineReservedUnused(IORegistryEntry
, 23);
2101 OSMetaClassDefineReservedUnused(IORegistryEntry
, 24);
2102 OSMetaClassDefineReservedUnused(IORegistryEntry
, 25);
2103 OSMetaClassDefineReservedUnused(IORegistryEntry
, 26);
2104 OSMetaClassDefineReservedUnused(IORegistryEntry
, 27);
2105 OSMetaClassDefineReservedUnused(IORegistryEntry
, 28);
2106 OSMetaClassDefineReservedUnused(IORegistryEntry
, 29);
2107 OSMetaClassDefineReservedUnused(IORegistryEntry
, 30);
2108 OSMetaClassDefineReservedUnused(IORegistryEntry
, 31);
2110 /* inline function implementation */
2111 OSDictionary
* IORegistryEntry::getPropertyTable( void ) const
2112 { return(fPropertyTable
); }