Relationship between Linux characters cdev and inode

Source: Internet
Author: User

The inode mentioned in this article is a struct inode struct and is not an inode node in the inode block.

 

Char device driver

Related Data Structure:

Struct cdev {

  Struct kobject kobj;

Struct module * owner;

  Const struct file_operations * OPS;

Struct list_head list;

  Dev_t dev;

Unsigned int count;

};

 

Struct kobj_map {

Struct probe {

Struct probe * next;

Dev_t dev;

Unsigned long range;

Struct module * owner;

Kobj_probe_t * Get;

INT (* Lock) (dev_t, void *);

Void * data;

} * Probes [255];

Struct mutex * lock;

};

 

Static struct char_device_struct {

Struct char_device_struct * next;

Unsigned int Major;

Unsigned int baseminor;

Int minorct;

Char name [64];

Struct file_operations * fops;

Struct cdev * cdev;/* Will die */

} * Chrdevs [chrdev_major_hash_size];

 

# Define chrdev_major_hash_size 255

 

The following article describes the character device driver in three aspects and their association:

1. Character-driven model

2. device Number of the character device

3. Access to character device files in the file system

 

Character-driven model

Each character is driven byCdev Structure.

In the device driver model, the kobject mapping domain is used to record the device driver. this is represented by the struct kobj_map structure. it is embedded with an array of 255 struct probe pointers. kobj_map is referenced by the global variable cdev_map:

Static struct kobj_map * cdev_map;

Function Description:

Cdev_alloc () is used to create a cdev object.

  Cdev_add () is used to add the cdev object to the driver model., Mainly throughKobj_map.

Kobj_map () creates a probe object and inserts it into an item in cdev_map (How does one determine whether it is based on the device's master device number?), And associate probe-> data to point to cdev

Struct kobject *Kobj_lookup(Struct kobj_map * domain, dev_t Dev, int * index), find the nested kobject of its cdev object in cdev_map Based on the device number. (probe-> data-> kobj), the returned result is the cdev kobject (This function is called to open a device file and is not required when registering a character device.)

 

Device Number of the character device

Character device master and sub-device number allocation:

  Global Array chrdevsContains 255 (value of chrdev_major_hash_size) elements of struct char_device_struct.

  Each corresponding primary device ID.

If a device number is assigned, a struct char_device_struct object is created and added to chrdevs.

In this way, through the chrdevs array, we can know which device numbers are allocated.

Related functions:

Register_chrdev_region () allocates the specified device number range

Alloc_chrdev_region () dynamically allocates the device range

They are mainly implemented by calling the function _ register_chrdev_region ().

Note,These two functions are only used to register the device number! To associate with cdev, call cdev_add ()

Register_chrdev () requests a specified device number and registers it to the device driver model.

What it does is:

1. register the device number by calling _ register_chrdev_region ().

2. Allocate a cdev and implement it by calling cdev_alloc ().

3. Add cdev to the driver model. In this step, the device number is associated with the driver, which is implemented by calling cdev_add ().

4. Point the cdev of the struct char_device_struct object created in step 1 to the cdev allocated in step 2. Since register_chrdev () is an old interface, this step is not required in the new interface.

 

Access to character device files in the file system

For a character device file, its inode-> I _cdev points to the character drive object cdev. If I _cdev is null, the file is not opened.

Because multiple devices can share the same driver, the I _devices IN THE inode of the character device and the list in the cdev form a linked list.

 

First, when the system calls open to open a character device, the chrdev_open will be executed through a series of calls.

(This article will not discuss the call process by calling. Open in def_chr_fops, and def_chr_fops.open = chrdev_open)

Int chrdev_open (struct inode * inode, struct file * filp)

What chrdev_open () Does can be summarized as follows:

1. based on the device number (inode-> I _rdev), find the corresponding driver in the character device driver model, which is achieved through kobj_lookup (), kobj_lookup () the corresponding driver cdev's kobject will be returned.

2. Set inode-> I _cdev to point to the cdev.

3. Add inode to the list of cdev-> list.

4. Use the ops of cdev to set the f_op of the file object

5. If the open method is defined in OPS, the open method is called.

6. Return.

After chrdev_open () is executed, the f_op of the file object points to the ops of cdev. Therefore, after reading and writing the device, the corresponding operations of cdev will be executed.

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.