Kobject, kset and its relationship of device model

  
Linux 2.6 device drivers are built on the basis of the device model, therefore, to write device drivers under linux, whether it is usb device, pci device, etc. Need to understand the device model.
The basic structure of the device model is mainly kobject, kset two structures:
struct kobject { char * k_name; char name[KOBJ_NAME_LEN]; struct kref 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;};

Subsys structure, but the subsys structure is similar to kset, there is an exclusive access semaphore, so there is no need to list it, there is also a structure
struct kobj_type { void (*release)(struct Kobject *); struct sysfs_ops * sysfs_ops; struct attribute ** default_attrs; };

is used to indicate the type of kobject, kset.

A kobject structure is shown in the kobject type part of the figure below, and a kset structure is as shown in the kset type part of the figure. A kobject is added to a kset, mainly the related fields in the kobject structure record the corresponding kset information, 1 Record the kset corresponding to kobject, which points to the address of the kobject contained in kset, 2 records the kset pointer of kset corresponding to kobject, 3 records the type of kobject, 4 records the chain of all kobjects of kset, this The chain is a doubly linked list. Whenever a kobject is added to the current kset, the list_add_tail() function is called, and the kobject to be added to the kset is connected to the end of the list to form a linked list.
kobjec-kset

When there is another kobject to join the current kset, the 123 step is the same as the first kobject added to the current kset, that is, the member of the kobject to be added. Make it point to the current kset corresponding data, and 4 needs to add kobject to the end of kset's list. The following figure shows the kobject b added to kset A:
kobjec-kset

When there is A kset needs to be added to the current kset. The method is also the same as adding a kobject to the current kset, that is, setting the members of the kobject contained in the kset to be added, so that these members point to the corresponding data of the corresponding kset. The current kset is to be added to another kset, the way is the same as the kset added to the current kset, is the kobject in the device kset, so that the members of the kobject point to the corresponding data of the kset to be added, the following figure shows a kset B is added to the icon in kset A.
kobjec-kset

A simple kset, kobject relationship diagram is as follows:
kobjec-kset

Copyright © Windows knowledge All Rights Reserved