ubuntu下驱动程序的hello world

本文纯属转贴,原文如下

http://dev.firnow.com/course/6_system/linux/Linuxjs/200865/122399.html

记录几个命令。

1.源码树建立

//OS版本取得
shana@shana:~$ uname -r

//查询源码
shana@shana:/usr/src$ apt-cache search linux-source

//取得源码
shana@shana:/usr/src$ sudo apt-get install linux-source-2.6.22

//解冻源码
root@shana:/usr/src#tar jxvf linux-source-2.6.20.tar.bz2

//配置源码
root@shana:/usr/src/linux-source-2.6.22# make oldconfig

//编译源码
shana@shana:/usr/src/linux-source-2.6.22$ make
shana@shana:/usr/src/linux-source-2.6.22$ make bzImage
root@shana:/usr/src/linux-source-2.6.22#make modules
root@shana:/usr/src/linux-source-2.6.22#make modules_install

//确认源码树
cd /lib/modules/2.6.22-14-generic/

2.驱动程序例子

    hello.c

//hello.c
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");

static int hello_init(void)
{
	printk(KERN_ALERT "Hello, world\n");
	return 0;
}

static void hello_exit(void)
{
	printk(KERN_ALERT"Goodbye, cruel world\n");
}

module_init(hello_init);
module_exit(hello_exit);

    Makefile

obj-m := hello.o
KERNELDIR := /lib/modules/2.6.20/build
PWD := $(shell pwd)

modules:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

modules_install:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
 

3.编译,加载,卸载

扫描二维码关注公众号,回复: 1418110 查看本文章
//加载驱动
root@shana:/home/shana/driver# insmod ./hello.ko

//查看驱动
root@shana:/home/shana/driver# lsmod

//卸载驱动
root@shana:/home/shana/driver# rmmod hello

//查看日志
shana@shana:~/driver$ cat /var/log/syslog |grep world
 

猜你喜欢

转载自dncsoft.iteye.com/blog/771402
今日推荐