Writing Device Drivers in Linux: a brief tutorial (3)

Source: Internet
Author: User

"Helloworld DRIVER: loading and detaching drivers in kernel space

When a device driver module is loaded into the kernel, preparations such as resetting devices, reserved RAM (reserving RAM), interruptions, and input/output ports are often executed.

We need to display two functions: module_init and module_exit. These two functions have completed the preceding tasks in the kernel. They correspond to the insmod and rmmod of the user space. As mentioned above, these two commands are used to install and uninstall modules. In general, the user commands insmod and rmmod use the kernel functions module_init and module_exit.

Let's look at a program that implements the traditional program Hello World:

 

<Hello. c> =

 

# Include <Linux/init. h>

# Include <Linux/module. h>

# Include <Linux/kernel. h>

Module_license ("dualbsd/GPL ");

Static inthello_init (void ){

Printk ("<1> Hello world! \ N ");

Return 0;

}

Static voidhello_exit (void ){

Printk ("<1> bye, cruel world \ n ");

}

Module_init (hello_init );

Module_exit (hello_exit );

 

In fact, the hello_init and hello_exit functions can take any name you want. However, to correspond to the corresponding loading and unloading functions, they must be passed as parameters to the corresponding module_init and module_exit functions.

The printk function is used here, except that it only works in the kernel space. It is very similar to the well-known printf function. The symbol "<1>" indicates that the message has a high priority (small number ). In this way, you can receive the message in the kernel system log file.

After adding its name to "makefile", this module can also be compiled with the previous command.

 

<Makefile2> =

 

OBJ-M: = nothing. O hello. o

 

After this article, I wrote makefiles as an exercise for readers. A makefile that can compile all modules in this guide can be found in Appendix A of this document.

When a module is loaded or uninstalled, messages written in the printk function are printed on the system console. If these messages do not appear on the console, you can use the dmesg command to view them or use the command "cat/var/log/syslog" to view system logs.

The following table shows the two new functions.

Event

User Functions

Kernel functions

Load Module

Insmod

Module_init ()

Open Device

 

 

Read Device

 

 

Write Device

 

 

Close Device

 

 

Remove Module

Rmmod

Module_exit ()

Table Device Driver events and corresponding interaction functions in user space and kernel space

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.