Compilation and loading of Linux kernel driver modules

Source: Internet
Author: User

HOST: Windows 7 flagship edition, vmware7.1.2, fedora9

Compiling environment: Arm-Linux-GCC 4.4.3

Linux: linux-2.6.32.2

Busybox version: busybox-1.19.4

Target Board: friendly arm micro2440


How to compile and use the kernel module:

Write a simple Linux kernel module Hello world and hello. C. The content is as follows:

# Include
<Linux/init. h>

# Include <Linux/module. h>

Module_license ("dual BSD/GPL ");

Static int hello_init (void)

{

Printk (kern_alert
"Hello world enter \ n ");

Return 0;

}

Static void hello_exit (void)

{

Printk (kern_alert
"Hello World exit \ n ");

}

Module_init (hello_init );

Module_exit (hello_exit );

Module_author ("Charles ");

Module_description ("a simple hello World module ");

Module_alias ("a simplest module ");

Compile a simple makefile as follows:

OBJ-M: = Hello. o

Use the following command to compile the hello World module:

# Make-C $ linux_kernel_path M = $ (PWD) Modules

$ Linux_kernel_path is the absolute path of the kernel file. After compilation, the kernel module hello. Ko will be generated in the current directory.

Load, view, and uninstall modules:

# Insmod hello. Ko // load the hello. Ko Module

# Lsmod // view the current loading Module

# Rmmod Hello // uninstall the module without. Ko


Compile and load the LED driver:

Write the LED driver next time. Assume that the LED driver has been written, and the driver file name is micro2440_leds. Copy it to the driver/Char directory and add it to the makefile directory:

OBJ-M + = micro2440_leds.o

Run the following command in the top-level directory of the kernel source code:

# Make M = Drivers/Char/modules

Generate the micro2440_leds.ko file.

Copy micro2440_leds.ko to the target board directory/lib/modules/and load the LED driver module:

# Insmod/lib/modules/micro2440_leds.ko


Problems encountered when the target board is detached from the module:

[Root @ Debian modules] # rmmod micro2440_leds

Rmmod: chdir (2.6.32.2): no such file or directory

The target board has already created/lib/modules/, and the modules that need to be dynamically loaded are stored in this directory. However, an empty directory/lib/modules/2.6.32.2 must be created to uninstall the module, 2.6.32.2 is the corresponding kernel version.


Use make menuconfig to configure the LED driver:

Modify driver/Char/makefile and add the following line:

OBJ-M + = micro2440_leds.o

Changed:

OBJ-$ (config_mini2440_led) + = micro2440_leds.o

Add the following to the konfig file in the corresponding directory:

Config mini2440_led

Tristate "LED driver for friendlyarm mini2440 Development Boards"

Depends on mach_mini2440

Default y if mach_mini2440

Help

This is LED driver for friendlyarm mini2440 Development Boards

Tristate indicates that the driver can be compiled into a module through make menuconfig. The bool type can only be compiled into or not compiled into the kernel. Depends on indicates dependency. Default y indicates that the kernel is compiled by default, which has three options: Y, M, and N. M indicates that the kernel module is compiled, and N indicates that the kernel module is not compiled.

Run make menuconfig again to go to the character device driver page:

Device Drivers --->

Character devices --->

<*> LED driver for friendlyarm mini2440 Development Boards

We found that the LED driver configuration option was added.


Do not modify the Kernel File compilation driver module:

Create a folder named led. Copy the LED driver source code micro2440_leds.c to this directory. Create a makefile in this directory as follows:

OBJ-M: = micro2440_leds.o

Kernel_dir? =/Home/samba/linux-2.6.32.2-micro2440

ALL:

$ (Make)-C $ (kernel_dir) M = $ (PWD) Modules

Clean:

Rm-f *. O *. Ko *. Mod. **. Order *. symvers

After saving the file, run make to generate the required driver module File micro2440_leds.ko in the current directory.

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.