Build your kernel driver development environment in Ubuntu

Source: Internet
Author: User
Tags dmesg
Preface: 1. I just learned how to develop a Linux driver. I have seen many online saying that I want to build a kernel tree. When I download the source code from my website, the following describes the source code: Ifyouaresimplytryingtobuildthird-partymodulesforyourkernel, youdonotwantthispackage. InstalltheappropriateLinux-header

Preface:
1. I just learned how to develop a Linux driver. I have seen many online saying that I want to build a kernel tree. When the author downloads the source code from the official website, the following describes the source code: If you are simply trying to build third-party modules for your kernel, you do not want this package. install the appropriate Linux-headers package instead. if you only want to compile a third-party module for the kernel, you do not need to download the source code package. Installing the kernel header package may be more suitable for you.

2. if your Ubuntu is updated, your system is installed with the kernel header file package, do not believe you to view under the/usr/src directory, is there a Linux-headers-2.6.xx-xx-generic folder, huh, huh. Now I can say that you can develop your driver here.

3. I am also a beginner. I have a little bit of laughter.

Emial: tonychen123@qq.com; reprinted with the source: http://sites.google.com/site/tonychen123site, Author: Tony

4. The environment already exists, so my article should have been called the simplest example of driver development ...... In addition, the example is the simplest and useless driver demonstration. It is called tst and contains the source code tst. c and makefile files. The module compiled by www.Linuxidc.com is called tst. ko.

Content:

1) Create your experiment directory and create the tst. c source file:

Code:
Tony @ Ubuntu :~ /Mini2440 $ mkdir mtst; cd mtst
Tony @ Ubuntu :~ /Mini2440/mtst $ vim tst. c

==============================================

 

# Include
# Include
# Include

Static int _ init tst_start (viod)
{
Printk (KERN_INFO "Loading my so simple module... \ n ");
Printk (KERN_INFO "I love Ubuntu \ n ");
Return 0;
}

Static void _ exit tst_end (void)
{
Printk (KERN_INFO "From tst module: Goodbye, Tony. \ n ");
}

Module_init (tst_start );
Module_exit (tst_end );

 

 

2) create a Makefile:


Code:
Tony @ Ubuntu :~ /Mini2440/mtst $ vim Makefile
========================================================== ======================================
Obj-m = tst. o

K_DIR = $ (shell uname-r)
PWD = $ (shell pwd)

All:
Make-C/lib/modules/$ (K_DIR)/build M = $ (PWD) modules
Clean:
Make-C/lib/modules/$ (K_DIR)/build M = $ (PWD) clean

# End #


3) Compile


Code:
Tony @ Ubuntu :~ /Mini2440/mtst $ sudo make Note: It seems that sudo is required!
Make-C/lib/modules/2.6.28-12-generic/build M =/home/tony/mini2440/mtst modules
Make [1]: Entering directory '/usr/src/Linux-headers-2.6.28-12-generic'
CC [M]/home/tony/mini2440/mtst/tst. o
/Home/tony/mini2440/mtst/tst. c: 19: warning: function declaration isn't a prototype
Building modules, stage 2.
MODPOST 1 modules Note: generate a module!
CC/home/tony/mini2440/mtst/tst. mod. o
LD [M]/home/tony/mini2440/mtst/tst. ko
Make [1]: Leaving directory '/usr/src/Linux-headers-2.6.28-12-generic'
Tony @ Ubuntu :~ /Mini2440/mtst $


4) test

Code:
Tony @ Ubuntu :~ /Mini2440/mtst $ su Note: to load a module, you must have the root privilege.
Password:
Root @ Ubuntu:/home/tony/mini2440/mtst # insmod tst. ko Note: The loading module must have the root privilege.
Root @ Ubuntu:/home/tony/mini2440/mtst # dmesg | tail
[1, 25.464531] r8169: eth0: link up
[1, 25.464534] r8169: eth0: link up
[1, 54.931357] UDF-fs: No VRS found
[54.981587] ISO 9660 Extensions: Microsoft Joliet Level 3
[55.013712] ISOFS: changing to secondary root
[2, 2932.746291] module
[2, 3138.097884] Cleanup module
[7444.574136] tst: module license 'Unspecified 'taints kernel.
[7444.575117] Loading my so simple module... Note: These two lines are the Loading information of our modules.
[7444.575121] I love Ubuntu
Root @ Ubuntu:/home/tony/mini2440/mtst # lsmod | grep tst
Tst 9344 0
Root @ Ubuntu:/home/tony/mini2440/mtst # rmmod tst. ko
Root @ Ubuntu:/home/tony/mini2440/mtst # lsmod | grep tst
Root @ Ubuntu:/home/tony/mini2440/mtst # dmesg | tail
[1, 25.464534] r8169: eth0: link up
[1, 54.931357] UDF-fs: No VRS found
[54.981587] ISO 9660 Extensions: Microsoft Joliet Level 3
[55.013712] ISOFS: changing to secondary root
[2, 2932.746291] module
[2, 3138.097884] Cleanup
[7444.574136] tst: module license 'Unspecified 'taints kernel.
[7444.575117] Loading my so simple module...
[7444.575121] I love Ubuntu
[7559.570808] From tst module: Goodbye, Tony. Note: Haha, the module is uninstalled normally.
Root @ Ubuntu:/home/tony/mini2440/mtst #

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.