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

Source: Internet
Author: User

Interface functions between kernel space and hardware devices

There are also some functions in the kernel space to control devices or exchange information between the kernel and hardware. The following table describes these concepts. This table will also be improved with the introduction of some concepts.

 

Events

Kernel functions

Read data

 

Write Data

 

Table Device Driver events and related functions between kernel space and hardware devices

 

First driver: load and uninstall the driver in the user space

I will show you how to develop your first Linux Device Driver, which will be loaded to the kernel in the form of modules.

To achieve the above purpose, I will write the following program and name it nothing. c

 

# Include <Linux/module. h>

Module_license ("dualbsd/GPL ");

 

Because the current kernel release version is 2.6.x, the compilation module has become a little more complex. First, you need to have a complete, compiled kernel source code tree. If you have a Debian sarge system, follow the steps in Appendix B (later in this article ). In the future, I will assume that you are using the 2.6.8 kernel version.

Then, you need to compile a MAKEFILE file. For our current example, we should compile a file named "makefile" with the following content:

 

OBJ-M: = nothing. o

 

Different from the previous kernel versions, you can use the later kernel version of the generated module, when compiling this module, you must use the same kernel as your target kernel. To compile it, you can enter the following command:

 

$ Make-C/usr/src/kernel-source-2.6.8 M = PWD modules

 

This very simple module is part of the kernel space. It becomes a part of the kernel when it is loaded. In user space, you can use the root user permission to type the following command line to load this module:

 

# Insmod nothing. Ko

 

Use the insmod command to install this module to the kernel. However, in fact, this special module itself is not very useful.

To check whether our modules are correctly installed, run the following command to view all the modules we have installed:

 

# Lsmod

 

Finally, we can use the following command to remove the installed modules:

 

# Rmmod nothing

 

Now, by typing the lsmod command again, you can confirm that this module does not exist in the kernel.

The summary is as follows:

 

Events

User Functions

Kernel functions

Load Module

Insmod

 

Open Device

 

 

Read Device

 

 

Write Device

 

 

Close Device

 

 

Remove Module

Rmmod

 

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.