For Linux, I am the underlying model of sysfs (2) Linux devices.

Source: Internet
Author: User

I have some discussions about the Linux Device Model on the Internet, and I have modified and sorted out some things.

§ 1Kobject
Kobject is a new device management mechanism introduced in Linux 2.6. It is represented by struct kobject in the kernel. Through this data structure, all devices have a unified interface at the underlying layer. kobject provides basic object management and is the core structure of the linux2.6 device model. It is closely associated with the sysfs file system, each kobject object registered in the kernel corresponds to a directory in the sysfs file system. Kobject is the basic structure of the device model. Similar to the base class in C ++, it is embedded in a larger object-a so-called container-a component used to describe the device model. Such as bus, devices, and drivers are typical containers. These containers are connected through kobject to form a tree structure. The tree structure corresponds to the/sys direction.
The kobject structure provides basic object management for some big data structures and subsystems, avoiding repeated implementations of similar functions. These functions include
-Object reference count.
-Maintain the object linked list (SET ).
-Object locking.
-Representation in the user space.

The kobject structure is defined:
Struct kobject {
Char * k name; pointer to device name
Char name [kobj name len]; device name
Struct kref; object reference count
Struct list head entry; mounted to the Unit in the kset
Struct kobject * parent; pointer to parent object
Struct kset * kset; pointer to the kset
Struct kobj type * ktype; pointer to its object type descriptor
Struct dentry * dentry; file Node path pointer corresponding to this object in sysfs File System
};
The kref field indicates the reference count of the object. The kernel manages the reference count of the object through kref. The Kernel provides two functions, kobject_get () and kobject_put (), to increase and decrease the reference count respectively, when the reference count is 0, all resources used by this object are released. The ktype field is a pointer to the kobj type structure, indicating the type of the object.

Related functions
Void kobject_init (struct kobject * kobj); kobject initialization function.
Int kobject_set_name (struct kobject * kobj, const char * format,...); set the name of the specified kobject.
Struct kobject * kobject_get (struct kobject * kobj); adds the reference count of the kobj object to 1 and returns the pointer of the object.
Void kobject_put (struct kobject * kobj); subtract 1 from the reference count of the kobj object. If the reference count is reduced to 0, call kobject release () to release the kobject object.
Int kobject_add (struct kobject * kobj); adds the kobj object to the Linux device level. Mount the kobject object to the list chain of the kset, increase the reference count of kobject at all levels in the parent directory, create a file node under the directory to which the parent points, and start the hotplug function of the kernel object of this type.
Int kobject_register (struct kobject * kobj); kobject registers a function. Call kobject Init () to initialize kobj, and then call kobject_add () to register the kernel object.
Void kobject_del (struct kobject * kobj); Delete the kobj object from the Linux device level (hierarchy.
Void kobject_unregister (struct kobject * kobj); kobject deregister the function. Unlike kobject register (), it first calls kobject del to delete the object from the device level, and then calls kobject put () to reduce the reference count of the object. If the reference count is reduced to 0, then, the kobject object is released.

§ 2 kobj type
Struct kobj_type {
Void (* release) (struct kobject *);
Struct sysfs_ops * sysfs_ops;
Struct attribute ** default_attrs;
};
The kobj type data structure contains three fields: A release method is used to release resources occupied by kobject; A sysfs ops Pointer Points to the sysfs operation table and a default attribute list of sysfs file systems. The sysfs operation table includes two functions: store () and show (). When the user State reads the attribute, the show () function is called. The Function Code specifies that the attribute value is stored in the buffer and returned to the user State. The store () function is used to store the attribute value passed in the user State.
Attribute
Struct attribute {
Char * Name;
Struct module * owner;
Mode_t mode;
};
Attribute. It is output to the sysfs directory as a file. Under the corresponding kobject directory. File
The name is name. The file read/write method corresponds to sysfs ops in kobj type.

§ 3. kset
The most important thing about kset is to establish the correlation between the upper layer (sub-system) and the lower layer (kobject. Kobject will also use it to identify which type it belongs to, and then create the correct directory location under/sys. While kset has a high priority, kobject uses its own * kset to find its own kset and specifies * ktype as the ktype under the kset unless no kset is defined, before using ktype to establish a link. Kobject is organized into a hierarchical structure through kset. kset is a set of kobject of the same type. It is expressed in the kernel using the kset data structure and is defined:
Struct kset {
Struct subsystem * subsys; pointer to the subsystem
Struct kobj type * ktype; pointer to the kset object type descriptor
Struct list head list; used to connect the chain table headers of all kobject in the kset
Struct kobject kobj; embedded kobject
Struct kset hotplug ops * hotplug OPS; pointer to hot swapping operation table
};
All the kobject contained in the kset is organized into a bidirectional cyclic linked list. The list field is the header of the linked list. The ktype field points to a kobj type structure, which is shared by all the kobject in the kset, indicating the type of these objects. The kset data structure is also embedded with a kobject object (represented by the kobj domain). The parent fields of all the kobject objects belonging to this kset point to this embedded object. In addition, kset depends on kobj to maintain reference count: kset reference count is actually the reference count of the embedded kobject object.
See Figure 1. Relationship between kset and kobject

 

This figure is classic and reflects the connection of the entire kobject.

Related functions
Similar to kobject, kset_init () initializes the specified kset, and kset_get () and kset_put () increase and decrease the reference count of the kset object respectively. The kset_add () and kset_del () functions add the specified keset object to the device level and delete it from the device level respectively. The kset_register () function completes kset registration and kset_unregister () the function is used to cancel the kset operation.

§ 4 Subsystem
If kset is used to manage the collection of kobject, the subsystem is used to manage the collection of kset. It describes a type of device subsystem in the system. For example, block subsys indicates all Block devices, corresponding to the block directory in the sysfs file system. Similarly, the devices subsys corresponds to the devices directory in sysfs and describes all the devices in the system. The subsystem is described by the struct subsystem data structure and is defined:
Struct subsystem {
Struct kset; embedded kset object
Struct RW semaphore rwsem; mutex access semaphore
};

It can be seen that the difference between subsystem and kset is that there is an additional semaphore, so in later code, subsystem has been completely banned by kset.

Each kset belongs to a subsystem. You can add a kset to the subsystem by setting the subsys field in the kset structure to point to the specified subsystem. All ksets attached to the same subsystem share the same rwsem semaphore, which is used to synchronously access the linked list in the kset.
Related functions
Subsystem has a group of similar functions:
Void subsystem_init (struct subsystem * subsys );
Int subsystem_register (struct subsystem * subsys );
Void subsystem_unregister (struct subsystem * subsys );
Struct subsystem * subsys_get (struct subsystem * subsys)
Void subsys_put (struct subsystem * subsys );

The usage of those functions will be detailed in the following examples. Here is just an introduction.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.