Basic process for writing Linux character device drivers

Source: Internet
Author: User
Linux character device driver programming process 1. first, some version information is useless, but not less # define _ NO_VERSION _ www.2cto.com # include & lt; linux/modules. h & gt; # include & lt; linux/version. h & gt; charkern...
Basic process for writing Linux character device drivers 1. First, some version information is useless, but not less # define _ NO_VERSION _ www.2cto.com # include # Include Char kernel_version [] = UTS_RELEASE; 2. to associate system calls with drivers, a critical data structure is required: struct file_operations. The name of each member in the file_operations structure corresponds to a system call. When a user process uses a system call to perform read/write operations on device files, the system calls the system to find the corresponding device driver through the master device number of the device file, then read the corresponding function pointer of the data structure, and then give the control to the function. This is the basic principle of linux Device Drivers. The main task of writing a device driver is to write a sub-function and fill in all file_operations fields. 3. write a simple driver (test. c): a. contains some basic header files. B. compile some functional functions, such as read () and write. When these functions are called, the system enters the core state. Www.2cto.com c. defines the object of the struct file_operations structure and fills in the struct. The order of functions in the struct cannot be changed. if some functions are not implemented, fill them with NULL. the implemented functions such as read () and write () are added to the corresponding positions respectively. This step implements function registration. Here, the main body of the driver can be said to be written. Now we need to embed the driver into the kernel. D. register the device driver and use register_chrdev to register the drivers. The function is prototype: int register_chrdev (0, "test_name", & test_file_operations). the function returns the primary device number. if the registration is successful, the return value is greater than 0. The first parameter is the master device number. The second parameter is the registered device name. Third parameter: struct name (device-related operation method, pointer of the function in which the driver actually performs the operation ). This function is called by the int init_module (void) function, which is called when the system registers to the kernel. E. When rmmod is used to uninstall the module, the cleanup_module function is called, which releases the table items that the character device test occupies in the system character device table. Void cleanup_module (void) {www.2cto.com unregister_chrdev (test_major, "test");} the test. c is basically written here. A simple character device can be written. 4. compile $ gcc-O2-DMODULE-d1_kernel _-c test. o test. c to get the file test. o, which is a device driver. If the device driver has multiple files, compile each file according to the preceding command line. then, the ld-r file1.o file2.o-o modulename driver has been compiled, now install it in the system. $ Insmod-f test. o after installation is successful, the device test is displayed in the/proc/devices file, and the master device number is displayed. To uninstall and run: $ rmmod test www.2cto.com 5. create a device node mkmod/dev/test c major minor c is a character device, major is the master device number, minor is the slave device number, generally, setting 0 or above is the basic process of writing the linux driver.
Related Article

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.