Device Model of Linux Driver (1)

Source: Internet
Author: User

1. Overview

L The device model is a new feature introduced by kernel 2.6. It provides an independent mechanism to specifically represent the device and describe its topological structure in the system, so that the system has the following advantages:

N code duplication Minimization

N provides a unified mechanism such as reference count

N you can list all the devices in the system, observe their statuses, and view the bus they connect.

N can completely and effectively present all the device structures in the system in the form of a tree

N can connect the device with its corresponding driver, and vice versa.

N can classify devices by type. For example, devices are classified as input devices without understanding the topological structure of physical devices.

N can traverse the leaf of the device tree to its root to ensure that the power supply of the device is disabled in the correct order.

The last point is the initial motivation for implementing the device model.

 

L as a Linux driver engineer, it is necessary to master the device model. Master the device driver model, read the kernel source code, and find that you will read the code at a higher level.

 

2. kobject

2.1 Overview

The core part of the device model is kobject (Kernel Object), which is represented by the struct kobject struct and represented as a directory in sysfs.

 

2.2 kobject struct

L kobject is the basic structure of the device model. It is usually embedded in other structures so that you can access the structure through kobject.

Struct kobject {

Const char * Name;/* kobject name */

Struct list_head entry;/* used to chain the linked list of the kset to which it belongs */

Struct kobject * parent;/* pointing to the parent object of kobject */

Struct kset * kset;/* kset */

Struct kobj_type * ktype;/* ktype */

Struct sysfs_dirent * SD;/* directory items in sysfs */

Struct kref;/* provide reference count */

Unsigned intstate_initialized: 1;/* flag: initialization */

Unsigned intstate_in_sysfs: 1;/* flag: In sysfs */

Unsigned intstate_add_uevent_sent: 1;/* flag: kobj_adduevent has been issued */

Unsigned intstate_remove_uevent_sent: 1;/* flag: kobj_remove uevent has been issued */

Unsigned intuevent_suppress: 1;/* flag: Disable uevent */

};

 

2.3 Basic kobject operations

Kobject operation functions are implemented in the kobject. c file.

L Initialization

Voidkobject_init (struct kobject * kobj, struct kobj_type * ktype)

L add kobject to generate a directory in sysfs

Intkobject_add (struct kobject * kobj, struct kobject * parent,

Constchar * FMT ,...)

L initialization and Addition

Intkobject_init_and_add (struct kobject * kobj, struct kobj_type * ktype,

Struct kobject * parent, const char * FMT ,...)

L create and add

Struct kobject * kobject_create_and_add (const char * Name, struct kobject * parent)

L Delete

Void kobject_del (struct kobject * kobj)

L Reference shujia

Struct kobject * kobject_get (struct kobject * kobj)

L The number of references is reduced by one. When the reference count is reduced to 0, the release function is called to destroy the reference count.

Voidkobject_put (struct kobject * kobj)

 

2.3 instance resolution

Create a kobject and create an attribute File

/*

* Forlearn kobject

*/

# Include <Linux/module. h>

# Include <Linux/init. h>

# Include <Linux/kobject. h>

# Include <Linux/sysfs. h>

# Include <Linux/string. h>

 

Static int n = 6;

/* When the user space reads the attribute file, this method is called to encode the specified value and put it into the buffer */

Static ssize_t SC _show (struct kobject * kobj,

Structkobj_attribute * ATTR, char * BUF)

{

Returnsprintf (BUF, "% d \ n", N );

}

/* When a user space writes a property file, this method is called to decode the specified value and put it into the corresponding variable */

Static ssize_t SC _store (struct kobject * kobj,

Structkobj_attribute * ATTR, const char * Buf, size_t count)

{

Sscanf (BUF, "% du", & N );

Returncount;

}

 

/* Construct kobject attributes */

Static struct kobj_attribute SC _attrb =

_ ATTR (maid, 0666, SC _show, SC _store );

 

Static struct kobject * kobj;

 

Static int _ init example_init (void)

{

Intret;

 

/* Create kobject, A Dir (kobj_example) in sys /*/

Kobj = kobject_create_and_add ("kobj_example", null );

If (! Kobj)

Return-enomem;

/* Create a attribute file (SC _example) in kobj_example */

Ret = sysfs_create_file (kobj, & SC _attrb.attr );

If (RET)

Gotoattr_file_failed;

Return0;

Attr_file_failed:

Kobject_put (kobj );

Returnret;

}

 

Static void _ exit example_exit (void)

{

Sysfs_remove_file (kobj, & SC _attrb.attr );

Kobject_put (kobj );

}

 

Module_init (example_init );

Module_exit (example_exit );

 

Module_license ("dual BSD/GPL ");

Module_author ("cjok <cjok.liao@gmail.com> ");

 

Test results:

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.