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: