While learning the platform platform driver, I slowly discovered that the bottom layer is calledlinux device driver models things. The discovery is that it is an important organizational structure for the underlying drive, and now it is aimed atDevice driver modelPerform analysis. So as to deepen the understanding of the entire device driver registration.
The user space program accesses the relevant information of the device through the sysfs virtual file system. This information is organized into a hierarchical structure and represented by the sysfs virtual file system. The user can control the device or read the information of the device by operating the sysfs.
kobj_type-->Properties file
struct kobject {
const char *name; The name of the kobject, displayed in the sysfs file system, as the name of a directory.
struct list_head entry; link to the next kobject structure
struct kobject *parent; points to the parent kobject structure
struct kset *kset; points to the kest set it belongs to
struct kobj_type *ktype; points to the properties file of kobject, each object has properties
Organize the attributes separately into a data structure kobj_type and store them in ktype
For ordinary file read and write operations in sysfs, kobject->ktype->sysfs_ops is used to complete
struct sysfs_dirent *sd; corresponding to the file directory of sysfs
struct kref kref; reference count of kobject
unsigned int state_initialized:1; Initialized state
unsigned int state_in_sysfs:1; Whether it has been added to sysfs
unsigned int state_add_uevent_sent:1;
unsigned int state_remove_uevent_sent:1;
unsigned int uevent_suppress:1;
};
name represents the name of kobject in sysfs;
The pointer parent is used to point to the parent of kobject;
Kref should be familiar to everyone, kobject uses it to implement reference counting;
The Kset pointer is used to point to the kset to which this kobject belongs;
For ktype, it is used to describe the type information of kobject.
The role of kobject:
(1)kobject always represents a directory in the sysfs file system, and the name member specifies the name of the directory, not a file.
(2)parent member specifiedThe directory of kobject in sysfs, thus forming a tree structure.
(3)ktype isThe attributes of kobject, which are represented by files, placed inkobject corresponds to the directory.
struct kobj_type {
void (*release)(struct kobject *kobj); release kobject and its resource-occupied function
const struct sysfs_ops *sysfs_ops; The method of operating the next attribute array
struct attribute **default_attrs; attribute array
const struct kobj_ns_type_operations *(*child_ns_type)(struct kobject *kobj);
const void *(*namespace)(struct kobject *kobj);
};The default_attrs member of kobj_type saves an array of attributes. Each kobject object can have one or more attributes. The attributes are defined as follows:struct attribute {
const char *name; The name of the attribute, corresponding to the name of a file in the directory
umode_t mode; read and write permissions for attributes
#ifdef CONFIG_DEBUG_LOCK_ALLOC
struct lock_class_key *key;
struct lock_class_key skey;
#endif
};The default_attrs array of kobj_type indicates which attributes kobject has, but it does not explain how to operate these attributes. This task is determined by kobj_typestruct sysfs_ops {
ssize_t (*show)(struct kobject *, struct attribute *,char *); read attribute operation function
ssize_t (*store)(struct kobject *,struct attribute *,const char *, size_t); write attribute operation function
const void *(*namespace)(struct kobject *, const struct attribute *);
};kobject: the kobject pointer to be read and written, attribute: the attribute to be read and written.struct kset {
struct list_head list;
spinlock_t list_lock;
struct kobject kobj;
const struct kset_uevent_ops *uevent_ops;
};
kobject andKset correspondence:
The current simple understanding:
Linux has a virtual file system /sysfs. After the kernel starts to mount the file system, the sysfs file system will be mounted under /sys. When the kernel is started, some buses and devices will be initialized and registered. These buses and devices will create directories under sys for storage. For example, platform device information is stored under /sys/bus/platform. How to organize and manage equipment and create a catalog?Device driver modelSeveral important structures: Kobject, kobj_type, kset to organize and manage the directory and file structure.
prior tohttps://mp.csdn.net/mdeditor/84722837#This blog introduces the relationship between the three structures of kobject, set, ktype and its role, which can be used as a reference. The following de...
This blog has an analysis of the role of kobject, this blog is a practical application of kobject, the code is as follows: <kobject.c> The actual operation results are as follows:...
In the "Device Driver Model: kobject, kset, ktype (four)" blog has detailed the operation of kset and the relationship between kset and kobject, the following is some practical operations fo...
First, KOBJECT Each directory (including subdirectory) under the SYS directory (ie, the sysfs virtual file system) corresponds to a KOBJECT structure instance, you need to specify a directory name whe...
Cited literature:Linux sysfs file system analysis_jansert's blog-CSDN blog §1 Kobject The Kobject structure is defined as: struct kobject { char * kname; pointer to device name char na...
The functions of kobject are: In the Linux kernel, kobject is the basic data type that composes the Linux device model. Each kobject object registered in the kernel corresponds to a directory in the s...
table of Contents 1. Introduction to kset 1.1 Definition 1.2 Hot swap events 1.2.1 Two ways of message notification 1.2.2 Operation interface 2. The relationship between kobject and kset 2.1 Inheritan...
table of Contents 1. Introduction to Device Driver Model 2. Introduction to kobject 2.1 kobject kernel data structure 2.2 kobject operation kernel interface Three. kobject instance 3.1 Makefile 3.2 Ex...
With the development of the Linux kernel, there are more and more devices they support, but there is no good way to manage the increased equipment drive. In order to provide a unified mechanism in the...
Foreword: The linux device model includes kobject, kobject_type, and kset. For a better understanding, take a chestnut: kset is like a spider web, and there are many intersections of lines on the web....