Ubuntu下轻松建立你的内核驱动开发环境

转载地址: http://forum.ubuntu.org.cn/viewtopic.php?t=208453

Preface:
1.笔者刚学linux驱动开发,看到网上很多说要现建立内核树。在笔者到官网下载源码时,源码下面有如下说明: 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. 意思是,如果你只是想为内核编译第三方的模块,那么,你不需下载此源码包。安装内核头文件包或许会更适合你。

2.如果你的ubuntu是保持更新的,你的系统是安装有内核头文件包的,不信你到/usr/src目录下查看,是不是有linux-headers-2.6.xx-xx-generic的文件夹呢,呵呵。我现在可以说,你可以在此开发你的驱动程序了。

3.笔者也是初学,有见笑的,大家多指正,emial: [email protected]; 转载注明出处:http://sites.google.com/site/tonychen123site,作者:tony

4.环境本来就有了看来,所以我的文章,本来应该叫驱动开发最简单例子才合适……另,我要示例的是最简单,也是个最没用的驱动演示,名字叫tst,包含源码tst.c及makefile文件,编译出来的模块名叫tst.ko

Content:

1)建立你的实验目录并创建tst.c源文件:

/*
 * File: tst.c
 *
 * A very simple kernel module demo
 *
 * Copyright 2009, Tony, <[email protected]>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation;
 *
 */

#include <linux/module.h>   /* Needed by all modules */
#include <linux/kernel.h>   /* Needed for KERN_INFO  */
#include <linux/init.h>     /*Needed for the macros */

static int __init tst_start(void)
{
    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);

/* end */

2)创建Makefile文件:

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)编译

tony@ubuntu:~/mini2440/mtst$ sudo make Note:好像要用sudo才行!
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:生成一个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)测试

tony@ubuntu:~/mini2440/mtst$ su     Note:加载模块要有root特权                                           
Password:                                                                                      
root@ubuntu:/home/tony/mini2440/mtst# insmod tst.ko  Note:加载模块要有root特权
root@ubuntu:/home/tony/mini2440/mtst# dmesg | tail   
[   25.464531] r8169: eth0: link up              
[   25.464534] r8169: eth0: link up              
[   54.931357] UDF-fs: No VRS found              
[   54.981587] ISO 9660 Extensions: Microsoft Joliet Level 3
[   55.013712] ISOFS: changing to secondary root           
[ 2932.746291] module init                                 
[ 3138.097884] Cleanup module                              
[ 7444.574136] tst: module license 'unspecified' taints kernel.
[ 7444.575117] Loading my so simple module...   Note:注意这两行,就是我们的模块加载信息  
[ 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
[   25.464534] r8169: eth0: link up
[   54.931357] UDF-fs: No VRS found
[   54.981587] ISO 9660 Extensions: Microsoft Joliet Level 3
[   55.013712] ISOFS: changing to secondary root
[ 2932.746291] module init
[ 3138.097884] Cleanup module
[ 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:呵呵,模块正常卸载
root@ubuntu:/home/tony/mini2440/mtst#  /* end Bye ~ */



你可能感兴趣的:(linux,驱动开发)