1, Kobject
Kobject is the core structure of the device-driven model, which enables all devices to have a unified interface at the bottom. The Kobject object registered in the kernel corresponds to a directory in the Sysfs file system (the directory name is k_name specified in the kobject structure)
struct Kobject {
const char * K_NAME; Pointer to device name
Char Name[kobj_name_len]; Device Name
struct Kref kref; Reference count
struct List_head entry; Owning Kset Collection List member
struct Kobject * parent; Point to Parent Node object
struct Kset * kset; Owning Kset Collection
struct Kobj_type * KTYPE; Device type
struct Dentry * dentry; The file node pointer that corresponds to the object
Wait_queue_head_t poll; Wait queue
};
Related operation functions:
extern void Kobject_put (struct kobject *);
The Kobject reference count is reduced by 1 (that is, Kref minus 1), and when the KEF value is 0 o'clock, the release function is called to free the Kobject object;
struct Kobject * kobject_get (struct kobject * kobj)
Kobject reference count increased by 1;
extern int Kobject_add (struct kobject *);
Add the Kobject object to the Kset collection to which it belongs, and create the corresponding file directory (see kobject.c in the source code);
extern void Kobject_del (struct kobject *);
Delete the Kobject object;
extern int Kobject_register (struct kobject *);
Register the Kobject object, call Kobject_init to initialize, then call Kobject_add to complete the registration of the object, and finally call Kobject_uevent Register Kobject add event;
extern void Kobject_unregister (struct kobject *);
Unregister the Kobject object, call Kobject_uevent to register the Kobj_remove event (event notification function), and then call Kobject_del to delete the object, and finally call Kobject_put to reduce the object's reference count. The release destroy function is also called when the reference count is 0 o'clock;
2, Kset
struct Kset {
struct subsystem * SUBSYS; Subordinate subsystem
struct Kobj_type * KTYPE; The type of kobject that belongs to the Kset collection
struct List_head list; Kobject object chain header for the owning Kset
spinlock_t List_lock; Kset of mutual exclusion lock
struct Kobject kobj; Parent node of the Kobject object that owns the Kset
struct Kset_uevent_ops * uevent_ops; Kset Related event handler function
};
Related operation functions:
static inline void Kset_put (struct kset * k);
Kset the reference count minus 1, the actual Kset reference count is used for the reference count of the embedded Kobject object;
static inline struct Kset * kset_get (struct kset * k)
Kset the reference count plus 1;
int Kset_add (struct kset * k)
Kset_add internal call is Kobject_add, in fact, kset embedded Kobject object is attached to the upper subsystem;
Kset_register, Kset_unregister and kobject corresponding operation function implementation is similar (source details kobject.c)
The diagram of Kobject and Kset is as follows:
3, Kobj_type
struct Kobj_type {
void (*release) (struct kobject *); Releasing the Kobject function
struct Sysfs_ops * sysfs_ops; Attribute action fields (read and write)
struct attribute * * DEFAULT_ATTRS; The default property field, which specifies the property structure to be exported to the SYSFS directory as a file;
struct Sysfs_ops {
ssize_t (*show) (struct kobject *, struct attribute *,char *buf); Handle the user to read the property value, the value after the property read is saved in the parameter buf;
ssize_t (*store) (struct kobject *,struct attribute *,const char *, size_t); Handling user-Set property values
};
struct Attribute {
const char * name; Filename
struct module * owner; Owning module
mode_t mode;
};
Linux Kernel series device model (i) Kobject and Kset