Because some interface changes in the 3.2 kernel are hard to understand, we analyze the kernel from 2.6.22
Kobject is the basic structure of the device model. After 2.6, the kernel is added to it, which perfectly supports sysfs and provides hot plug-and-plug functions. It is a common data structure of the kernel.
Add kobject to kset
Kobject_register (struct kobject * kobj) // after this interface reaches the 3.2 kernel, it becomes a pointer function kobject_create_and_add
Kobject_init
Kref_init (& kobj-> kref );
Atomic_set (& kref-> refcount, 1); // set the reference count to 1.
Init_list_head (& kobj-> entry );
Kobject_add (kobj); // Add to kset
Kobject_shadow_add (kobj, null );
Kobject_get (kobj)
Kref_get (& kobj-> kref); // Add a reference count, so calling kobject_add will increase the reference count.
Parent = kobject_get (kobj-> parent); // Add a reference count for the parent object
If (kobj-> kset ){
If (! Parent)
Parent = kobject_get (& kobj-> kset-> kobj); // if no parent is specified, set the parent to its kset
List_add_tail (& kobj-> entry, & kobj-> kset-> list); // Add kobject to the kset linked list, which is equivalent to adding the kset directory.
Kobj-> parent = parent;
}
Create_dir (kobj, shadow_parent );
Sysfs_create_dir (kobj, shadow_parent); // generate a file directory under sysfs
Populate_dir (kobj) // generate a directory based on the attribute of kobj
If (T & T-> default_attrs) {// default_attrs is a structure array pointer.
For (I = 0; (ATTR = T-> default_attrs [I])! = NULL; I ++ ){
If (error = sysfs_create_file (kobj, ATTR )))
Break;
}
Next, analyze sysfs_create_file.
Sysfs_create_file
Sysfs_add_file (kobj-> dentry, ATTR, sysfs_kobj_attr );
Mode = (ATTR-> mode & s_iallugo) | s_ifreg;
If (! Sysfs_di1__exist (parent_sd, ATTR-> name) // If the directory does not exist
Error = sysfs_make_dirent (parent_sd, null, (void *) ATTR, mode, type); // create this directory based on ATTR-> mode
.......
Delete kobject from kset
Void kobject_del (struct kobject * kobj)
Sysfs_remove_dir (kobj );
.............
Analysis of Some kset Interfaces
Kset_init (struct kset * K)
Kobject_init (& K-> kobj); // It actually initializes kobject.
Init_list_head (& K-> list );
Int kobject_add (struct kobject * kobj)
Kobject_shadow_add (kobj, null );
...... Going back to the above
2.6.22 kernel kobject Analysis