Linux Device driver basic framework (character device)

Source: Internet
Author: User

Linux device drivers are in the form of kernel modules, but the modules are not necessarily drivers. The driver can be compiled into the kernel, when configuring the kernel, if a configuration item is set to M, then it will be compiled with the kernel compiled into a module, but to add drivers to the kernel will make the kernel very large, and in addition, modify, delete the driver need to compile the kernel again, this is extremely troublesome. So the general driver is the modular load, when needed to use the insmod loaded into the kernel, do not need to use the Rmmod to unload the driver module.

Key related commands for kernel modules:

    1. LSMOD: See which modules are loaded by the current kernel

    2. Insmod: Load Module

    3. Rmmod: Uninstalling the module

    4. Modinfo: Viewing module information

Drivers are part of the kernel, write drivers to follow certain kernel development rules, generally write a driver from a few steps:

    1. View schematics to learn how the device works

    2. Finding similar development programs in the kernel, mimicking their format and usage, is really not going to start from scratch.

    3. Implementing the initialization of the driver

    4. Implementing the corresponding operation function of the application interface

    5. Compiling the loader

    6. Test

The basic framework of the driver writing is similar, is to implement the system call to the corresponding function, and in the function to implement the corresponding function, a simple LED driver, for example, according to the character device in the core data structures file_operations structure (search in the kernel), The member functions in this struct are the main content of the character device driver program, which actually uses open (), read (), write ... in application Linux. is called at the end of the system call. 650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/77/85/wKioL1ZpN9nyOSUcAADuTPfBCbo563.png "title=" capture. PNG "alt=" Wkiol1zpn9nyosucaadutpfbcbo563.png "/>

Implement the corresponding function in the driver based on the requirements of this struct and the actual application

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/77/85/wKioL1ZpOTTSVKblAAKMRiN36LU584.png "title=" capture. PNG "alt=" Wkiol1zpottsvkblaakmrin36lu584.png "/>

The above is a basic driver framework, first in the use of Insmod Mydev.ko mount driver, execution: Module_init->my_init to complete the initialization of the work, when unloaded Rmmod mydev.ko->module_exit- >my_exit Complete the cleanup work (turn off the device, etc.). After the driver is loaded successfully, the application calls to the driver's My_open, My_read, and then to the corresponding device files using open, read and other system invocations. The Last Module_license ("GPL") is a statement of the license that declares this process to prevent kernel contamination.

This article is from the "June Feng Eureka" blog, please be sure to keep this source http://10274409.blog.51cto.com/10264409/1721624

Linux Device driver basic framework (character device)

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.