Device Driver base 0: kobject and kset of device model and Their Relationship

Source: Internet
Author: User

The device drivers after linux2.6 are built based on the device model. Therefore, you need to understand the device model for writing the device drivers in Linux, such as USB devices and PCI devices.

The basic structure of the device model is kobject and kset:

Struct kobject {

Char * k_name;

Char name [kobj_name_len];

Struct kref;

Struct list_head entry;

Struct kobject * parent;

Struct kset * kset;

Struct kobj_type * ktype;

Struct dentry * dentry;

};

 

Struct kset {

Struct subsystem * subsys;

Struct kobj_type * ktype;

Struct list_head list;

Struct kobject kobj;

Struct kset_hotplug_ops * hotplug_ops;

};

 

There is also a subsys struct, but the subsys struct is similar to kset, so there is an extra mutex access semaphore, so you do not need to list it. There is also a struct

Struct kobj_type {

Void (* release) (struct kobject *);

Struct sysfs_ops * sysfs_ops;

Struct attribute ** default_attrs;

};

Indicates the type of kobject and kset. 

A kobject structure is the section of the kobject type, while a kset structure is the section of the kset type. A kobject is added to a kset. The relevant fields in the kobject structure record the corresponding kset information, ① records the kset corresponding to the kobject, which points to the address of the kobject contained in the kset, ② records the kset pointer of the kset corresponding to the kobject, and ③ records the type of the kobject, ④ records the chains of all kobject in the kset. This chain is a two-way linked list. Every time a kobject is added to the current kset, The list_add_tail () function is called, connect the kobject to be added to the kset to the end of the linked list to form a linked list.

When another kobject is to be added to the current kset, step ① (③) is the same as the first kobject to be added to the current kset, that is, set the member of the kobject to be added, to point to the corresponding data of the current kset, and ④ add kobject to the end of the list of kset, indicating that kobject B is added to the graph of kset:

When a kset needs to be added to the current kset, the method is the same as that for a kobject to be added to the current kset, that is, to set the members of The kobject contained in the kset to be added, point these Members to the corresponding kset data. The current kset needs to be added to another kset. The method is the same as adding a kset to the current kset. It is the kobject in the device kset, so that the kobject members can point to the corresponding data of the kset to be added, the following figure shows how kset B is added to kset.

A simple kset and kobject relationship diagram is as follows:

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.