linux系统编写驱动程序,Linux驱动--为Ubuntu系统编写驱动程序入门

先查看自己系统使用的内核版本:

wang@wang:~$ uname -r

3.13.0-96-generic

查看需要下载的源码包:

wang@wang:/usr/src$ apt-cache search linux-source

linux-source - Linux kernel source with Ubuntu patches

linux-source-3.13.0 - Linux kernel source for version 3.13.0 with Ubuntu patches

sudo apt-get install linux-source-3.13.0

下载完成之后,会在/usr/src目录下有一个linux-source-3.13.0.tar.bz2压缩包,进行解压。

root@wang:/usr/src#tar jxvf  linux-source-3.13.0.tar.bz2

开始配置内核选择最快的原版的配置(默认)方式:

root@wang:/usr/src/linux-source-3.13.0#make oldconfig

然后开始make,大约需要一个小时:

root@wang:/usr/src/linux-source-3.13.0#make

root@wang:/usr/src/linux-source-3.13.0#make bzImage

root@wang:/usr/src/linux-source-3.13.0#make modules

root@wang:/usr/src/linux-source-3.13.0#make mudules_install

make bzImage会在当前目录下生成vmlinux文件,重启系统,至此,内核树已经建立完成。

编写hello.c和Makefile文件

hello.c文件

#include

#include

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/3.13.11-ckt39/build

PWD := $(shell pwd)

modules:

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

modules_install:

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

(注意空格处是tab键)

编写完成后执行make操作:

wang@wang:~/test/test$ make

wang@wang:~/test/test$ ls

hello.c  hello.ko  hello.mod.c  hello.mod.o  hello.o  Makefile  modules.order  Module.symvers

加载模块:

root@wang:/home/wang/test/test# insmod hello.ko

卸载模块:

root@wang:/home/wang/test/test# rmmod hello.ko

查看效果:

root@wang:/home/wang/test/test2# cat /var/log/syslog | grep world

Oct  9 20:11:55 wang kernel: [ 1175.967987] Hello, world

Oct  9 20:13:22 wang kernel: [ 1262.909541] Goodbye, cruel world

Oct  9 20:46:38 wang kernel: [ 3259.997105] Hello, world

Oct  9 20:47:12 wang kernel: [ 3294.438485] Goodbye, cruel world

如果碰到

insmod error required key not available

错误,就在电脑的开机启动项中关闭安全启动模式即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值