Linux Kernel series device model (i) Kobject and Kset

Source: Internet
Author: User

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

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.