码迷,mamicode.com
首页 > 系统相关 > 详细

ubuntu下编译内核驱动。

时间:2014-10-23 12:11:38      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   ar   使用   for   strong   

目的:

1. 驱动热身。网上有很多类似的文章可供参考。

2. 在操作系统中, 编写这个设备的驱动。

3. 为写qemu的watchdog驱动练手。

有朋友问make的 watchdog驱动 需要什么准备,所以写这个blog。

 

环境:

ubuntu 12.04.4

热身:

  首先编写一个简单的 hello world驱动。

耗时(基于熟悉linux环境和操作系统的条件下):

  1. 安装操作系统 30-60分钟。

  2. 配置操作系统 30分钟。主要是配置中文书法。无其他特殊的配置。

  3. 代码编写运行 40分钟。

 

查看自己的内核版本:

Search for kernel version (optional)(搜寻内核的版本)

Type the following command:

$ apt-cache search linux-headers-$(uname -r)

Install linux-header package under Debina or Ubuntu Linux(更新内核的版本)

Type the following command:

$ sudo apt-get install linux-headers-$(uname -r)

 

代码:

// hello.c的驱动程序:
 
#include<linux/init.h>  //初始换函数
#include<linux/kernel.h>  //内核头文件
#include<linux/module.h>  //模块的头文件
 
MODULE_LICENSE("shaohef  BSD/GPL");
 
static int __init hello_start(void)
{
    printk(KERN_ALERT "Loading hello module...\n");
    printk(KERN_ALERT "Hello world\n");
    return 0;
}
 
static void __exit hello_end(void)
{
    printk(KERN_ALERT "Goodbye Mr.\n");
}
module_init(hello_start);
module_exit(hello_end);

 

// makefile文件:
ifeq ($(KERNELRELEASE),)
 KVERSION = $(shell uname -r)
all:
 make -C /lib/modules/$(KVERSION)/build M=$(shell pwd) modules
clean:
 make -C /lib/modules/$(KVERSION)/build M=$(shell pwd) clean
modules_install
 $(MAKE)  -C  $(KERNELDIR)M=$(PWD)  modules_install
else
 obj-m :=hello.o
endif
 

安装驱动(可能需要重启):

编译

$ make

挂载驱动

$ sudo insmod hello.ko

卸载驱动

$ sudo rmmod hello

查看驱动, 最好加上管道 |more指令 或者|grep

lsmod |grep hello

 

系统启动的时加载模块

文件 /etc/modules 设置加载的内核,这个文件里包含了系统启动的时候要加载的模块,每一个模块一行。

首先 hello 模块 拷贝到 /lib/modules/$(uname -r)/kernel/drivers.

建议的步骤:

(a)为 hello 模块建立一个目录

$ sudo mkdir -p /lib/modules/$(uname -r)/kernel/drivers/hello

(b)拷贝模块

$ sudo cp hello.ko /lib/modules/$(uname -r)/kernel/drivers/hello/

(c)编辑 /etc/modules 文件

$ sudo vim /etc/modules

加上下面一行

hello

(d)重启查看是否加载。

$ sudo cat /proc/modules 

或者

$ sudo lsmod | less

查看程序的输出:

#cat   /var/log/syslog  |grep  world

如果syslog没有输出, 查看 /var/log/message 里面的信息

# tail -f /var/log/message

 注:

insmod的本质就是将ko文件与运行的内核进行链接的过程。类似于编译helloworld的链接过程。

链接必然需要先进行编译,以便确定所需的外部符号(EXPORT_SYMBOLS)是否存在,因为有些符号(函数或全局变量)在内核中。在驱动中如果使用到这些符号,必须预留一个位置,insmod时进一步确定这些符号的具体位置(符号绑定)。

如果内核都没有编译过,怎么知道这些符号有没有编入内核中

 

ubuntu下编译内核驱动。

标签:style   blog   http   color   io   ar   使用   for   strong   

原文地址:http://www.cnblogs.com/shaohef/p/3799117.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!