HelloWorld for linux driver development

Source: Internet
Author: User
Tags what header

My recent internship was about flat development, and my assignment was to load the driver into the kernel.

Preparations: There are two ways to load: Dynamic Loading and uninstallation, that is, module loading, and direct compilation into the kernel; the Linux kernel divides drivers into three types: character devices, Block devices, and network devices. Character devices and Block devices can be accessed as files. The main difference between them is not whether seek can be used, but the system manages the two types of devices. Each I/O operation of the character device is directly transmitted to the driver corresponding to the system kernel. The operation of the application on the block device must be managed by the system buffer zone, indirectly passed to the driver for processing. Block device management is optimized for storage, while character device management is optimized for operations. As for network devices, it is a special type of device in Linux. It is not accessed by character devices or block devices through the corresponding device file nodes, the kernel no longer accesses network devices through calls such as read and write. The Linux network system is mainly based on the bsd unix socket mechanism. There is a special data structure between the system and the driver for data transmission. The system supports caching data transmission and data receiving, provides a traffic control mechanism and more protocol support.

As a tom, start with the simplest HelloWorld.

HelloWorld source program:

# Ifndef MODULE
# Define MODULE
# Endif

# Include <linux/init. h>
# Include <linux/module. h>

MODULE_LICENCE ("GPL ");

Static int hello_init (void)
{
Printk ("Hello world \ n ");
Return 0;
}

Static void hello_exit (void)
{
Printk ("Goodbye World \ n ");
}

Module_init (hello_init );
Module_exit (hello_exit );

This program is very simple, but it should be noted that for a driver, there is an entry function hello_init (), which is equivalent to the main function. The two header files here have a big problem and will be mentioned later!

After the source program is created, a makefile file is required to execute the program:

Obj-m: = HelloWorld. o
KDIR: =/lib/modules/$ (shell uname-r)/build
PWD: = $ (shell pwd)
Default:
[Tab] make-C $ (KDIR) M = $ (PWD) modules
Make directly under the directory where the Helloworld source file and Makefile file are located. This is the practice of the above general online tutorials, but I cannot compile and pass it. The error is: the file or directory cannot be found, after some efforts, we found the problem lies in the kernel source code tree.

Why should we install the kernel source code tree? The reason is that the source code tree contains the corresponding header file and function implementation. Without the source code tree, you cannot execute your own applications. What header files are written by the driver itself are in the folder under the source code tree, which is included in the kernel.

The driver connects to the kernel module as a module and runs in the kernel space. REFERENCE The LDD statement "because the 2.6 kernel module must be connected to the target file in the kernel source code tree, a more robust module loader can be obtained in this way, however, these target files must exist in the kernel directory tree ". The kernel directory tree mentioned here is the kernel source code tree that needs to be configured in our system before running the module we constructed, then connect the constructed target module to the kernel tree and run it again.

Check whether the kernel tree has been configured in your system: in the/lib/modules/2.6.35-30-generic Directory, check whether the build folder exists. If yes, it indicates that we already have a kernel tree in our system. If not, we need to build a kernel tree ourselves.

To construct a kernel tree, follow these steps:

1. install the software required to compile the kernel (you must install it using the make menuconfig command, and do not need to install it using make oldconfig ).

sudo apt-get install build-essential kernel-package libncurses5-dev fakeroot


2. Download the kernel source code

apt-cache search linux-source


If you run this command, the system will prompt you to install the kernel source code suitable for your kernel version.

 apt-get install Linux-source-2.6.35


Execute this command will automatically download and install the kernel source code that fits my system kernel Linux-source-2.6.35

3. decompress the kernel source code package
Go to/usr/src/and find linux-source-2.6.35.tar.bz2. Unzip the package with the unzip command.

tar jxvf linux-source-2.6.32.tar.bz2



4. Copy the config-2.6.35-30-generic under the/boot directory to the directory that has just been decompressed and renamed it. config

 sudo cp /boot/config-2.6.35-30-generic /usr/src/linux-source-2.6.35/.config



5. Switch to the root user for Kernel configuration

sudo -i
      cd /usr/src/linux-source-2.6.35
      make menuconfig


Next, a configuration interface is displayed, and the last two options are selected:
Load an Alternate configuration File and save an Alternate configuration File
Save and exit, and then exit the configuration environment.
6. Compile the kernel
Compile the kernel and run it under the administrator account.

#cd /usr/src/linux-source-2.6.35
      #make


If the computer is dual-core, you can add a parameter after make, for example:

make -j4

The make process takes a long time. I spent about 2 hours compiling in the VM...

Run again

#make bzImage

After that, a vmlinux file is generated in the current directory.

7. Compilation Module
The following problems may occur during module Compilation:
(Ld:/ubuntu/omnibook/sections. lds: No such file or directory)

Solution:
Add a sentence before ifeq ($ (KERNELRELEASE) in/usr/src/linux-source-2.6.35/ubuntu/omnibook/Makefile:

 PWD=$(shell pwd)



Then start compiling the module.

#make modules

Run again

make modules_install

Command to generate a directory under the/lib/modules directory

Then test the HelloWorld module:

1. Load the module to the kernel:

#insmod ./HelloWorld.ko

2. Run the lsmod command to view all the current driver modules. The result is hello 692 0.

#lsmod|grep HelloWorld

3. Remove the hello module.

#rmmod HelloWorld

4. Because printk does not output the result to the terminal, run the following command to view the result:

dmesg |grep world

After compilation, the HelloWorld. ko file is generated.

However, another problem is that it can be loaded but cannot be uninstalled. To solve this problem, the ERROR returned is ERROR: Removing 'helloworld': Device or resource busy.

According to online materials, this kind of driver loading is temporary. When the computer restarts, the driver will be automatically uninstalled and tried once.

 

 

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.