Linux device driver programming-the simplest hellomod

Source: Internet
Author: User
1. download the source code package corresponding to the system kernel version. view the version by uname-r. My is 2632 download command: apt-getinstalllinux-source-2632 I download linux-source-2632tarbz22, configure kernel: tarjxvflinux-so

1. download the source code package corresponding to the system kernel version. view the version by uname-r.

My account is 2.6.32.

Download command: apt-get install linux-source-2.6.32

What I download is linux-source-2.6.32.tar.bz2.

2. configure the kernel:

Tar jxvf linux-source-2.6.32.tar.bz2

Cd linux-source-2.6.32

Make oldconfig # configure the original version

3, compile the kernel (this process takes a long time, after compilation in the linux-source-2.6.32 more than a vmlinux)

Make

4. compilation module

Make modules

5. install the module (the directory/lib/modules/2.6.32.61 + drm33.26 is displayed after the operation)

Make modules_install

6. test

①. Hellomod. c

# Include
 
  
// Initial function change # include
  
   
// Kernel header file # include
   
    
// Module header file MODULE_LICENSE ("GPL"); // prompt that the GNU license static int lkp_init (void) may not exist. // initialize {printk ("Hello, world \ n "); // the module runs in the kernel state. instead of using the printf function in the user State C library function, use the printk function return 0;} static int lkp_cleanup (void) // exit and cleanup functions {printk ("Goodbye, Hello world \ n");} module_init (lkp_init); // driver initialization entry point module_exit (lkp_cleanup ); // call the cleanup_module () function
   
  
 

② Makefile
obj-m :=hellomod.oKERNELDIR=/lib/modules/2.6.32.61+drm33.26/buildPWD :=$(shell pwd)modules:        $(MAKE) -C $(KERNELDIR) M=$(PWD) modulesmodules_install:        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
③ Execute make

Output:

root@master:~/Downloads/.1/lken# makemake -C /lib/modules/2.6.32.61+drm33.26/build M=/root/Downloads/.1/lken modulesmake[1]: Entering directory `/usr/src/linux-source-2.6.32'  Building modules, stage 2.  MODPOST 1 modules  CC      /root/Downloads/.1/lken/hellomod.mod.o  LD [M]  /root/Downloads/.1/lken/hellomod.komake[1]: Leaving directory `/usr/src/linux-source-2.6.32'

The hellomod. ko file is generated in the directory.

④ Insert and view the kernel

Insmod hellomod. ko

Root @ master :~ /Downloads/. 1/lken # lsmod | grep hellomod
Hellomod0 679

⑤ Remove a module

Rmmod hellomod

View Diaries

Tail/var/log/message

Output:

Jan  5 16:18:49 master kernel: [161488.154792] Hello, worldJan  5 16:19:20 master kernel: [161519.197040] Goodbye,Hello world
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.