Linux character device driver authoring process

Source: Internet
Author: User
Tags function prototype

The driver writes the basic process:

1. First of all, some version information, no use, but not less

#define __NO_VERSION__

#include <linux/modules.h>

#include <linux/version.h>

Char kernel_version[] = uts_release;

2. In order to correlate system calls with drivers, a very critical data structure is required: struct file_operations. The name of each member of the file_operations structure corresponds to a system call. User processes use system calls to perform such read/write operations on device files, the system invokes the device driver by the main device number of the device file, then reads the corresponding function pointer to the data structure, and then gives control to the function. This is the basic principle of the Linux device driver work. The primary task of writing device drivers is to write child functions and populate file_operations fields

3. Writing of simple Drivers (test.c):

A. Contains some basic header files.

B. Write some functional functions, such as read (), write (), and so on. These functions are invoked when the system enters the nuclear mentality.

C. Define the objects of the struct file_operations structure, and populate the structure body. The order of functions in the structure can not be changed, if some functions are not implemented with NULL padding, the implemented functions such as read (), write () are added to the corresponding location respectively. This step implements the registration of the function. Here the driver's main body can be said to be written well. Now you need to embed the driver in the kernel.

D. Registering device drivers and registering character devices using Register_chrdev. The function prototype is:

int Register_chrdev (0, "Test_name", &test_file_operations)

The function returns the main device number, which returns a value greater than 0 if the registration succeeds.

First parameter: Main device number. Second parameter: the registered device name. Third parameter: The name of the struct (device-related operation, a pointer to the function in which the driver actually performs the action).

This function is called by the int init_module (void) function, which is called when the system is registered to the kernel when it is started.

E. When unloading a module with rmmod, the Cleanup_module function is invoked to free the table entries that the character device test occupies in the System character device table.

void Cleanup_module (void)

{

Unregister_chrdev (test_major, "test");

}

Here the test.c is basically written and finished. A simple character device can be said to be written well.

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.